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 #include "mbedtls/platform_util.h"
43 
44 #include <string.h>
45 
46 #if defined(MBEDTLS_PEM_PARSE_C)
47 #include "mbedtls/pem.h"
48 #endif
49 
50 #if defined(MBEDTLS_PLATFORM_C)
51 #include "mbedtls/platform.h"
52 #else
53 #include <stdlib.h>
54 #include <stdio.h>
55 #define mbedtls_free       free
56 #define mbedtls_calloc    calloc
57 #define mbedtls_snprintf   snprintf
58 #endif
59 
60 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
61 #include <stdio.h>
62 #endif
63 
64 /*
65  *  Version  ::=  INTEGER  {  v1(0)  }
66  */
x509_csr_get_version(unsigned char ** p,const unsigned char * end,int * ver)67 static int x509_csr_get_version( unsigned char **p,
68                              const unsigned char *end,
69                              int *ver )
70 {
71     int ret;
72 
73     if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
74     {
75         if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
76         {
77             *ver = 0;
78             return( 0 );
79         }
80 
81         return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
82     }
83 
84     return( 0 );
85 }
86 
87 /*
88  * Parse a CSR in DER format
89  */
mbedtls_x509_csr_parse_der(mbedtls_x509_csr * csr,const unsigned char * buf,size_t buflen)90 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
91                         const unsigned char *buf, size_t buflen )
92 {
93     int ret;
94     size_t len;
95     unsigned char *p, *end;
96     mbedtls_x509_buf sig_params;
97 
98     memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) );
99 
100     /*
101      * Check for valid input
102      */
103     if( csr == NULL || buf == NULL || buflen == 0 )
104         return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
105 
106     mbedtls_x509_csr_init( csr );
107 
108     /*
109      * first copy the raw DER data
110      */
111     p = mbedtls_calloc( 1, len = buflen );
112 
113     if( p == NULL )
114         return( MBEDTLS_ERR_X509_ALLOC_FAILED );
115 
116     memcpy( p, buf, buflen );
117 
118     csr->raw.p = p;
119     csr->raw.len = len;
120     end = p + len;
121 
122     /*
123      *  CertificationRequest ::= SEQUENCE {
124      *       certificationRequestInfo CertificationRequestInfo,
125      *       signatureAlgorithm AlgorithmIdentifier,
126      *       signature          BIT STRING
127      *  }
128      */
129     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
130             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
131     {
132         mbedtls_x509_csr_free( csr );
133         return( MBEDTLS_ERR_X509_INVALID_FORMAT );
134     }
135 
136     if( len != (size_t) ( end - p ) )
137     {
138         mbedtls_x509_csr_free( csr );
139         return( MBEDTLS_ERR_X509_INVALID_FORMAT +
140                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
141     }
142 
143     /*
144      *  CertificationRequestInfo ::= SEQUENCE {
145      */
146     csr->cri.p = p;
147 
148     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
149             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
150     {
151         mbedtls_x509_csr_free( csr );
152         return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
153     }
154 
155     end = p + len;
156     csr->cri.len = end - csr->cri.p;
157 
158     /*
159      *  Version  ::=  INTEGER {  v1(0) }
160      */
161     if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
162     {
163         mbedtls_x509_csr_free( csr );
164         return( ret );
165     }
166 
167     if( csr->version != 0 )
168     {
169         mbedtls_x509_csr_free( csr );
170         return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
171     }
172 
173     csr->version++;
174 
175     /*
176      *  subject               Name
177      */
178     csr->subject_raw.p = p;
179 
180     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
181             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
182     {
183         mbedtls_x509_csr_free( csr );
184         return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
185     }
186 
187     if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
188     {
189         mbedtls_x509_csr_free( csr );
190         return( ret );
191     }
192 
193     csr->subject_raw.len = p - csr->subject_raw.p;
194 
195     /*
196      *  subjectPKInfo SubjectPublicKeyInfo
197      */
198     if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
199     {
200         mbedtls_x509_csr_free( csr );
201         return( ret );
202     }
203 
204     /*
205      *  attributes    [0] Attributes
206      *
207      *  The list of possible attributes is open-ended, though RFC 2985
208      *  (PKCS#9) defines a few in section 5.4. We currently don't support any,
209      *  so we just ignore them. This is a safe thing to do as the worst thing
210      *  that could happen is that we issue a certificate that does not match
211      *  the requester's expectations - this cannot cause a violation of our
212      *  signature policies.
213      */
214     if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
215             MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
216     {
217         mbedtls_x509_csr_free( csr );
218         return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
219     }
220 
221     p += len;
222 
223     end = csr->raw.p + csr->raw.len;
224 
225     /*
226      *  signatureAlgorithm   AlgorithmIdentifier,
227      *  signature            BIT STRING
228      */
229     if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
230     {
231         mbedtls_x509_csr_free( csr );
232         return( ret );
233     }
234 
235     if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params,
236                                   &csr->sig_md, &csr->sig_pk,
237                                   &csr->sig_opts ) ) != 0 )
238     {
239         mbedtls_x509_csr_free( csr );
240         return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
241     }
242 
243     if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 )
244     {
245         mbedtls_x509_csr_free( csr );
246         return( ret );
247     }
248 
249     if( p != end )
250     {
251         mbedtls_x509_csr_free( csr );
252         return( MBEDTLS_ERR_X509_INVALID_FORMAT +
253                 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
254     }
255 
256     return( 0 );
257 }
258 
259 /*
260  * Parse a CSR, allowing for PEM or raw DER encoding
261  */
mbedtls_x509_csr_parse(mbedtls_x509_csr * csr,const unsigned char * buf,size_t buflen)262 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen )
263 {
264 #if defined(MBEDTLS_PEM_PARSE_C)
265     int ret;
266     size_t use_len;
267     mbedtls_pem_context pem;
268 #endif
269 
270     /*
271      * Check for valid input
272      */
273     if( csr == NULL || buf == NULL || buflen == 0 )
274         return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
275 
276 #if defined(MBEDTLS_PEM_PARSE_C)
277     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
278     if( buf[buflen - 1] == '\0' )
279     {
280         mbedtls_pem_init( &pem );
281         ret = mbedtls_pem_read_buffer( &pem,
282                                "-----BEGIN CERTIFICATE REQUEST-----",
283                                "-----END CERTIFICATE REQUEST-----",
284                                buf, NULL, 0, &use_len );
285 
286         if( ret == 0 )
287             /*
288              * Was PEM encoded, parse the result
289              */
290             ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen );
291 
292         mbedtls_pem_free( &pem );
293         if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
294             return( ret );
295     }
296 #endif /* MBEDTLS_PEM_PARSE_C */
297     return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) );
298 }
299 
300 #if defined(MBEDTLS_FS_IO)
301 /*
302  * Load a CSR into the structure
303  */
mbedtls_x509_csr_parse_file(mbedtls_x509_csr * csr,const char * path)304 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path )
305 {
306     int ret;
307     size_t n;
308     unsigned char *buf;
309 
310     if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
311         return( ret );
312 
313     ret = mbedtls_x509_csr_parse( csr, buf, n );
314 
315     mbedtls_platform_zeroize( buf, n );
316     mbedtls_free( buf );
317 
318     return( ret );
319 }
320 #endif /* MBEDTLS_FS_IO */
321 
322 #define BEFORE_COLON    14
323 #define BC              "14"
324 /*
325  * Return an informational string about the CSR.
326  */
mbedtls_x509_csr_info(char * buf,size_t size,const char * prefix,const mbedtls_x509_csr * csr)327 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
328                    const mbedtls_x509_csr *csr )
329 {
330     int ret;
331     size_t n;
332     char *p;
333     char key_size_str[BEFORE_COLON];
334 
335     p = buf;
336     n = size;
337 
338     ret = mbedtls_snprintf( p, n, "%sCSR version   : %d",
339                                prefix, csr->version );
340     MBEDTLS_X509_SAFE_SNPRINTF;
341 
342     ret = mbedtls_snprintf( p, n, "\n%ssubject name  : ", prefix );
343     MBEDTLS_X509_SAFE_SNPRINTF;
344     ret = mbedtls_x509_dn_gets( p, n, &csr->subject );
345     MBEDTLS_X509_SAFE_SNPRINTF;
346 
347     ret = mbedtls_snprintf( p, n, "\n%ssigned using  : ", prefix );
348     MBEDTLS_X509_SAFE_SNPRINTF;
349 
350     ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
351                              csr->sig_opts );
352     MBEDTLS_X509_SAFE_SNPRINTF;
353 
354     if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
355                                       mbedtls_pk_get_name( &csr->pk ) ) ) != 0 )
356     {
357         return( ret );
358     }
359 
360     ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
361                           (int) mbedtls_pk_get_bitlen( &csr->pk ) );
362     MBEDTLS_X509_SAFE_SNPRINTF;
363 
364     return( (int) ( size - n ) );
365 }
366 
367 /*
368  * Initialize a CSR
369  */
mbedtls_x509_csr_init(mbedtls_x509_csr * csr)370 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr )
371 {
372     memset( csr, 0, sizeof(mbedtls_x509_csr) );
373 }
374 
375 /*
376  * Unallocate all CSR data
377  */
mbedtls_x509_csr_free(mbedtls_x509_csr * csr)378 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr )
379 {
380     mbedtls_x509_name *name_cur;
381     mbedtls_x509_name *name_prv;
382 
383     if( csr == NULL )
384         return;
385 
386     mbedtls_pk_free( &csr->pk );
387 
388 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
389     mbedtls_free( csr->sig_opts );
390 #endif
391 
392     name_cur = csr->subject.next;
393     while( name_cur != NULL )
394     {
395         name_prv = name_cur;
396         name_cur = name_cur->next;
397         mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
398         mbedtls_free( name_prv );
399     }
400 
401     if( csr->raw.p != NULL )
402     {
403         mbedtls_platform_zeroize( csr->raw.p, csr->raw.len );
404         mbedtls_free( csr->raw.p );
405     }
406 
407     mbedtls_platform_zeroize( csr, sizeof( mbedtls_x509_csr ) );
408 }
409 
410 #endif /* MBEDTLS_X509_CSR_PARSE_C */
411