x509_csr.c 12 KB

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