1 /*
2  *  X.509 Certificate Signing Request (CSR) parsing
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 /*
22  *  The ITU-T X.509 standard defines a certificate format for PKI.
23  *
24  *  http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25  *  http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26  *  http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
27  *
28  *  http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29  *  http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30  */
31 
32 #if !defined(MBEDTLS_CONFIG_FILE)
33 #include "mbedtls/config.h"
34 #else
35 #include MBEDTLS_CONFIG_FILE
36 #endif
37 
38 #if defined(MBEDTLS_X509_CSR_PARSE_C)
39 
40 #include "mbedtls/x509_csr.h"
41 #include "mbedtls/oid.h"
42 
43 #include <string.h>
44 
45 #if defined(MBEDTLS_PEM_PARSE_C)
46 #include "mbedtls/pem.h"
47 #endif
48 
49 #if defined(MBEDTLS_PLATFORM_C)
50 #include "mbedtls/platform.h"
51 #else
52 #include <stdlib.h>
53 #include <stdio.h>
54 #define mbedtls_free       free
55 #define mbedtls_calloc    calloc
56 #define mbedtls_snprintf   snprintf
57 #endif
58 
59 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
60 #include <stdio.h>
61 #endif
62 
63 /* Implementation that should never be optimized out by the compiler */
mbedtls_zeroize(void * v,size_t n)64 static void mbedtls_zeroize( void *v, size_t n ) {
65     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
66 }
67 
68 /*
69  *  Version  ::=  INTEGER  {  v1(0)  }
70  */
x509_csr_get_version(unsigned char ** p,const unsigned char * end,int * ver)71 static int x509_csr_get_version( unsigned char **p,
72                              const unsigned char *end,
73                              int *ver )
74 {
75     int ret;
76 
77     if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
78     {
79         if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
80         {
81             *ver = 0;
82             return( 0 );
83         }
84 
85         return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
86     }
87 
88     return( 0 );
89 }
90 
91 /*
92  * Parse a CSR in DER format
93  */
mbedtls_x509_csr_parse_der(mbedtls_x509_csr * csr,const unsigned char * buf,size_t buflen)94 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
95                         const unsigned char *buf, size_t buflen )
96 {
97     int ret;
98     size_t len;
99     unsigned char *p, *end;
100     mbedtls_x509_buf sig_params;
101 
102     memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) );
103 
104     /*
105      * Check for valid input
106      */
107     if( csr == NULL || buf == NULL || buflen == 0 )
108         return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
109 
110     mbedtls_x509_csr_init( csr );
111 
112     /*
113      * first copy the raw DER data
114      */
115     p = mbedtls_calloc( 1, len = buflen );
116 
117     if( p == NULL )
118         return( MBEDTLS_ERR_X509_ALLOC_FAILED );
119 
120     memcpy( p, buf, buflen );
121 
122     csr->raw.p = p;
123     csr->raw.len = len;
124     end = p + len;
125 
126     /*
127      *  CertificationRequest ::= SEQUENCE {
128      *       certificationRequestInfo CertificationRequestInfo,
129      *       signatureAlgorithm AlgorithmIdentifier,
130      *       signature          BIT STRING
131      *  }
132      */
133     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
134             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
135     {
136         mbedtls_x509_csr_free( csr );
137         return( MBEDTLS_ERR_X509_INVALID_FORMAT );
138     }
139 
140     if( len != (size_t) ( end - p ) )
141     {
142         mbedtls_x509_csr_free( csr );
143         return( MBEDTLS_ERR_X509_INVALID_FORMAT +
144                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
145     }
146 
147     /*
148      *  CertificationRequestInfo ::= SEQUENCE {
149      */
150     csr->cri.p = p;
151 
152     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
153             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
154     {
155         mbedtls_x509_csr_free( csr );
156         return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
157     }
158 
159     end = p + len;
160     csr->cri.len = end - csr->cri.p;
161 
162     /*
163      *  Version  ::=  INTEGER {  v1(0) }
164      */
165     if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
166     {
167         mbedtls_x509_csr_free( csr );
168         return( ret );
169     }
170 
171     csr->version++;
172 
173     if( csr->version != 1 )
174     {
175         mbedtls_x509_csr_free( csr );
176         return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
177     }
178 
179     /*
180      *  subject               Name
181      */
182     csr->subject_raw.p = p;
183 
184     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
185             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
186     {
187         mbedtls_x509_csr_free( csr );
188         return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
189     }
190 
191     if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
192     {
193         mbedtls_x509_csr_free( csr );
194         return( ret );
195     }
196 
197     csr->subject_raw.len = p - csr->subject_raw.p;
198 
199     /*
200      *  subjectPKInfo SubjectPublicKeyInfo
201      */
202     if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
203     {
204         mbedtls_x509_csr_free( csr );
205         return( ret );
206     }
207 
208     /*
209      *  attributes    [0] Attributes
210      *
211      *  The list of possible attributes is open-ended, though RFC 2985
212      *  (PKCS#9) defines a few in section 5.4. We currently don't support any,
213      *  so we just ignore them. This is a safe thing to do as the worst thing
214      *  that could happen is that we issue a certificate that does not match
215      *  the requester's expectations - this cannot cause a violation of our
216      *  signature policies.
217      */
218     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
219             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
220     {
221         mbedtls_x509_csr_free( csr );
222         return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
223     }
224 
225     p += len;
226 
227     end = csr->raw.p + csr->raw.len;
228 
229     /*
230      *  signatureAlgorithm   AlgorithmIdentifier,
231      *  signature            BIT STRING
232      */
233     if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
234     {
235         mbedtls_x509_csr_free( csr );
236         return( ret );
237     }
238 
239     if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params,
240                                   &csr->sig_md, &csr->sig_pk,
241                                   &csr->sig_opts ) ) != 0 )
242     {
243         mbedtls_x509_csr_free( csr );
244         return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
245     }
246 
247     if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 )
248     {
249         mbedtls_x509_csr_free( csr );
250         return( ret );
251     }
252 
253     if( p != end )
254     {
255         mbedtls_x509_csr_free( csr );
256         return( MBEDTLS_ERR_X509_INVALID_FORMAT +
257                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
258     }
259 
260     return( 0 );
261 }
262 
263 /*
264  * Parse a CSR, allowing for PEM or raw DER encoding
265  */
mbedtls_x509_csr_parse(mbedtls_x509_csr * csr,const unsigned char * buf,size_t buflen)266 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen )
267 {
268     int ret;
269 #if defined(MBEDTLS_PEM_PARSE_C)
270     size_t use_len;
271     mbedtls_pem_context pem;
272 #endif
273 
274     /*
275      * Check for valid input
276      */
277     if( csr == NULL || buf == NULL || buflen == 0 )
278         return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
279 
280 #if defined(MBEDTLS_PEM_PARSE_C)
281     mbedtls_pem_init( &pem );
282 
283     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
284     if( buf[buflen - 1] != '\0' )
285         ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
286     else
287         ret = mbedtls_pem_read_buffer( &pem,
288                                "-----BEGIN CERTIFICATE REQUEST-----",
289                                "-----END CERTIFICATE REQUEST-----",
290                                buf, NULL, 0, &use_len );
291 
292     if( ret == 0 )
293     {
294         /*
295          * Was PEM encoded, parse the result
296          */
297         if( ( ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen ) ) != 0 )
298             return( ret );
299 
300         mbedtls_pem_free( &pem );
301         return( 0 );
302     }
303     else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
304     {
305         mbedtls_pem_free( &pem );
306         return( ret );
307     }
308     else
309 #endif /* MBEDTLS_PEM_PARSE_C */
310     return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) );
311 }
312 
313 #if defined(MBEDTLS_FS_IO)
314 /*
315  * Load a CSR into the structure
316  */
mbedtls_x509_csr_parse_file(mbedtls_x509_csr * csr,const char * path)317 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path )
318 {
319     int ret;
320     size_t n;
321     unsigned char *buf;
322 
323     if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
324         return( ret );
325 
326     ret = mbedtls_x509_csr_parse( csr, buf, n );
327 
328     mbedtls_zeroize( buf, n );
329     mbedtls_free( buf );
330 
331     return( ret );
332 }
333 #endif /* MBEDTLS_FS_IO */
334 
335 #define BEFORE_COLON    14
336 #define BC              "14"
337 /*
338  * Return an informational string about the CSR.
339  */
mbedtls_x509_csr_info(char * buf,size_t size,const char * prefix,const mbedtls_x509_csr * csr)340 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
341                    const mbedtls_x509_csr *csr )
342 {
343     int ret;
344     size_t n;
345     char *p;
346     char key_size_str[BEFORE_COLON];
347 
348     p = buf;
349     n = size;
350 
351     ret = mbedtls_snprintf( p, n, "%sCSR version   : %d",
352                                prefix, csr->version );
353     MBEDTLS_X509_SAFE_SNPRINTF;
354 
355     ret = mbedtls_snprintf( p, n, "\n%ssubject name  : ", prefix );
356     MBEDTLS_X509_SAFE_SNPRINTF;
357     ret = mbedtls_x509_dn_gets( p, n, &csr->subject );
358     MBEDTLS_X509_SAFE_SNPRINTF;
359 
360     ret = mbedtls_snprintf( p, n, "\n%ssigned using  : ", prefix );
361     MBEDTLS_X509_SAFE_SNPRINTF;
362 
363     ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
364                              csr->sig_opts );
365     MBEDTLS_X509_SAFE_SNPRINTF;
366 
367     if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
368                                       mbedtls_pk_get_name( &csr->pk ) ) ) != 0 )
369     {
370         return( ret );
371     }
372 
373     ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
374                           (int) mbedtls_pk_get_bitlen( &csr->pk ) );
375     MBEDTLS_X509_SAFE_SNPRINTF;
376 
377     return( (int) ( size - n ) );
378 }
379 
380 /*
381  * Initialize a CSR
382  */
mbedtls_x509_csr_init(mbedtls_x509_csr * csr)383 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr )
384 {
385     memset( csr, 0, sizeof(mbedtls_x509_csr) );
386 }
387 
388 /*
389  * Unallocate all CSR data
390  */
mbedtls_x509_csr_free(mbedtls_x509_csr * csr)391 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr )
392 {
393     mbedtls_x509_name *name_cur;
394     mbedtls_x509_name *name_prv;
395 
396     if( csr == NULL )
397         return;
398 
399     mbedtls_pk_free( &csr->pk );
400 
401 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
402     mbedtls_free( csr->sig_opts );
403 #endif
404 
405     name_cur = csr->subject.next;
406     while( name_cur != NULL )
407     {
408         name_prv = name_cur;
409         name_cur = name_cur->next;
410         mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
411         mbedtls_free( name_prv );
412     }
413 
414     if( csr->raw.p != NULL )
415     {
416         mbedtls_zeroize( csr->raw.p, csr->raw.len );
417         mbedtls_free( csr->raw.p );
418     }
419 
420     mbedtls_zeroize( csr, sizeof( mbedtls_x509_csr ) );
421 }
422 
423 #endif /* MBEDTLS_X509_CSR_PARSE_C */
424