x509.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * X.509 common functions for parsing and verification
  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_USE_C)
  31. #include "mbedtls/x509.h"
  32. #include "mbedtls/asn1.h"
  33. #include "mbedtls/error.h"
  34. #include "mbedtls/oid.h"
  35. #include <stdio.h>
  36. #include <string.h>
  37. #if defined(MBEDTLS_PEM_PARSE_C)
  38. #include "mbedtls/pem.h"
  39. #endif
  40. #if defined(MBEDTLS_PLATFORM_C)
  41. #include "mbedtls/platform.h"
  42. #else
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #define mbedtls_free free
  46. #define mbedtls_calloc calloc
  47. #define mbedtls_printf printf
  48. #define mbedtls_snprintf snprintf
  49. #endif
  50. #if defined(MBEDTLS_HAVE_TIME)
  51. #include "mbedtls/platform_time.h"
  52. #endif
  53. #if defined(MBEDTLS_HAVE_TIME_DATE)
  54. #include "mbedtls/platform_util.h"
  55. #include <time.h>
  56. #endif
  57. #define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
  58. #define CHECK_RANGE(min, max, val) \
  59. do \
  60. { \
  61. if( ( val ) < ( min ) || ( val ) > ( max ) ) \
  62. { \
  63. return( ret ); \
  64. } \
  65. } while( 0 )
  66. /*
  67. * CertificateSerialNumber ::= INTEGER
  68. */
  69. int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
  70. mbedtls_x509_buf *serial )
  71. {
  72. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  73. if( ( end - *p ) < 1 )
  74. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
  75. MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
  76. if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
  77. **p != MBEDTLS_ASN1_INTEGER )
  78. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
  79. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  80. serial->tag = *(*p)++;
  81. if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
  82. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) );
  83. serial->p = *p;
  84. *p += serial->len;
  85. return( 0 );
  86. }
  87. /* Get an algorithm identifier without parameters (eg for signatures)
  88. *
  89. * AlgorithmIdentifier ::= SEQUENCE {
  90. * algorithm OBJECT IDENTIFIER,
  91. * parameters ANY DEFINED BY algorithm OPTIONAL }
  92. */
  93. int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
  94. mbedtls_x509_buf *alg )
  95. {
  96. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  97. if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
  98. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  99. return( 0 );
  100. }
  101. /*
  102. * Parse an algorithm identifier with (optional) parameters
  103. */
  104. int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
  105. mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
  106. {
  107. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  108. if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
  109. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  110. return( 0 );
  111. }
  112. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  113. /*
  114. * HashAlgorithm ::= AlgorithmIdentifier
  115. *
  116. * AlgorithmIdentifier ::= SEQUENCE {
  117. * algorithm OBJECT IDENTIFIER,
  118. * parameters ANY DEFINED BY algorithm OPTIONAL }
  119. *
  120. * For HashAlgorithm, parameters MUST be NULL or absent.
  121. */
  122. static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
  123. {
  124. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  125. unsigned char *p;
  126. const unsigned char *end;
  127. mbedtls_x509_buf md_oid;
  128. size_t len;
  129. /* Make sure we got a SEQUENCE and setup bounds */
  130. if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  131. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  132. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  133. p = alg->p;
  134. end = p + alg->len;
  135. if( p >= end )
  136. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  137. MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
  138. /* Parse md_oid */
  139. md_oid.tag = *p;
  140. if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
  141. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  142. md_oid.p = p;
  143. p += md_oid.len;
  144. /* Get md_alg from md_oid */
  145. if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
  146. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  147. /* Make sure params is absent of NULL */
  148. if( p == end )
  149. return( 0 );
  150. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
  151. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  152. if( p != end )
  153. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  154. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  155. return( 0 );
  156. }
  157. /*
  158. * RSASSA-PSS-params ::= SEQUENCE {
  159. * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
  160. * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
  161. * saltLength [2] INTEGER DEFAULT 20,
  162. * trailerField [3] INTEGER DEFAULT 1 }
  163. * -- Note that the tags in this Sequence are explicit.
  164. *
  165. * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
  166. * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
  167. * option. Enfore this at parsing time.
  168. */
  169. int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
  170. mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
  171. int *salt_len )
  172. {
  173. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  174. unsigned char *p;
  175. const unsigned char *end, *end2;
  176. size_t len;
  177. mbedtls_x509_buf alg_id, alg_params;
  178. /* First set everything to defaults */
  179. *md_alg = MBEDTLS_MD_SHA1;
  180. *mgf_md = MBEDTLS_MD_SHA1;
  181. *salt_len = 20;
  182. /* Make sure params is a SEQUENCE and setup bounds */
  183. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  184. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  185. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  186. p = (unsigned char *) params->p;
  187. end = p + params->len;
  188. if( p == end )
  189. return( 0 );
  190. /*
  191. * HashAlgorithm
  192. */
  193. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  194. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
  195. {
  196. end2 = p + len;
  197. /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
  198. if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
  199. return( ret );
  200. if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
  201. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  202. if( p != end2 )
  203. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  204. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  205. }
  206. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  207. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  208. if( p == end )
  209. return( 0 );
  210. /*
  211. * MaskGenAlgorithm
  212. */
  213. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  214. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
  215. {
  216. end2 = p + len;
  217. /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
  218. if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
  219. return( ret );
  220. /* Only MFG1 is recognised for now */
  221. if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
  222. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
  223. MBEDTLS_ERR_OID_NOT_FOUND ) );
  224. /* Parse HashAlgorithm */
  225. if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
  226. return( ret );
  227. if( p != end2 )
  228. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  229. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  230. }
  231. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  232. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  233. if( p == end )
  234. return( 0 );
  235. /*
  236. * salt_len
  237. */
  238. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  239. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
  240. {
  241. end2 = p + len;
  242. if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
  243. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  244. if( p != end2 )
  245. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  246. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  247. }
  248. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  249. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  250. if( p == end )
  251. return( 0 );
  252. /*
  253. * trailer_field (if present, must be 1)
  254. */
  255. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  256. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
  257. {
  258. int trailer_field;
  259. end2 = p + len;
  260. if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
  261. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  262. if( p != end2 )
  263. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  264. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  265. if( trailer_field != 1 )
  266. return( MBEDTLS_ERR_X509_INVALID_ALG );
  267. }
  268. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  269. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
  270. if( p != end )
  271. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
  272. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  273. return( 0 );
  274. }
  275. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  276. /*
  277. * AttributeTypeAndValue ::= SEQUENCE {
  278. * type AttributeType,
  279. * value AttributeValue }
  280. *
  281. * AttributeType ::= OBJECT IDENTIFIER
  282. *
  283. * AttributeValue ::= ANY DEFINED BY AttributeType
  284. */
  285. static int x509_get_attr_type_value( unsigned char **p,
  286. const unsigned char *end,
  287. mbedtls_x509_name *cur )
  288. {
  289. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  290. size_t len;
  291. mbedtls_x509_buf *oid;
  292. mbedtls_x509_buf *val;
  293. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  294. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  295. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
  296. end = *p + len;
  297. if( ( end - *p ) < 1 )
  298. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
  299. MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
  300. oid = &cur->oid;
  301. oid->tag = **p;
  302. if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
  303. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
  304. oid->p = *p;
  305. *p += oid->len;
  306. if( ( end - *p ) < 1 )
  307. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
  308. MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
  309. if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
  310. **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
  311. **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
  312. **p != MBEDTLS_ASN1_BIT_STRING )
  313. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
  314. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  315. val = &cur->val;
  316. val->tag = *(*p)++;
  317. if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
  318. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
  319. val->p = *p;
  320. *p += val->len;
  321. if( *p != end )
  322. {
  323. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
  324. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  325. }
  326. cur->next = NULL;
  327. return( 0 );
  328. }
  329. /*
  330. * Name ::= CHOICE { -- only one possibility for now --
  331. * rdnSequence RDNSequence }
  332. *
  333. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  334. *
  335. * RelativeDistinguishedName ::=
  336. * SET OF AttributeTypeAndValue
  337. *
  338. * AttributeTypeAndValue ::= SEQUENCE {
  339. * type AttributeType,
  340. * value AttributeValue }
  341. *
  342. * AttributeType ::= OBJECT IDENTIFIER
  343. *
  344. * AttributeValue ::= ANY DEFINED BY AttributeType
  345. *
  346. * The data structure is optimized for the common case where each RDN has only
  347. * one element, which is represented as a list of AttributeTypeAndValue.
  348. * For the general case we still use a flat list, but we mark elements of the
  349. * same set so that they are "merged" together in the functions that consume
  350. * this list, eg mbedtls_x509_dn_gets().
  351. */
  352. int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
  353. mbedtls_x509_name *cur )
  354. {
  355. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  356. size_t set_len;
  357. const unsigned char *end_set;
  358. /* don't use recursion, we'd risk stack overflow if not optimized */
  359. while( 1 )
  360. {
  361. /*
  362. * parse SET
  363. */
  364. if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
  365. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
  366. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
  367. end_set = *p + set_len;
  368. while( 1 )
  369. {
  370. if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
  371. return( ret );
  372. if( *p == end_set )
  373. break;
  374. /* Mark this item as being no the only one in a set */
  375. cur->next_merged = 1;
  376. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
  377. if( cur->next == NULL )
  378. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  379. cur = cur->next;
  380. }
  381. /*
  382. * continue until end of SEQUENCE is reached
  383. */
  384. if( *p == end )
  385. return( 0 );
  386. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
  387. if( cur->next == NULL )
  388. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  389. cur = cur->next;
  390. }
  391. }
  392. static int x509_parse_int( unsigned char **p, size_t n, int *res )
  393. {
  394. *res = 0;
  395. for( ; n > 0; --n )
  396. {
  397. if( ( **p < '0') || ( **p > '9' ) )
  398. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  399. *res *= 10;
  400. *res += ( *(*p)++ - '0' );
  401. }
  402. return( 0 );
  403. }
  404. static int x509_date_is_valid(const mbedtls_x509_time *t )
  405. {
  406. int ret = MBEDTLS_ERR_X509_INVALID_DATE;
  407. int month_len;
  408. CHECK_RANGE( 0, 9999, t->year );
  409. CHECK_RANGE( 0, 23, t->hour );
  410. CHECK_RANGE( 0, 59, t->min );
  411. CHECK_RANGE( 0, 59, t->sec );
  412. switch( t->mon )
  413. {
  414. case 1: case 3: case 5: case 7: case 8: case 10: case 12:
  415. month_len = 31;
  416. break;
  417. case 4: case 6: case 9: case 11:
  418. month_len = 30;
  419. break;
  420. case 2:
  421. if( ( !( t->year % 4 ) && t->year % 100 ) ||
  422. !( t->year % 400 ) )
  423. month_len = 29;
  424. else
  425. month_len = 28;
  426. break;
  427. default:
  428. return( ret );
  429. }
  430. CHECK_RANGE( 1, month_len, t->day );
  431. return( 0 );
  432. }
  433. /*
  434. * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
  435. * field.
  436. */
  437. static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
  438. mbedtls_x509_time *tm )
  439. {
  440. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  441. /*
  442. * Minimum length is 10 or 12 depending on yearlen
  443. */
  444. if ( len < yearlen + 8 )
  445. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  446. len -= yearlen + 8;
  447. /*
  448. * Parse year, month, day, hour, minute
  449. */
  450. CHECK( x509_parse_int( p, yearlen, &tm->year ) );
  451. if ( 2 == yearlen )
  452. {
  453. if ( tm->year < 50 )
  454. tm->year += 100;
  455. tm->year += 1900;
  456. }
  457. CHECK( x509_parse_int( p, 2, &tm->mon ) );
  458. CHECK( x509_parse_int( p, 2, &tm->day ) );
  459. CHECK( x509_parse_int( p, 2, &tm->hour ) );
  460. CHECK( x509_parse_int( p, 2, &tm->min ) );
  461. /*
  462. * Parse seconds if present
  463. */
  464. if ( len >= 2 )
  465. {
  466. CHECK( x509_parse_int( p, 2, &tm->sec ) );
  467. len -= 2;
  468. }
  469. else
  470. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  471. /*
  472. * Parse trailing 'Z' if present
  473. */
  474. if ( 1 == len && 'Z' == **p )
  475. {
  476. (*p)++;
  477. len--;
  478. }
  479. /*
  480. * We should have parsed all characters at this point
  481. */
  482. if ( 0 != len )
  483. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  484. CHECK( x509_date_is_valid( tm ) );
  485. return ( 0 );
  486. }
  487. /*
  488. * Time ::= CHOICE {
  489. * utcTime UTCTime,
  490. * generalTime GeneralizedTime }
  491. */
  492. int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
  493. mbedtls_x509_time *tm )
  494. {
  495. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  496. size_t len, year_len;
  497. unsigned char tag;
  498. if( ( end - *p ) < 1 )
  499. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
  500. MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
  501. tag = **p;
  502. if( tag == MBEDTLS_ASN1_UTC_TIME )
  503. year_len = 2;
  504. else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
  505. year_len = 4;
  506. else
  507. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
  508. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  509. (*p)++;
  510. ret = mbedtls_asn1_get_len( p, end, &len );
  511. if( ret != 0 )
  512. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
  513. return x509_parse_time( p, len, year_len, tm );
  514. }
  515. int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
  516. {
  517. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  518. size_t len;
  519. int tag_type;
  520. if( ( end - *p ) < 1 )
  521. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE,
  522. MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
  523. tag_type = **p;
  524. if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
  525. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) );
  526. sig->tag = tag_type;
  527. sig->len = len;
  528. sig->p = *p;
  529. *p += len;
  530. return( 0 );
  531. }
  532. /*
  533. * Get signature algorithm from alg OID and optional parameters
  534. */
  535. int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
  536. mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
  537. void **sig_opts )
  538. {
  539. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  540. if( *sig_opts != NULL )
  541. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  542. if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
  543. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) );
  544. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  545. if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
  546. {
  547. mbedtls_pk_rsassa_pss_options *pss_opts;
  548. pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
  549. if( pss_opts == NULL )
  550. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  551. ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
  552. md_alg,
  553. &pss_opts->mgf1_hash_id,
  554. &pss_opts->expected_salt_len );
  555. if( ret != 0 )
  556. {
  557. mbedtls_free( pss_opts );
  558. return( ret );
  559. }
  560. *sig_opts = (void *) pss_opts;
  561. }
  562. else
  563. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  564. {
  565. /* Make sure parameters are absent or NULL */
  566. if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
  567. sig_params->len != 0 )
  568. return( MBEDTLS_ERR_X509_INVALID_ALG );
  569. }
  570. return( 0 );
  571. }
  572. /*
  573. * X.509 Extensions (No parsing of extensions, pointer should
  574. * be either manually updated or extensions should be parsed!)
  575. */
  576. int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
  577. mbedtls_x509_buf *ext, int tag )
  578. {
  579. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  580. size_t len;
  581. /* Extension structure use EXPLICIT tagging. That is, the actual
  582. * `Extensions` structure is wrapped by a tag-length pair using
  583. * the respective context-specific tag. */
  584. ret = mbedtls_asn1_get_tag( p, end, &ext->len,
  585. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
  586. if( ret != 0 )
  587. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  588. ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
  589. ext->p = *p;
  590. end = *p + ext->len;
  591. /*
  592. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  593. */
  594. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  595. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  596. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  597. if( end != *p + len )
  598. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  599. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  600. return( 0 );
  601. }
  602. /*
  603. * Store the name in printable form into buf; no more
  604. * than size characters will be written
  605. */
  606. int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
  607. {
  608. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  609. size_t i, n;
  610. unsigned char c, merge = 0;
  611. const mbedtls_x509_name *name;
  612. const char *short_name = NULL;
  613. char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
  614. memset( s, 0, sizeof( s ) );
  615. name = dn;
  616. p = buf;
  617. n = size;
  618. while( name != NULL )
  619. {
  620. if( !name->oid.p )
  621. {
  622. name = name->next;
  623. continue;
  624. }
  625. if( name != dn )
  626. {
  627. ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
  628. MBEDTLS_X509_SAFE_SNPRINTF;
  629. }
  630. ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
  631. if( ret == 0 )
  632. ret = mbedtls_snprintf( p, n, "%s=", short_name );
  633. else
  634. ret = mbedtls_snprintf( p, n, "\?\?=" );
  635. MBEDTLS_X509_SAFE_SNPRINTF;
  636. for( i = 0; i < name->val.len; i++ )
  637. {
  638. if( i >= sizeof( s ) - 1 )
  639. break;
  640. c = name->val.p[i];
  641. if( c < 32 || c >= 127 )
  642. s[i] = '?';
  643. else s[i] = c;
  644. }
  645. s[i] = '\0';
  646. ret = mbedtls_snprintf( p, n, "%s", s );
  647. MBEDTLS_X509_SAFE_SNPRINTF;
  648. merge = name->next_merged;
  649. name = name->next;
  650. }
  651. return( (int) ( size - n ) );
  652. }
  653. /*
  654. * Store the serial in printable form into buf; no more
  655. * than size characters will be written
  656. */
  657. int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
  658. {
  659. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  660. size_t i, n, nr;
  661. char *p;
  662. p = buf;
  663. n = size;
  664. nr = ( serial->len <= 32 )
  665. ? serial->len : 28;
  666. for( i = 0; i < nr; i++ )
  667. {
  668. if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
  669. continue;
  670. ret = mbedtls_snprintf( p, n, "%02X%s",
  671. serial->p[i], ( i < nr - 1 ) ? ":" : "" );
  672. MBEDTLS_X509_SAFE_SNPRINTF;
  673. }
  674. if( nr != serial->len )
  675. {
  676. ret = mbedtls_snprintf( p, n, "...." );
  677. MBEDTLS_X509_SAFE_SNPRINTF;
  678. }
  679. return( (int) ( size - n ) );
  680. }
  681. /*
  682. * Helper for writing signature algorithms
  683. */
  684. int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
  685. mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
  686. const void *sig_opts )
  687. {
  688. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  689. char *p = buf;
  690. size_t n = size;
  691. const char *desc = NULL;
  692. ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
  693. if( ret != 0 )
  694. ret = mbedtls_snprintf( p, n, "???" );
  695. else
  696. ret = mbedtls_snprintf( p, n, "%s", desc );
  697. MBEDTLS_X509_SAFE_SNPRINTF;
  698. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  699. if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
  700. {
  701. const mbedtls_pk_rsassa_pss_options *pss_opts;
  702. const mbedtls_md_info_t *md_info, *mgf_md_info;
  703. pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
  704. md_info = mbedtls_md_info_from_type( md_alg );
  705. mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
  706. ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
  707. md_info ? mbedtls_md_get_name( md_info ) : "???",
  708. mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
  709. (unsigned int) pss_opts->expected_salt_len );
  710. MBEDTLS_X509_SAFE_SNPRINTF;
  711. }
  712. #else
  713. ((void) pk_alg);
  714. ((void) md_alg);
  715. ((void) sig_opts);
  716. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  717. return( (int)( size - n ) );
  718. }
  719. /*
  720. * Helper for writing "RSA key size", "EC key size", etc
  721. */
  722. int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
  723. {
  724. char *p = buf;
  725. size_t n = buf_size;
  726. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  727. ret = mbedtls_snprintf( p, n, "%s key size", name );
  728. MBEDTLS_X509_SAFE_SNPRINTF;
  729. return( 0 );
  730. }
  731. #if defined(MBEDTLS_HAVE_TIME_DATE)
  732. /*
  733. * Set the time structure to the current time.
  734. * Return 0 on success, non-zero on failure.
  735. */
  736. static int x509_get_current_time( mbedtls_x509_time *now )
  737. {
  738. struct tm *lt, tm_buf;
  739. mbedtls_time_t tt;
  740. int ret = 0;
  741. tt = mbedtls_time( NULL );
  742. lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
  743. if( lt == NULL )
  744. ret = -1;
  745. else
  746. {
  747. now->year = lt->tm_year + 1900;
  748. now->mon = lt->tm_mon + 1;
  749. now->day = lt->tm_mday;
  750. now->hour = lt->tm_hour;
  751. now->min = lt->tm_min;
  752. now->sec = lt->tm_sec;
  753. }
  754. return( ret );
  755. }
  756. /*
  757. * Return 0 if before <= after, 1 otherwise
  758. */
  759. static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
  760. {
  761. if( before->year > after->year )
  762. return( 1 );
  763. if( before->year == after->year &&
  764. before->mon > after->mon )
  765. return( 1 );
  766. if( before->year == after->year &&
  767. before->mon == after->mon &&
  768. before->day > after->day )
  769. return( 1 );
  770. if( before->year == after->year &&
  771. before->mon == after->mon &&
  772. before->day == after->day &&
  773. before->hour > after->hour )
  774. return( 1 );
  775. if( before->year == after->year &&
  776. before->mon == after->mon &&
  777. before->day == after->day &&
  778. before->hour == after->hour &&
  779. before->min > after->min )
  780. return( 1 );
  781. if( before->year == after->year &&
  782. before->mon == after->mon &&
  783. before->day == after->day &&
  784. before->hour == after->hour &&
  785. before->min == after->min &&
  786. before->sec > after->sec )
  787. return( 1 );
  788. return( 0 );
  789. }
  790. int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
  791. {
  792. mbedtls_x509_time now;
  793. if( x509_get_current_time( &now ) != 0 )
  794. return( 1 );
  795. return( x509_check_time( &now, to ) );
  796. }
  797. int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
  798. {
  799. mbedtls_x509_time now;
  800. if( x509_get_current_time( &now ) != 0 )
  801. return( 1 );
  802. return( x509_check_time( from, &now ) );
  803. }
  804. #else /* MBEDTLS_HAVE_TIME_DATE */
  805. int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
  806. {
  807. ((void) to);
  808. return( 0 );
  809. }
  810. int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
  811. {
  812. ((void) from);
  813. return( 0 );
  814. }
  815. #endif /* MBEDTLS_HAVE_TIME_DATE */
  816. #if defined(MBEDTLS_SELF_TEST)
  817. #include "mbedtls/x509_crt.h"
  818. #include "mbedtls/certs.h"
  819. /*
  820. * Checkup routine
  821. */
  822. int mbedtls_x509_self_test( int verbose )
  823. {
  824. int ret = 0;
  825. #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
  826. uint32_t flags;
  827. mbedtls_x509_crt cacert;
  828. mbedtls_x509_crt clicert;
  829. if( verbose != 0 )
  830. mbedtls_printf( " X.509 certificate load: " );
  831. mbedtls_x509_crt_init( &cacert );
  832. mbedtls_x509_crt_init( &clicert );
  833. ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
  834. mbedtls_test_cli_crt_len );
  835. if( ret != 0 )
  836. {
  837. if( verbose != 0 )
  838. mbedtls_printf( "failed\n" );
  839. goto cleanup;
  840. }
  841. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
  842. mbedtls_test_ca_crt_len );
  843. if( ret != 0 )
  844. {
  845. if( verbose != 0 )
  846. mbedtls_printf( "failed\n" );
  847. goto cleanup;
  848. }
  849. if( verbose != 0 )
  850. mbedtls_printf( "passed\n X.509 signature verify: ");
  851. ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
  852. if( ret != 0 )
  853. {
  854. if( verbose != 0 )
  855. mbedtls_printf( "failed\n" );
  856. goto cleanup;
  857. }
  858. if( verbose != 0 )
  859. mbedtls_printf( "passed\n\n");
  860. cleanup:
  861. mbedtls_x509_crt_free( &cacert );
  862. mbedtls_x509_crt_free( &clicert );
  863. #else
  864. ((void) verbose);
  865. #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
  866. return( ret );
  867. }
  868. #endif /* MBEDTLS_SELF_TEST */
  869. #endif /* MBEDTLS_X509_USE_C */