PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/features/mbedtls/src/x509_csr.c

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