x509_csr.c 11 KB

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