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