x509.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * X.509 common functions for parsing and verification
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * The ITU-T X.509 standard defines a certificate format for PKI.
  25. *
  26. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  27. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  28. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  29. *
  30. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  31. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  32. */
  33. #if !defined(MBEDTLS_CONFIG_FILE)
  34. #include "mbedtls/config.h"
  35. #else
  36. #include MBEDTLS_CONFIG_FILE
  37. #endif
  38. #if defined(MBEDTLS_X509_USE_C)
  39. #include "mbedtls/x509.h"
  40. #include "mbedtls/asn1.h"
  41. #include "mbedtls/oid.h"
  42. #include <stdio.h>
  43. #include <string.h>
  44. #if defined(MBEDTLS_PEM_PARSE_C)
  45. #include "mbedtls/pem.h"
  46. #endif
  47. #if defined(MBEDTLS_PLATFORM_C)
  48. #include "mbedtls/platform.h"
  49. #else
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #define mbedtls_free free
  53. #define mbedtls_calloc calloc
  54. #define mbedtls_printf printf
  55. #define mbedtls_snprintf snprintf
  56. #endif
  57. #if defined(MBEDTLS_HAVE_TIME)
  58. #include "mbedtls/platform_time.h"
  59. #endif
  60. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  61. #include <windows.h>
  62. #else
  63. #include <time.h>
  64. #endif
  65. #if defined(MBEDTLS_FS_IO)
  66. #include <stdio.h>
  67. #if !defined(_WIN32)
  68. #include <sys/types.h>
  69. #include <sys/stat.h>
  70. #include <dirent.h>
  71. #endif
  72. #endif
  73. #define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
  74. #define CHECK_RANGE(min, max, val) if( val < min || val > max ){ return( ret ); }
  75. /*
  76. * CertificateSerialNumber ::= INTEGER
  77. */
  78. int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
  79. mbedtls_x509_buf *serial )
  80. {
  81. int ret;
  82. if( ( end - *p ) < 1 )
  83. return( MBEDTLS_ERR_X509_INVALID_SERIAL +
  84. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  85. if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
  86. **p != MBEDTLS_ASN1_INTEGER )
  87. return( MBEDTLS_ERR_X509_INVALID_SERIAL +
  88. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  89. serial->tag = *(*p)++;
  90. if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
  91. return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
  92. serial->p = *p;
  93. *p += serial->len;
  94. return( 0 );
  95. }
  96. /* Get an algorithm identifier without parameters (eg for signatures)
  97. *
  98. * AlgorithmIdentifier ::= SEQUENCE {
  99. * algorithm OBJECT IDENTIFIER,
  100. * parameters ANY DEFINED BY algorithm OPTIONAL }
  101. */
  102. int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
  103. mbedtls_x509_buf *alg )
  104. {
  105. int ret;
  106. if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
  107. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  108. return( 0 );
  109. }
  110. /*
  111. * Parse an algorithm identifier with (optional) paramaters
  112. */
  113. int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
  114. mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
  115. {
  116. int ret;
  117. if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
  118. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  119. return( 0 );
  120. }
  121. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  122. /*
  123. * HashAlgorithm ::= AlgorithmIdentifier
  124. *
  125. * AlgorithmIdentifier ::= SEQUENCE {
  126. * algorithm OBJECT IDENTIFIER,
  127. * parameters ANY DEFINED BY algorithm OPTIONAL }
  128. *
  129. * For HashAlgorithm, parameters MUST be NULL or absent.
  130. */
  131. static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
  132. {
  133. int ret;
  134. unsigned char *p;
  135. const unsigned char *end;
  136. mbedtls_x509_buf md_oid;
  137. size_t len;
  138. /* Make sure we got a SEQUENCE and setup bounds */
  139. if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  140. return( MBEDTLS_ERR_X509_INVALID_ALG +
  141. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  142. p = (unsigned char *) alg->p;
  143. end = p + alg->len;
  144. if( p >= end )
  145. return( MBEDTLS_ERR_X509_INVALID_ALG +
  146. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  147. /* Parse md_oid */
  148. md_oid.tag = *p;
  149. if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
  150. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  151. md_oid.p = p;
  152. p += md_oid.len;
  153. /* Get md_alg from md_oid */
  154. if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
  155. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  156. /* Make sure params is absent of NULL */
  157. if( p == end )
  158. return( 0 );
  159. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
  160. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  161. if( p != end )
  162. return( MBEDTLS_ERR_X509_INVALID_ALG +
  163. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  164. return( 0 );
  165. }
  166. /*
  167. * RSASSA-PSS-params ::= SEQUENCE {
  168. * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
  169. * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
  170. * saltLength [2] INTEGER DEFAULT 20,
  171. * trailerField [3] INTEGER DEFAULT 1 }
  172. * -- Note that the tags in this Sequence are explicit.
  173. *
  174. * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
  175. * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
  176. * option. Enfore this at parsing time.
  177. */
  178. int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
  179. mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
  180. int *salt_len )
  181. {
  182. int ret;
  183. unsigned char *p;
  184. const unsigned char *end, *end2;
  185. size_t len;
  186. mbedtls_x509_buf alg_id, alg_params;
  187. /* First set everything to defaults */
  188. *md_alg = MBEDTLS_MD_SHA1;
  189. *mgf_md = MBEDTLS_MD_SHA1;
  190. *salt_len = 20;
  191. /* Make sure params is a SEQUENCE and setup bounds */
  192. if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
  193. return( MBEDTLS_ERR_X509_INVALID_ALG +
  194. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  195. p = (unsigned char *) params->p;
  196. end = p + params->len;
  197. if( p == end )
  198. return( 0 );
  199. /*
  200. * HashAlgorithm
  201. */
  202. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  203. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
  204. {
  205. end2 = p + len;
  206. /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
  207. if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
  208. return( ret );
  209. if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
  210. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  211. if( p != end2 )
  212. return( MBEDTLS_ERR_X509_INVALID_ALG +
  213. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  214. }
  215. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  216. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  217. if( p == end )
  218. return( 0 );
  219. /*
  220. * MaskGenAlgorithm
  221. */
  222. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  223. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
  224. {
  225. end2 = p + len;
  226. /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
  227. if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
  228. return( ret );
  229. /* Only MFG1 is recognised for now */
  230. if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
  231. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
  232. MBEDTLS_ERR_OID_NOT_FOUND );
  233. /* Parse HashAlgorithm */
  234. if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
  235. return( ret );
  236. if( p != end2 )
  237. return( MBEDTLS_ERR_X509_INVALID_ALG +
  238. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  239. }
  240. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  241. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  242. if( p == end )
  243. return( 0 );
  244. /*
  245. * salt_len
  246. */
  247. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  248. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
  249. {
  250. end2 = p + len;
  251. if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
  252. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  253. if( p != end2 )
  254. return( MBEDTLS_ERR_X509_INVALID_ALG +
  255. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  256. }
  257. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  258. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  259. if( p == end )
  260. return( 0 );
  261. /*
  262. * trailer_field (if present, must be 1)
  263. */
  264. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  265. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
  266. {
  267. int trailer_field;
  268. end2 = p + len;
  269. if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
  270. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  271. if( p != end2 )
  272. return( MBEDTLS_ERR_X509_INVALID_ALG +
  273. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  274. if( trailer_field != 1 )
  275. return( MBEDTLS_ERR_X509_INVALID_ALG );
  276. }
  277. else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  278. return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
  279. if( p != end )
  280. return( MBEDTLS_ERR_X509_INVALID_ALG +
  281. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  282. return( 0 );
  283. }
  284. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  285. /*
  286. * AttributeTypeAndValue ::= SEQUENCE {
  287. * type AttributeType,
  288. * value AttributeValue }
  289. *
  290. * AttributeType ::= OBJECT IDENTIFIER
  291. *
  292. * AttributeValue ::= ANY DEFINED BY AttributeType
  293. */
  294. static int x509_get_attr_type_value( unsigned char **p,
  295. const unsigned char *end,
  296. mbedtls_x509_name *cur )
  297. {
  298. int ret;
  299. size_t len;
  300. mbedtls_x509_buf *oid;
  301. mbedtls_x509_buf *val;
  302. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  303. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  304. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  305. if( ( end - *p ) < 1 )
  306. return( MBEDTLS_ERR_X509_INVALID_NAME +
  307. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  308. oid = &cur->oid;
  309. oid->tag = **p;
  310. if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
  311. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  312. oid->p = *p;
  313. *p += oid->len;
  314. if( ( end - *p ) < 1 )
  315. return( MBEDTLS_ERR_X509_INVALID_NAME +
  316. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  317. if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
  318. **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
  319. **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
  320. **p != MBEDTLS_ASN1_BIT_STRING )
  321. return( MBEDTLS_ERR_X509_INVALID_NAME +
  322. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  323. val = &cur->val;
  324. val->tag = *(*p)++;
  325. if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
  326. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  327. val->p = *p;
  328. *p += val->len;
  329. cur->next = NULL;
  330. return( 0 );
  331. }
  332. /*
  333. * Name ::= CHOICE { -- only one possibility for now --
  334. * rdnSequence RDNSequence }
  335. *
  336. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  337. *
  338. * RelativeDistinguishedName ::=
  339. * SET OF AttributeTypeAndValue
  340. *
  341. * AttributeTypeAndValue ::= SEQUENCE {
  342. * type AttributeType,
  343. * value AttributeValue }
  344. *
  345. * AttributeType ::= OBJECT IDENTIFIER
  346. *
  347. * AttributeValue ::= ANY DEFINED BY AttributeType
  348. *
  349. * The data structure is optimized for the common case where each RDN has only
  350. * one element, which is represented as a list of AttributeTypeAndValue.
  351. * For the general case we still use a flat list, but we mark elements of the
  352. * same set so that they are "merged" together in the functions that consume
  353. * this list, eg mbedtls_x509_dn_gets().
  354. */
  355. int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
  356. mbedtls_x509_name *cur )
  357. {
  358. int ret;
  359. size_t set_len;
  360. const unsigned char *end_set;
  361. /* don't use recursion, we'd risk stack overflow if not optimized */
  362. while( 1 )
  363. {
  364. /*
  365. * parse SET
  366. */
  367. if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
  368. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
  369. return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
  370. end_set = *p + set_len;
  371. while( 1 )
  372. {
  373. if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
  374. return( ret );
  375. if( *p == end_set )
  376. break;
  377. /* Mark this item as being no the only one in a set */
  378. cur->next_merged = 1;
  379. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
  380. if( cur->next == NULL )
  381. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  382. cur = cur->next;
  383. }
  384. /*
  385. * continue until end of SEQUENCE is reached
  386. */
  387. if( *p == end )
  388. return( 0 );
  389. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
  390. if( cur->next == NULL )
  391. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  392. cur = cur->next;
  393. }
  394. }
  395. static int x509_parse_int( unsigned char **p, size_t n, int *res )
  396. {
  397. *res = 0;
  398. for( ; n > 0; --n )
  399. {
  400. if( ( **p < '0') || ( **p > '9' ) )
  401. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  402. *res *= 10;
  403. *res += ( *(*p)++ - '0' );
  404. }
  405. return( 0 );
  406. }
  407. static int x509_date_is_valid(const mbedtls_x509_time *time)
  408. {
  409. int ret = MBEDTLS_ERR_X509_INVALID_DATE;
  410. CHECK_RANGE( 0, 9999, time->year );
  411. CHECK_RANGE( 0, 23, time->hour );
  412. CHECK_RANGE( 0, 59, time->min );
  413. CHECK_RANGE( 0, 59, time->sec );
  414. switch( time->mon )
  415. {
  416. case 1: case 3: case 5: case 7: case 8: case 10: case 12:
  417. CHECK_RANGE( 1, 31, time->day );
  418. break;
  419. case 4: case 6: case 9: case 11:
  420. CHECK_RANGE( 1, 30, time->day );
  421. break;
  422. case 2:
  423. CHECK_RANGE( 1, 28 + (time->year % 4 == 0), time->day );
  424. break;
  425. default:
  426. return( ret );
  427. }
  428. return( 0 );
  429. }
  430. /*
  431. * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
  432. * field.
  433. */
  434. static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
  435. mbedtls_x509_time *time )
  436. {
  437. int ret;
  438. /*
  439. * Minimum length is 10 or 12 depending on yearlen
  440. */
  441. if ( len < yearlen + 8 )
  442. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  443. len -= yearlen + 8;
  444. /*
  445. * Parse year, month, day, hour, minute
  446. */
  447. CHECK( x509_parse_int( p, yearlen, &time->year ) );
  448. if ( 2 == yearlen )
  449. {
  450. if ( time->year < 50 )
  451. time->year += 100;
  452. time->year += 1900;
  453. }
  454. CHECK( x509_parse_int( p, 2, &time->mon ) );
  455. CHECK( x509_parse_int( p, 2, &time->day ) );
  456. CHECK( x509_parse_int( p, 2, &time->hour ) );
  457. CHECK( x509_parse_int( p, 2, &time->min ) );
  458. /*
  459. * Parse seconds if present
  460. */
  461. if ( len >= 2 )
  462. {
  463. CHECK( x509_parse_int( p, 2, &time->sec ) );
  464. len -= 2;
  465. }
  466. else
  467. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  468. /*
  469. * Parse trailing 'Z' if present
  470. */
  471. if ( 1 == len && 'Z' == **p )
  472. {
  473. (*p)++;
  474. len--;
  475. }
  476. /*
  477. * We should have parsed all characters at this point
  478. */
  479. if ( 0 != len )
  480. return ( MBEDTLS_ERR_X509_INVALID_DATE );
  481. CHECK( x509_date_is_valid( time ) );
  482. return ( 0 );
  483. }
  484. /*
  485. * Time ::= CHOICE {
  486. * utcTime UTCTime,
  487. * generalTime GeneralizedTime }
  488. */
  489. int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
  490. mbedtls_x509_time *time )
  491. {
  492. int ret;
  493. size_t len, year_len;
  494. unsigned char tag;
  495. if( ( end - *p ) < 1 )
  496. return( MBEDTLS_ERR_X509_INVALID_DATE +
  497. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  498. tag = **p;
  499. if( tag == MBEDTLS_ASN1_UTC_TIME )
  500. year_len = 2;
  501. else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
  502. year_len = 4;
  503. else
  504. return( MBEDTLS_ERR_X509_INVALID_DATE +
  505. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  506. (*p)++;
  507. ret = mbedtls_asn1_get_len( p, end, &len );
  508. if( ret != 0 )
  509. return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
  510. return x509_parse_time( p, len, year_len, time );
  511. }
  512. int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
  513. {
  514. int ret;
  515. size_t len;
  516. int tag_type;
  517. if( ( end - *p ) < 1 )
  518. return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
  519. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  520. tag_type = **p;
  521. if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
  522. return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
  523. sig->tag = tag_type;
  524. sig->len = len;
  525. sig->p = *p;
  526. *p += len;
  527. return( 0 );
  528. }
  529. /*
  530. * Get signature algorithm from alg OID and optional parameters
  531. */
  532. int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
  533. mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
  534. void **sig_opts )
  535. {
  536. int ret;
  537. if( *sig_opts != NULL )
  538. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  539. if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
  540. return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
  541. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  542. if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
  543. {
  544. mbedtls_pk_rsassa_pss_options *pss_opts;
  545. pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
  546. if( pss_opts == NULL )
  547. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  548. ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
  549. md_alg,
  550. &pss_opts->mgf1_hash_id,
  551. &pss_opts->expected_salt_len );
  552. if( ret != 0 )
  553. {
  554. mbedtls_free( pss_opts );
  555. return( ret );
  556. }
  557. *sig_opts = (void *) pss_opts;
  558. }
  559. else
  560. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  561. {
  562. /* Make sure parameters are absent or NULL */
  563. if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
  564. sig_params->len != 0 )
  565. return( MBEDTLS_ERR_X509_INVALID_ALG );
  566. }
  567. return( 0 );
  568. }
  569. /*
  570. * X.509 Extensions (No parsing of extensions, pointer should
  571. * be either manually updated or extensions should be parsed!)
  572. */
  573. int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
  574. mbedtls_x509_buf *ext, int tag )
  575. {
  576. int ret;
  577. size_t len;
  578. if( *p == end )
  579. return( 0 );
  580. ext->tag = **p;
  581. if( ( ret = mbedtls_asn1_get_tag( p, end, &ext->len,
  582. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag ) ) != 0 )
  583. return( ret );
  584. ext->p = *p;
  585. end = *p + ext->len;
  586. /*
  587. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  588. *
  589. * Extension ::= SEQUENCE {
  590. * extnID OBJECT IDENTIFIER,
  591. * critical BOOLEAN DEFAULT FALSE,
  592. * extnValue OCTET STRING }
  593. */
  594. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  595. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  596. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  597. if( end != *p + len )
  598. return( 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;
  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 || ( c > 128 && c < 160 ) )
  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;
  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;
  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. 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;
  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. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  737. static int x509_get_current_time( mbedtls_x509_time *now )
  738. {
  739. SYSTEMTIME st;
  740. GetSystemTime( &st );
  741. now->year = st.wYear;
  742. now->mon = st.wMonth;
  743. now->day = st.wDay;
  744. now->hour = st.wHour;
  745. now->min = st.wMinute;
  746. now->sec = st.wSecond;
  747. return( 0 );
  748. }
  749. #else
  750. static int x509_get_current_time( mbedtls_x509_time *now )
  751. {
  752. struct tm *lt;
  753. mbedtls_time_t tt;
  754. int ret = 0;
  755. #if defined(MBEDTLS_THREADING_C)
  756. if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
  757. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  758. #endif
  759. tt = mbedtls_time( NULL );
  760. lt = gmtime( &tt );
  761. if( lt == NULL )
  762. ret = -1;
  763. else
  764. {
  765. now->year = lt->tm_year + 1900;
  766. now->mon = lt->tm_mon + 1;
  767. now->day = lt->tm_mday;
  768. now->hour = lt->tm_hour;
  769. now->min = lt->tm_min;
  770. now->sec = lt->tm_sec;
  771. }
  772. #if defined(MBEDTLS_THREADING_C)
  773. if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
  774. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  775. #endif
  776. return( ret );
  777. }
  778. #endif /* _WIN32 && !EFIX64 && !EFI32 */
  779. /*
  780. * Return 0 if before <= after, 1 otherwise
  781. */
  782. static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
  783. {
  784. if( before->year > after->year )
  785. return( 1 );
  786. if( before->year == after->year &&
  787. before->mon > after->mon )
  788. return( 1 );
  789. if( before->year == after->year &&
  790. before->mon == after->mon &&
  791. before->day > after->day )
  792. return( 1 );
  793. if( before->year == after->year &&
  794. before->mon == after->mon &&
  795. before->day == after->day &&
  796. before->hour > after->hour )
  797. return( 1 );
  798. if( before->year == after->year &&
  799. before->mon == after->mon &&
  800. before->day == after->day &&
  801. before->hour == after->hour &&
  802. before->min > after->min )
  803. return( 1 );
  804. if( before->year == after->year &&
  805. before->mon == after->mon &&
  806. before->day == after->day &&
  807. before->hour == after->hour &&
  808. before->min == after->min &&
  809. before->sec > after->sec )
  810. return( 1 );
  811. return( 0 );
  812. }
  813. int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
  814. {
  815. mbedtls_x509_time now;
  816. if( x509_get_current_time( &now ) != 0 )
  817. return( 1 );
  818. return( x509_check_time( &now, to ) );
  819. }
  820. int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
  821. {
  822. mbedtls_x509_time now;
  823. if( x509_get_current_time( &now ) != 0 )
  824. return( 1 );
  825. return( x509_check_time( from, &now ) );
  826. }
  827. #else /* MBEDTLS_HAVE_TIME_DATE */
  828. int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
  829. {
  830. ((void) to);
  831. return( 0 );
  832. }
  833. int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
  834. {
  835. ((void) from);
  836. return( 0 );
  837. }
  838. #endif /* MBEDTLS_HAVE_TIME_DATE */
  839. #if defined(MBEDTLS_SELF_TEST)
  840. #include "mbedtls/x509_crt.h"
  841. #include "mbedtls/certs.h"
  842. /*
  843. * Checkup routine
  844. */
  845. int mbedtls_x509_self_test( int verbose )
  846. {
  847. #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA1_C)
  848. int ret;
  849. uint32_t flags;
  850. mbedtls_x509_crt cacert;
  851. mbedtls_x509_crt clicert;
  852. if( verbose != 0 )
  853. mbedtls_printf( " X.509 certificate load: " );
  854. mbedtls_x509_crt_init( &clicert );
  855. ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
  856. mbedtls_test_cli_crt_len );
  857. if( ret != 0 )
  858. {
  859. if( verbose != 0 )
  860. mbedtls_printf( "failed\n" );
  861. return( ret );
  862. }
  863. mbedtls_x509_crt_init( &cacert );
  864. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
  865. mbedtls_test_ca_crt_len );
  866. if( ret != 0 )
  867. {
  868. if( verbose != 0 )
  869. mbedtls_printf( "failed\n" );
  870. return( ret );
  871. }
  872. if( verbose != 0 )
  873. mbedtls_printf( "passed\n X.509 signature verify: ");
  874. ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
  875. if( ret != 0 )
  876. {
  877. if( verbose != 0 )
  878. mbedtls_printf( "failed\n" );
  879. return( ret );
  880. }
  881. if( verbose != 0 )
  882. mbedtls_printf( "passed\n\n");
  883. mbedtls_x509_crt_free( &cacert );
  884. mbedtls_x509_crt_free( &clicert );
  885. return( 0 );
  886. #else
  887. ((void) verbose);
  888. return( 0 );
  889. #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
  890. }
  891. #endif /* MBEDTLS_SELF_TEST */
  892. #endif /* MBEDTLS_X509_USE_C */