x509_csr.c 11 KB

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