1c2c66affSColin Finck /*
2c2c66affSColin Finck * X.509 common functions for parsing and verification
3c2c66affSColin Finck *
4218e2596SThomas Faber * Copyright The Mbed TLS Contributors
5e57126f5SThomas Faber * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6e57126f5SThomas Faber *
7e57126f5SThomas Faber * This file is provided under the Apache License 2.0, or the
8e57126f5SThomas Faber * GNU General Public License v2.0 or later.
9e57126f5SThomas Faber *
10e57126f5SThomas Faber * **********
11e57126f5SThomas Faber * Apache License 2.0:
12e57126f5SThomas Faber *
13e57126f5SThomas Faber * Licensed under the Apache License, Version 2.0 (the "License"); you may
14e57126f5SThomas Faber * not use this file except in compliance with the License.
15e57126f5SThomas Faber * You may obtain a copy of the License at
16e57126f5SThomas Faber *
17e57126f5SThomas Faber * http://www.apache.org/licenses/LICENSE-2.0
18e57126f5SThomas Faber *
19e57126f5SThomas Faber * Unless required by applicable law or agreed to in writing, software
20e57126f5SThomas Faber * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21e57126f5SThomas Faber * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22e57126f5SThomas Faber * See the License for the specific language governing permissions and
23e57126f5SThomas Faber * limitations under the License.
24e57126f5SThomas Faber *
25e57126f5SThomas Faber * **********
26e57126f5SThomas Faber *
27e57126f5SThomas Faber * **********
28e57126f5SThomas Faber * GNU General Public License v2.0 or later:
29c2c66affSColin Finck *
30c2c66affSColin Finck * This program is free software; you can redistribute it and/or modify
31c2c66affSColin Finck * it under the terms of the GNU General Public License as published by
32c2c66affSColin Finck * the Free Software Foundation; either version 2 of the License, or
33c2c66affSColin Finck * (at your option) any later version.
34c2c66affSColin Finck *
35c2c66affSColin Finck * This program is distributed in the hope that it will be useful,
36c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
37c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38c2c66affSColin Finck * GNU General Public License for more details.
39c2c66affSColin Finck *
40c2c66affSColin Finck * You should have received a copy of the GNU General Public License along
41c2c66affSColin Finck * with this program; if not, write to the Free Software Foundation, Inc.,
42c2c66affSColin Finck * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43c2c66affSColin Finck *
44e57126f5SThomas Faber * **********
45c2c66affSColin Finck */
46c2c66affSColin Finck /*
47c2c66affSColin Finck * The ITU-T X.509 standard defines a certificate format for PKI.
48c2c66affSColin Finck *
49c2c66affSColin Finck * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
50c2c66affSColin Finck * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
51c2c66affSColin Finck * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
52c2c66affSColin Finck *
53c2c66affSColin Finck * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
54c2c66affSColin Finck * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
55c2c66affSColin Finck */
56c2c66affSColin Finck
57c2c66affSColin Finck #if !defined(MBEDTLS_CONFIG_FILE)
58c2c66affSColin Finck #include "mbedtls/config.h"
59c2c66affSColin Finck #else
60c2c66affSColin Finck #include MBEDTLS_CONFIG_FILE
61c2c66affSColin Finck #endif
62c2c66affSColin Finck
63c2c66affSColin Finck #if defined(MBEDTLS_X509_USE_C)
64c2c66affSColin Finck
65c2c66affSColin Finck #include "mbedtls/x509.h"
66c2c66affSColin Finck #include "mbedtls/asn1.h"
67c2c66affSColin Finck #include "mbedtls/oid.h"
68c2c66affSColin Finck
69c2c66affSColin Finck #include <stdio.h>
70c2c66affSColin Finck #include <string.h>
71c2c66affSColin Finck
72c2c66affSColin Finck #if defined(MBEDTLS_PEM_PARSE_C)
73c2c66affSColin Finck #include "mbedtls/pem.h"
74c2c66affSColin Finck #endif
75c2c66affSColin Finck
76c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_C)
77c2c66affSColin Finck #include "mbedtls/platform.h"
78c2c66affSColin Finck #else
79c2c66affSColin Finck #include <stdio.h>
80c2c66affSColin Finck #include <stdlib.h>
81c2c66affSColin Finck #define mbedtls_free free
82c2c66affSColin Finck #define mbedtls_calloc calloc
83c2c66affSColin Finck #define mbedtls_printf printf
84c2c66affSColin Finck #define mbedtls_snprintf snprintf
85c2c66affSColin Finck #endif
86c2c66affSColin Finck
87c2c66affSColin Finck #if defined(MBEDTLS_HAVE_TIME)
88c2c66affSColin Finck #include "mbedtls/platform_time.h"
89c2c66affSColin Finck #endif
90*cbda039fSThomas Faber #if defined(MBEDTLS_HAVE_TIME_DATE)
91*cbda039fSThomas Faber #include "mbedtls/platform_util.h"
92c2c66affSColin Finck #include <time.h>
93c2c66affSColin Finck #endif
94c2c66affSColin Finck
95*cbda039fSThomas Faber #define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
96*cbda039fSThomas Faber #define CHECK_RANGE(min, max, val) \
97*cbda039fSThomas Faber do \
98*cbda039fSThomas Faber { \
99*cbda039fSThomas Faber if( ( val ) < ( min ) || ( val ) > ( max ) ) \
100*cbda039fSThomas Faber { \
101*cbda039fSThomas Faber return( ret ); \
102*cbda039fSThomas Faber } \
103*cbda039fSThomas Faber } while( 0 )
104c2c66affSColin Finck
105c2c66affSColin Finck /*
106c2c66affSColin Finck * CertificateSerialNumber ::= INTEGER
107c2c66affSColin Finck */
mbedtls_x509_get_serial(unsigned char ** p,const unsigned char * end,mbedtls_x509_buf * serial)108c2c66affSColin Finck int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
109c2c66affSColin Finck mbedtls_x509_buf *serial )
110c2c66affSColin Finck {
111c2c66affSColin Finck int ret;
112c2c66affSColin Finck
113c2c66affSColin Finck if( ( end - *p ) < 1 )
114c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_SERIAL +
115c2c66affSColin Finck MBEDTLS_ERR_ASN1_OUT_OF_DATA );
116c2c66affSColin Finck
117c2c66affSColin Finck if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
118c2c66affSColin Finck **p != MBEDTLS_ASN1_INTEGER )
119c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_SERIAL +
120c2c66affSColin Finck MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
121c2c66affSColin Finck
122c2c66affSColin Finck serial->tag = *(*p)++;
123c2c66affSColin Finck
124c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
125c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
126c2c66affSColin Finck
127c2c66affSColin Finck serial->p = *p;
128c2c66affSColin Finck *p += serial->len;
129c2c66affSColin Finck
130c2c66affSColin Finck return( 0 );
131c2c66affSColin Finck }
132c2c66affSColin Finck
133c2c66affSColin Finck /* Get an algorithm identifier without parameters (eg for signatures)
134c2c66affSColin Finck *
135c2c66affSColin Finck * AlgorithmIdentifier ::= SEQUENCE {
136c2c66affSColin Finck * algorithm OBJECT IDENTIFIER,
137c2c66affSColin Finck * parameters ANY DEFINED BY algorithm OPTIONAL }
138c2c66affSColin Finck */
mbedtls_x509_get_alg_null(unsigned char ** p,const unsigned char * end,mbedtls_x509_buf * alg)139c2c66affSColin Finck int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
140c2c66affSColin Finck mbedtls_x509_buf *alg )
141c2c66affSColin Finck {
142c2c66affSColin Finck int ret;
143c2c66affSColin Finck
144c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
145c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
146c2c66affSColin Finck
147c2c66affSColin Finck return( 0 );
148c2c66affSColin Finck }
149c2c66affSColin Finck
150c2c66affSColin Finck /*
151c1eccaffSThomas Faber * Parse an algorithm identifier with (optional) parameters
152c2c66affSColin Finck */
mbedtls_x509_get_alg(unsigned char ** p,const unsigned char * end,mbedtls_x509_buf * alg,mbedtls_x509_buf * params)153c2c66affSColin Finck int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
154c2c66affSColin Finck mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
155c2c66affSColin Finck {
156c2c66affSColin Finck int ret;
157c2c66affSColin Finck
158c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
159c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
160c2c66affSColin Finck
161c2c66affSColin Finck return( 0 );
162c2c66affSColin Finck }
163c2c66affSColin Finck
164c2c66affSColin Finck #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
165c2c66affSColin Finck /*
166c2c66affSColin Finck * HashAlgorithm ::= AlgorithmIdentifier
167c2c66affSColin Finck *
168c2c66affSColin Finck * AlgorithmIdentifier ::= SEQUENCE {
169c2c66affSColin Finck * algorithm OBJECT IDENTIFIER,
170c2c66affSColin Finck * parameters ANY DEFINED BY algorithm OPTIONAL }
171c2c66affSColin Finck *
172c2c66affSColin Finck * For HashAlgorithm, parameters MUST be NULL or absent.
173c2c66affSColin Finck */
x509_get_hash_alg(const mbedtls_x509_buf * alg,mbedtls_md_type_t * md_alg)174c2c66affSColin Finck static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
175c2c66affSColin Finck {
176c2c66affSColin Finck int ret;
177c2c66affSColin Finck unsigned char *p;
178c2c66affSColin Finck const unsigned char *end;
179c2c66affSColin Finck mbedtls_x509_buf md_oid;
180c2c66affSColin Finck size_t len;
181c2c66affSColin Finck
182c2c66affSColin Finck /* Make sure we got a SEQUENCE and setup bounds */
183c2c66affSColin Finck if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
184c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
185c2c66affSColin Finck MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
186c2c66affSColin Finck
187c2c66affSColin Finck p = (unsigned char *) alg->p;
188c2c66affSColin Finck end = p + alg->len;
189c2c66affSColin Finck
190c2c66affSColin Finck if( p >= end )
191c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
192c2c66affSColin Finck MBEDTLS_ERR_ASN1_OUT_OF_DATA );
193c2c66affSColin Finck
194c2c66affSColin Finck /* Parse md_oid */
195c2c66affSColin Finck md_oid.tag = *p;
196c2c66affSColin Finck
197c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
198c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
199c2c66affSColin Finck
200c2c66affSColin Finck md_oid.p = p;
201c2c66affSColin Finck p += md_oid.len;
202c2c66affSColin Finck
203c2c66affSColin Finck /* Get md_alg from md_oid */
204c2c66affSColin Finck if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
205c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
206c2c66affSColin Finck
207c2c66affSColin Finck /* Make sure params is absent of NULL */
208c2c66affSColin Finck if( p == end )
209c2c66affSColin Finck return( 0 );
210c2c66affSColin Finck
211c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
212c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
213c2c66affSColin Finck
214c2c66affSColin Finck if( p != end )
215c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
216c2c66affSColin Finck MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
217c2c66affSColin Finck
218c2c66affSColin Finck return( 0 );
219c2c66affSColin Finck }
220c2c66affSColin Finck
221c2c66affSColin Finck /*
222c2c66affSColin Finck * RSASSA-PSS-params ::= SEQUENCE {
223c2c66affSColin Finck * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
224c2c66affSColin Finck * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
225c2c66affSColin Finck * saltLength [2] INTEGER DEFAULT 20,
226c2c66affSColin Finck * trailerField [3] INTEGER DEFAULT 1 }
227c2c66affSColin Finck * -- Note that the tags in this Sequence are explicit.
228c2c66affSColin Finck *
229c2c66affSColin Finck * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
230c2c66affSColin Finck * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
231c2c66affSColin Finck * option. Enfore this at parsing time.
232c2c66affSColin Finck */
mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf * params,mbedtls_md_type_t * md_alg,mbedtls_md_type_t * mgf_md,int * salt_len)233c2c66affSColin Finck int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
234c2c66affSColin Finck mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
235c2c66affSColin Finck int *salt_len )
236c2c66affSColin Finck {
237c2c66affSColin Finck int ret;
238c2c66affSColin Finck unsigned char *p;
239c2c66affSColin Finck const unsigned char *end, *end2;
240c2c66affSColin Finck size_t len;
241c2c66affSColin Finck mbedtls_x509_buf alg_id, alg_params;
242c2c66affSColin Finck
243c2c66affSColin Finck /* First set everything to defaults */
244c2c66affSColin Finck *md_alg = MBEDTLS_MD_SHA1;
245c2c66affSColin Finck *mgf_md = MBEDTLS_MD_SHA1;
246c2c66affSColin Finck *salt_len = 20;
247c2c66affSColin Finck
248c2c66affSColin Finck /* Make sure params is a SEQUENCE and setup bounds */
249c2c66affSColin Finck if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
250c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
251c2c66affSColin Finck MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
252c2c66affSColin Finck
253c2c66affSColin Finck p = (unsigned char *) params->p;
254c2c66affSColin Finck end = p + params->len;
255c2c66affSColin Finck
256c2c66affSColin Finck if( p == end )
257c2c66affSColin Finck return( 0 );
258c2c66affSColin Finck
259c2c66affSColin Finck /*
260c2c66affSColin Finck * HashAlgorithm
261c2c66affSColin Finck */
262c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
263c2c66affSColin Finck MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
264c2c66affSColin Finck {
265c2c66affSColin Finck end2 = p + len;
266c2c66affSColin Finck
267c2c66affSColin Finck /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
268c2c66affSColin Finck if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
269c2c66affSColin Finck return( ret );
270c2c66affSColin Finck
271c2c66affSColin Finck if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
272c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
273c2c66affSColin Finck
274c2c66affSColin Finck if( p != end2 )
275c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
276c2c66affSColin Finck MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
277c2c66affSColin Finck }
278c2c66affSColin Finck else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
279c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
280c2c66affSColin Finck
281c2c66affSColin Finck if( p == end )
282c2c66affSColin Finck return( 0 );
283c2c66affSColin Finck
284c2c66affSColin Finck /*
285c2c66affSColin Finck * MaskGenAlgorithm
286c2c66affSColin Finck */
287c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
288c2c66affSColin Finck MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
289c2c66affSColin Finck {
290c2c66affSColin Finck end2 = p + len;
291c2c66affSColin Finck
292c2c66affSColin Finck /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
293c2c66affSColin Finck if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
294c2c66affSColin Finck return( ret );
295c2c66affSColin Finck
296c2c66affSColin Finck /* Only MFG1 is recognised for now */
297c2c66affSColin Finck if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
298c2c66affSColin Finck return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
299c2c66affSColin Finck MBEDTLS_ERR_OID_NOT_FOUND );
300c2c66affSColin Finck
301c2c66affSColin Finck /* Parse HashAlgorithm */
302c2c66affSColin Finck if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
303c2c66affSColin Finck return( ret );
304c2c66affSColin Finck
305c2c66affSColin Finck if( p != end2 )
306c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
307c2c66affSColin Finck MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
308c2c66affSColin Finck }
309c2c66affSColin Finck else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
310c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
311c2c66affSColin Finck
312c2c66affSColin Finck if( p == end )
313c2c66affSColin Finck return( 0 );
314c2c66affSColin Finck
315c2c66affSColin Finck /*
316c2c66affSColin Finck * salt_len
317c2c66affSColin Finck */
318c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
319c2c66affSColin Finck MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
320c2c66affSColin Finck {
321c2c66affSColin Finck end2 = p + len;
322c2c66affSColin Finck
323c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
324c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
325c2c66affSColin Finck
326c2c66affSColin Finck if( p != end2 )
327c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
328c2c66affSColin Finck MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
329c2c66affSColin Finck }
330c2c66affSColin Finck else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
331c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
332c2c66affSColin Finck
333c2c66affSColin Finck if( p == end )
334c2c66affSColin Finck return( 0 );
335c2c66affSColin Finck
336c2c66affSColin Finck /*
337c2c66affSColin Finck * trailer_field (if present, must be 1)
338c2c66affSColin Finck */
339c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
340c2c66affSColin Finck MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
341c2c66affSColin Finck {
342c2c66affSColin Finck int trailer_field;
343c2c66affSColin Finck
344c2c66affSColin Finck end2 = p + len;
345c2c66affSColin Finck
346c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
347c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
348c2c66affSColin Finck
349c2c66affSColin Finck if( p != end2 )
350c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
351c2c66affSColin Finck MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
352c2c66affSColin Finck
353c2c66affSColin Finck if( trailer_field != 1 )
354c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG );
355c2c66affSColin Finck }
356c2c66affSColin Finck else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
357c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
358c2c66affSColin Finck
359c2c66affSColin Finck if( p != end )
360c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG +
361c2c66affSColin Finck MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
362c2c66affSColin Finck
363c2c66affSColin Finck return( 0 );
364c2c66affSColin Finck }
365c2c66affSColin Finck #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
366c2c66affSColin Finck
367c2c66affSColin Finck /*
368c2c66affSColin Finck * AttributeTypeAndValue ::= SEQUENCE {
369c2c66affSColin Finck * type AttributeType,
370c2c66affSColin Finck * value AttributeValue }
371c2c66affSColin Finck *
372c2c66affSColin Finck * AttributeType ::= OBJECT IDENTIFIER
373c2c66affSColin Finck *
374c2c66affSColin Finck * AttributeValue ::= ANY DEFINED BY AttributeType
375c2c66affSColin Finck */
x509_get_attr_type_value(unsigned char ** p,const unsigned char * end,mbedtls_x509_name * cur)376c2c66affSColin Finck static int x509_get_attr_type_value( unsigned char **p,
377c2c66affSColin Finck const unsigned char *end,
378c2c66affSColin Finck mbedtls_x509_name *cur )
379c2c66affSColin Finck {
380c2c66affSColin Finck int ret;
381c2c66affSColin Finck size_t len;
382c2c66affSColin Finck mbedtls_x509_buf *oid;
383c2c66affSColin Finck mbedtls_x509_buf *val;
384c2c66affSColin Finck
385c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
386c2c66affSColin Finck MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
387c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
388c2c66affSColin Finck
389430656f0SThomas Faber end = *p + len;
390430656f0SThomas Faber
391c2c66affSColin Finck if( ( end - *p ) < 1 )
392c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_NAME +
393c2c66affSColin Finck MBEDTLS_ERR_ASN1_OUT_OF_DATA );
394c2c66affSColin Finck
395c2c66affSColin Finck oid = &cur->oid;
396c2c66affSColin Finck oid->tag = **p;
397c2c66affSColin Finck
398c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
399c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
400c2c66affSColin Finck
401c2c66affSColin Finck oid->p = *p;
402c2c66affSColin Finck *p += oid->len;
403c2c66affSColin Finck
404c2c66affSColin Finck if( ( end - *p ) < 1 )
405c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_NAME +
406c2c66affSColin Finck MBEDTLS_ERR_ASN1_OUT_OF_DATA );
407c2c66affSColin Finck
408c2c66affSColin Finck if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
409c2c66affSColin Finck **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
410c2c66affSColin Finck **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
411c2c66affSColin Finck **p != MBEDTLS_ASN1_BIT_STRING )
412c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_NAME +
413c2c66affSColin Finck MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
414c2c66affSColin Finck
415c2c66affSColin Finck val = &cur->val;
416c2c66affSColin Finck val->tag = *(*p)++;
417c2c66affSColin Finck
418c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
419c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
420c2c66affSColin Finck
421c2c66affSColin Finck val->p = *p;
422c2c66affSColin Finck *p += val->len;
423c2c66affSColin Finck
424430656f0SThomas Faber if( *p != end )
425430656f0SThomas Faber {
426430656f0SThomas Faber return( MBEDTLS_ERR_X509_INVALID_NAME +
427430656f0SThomas Faber MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
428430656f0SThomas Faber }
429430656f0SThomas Faber
430c2c66affSColin Finck cur->next = NULL;
431c2c66affSColin Finck
432c2c66affSColin Finck return( 0 );
433c2c66affSColin Finck }
434c2c66affSColin Finck
435c2c66affSColin Finck /*
436c2c66affSColin Finck * Name ::= CHOICE { -- only one possibility for now --
437c2c66affSColin Finck * rdnSequence RDNSequence }
438c2c66affSColin Finck *
439c2c66affSColin Finck * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
440c2c66affSColin Finck *
441c2c66affSColin Finck * RelativeDistinguishedName ::=
442c2c66affSColin Finck * SET OF AttributeTypeAndValue
443c2c66affSColin Finck *
444c2c66affSColin Finck * AttributeTypeAndValue ::= SEQUENCE {
445c2c66affSColin Finck * type AttributeType,
446c2c66affSColin Finck * value AttributeValue }
447c2c66affSColin Finck *
448c2c66affSColin Finck * AttributeType ::= OBJECT IDENTIFIER
449c2c66affSColin Finck *
450c2c66affSColin Finck * AttributeValue ::= ANY DEFINED BY AttributeType
451c2c66affSColin Finck *
452c2c66affSColin Finck * The data structure is optimized for the common case where each RDN has only
453c2c66affSColin Finck * one element, which is represented as a list of AttributeTypeAndValue.
454c2c66affSColin Finck * For the general case we still use a flat list, but we mark elements of the
455c2c66affSColin Finck * same set so that they are "merged" together in the functions that consume
456c2c66affSColin Finck * this list, eg mbedtls_x509_dn_gets().
457c2c66affSColin Finck */
mbedtls_x509_get_name(unsigned char ** p,const unsigned char * end,mbedtls_x509_name * cur)458c2c66affSColin Finck int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
459c2c66affSColin Finck mbedtls_x509_name *cur )
460c2c66affSColin Finck {
461c2c66affSColin Finck int ret;
462c2c66affSColin Finck size_t set_len;
463c2c66affSColin Finck const unsigned char *end_set;
464c2c66affSColin Finck
465c2c66affSColin Finck /* don't use recursion, we'd risk stack overflow if not optimized */
466c2c66affSColin Finck while( 1 )
467c2c66affSColin Finck {
468c2c66affSColin Finck /*
469c2c66affSColin Finck * parse SET
470c2c66affSColin Finck */
471c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
472c2c66affSColin Finck MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
473c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
474c2c66affSColin Finck
475c2c66affSColin Finck end_set = *p + set_len;
476c2c66affSColin Finck
477c2c66affSColin Finck while( 1 )
478c2c66affSColin Finck {
479c2c66affSColin Finck if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
480c2c66affSColin Finck return( ret );
481c2c66affSColin Finck
482c2c66affSColin Finck if( *p == end_set )
483c2c66affSColin Finck break;
484c2c66affSColin Finck
485c2c66affSColin Finck /* Mark this item as being no the only one in a set */
486c2c66affSColin Finck cur->next_merged = 1;
487c2c66affSColin Finck
488c2c66affSColin Finck cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
489c2c66affSColin Finck
490c2c66affSColin Finck if( cur->next == NULL )
491c2c66affSColin Finck return( MBEDTLS_ERR_X509_ALLOC_FAILED );
492c2c66affSColin Finck
493c2c66affSColin Finck cur = cur->next;
494c2c66affSColin Finck }
495c2c66affSColin Finck
496c2c66affSColin Finck /*
497c2c66affSColin Finck * continue until end of SEQUENCE is reached
498c2c66affSColin Finck */
499c2c66affSColin Finck if( *p == end )
500c2c66affSColin Finck return( 0 );
501c2c66affSColin Finck
502c2c66affSColin Finck cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
503c2c66affSColin Finck
504c2c66affSColin Finck if( cur->next == NULL )
505c2c66affSColin Finck return( MBEDTLS_ERR_X509_ALLOC_FAILED );
506c2c66affSColin Finck
507c2c66affSColin Finck cur = cur->next;
508c2c66affSColin Finck }
509c2c66affSColin Finck }
510c2c66affSColin Finck
x509_parse_int(unsigned char ** p,size_t n,int * res)511c2c66affSColin Finck static int x509_parse_int( unsigned char **p, size_t n, int *res )
512c2c66affSColin Finck {
513c2c66affSColin Finck *res = 0;
514c2c66affSColin Finck
515c2c66affSColin Finck for( ; n > 0; --n )
516c2c66affSColin Finck {
517c2c66affSColin Finck if( ( **p < '0') || ( **p > '9' ) )
518c2c66affSColin Finck return ( MBEDTLS_ERR_X509_INVALID_DATE );
519c2c66affSColin Finck
520c2c66affSColin Finck *res *= 10;
521c2c66affSColin Finck *res += ( *(*p)++ - '0' );
522c2c66affSColin Finck }
523c2c66affSColin Finck
524c2c66affSColin Finck return( 0 );
525c2c66affSColin Finck }
526c2c66affSColin Finck
x509_date_is_valid(const mbedtls_x509_time * t)527c2c66affSColin Finck static int x509_date_is_valid(const mbedtls_x509_time *t )
528c2c66affSColin Finck {
529c2c66affSColin Finck int ret = MBEDTLS_ERR_X509_INVALID_DATE;
530d9e6c9b5SThomas Faber int month_len;
531c2c66affSColin Finck
532c2c66affSColin Finck CHECK_RANGE( 0, 9999, t->year );
533c2c66affSColin Finck CHECK_RANGE( 0, 23, t->hour );
534c2c66affSColin Finck CHECK_RANGE( 0, 59, t->min );
535c2c66affSColin Finck CHECK_RANGE( 0, 59, t->sec );
536c2c66affSColin Finck
537c2c66affSColin Finck switch( t->mon )
538c2c66affSColin Finck {
539c2c66affSColin Finck case 1: case 3: case 5: case 7: case 8: case 10: case 12:
540d9e6c9b5SThomas Faber month_len = 31;
541c2c66affSColin Finck break;
542c2c66affSColin Finck case 4: case 6: case 9: case 11:
543d9e6c9b5SThomas Faber month_len = 30;
544c2c66affSColin Finck break;
545c2c66affSColin Finck case 2:
546d9e6c9b5SThomas Faber if( ( !( t->year % 4 ) && t->year % 100 ) ||
547d9e6c9b5SThomas Faber !( t->year % 400 ) )
548d9e6c9b5SThomas Faber month_len = 29;
549d9e6c9b5SThomas Faber else
550d9e6c9b5SThomas Faber month_len = 28;
551c2c66affSColin Finck break;
552c2c66affSColin Finck default:
553c2c66affSColin Finck return( ret );
554c2c66affSColin Finck }
555d9e6c9b5SThomas Faber CHECK_RANGE( 1, month_len, t->day );
556c2c66affSColin Finck
557c2c66affSColin Finck return( 0 );
558c2c66affSColin Finck }
559c2c66affSColin Finck
560c2c66affSColin Finck /*
561c2c66affSColin Finck * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
562c2c66affSColin Finck * field.
563c2c66affSColin Finck */
x509_parse_time(unsigned char ** p,size_t len,size_t yearlen,mbedtls_x509_time * tm)564c2c66affSColin Finck static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
565c2c66affSColin Finck mbedtls_x509_time *tm )
566c2c66affSColin Finck {
567c2c66affSColin Finck int ret;
568c2c66affSColin Finck
569c2c66affSColin Finck /*
570c2c66affSColin Finck * Minimum length is 10 or 12 depending on yearlen
571c2c66affSColin Finck */
572c2c66affSColin Finck if ( len < yearlen + 8 )
573c2c66affSColin Finck return ( MBEDTLS_ERR_X509_INVALID_DATE );
574c2c66affSColin Finck len -= yearlen + 8;
575c2c66affSColin Finck
576c2c66affSColin Finck /*
577c2c66affSColin Finck * Parse year, month, day, hour, minute
578c2c66affSColin Finck */
579c2c66affSColin Finck CHECK( x509_parse_int( p, yearlen, &tm->year ) );
580c2c66affSColin Finck if ( 2 == yearlen )
581c2c66affSColin Finck {
582c2c66affSColin Finck if ( tm->year < 50 )
583c2c66affSColin Finck tm->year += 100;
584c2c66affSColin Finck
585c2c66affSColin Finck tm->year += 1900;
586c2c66affSColin Finck }
587c2c66affSColin Finck
588c2c66affSColin Finck CHECK( x509_parse_int( p, 2, &tm->mon ) );
589c2c66affSColin Finck CHECK( x509_parse_int( p, 2, &tm->day ) );
590c2c66affSColin Finck CHECK( x509_parse_int( p, 2, &tm->hour ) );
591c2c66affSColin Finck CHECK( x509_parse_int( p, 2, &tm->min ) );
592c2c66affSColin Finck
593c2c66affSColin Finck /*
594c2c66affSColin Finck * Parse seconds if present
595c2c66affSColin Finck */
596c2c66affSColin Finck if ( len >= 2 )
597c2c66affSColin Finck {
598c2c66affSColin Finck CHECK( x509_parse_int( p, 2, &tm->sec ) );
599c2c66affSColin Finck len -= 2;
600c2c66affSColin Finck }
601c2c66affSColin Finck else
602c2c66affSColin Finck return ( MBEDTLS_ERR_X509_INVALID_DATE );
603c2c66affSColin Finck
604c2c66affSColin Finck /*
605c2c66affSColin Finck * Parse trailing 'Z' if present
606c2c66affSColin Finck */
607c2c66affSColin Finck if ( 1 == len && 'Z' == **p )
608c2c66affSColin Finck {
609c2c66affSColin Finck (*p)++;
610c2c66affSColin Finck len--;
611c2c66affSColin Finck }
612c2c66affSColin Finck
613c2c66affSColin Finck /*
614c2c66affSColin Finck * We should have parsed all characters at this point
615c2c66affSColin Finck */
616c2c66affSColin Finck if ( 0 != len )
617c2c66affSColin Finck return ( MBEDTLS_ERR_X509_INVALID_DATE );
618c2c66affSColin Finck
619c2c66affSColin Finck CHECK( x509_date_is_valid( tm ) );
620c2c66affSColin Finck
621c2c66affSColin Finck return ( 0 );
622c2c66affSColin Finck }
623c2c66affSColin Finck
624c2c66affSColin Finck /*
625c2c66affSColin Finck * Time ::= CHOICE {
626c2c66affSColin Finck * utcTime UTCTime,
627c2c66affSColin Finck * generalTime GeneralizedTime }
628c2c66affSColin Finck */
mbedtls_x509_get_time(unsigned char ** p,const unsigned char * end,mbedtls_x509_time * tm)629c2c66affSColin Finck int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
630c2c66affSColin Finck mbedtls_x509_time *tm )
631c2c66affSColin Finck {
632c2c66affSColin Finck int ret;
633c2c66affSColin Finck size_t len, year_len;
634c2c66affSColin Finck unsigned char tag;
635c2c66affSColin Finck
636c2c66affSColin Finck if( ( end - *p ) < 1 )
637c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_DATE +
638c2c66affSColin Finck MBEDTLS_ERR_ASN1_OUT_OF_DATA );
639c2c66affSColin Finck
640c2c66affSColin Finck tag = **p;
641c2c66affSColin Finck
642c2c66affSColin Finck if( tag == MBEDTLS_ASN1_UTC_TIME )
643c2c66affSColin Finck year_len = 2;
644c2c66affSColin Finck else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
645c2c66affSColin Finck year_len = 4;
646c2c66affSColin Finck else
647c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_DATE +
648c2c66affSColin Finck MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
649c2c66affSColin Finck
650c2c66affSColin Finck (*p)++;
651c2c66affSColin Finck ret = mbedtls_asn1_get_len( p, end, &len );
652c2c66affSColin Finck
653c2c66affSColin Finck if( ret != 0 )
654c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
655c2c66affSColin Finck
656c2c66affSColin Finck return x509_parse_time( p, len, year_len, tm );
657c2c66affSColin Finck }
658c2c66affSColin Finck
mbedtls_x509_get_sig(unsigned char ** p,const unsigned char * end,mbedtls_x509_buf * sig)659c2c66affSColin Finck int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
660c2c66affSColin Finck {
661c2c66affSColin Finck int ret;
662c2c66affSColin Finck size_t len;
663c2c66affSColin Finck int tag_type;
664c2c66affSColin Finck
665c2c66affSColin Finck if( ( end - *p ) < 1 )
666c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
667c2c66affSColin Finck MBEDTLS_ERR_ASN1_OUT_OF_DATA );
668c2c66affSColin Finck
669c2c66affSColin Finck tag_type = **p;
670c2c66affSColin Finck
671c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
672c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
673c2c66affSColin Finck
674c2c66affSColin Finck sig->tag = tag_type;
675c2c66affSColin Finck sig->len = len;
676c2c66affSColin Finck sig->p = *p;
677c2c66affSColin Finck
678c2c66affSColin Finck *p += len;
679c2c66affSColin Finck
680c2c66affSColin Finck return( 0 );
681c2c66affSColin Finck }
682c2c66affSColin Finck
683c2c66affSColin Finck /*
684c2c66affSColin Finck * Get signature algorithm from alg OID and optional parameters
685c2c66affSColin Finck */
mbedtls_x509_get_sig_alg(const mbedtls_x509_buf * sig_oid,const mbedtls_x509_buf * sig_params,mbedtls_md_type_t * md_alg,mbedtls_pk_type_t * pk_alg,void ** sig_opts)686c2c66affSColin Finck int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
687c2c66affSColin Finck mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
688c2c66affSColin Finck void **sig_opts )
689c2c66affSColin Finck {
690c2c66affSColin Finck int ret;
691c2c66affSColin Finck
692c2c66affSColin Finck if( *sig_opts != NULL )
693c2c66affSColin Finck return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
694c2c66affSColin Finck
695c2c66affSColin Finck if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
696c2c66affSColin Finck return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
697c2c66affSColin Finck
698c2c66affSColin Finck #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
699c2c66affSColin Finck if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
700c2c66affSColin Finck {
701c2c66affSColin Finck mbedtls_pk_rsassa_pss_options *pss_opts;
702c2c66affSColin Finck
703c2c66affSColin Finck pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
704c2c66affSColin Finck if( pss_opts == NULL )
705c2c66affSColin Finck return( MBEDTLS_ERR_X509_ALLOC_FAILED );
706c2c66affSColin Finck
707c2c66affSColin Finck ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
708c2c66affSColin Finck md_alg,
709c2c66affSColin Finck &pss_opts->mgf1_hash_id,
710c2c66affSColin Finck &pss_opts->expected_salt_len );
711c2c66affSColin Finck if( ret != 0 )
712c2c66affSColin Finck {
713c2c66affSColin Finck mbedtls_free( pss_opts );
714c2c66affSColin Finck return( ret );
715c2c66affSColin Finck }
716c2c66affSColin Finck
717c2c66affSColin Finck *sig_opts = (void *) pss_opts;
718c2c66affSColin Finck }
719c2c66affSColin Finck else
720c2c66affSColin Finck #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
721c2c66affSColin Finck {
722c2c66affSColin Finck /* Make sure parameters are absent or NULL */
723c2c66affSColin Finck if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
724c2c66affSColin Finck sig_params->len != 0 )
725c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_ALG );
726c2c66affSColin Finck }
727c2c66affSColin Finck
728c2c66affSColin Finck return( 0 );
729c2c66affSColin Finck }
730c2c66affSColin Finck
731c2c66affSColin Finck /*
732c2c66affSColin Finck * X.509 Extensions (No parsing of extensions, pointer should
733c2c66affSColin Finck * be either manually updated or extensions should be parsed!)
734c2c66affSColin Finck */
mbedtls_x509_get_ext(unsigned char ** p,const unsigned char * end,mbedtls_x509_buf * ext,int tag)735c2c66affSColin Finck int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
736c2c66affSColin Finck mbedtls_x509_buf *ext, int tag )
737c2c66affSColin Finck {
738c2c66affSColin Finck int ret;
739c2c66affSColin Finck size_t len;
740c2c66affSColin Finck
741430656f0SThomas Faber /* Extension structure use EXPLICIT tagging. That is, the actual
742430656f0SThomas Faber * `Extensions` structure is wrapped by a tag-length pair using
743430656f0SThomas Faber * the respective context-specific tag. */
744430656f0SThomas Faber ret = mbedtls_asn1_get_tag( p, end, &ext->len,
745430656f0SThomas Faber MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
746430656f0SThomas Faber if( ret != 0 )
747430656f0SThomas Faber return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
748c2c66affSColin Finck
749430656f0SThomas Faber ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
750c2c66affSColin Finck ext->p = *p;
751c2c66affSColin Finck end = *p + ext->len;
752c2c66affSColin Finck
753c2c66affSColin Finck /*
754c2c66affSColin Finck * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
755c2c66affSColin Finck */
756c2c66affSColin Finck if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
757c2c66affSColin Finck MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
758c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
759c2c66affSColin Finck
760c2c66affSColin Finck if( end != *p + len )
761c2c66affSColin Finck return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
762c2c66affSColin Finck MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
763c2c66affSColin Finck
764c2c66affSColin Finck return( 0 );
765c2c66affSColin Finck }
766c2c66affSColin Finck
767c2c66affSColin Finck /*
768c2c66affSColin Finck * Store the name in printable form into buf; no more
769c2c66affSColin Finck * than size characters will be written
770c2c66affSColin Finck */
mbedtls_x509_dn_gets(char * buf,size_t size,const mbedtls_x509_name * dn)771c2c66affSColin Finck int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
772c2c66affSColin Finck {
773c2c66affSColin Finck int ret;
774c2c66affSColin Finck size_t i, n;
775c2c66affSColin Finck unsigned char c, merge = 0;
776c2c66affSColin Finck const mbedtls_x509_name *name;
777c2c66affSColin Finck const char *short_name = NULL;
778c2c66affSColin Finck char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
779c2c66affSColin Finck
780c2c66affSColin Finck memset( s, 0, sizeof( s ) );
781c2c66affSColin Finck
782c2c66affSColin Finck name = dn;
783c2c66affSColin Finck p = buf;
784c2c66affSColin Finck n = size;
785c2c66affSColin Finck
786c2c66affSColin Finck while( name != NULL )
787c2c66affSColin Finck {
788c2c66affSColin Finck if( !name->oid.p )
789c2c66affSColin Finck {
790c2c66affSColin Finck name = name->next;
791c2c66affSColin Finck continue;
792c2c66affSColin Finck }
793c2c66affSColin Finck
794c2c66affSColin Finck if( name != dn )
795c2c66affSColin Finck {
796c2c66affSColin Finck ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
797c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
798c2c66affSColin Finck }
799c2c66affSColin Finck
800c2c66affSColin Finck ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
801c2c66affSColin Finck
802c2c66affSColin Finck if( ret == 0 )
803c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "%s=", short_name );
804c2c66affSColin Finck else
805c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "\?\?=" );
806c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
807c2c66affSColin Finck
808c2c66affSColin Finck for( i = 0; i < name->val.len; i++ )
809c2c66affSColin Finck {
810c2c66affSColin Finck if( i >= sizeof( s ) - 1 )
811c2c66affSColin Finck break;
812c2c66affSColin Finck
813c2c66affSColin Finck c = name->val.p[i];
814c2c66affSColin Finck if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
815c2c66affSColin Finck s[i] = '?';
816c2c66affSColin Finck else s[i] = c;
817c2c66affSColin Finck }
818c2c66affSColin Finck s[i] = '\0';
819c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "%s", s );
820c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
821c2c66affSColin Finck
822c2c66affSColin Finck merge = name->next_merged;
823c2c66affSColin Finck name = name->next;
824c2c66affSColin Finck }
825c2c66affSColin Finck
826c2c66affSColin Finck return( (int) ( size - n ) );
827c2c66affSColin Finck }
828c2c66affSColin Finck
829c2c66affSColin Finck /*
830c2c66affSColin Finck * Store the serial in printable form into buf; no more
831c2c66affSColin Finck * than size characters will be written
832c2c66affSColin Finck */
mbedtls_x509_serial_gets(char * buf,size_t size,const mbedtls_x509_buf * serial)833c2c66affSColin Finck int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
834c2c66affSColin Finck {
835c2c66affSColin Finck int ret;
836c2c66affSColin Finck size_t i, n, nr;
837c2c66affSColin Finck char *p;
838c2c66affSColin Finck
839c2c66affSColin Finck p = buf;
840c2c66affSColin Finck n = size;
841c2c66affSColin Finck
842c2c66affSColin Finck nr = ( serial->len <= 32 )
843c2c66affSColin Finck ? serial->len : 28;
844c2c66affSColin Finck
845c2c66affSColin Finck for( i = 0; i < nr; i++ )
846c2c66affSColin Finck {
847c2c66affSColin Finck if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
848c2c66affSColin Finck continue;
849c2c66affSColin Finck
850c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "%02X%s",
851c2c66affSColin Finck serial->p[i], ( i < nr - 1 ) ? ":" : "" );
852c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
853c2c66affSColin Finck }
854c2c66affSColin Finck
855c2c66affSColin Finck if( nr != serial->len )
856c2c66affSColin Finck {
857c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "...." );
858c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
859c2c66affSColin Finck }
860c2c66affSColin Finck
861c2c66affSColin Finck return( (int) ( size - n ) );
862c2c66affSColin Finck }
863c2c66affSColin Finck
864c2c66affSColin Finck /*
865c2c66affSColin Finck * Helper for writing signature algorithms
866c2c66affSColin Finck */
mbedtls_x509_sig_alg_gets(char * buf,size_t size,const mbedtls_x509_buf * sig_oid,mbedtls_pk_type_t pk_alg,mbedtls_md_type_t md_alg,const void * sig_opts)867c2c66affSColin Finck int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
868c2c66affSColin Finck mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
869c2c66affSColin Finck const void *sig_opts )
870c2c66affSColin Finck {
871c2c66affSColin Finck int ret;
872c2c66affSColin Finck char *p = buf;
873c2c66affSColin Finck size_t n = size;
874c2c66affSColin Finck const char *desc = NULL;
875c2c66affSColin Finck
876c2c66affSColin Finck ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
877c2c66affSColin Finck if( ret != 0 )
878c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "???" );
879c2c66affSColin Finck else
880c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "%s", desc );
881c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
882c2c66affSColin Finck
883c2c66affSColin Finck #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
884c2c66affSColin Finck if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
885c2c66affSColin Finck {
886c2c66affSColin Finck const mbedtls_pk_rsassa_pss_options *pss_opts;
887c2c66affSColin Finck const mbedtls_md_info_t *md_info, *mgf_md_info;
888c2c66affSColin Finck
889c2c66affSColin Finck pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
890c2c66affSColin Finck
891c2c66affSColin Finck md_info = mbedtls_md_info_from_type( md_alg );
892c2c66affSColin Finck mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
893c2c66affSColin Finck
894c2c66affSColin Finck ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
895c2c66affSColin Finck md_info ? mbedtls_md_get_name( md_info ) : "???",
896c2c66affSColin Finck mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
897c2c66affSColin Finck pss_opts->expected_salt_len );
898c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
899c2c66affSColin Finck }
900c2c66affSColin Finck #else
901c2c66affSColin Finck ((void) pk_alg);
902c2c66affSColin Finck ((void) md_alg);
903c2c66affSColin Finck ((void) sig_opts);
904c2c66affSColin Finck #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
905c2c66affSColin Finck
906c2c66affSColin Finck return( (int)( size - n ) );
907c2c66affSColin Finck }
908c2c66affSColin Finck
909c2c66affSColin Finck /*
910c2c66affSColin Finck * Helper for writing "RSA key size", "EC key size", etc
911c2c66affSColin Finck */
mbedtls_x509_key_size_helper(char * buf,size_t buf_size,const char * name)912c2c66affSColin Finck int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
913c2c66affSColin Finck {
914c2c66affSColin Finck char *p = buf;
915c2c66affSColin Finck size_t n = buf_size;
916c2c66affSColin Finck int ret;
917c2c66affSColin Finck
918c2c66affSColin Finck ret = mbedtls_snprintf( p, n, "%s key size", name );
919c2c66affSColin Finck MBEDTLS_X509_SAFE_SNPRINTF;
920c2c66affSColin Finck
921c2c66affSColin Finck return( 0 );
922c2c66affSColin Finck }
923c2c66affSColin Finck
924c2c66affSColin Finck #if defined(MBEDTLS_HAVE_TIME_DATE)
925c2c66affSColin Finck /*
926c2c66affSColin Finck * Set the time structure to the current time.
927c2c66affSColin Finck * Return 0 on success, non-zero on failure.
928c2c66affSColin Finck */
x509_get_current_time(mbedtls_x509_time * now)929c2c66affSColin Finck static int x509_get_current_time( mbedtls_x509_time *now )
930c2c66affSColin Finck {
931*cbda039fSThomas Faber struct tm *lt, tm_buf;
932c2c66affSColin Finck mbedtls_time_t tt;
933c2c66affSColin Finck int ret = 0;
934c2c66affSColin Finck
935c2c66affSColin Finck tt = mbedtls_time( NULL );
936*cbda039fSThomas Faber lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
937c2c66affSColin Finck
938c2c66affSColin Finck if( lt == NULL )
939c2c66affSColin Finck ret = -1;
940c2c66affSColin Finck else
941c2c66affSColin Finck {
942c2c66affSColin Finck now->year = lt->tm_year + 1900;
943c2c66affSColin Finck now->mon = lt->tm_mon + 1;
944c2c66affSColin Finck now->day = lt->tm_mday;
945c2c66affSColin Finck now->hour = lt->tm_hour;
946c2c66affSColin Finck now->min = lt->tm_min;
947c2c66affSColin Finck now->sec = lt->tm_sec;
948c2c66affSColin Finck }
949c2c66affSColin Finck
950c2c66affSColin Finck return( ret );
951c2c66affSColin Finck }
952c2c66affSColin Finck
953c2c66affSColin Finck /*
954c2c66affSColin Finck * Return 0 if before <= after, 1 otherwise
955c2c66affSColin Finck */
x509_check_time(const mbedtls_x509_time * before,const mbedtls_x509_time * after)956c2c66affSColin Finck static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
957c2c66affSColin Finck {
958c2c66affSColin Finck if( before->year > after->year )
959c2c66affSColin Finck return( 1 );
960c2c66affSColin Finck
961c2c66affSColin Finck if( before->year == after->year &&
962c2c66affSColin Finck before->mon > after->mon )
963c2c66affSColin Finck return( 1 );
964c2c66affSColin Finck
965c2c66affSColin Finck if( before->year == after->year &&
966c2c66affSColin Finck before->mon == after->mon &&
967c2c66affSColin Finck before->day > after->day )
968c2c66affSColin Finck return( 1 );
969c2c66affSColin Finck
970c2c66affSColin Finck if( before->year == after->year &&
971c2c66affSColin Finck before->mon == after->mon &&
972c2c66affSColin Finck before->day == after->day &&
973c2c66affSColin Finck before->hour > after->hour )
974c2c66affSColin Finck return( 1 );
975c2c66affSColin Finck
976c2c66affSColin Finck if( before->year == after->year &&
977c2c66affSColin Finck before->mon == after->mon &&
978c2c66affSColin Finck before->day == after->day &&
979c2c66affSColin Finck before->hour == after->hour &&
980c2c66affSColin Finck before->min > after->min )
981c2c66affSColin Finck return( 1 );
982c2c66affSColin Finck
983c2c66affSColin Finck if( before->year == after->year &&
984c2c66affSColin Finck before->mon == after->mon &&
985c2c66affSColin Finck before->day == after->day &&
986c2c66affSColin Finck before->hour == after->hour &&
987c2c66affSColin Finck before->min == after->min &&
988c2c66affSColin Finck before->sec > after->sec )
989c2c66affSColin Finck return( 1 );
990c2c66affSColin Finck
991c2c66affSColin Finck return( 0 );
992c2c66affSColin Finck }
993c2c66affSColin Finck
mbedtls_x509_time_is_past(const mbedtls_x509_time * to)994c2c66affSColin Finck int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
995c2c66affSColin Finck {
996c2c66affSColin Finck mbedtls_x509_time now;
997c2c66affSColin Finck
998c2c66affSColin Finck if( x509_get_current_time( &now ) != 0 )
999c2c66affSColin Finck return( 1 );
1000c2c66affSColin Finck
1001c2c66affSColin Finck return( x509_check_time( &now, to ) );
1002c2c66affSColin Finck }
1003c2c66affSColin Finck
mbedtls_x509_time_is_future(const mbedtls_x509_time * from)1004c2c66affSColin Finck int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
1005c2c66affSColin Finck {
1006c2c66affSColin Finck mbedtls_x509_time now;
1007c2c66affSColin Finck
1008c2c66affSColin Finck if( x509_get_current_time( &now ) != 0 )
1009c2c66affSColin Finck return( 1 );
1010c2c66affSColin Finck
1011c2c66affSColin Finck return( x509_check_time( from, &now ) );
1012c2c66affSColin Finck }
1013c2c66affSColin Finck
1014c2c66affSColin Finck #else /* MBEDTLS_HAVE_TIME_DATE */
1015c2c66affSColin Finck
mbedtls_x509_time_is_past(const mbedtls_x509_time * to)1016c2c66affSColin Finck int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
1017c2c66affSColin Finck {
1018c2c66affSColin Finck ((void) to);
1019c2c66affSColin Finck return( 0 );
1020c2c66affSColin Finck }
1021c2c66affSColin Finck
mbedtls_x509_time_is_future(const mbedtls_x509_time * from)1022c2c66affSColin Finck int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
1023c2c66affSColin Finck {
1024c2c66affSColin Finck ((void) from);
1025c2c66affSColin Finck return( 0 );
1026c2c66affSColin Finck }
1027c2c66affSColin Finck #endif /* MBEDTLS_HAVE_TIME_DATE */
1028c2c66affSColin Finck
1029c2c66affSColin Finck #if defined(MBEDTLS_SELF_TEST)
1030c2c66affSColin Finck
1031c2c66affSColin Finck #include "mbedtls/x509_crt.h"
1032c2c66affSColin Finck #include "mbedtls/certs.h"
1033c2c66affSColin Finck
1034c2c66affSColin Finck /*
1035c2c66affSColin Finck * Checkup routine
1036c2c66affSColin Finck */
mbedtls_x509_self_test(int verbose)1037c2c66affSColin Finck int mbedtls_x509_self_test( int verbose )
1038c2c66affSColin Finck {
1039430656f0SThomas Faber int ret = 0;
1040c2c66affSColin Finck #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
1041c2c66affSColin Finck uint32_t flags;
1042c2c66affSColin Finck mbedtls_x509_crt cacert;
1043c2c66affSColin Finck mbedtls_x509_crt clicert;
1044c2c66affSColin Finck
1045c2c66affSColin Finck if( verbose != 0 )
1046c2c66affSColin Finck mbedtls_printf( " X.509 certificate load: " );
1047c2c66affSColin Finck
1048430656f0SThomas Faber mbedtls_x509_crt_init( &cacert );
1049c2c66affSColin Finck mbedtls_x509_crt_init( &clicert );
1050c2c66affSColin Finck
1051c2c66affSColin Finck ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1052c2c66affSColin Finck mbedtls_test_cli_crt_len );
1053c2c66affSColin Finck if( ret != 0 )
1054c2c66affSColin Finck {
1055c2c66affSColin Finck if( verbose != 0 )
1056c2c66affSColin Finck mbedtls_printf( "failed\n" );
1057c2c66affSColin Finck
1058430656f0SThomas Faber goto cleanup;
1059c2c66affSColin Finck }
1060c2c66affSColin Finck
1061c2c66affSColin Finck ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
1062c2c66affSColin Finck mbedtls_test_ca_crt_len );
1063c2c66affSColin Finck if( ret != 0 )
1064c2c66affSColin Finck {
1065c2c66affSColin Finck if( verbose != 0 )
1066c2c66affSColin Finck mbedtls_printf( "failed\n" );
1067c2c66affSColin Finck
1068430656f0SThomas Faber goto cleanup;
1069c2c66affSColin Finck }
1070c2c66affSColin Finck
1071c2c66affSColin Finck if( verbose != 0 )
1072c2c66affSColin Finck mbedtls_printf( "passed\n X.509 signature verify: ");
1073c2c66affSColin Finck
1074c2c66affSColin Finck ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
1075c2c66affSColin Finck if( ret != 0 )
1076c2c66affSColin Finck {
1077c2c66affSColin Finck if( verbose != 0 )
1078c2c66affSColin Finck mbedtls_printf( "failed\n" );
1079c2c66affSColin Finck
1080430656f0SThomas Faber goto cleanup;
1081c2c66affSColin Finck }
1082c2c66affSColin Finck
1083c2c66affSColin Finck if( verbose != 0 )
1084c2c66affSColin Finck mbedtls_printf( "passed\n\n");
1085c2c66affSColin Finck
1086430656f0SThomas Faber cleanup:
1087c2c66affSColin Finck mbedtls_x509_crt_free( &cacert );
1088c2c66affSColin Finck mbedtls_x509_crt_free( &clicert );
1089c2c66affSColin Finck #else
1090c2c66affSColin Finck ((void) verbose);
1091c58d7a6dSThomas Faber #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
1092430656f0SThomas Faber return( ret );
1093c2c66affSColin Finck }
1094c2c66affSColin Finck
1095c2c66affSColin Finck #endif /* MBEDTLS_SELF_TEST */
1096c2c66affSColin Finck
1097c2c66affSColin Finck #endif /* MBEDTLS_X509_USE_C */
1098