x509.c 29 KB

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