x509_csr.c 11 KB

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