x509_csr.c 11 KB

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