x509_csr.c 11 KB

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