x509_csr.c 11 KB

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