x509.c 29 KB

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