Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

asn1.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stddef.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <time.h>
  31. #include <ipxe/tables.h>
  32. #include <ipxe/asn1.h>
  33. /** @file
  34. *
  35. * ASN.1 encoding
  36. *
  37. */
  38. /* Disambiguate the various error causes */
  39. #define EINVAL_ASN1_EMPTY \
  40. __einfo_error ( EINFO_EINVAL_ASN1_EMPTY )
  41. #define EINFO_EINVAL_ASN1_EMPTY \
  42. __einfo_uniqify ( EINFO_EINVAL, 0x01, "Empty or underlength cursor" )
  43. #define EINVAL_ASN1_LEN_LEN \
  44. __einfo_error ( EINFO_EINVAL_ASN1_LEN_LEN )
  45. #define EINFO_EINVAL_ASN1_LEN_LEN \
  46. __einfo_uniqify ( EINFO_EINVAL, 0x02, "Length field overruns cursor" )
  47. #define EINVAL_ASN1_LEN \
  48. __einfo_error ( EINFO_EINVAL_ASN1_LEN )
  49. #define EINFO_EINVAL_ASN1_LEN \
  50. __einfo_uniqify ( EINFO_EINVAL, 0x03, "Field overruns cursor" )
  51. #define EINVAL_ASN1_BOOLEAN \
  52. __einfo_error ( EINFO_EINVAL_ASN1_BOOLEAN )
  53. #define EINFO_EINVAL_ASN1_BOOLEAN \
  54. __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid boolean" )
  55. #define EINVAL_ASN1_INTEGER \
  56. __einfo_error ( EINFO_EINVAL_ASN1_INTEGER )
  57. #define EINFO_EINVAL_ASN1_INTEGER \
  58. __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid integer" )
  59. #define EINVAL_ASN1_TIME \
  60. __einfo_error ( EINFO_EINVAL_ASN1_TIME )
  61. #define EINFO_EINVAL_ASN1_TIME \
  62. __einfo_uniqify ( EINFO_EINVAL, 0x05, "Invalid time" )
  63. #define EINVAL_ASN1_ALGORITHM \
  64. __einfo_error ( EINFO_EINVAL_ASN1_ALGORITHM )
  65. #define EINFO_EINVAL_ASN1_ALGORITHM \
  66. __einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid algorithm" )
  67. #define EINVAL_BIT_STRING \
  68. __einfo_error ( EINFO_EINVAL_BIT_STRING )
  69. #define EINFO_EINVAL_BIT_STRING \
  70. __einfo_uniqify ( EINFO_EINVAL, 0x07, "Invalid bit string" )
  71. #define ENOTSUP_ALGORITHM \
  72. __einfo_error ( EINFO_ENOTSUP_ALGORITHM )
  73. #define EINFO_ENOTSUP_ALGORITHM \
  74. __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported algorithm" )
  75. #define ENOTTY_ALGORITHM \
  76. __einfo_error ( EINFO_ENOTTY_ALGORITHM )
  77. #define EINFO_ENOTTY_ALGORITHM \
  78. __einfo_uniqify ( EINFO_ENOTTY, 0x01, "Inappropriate algorithm" )
  79. /**
  80. * Start parsing ASN.1 object
  81. *
  82. * @v cursor ASN.1 object cursor
  83. * @v type Expected type, or ASN1_ANY
  84. * @ret len Length of object body, or negative error
  85. *
  86. * The object cursor will be updated to point to the start of the
  87. * object body (i.e. the first byte following the length byte(s)), and
  88. * the length of the object body (i.e. the number of bytes until the
  89. * following object tag, if any) is returned.
  90. */
  91. static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
  92. unsigned int len_len;
  93. unsigned int len;
  94. /* Sanity check */
  95. if ( cursor->len < 2 /* Tag byte and first length byte */ ) {
  96. if ( cursor->len )
  97. DBGC ( cursor, "ASN1 %p too short\n", cursor );
  98. return -EINVAL_ASN1_EMPTY;
  99. }
  100. /* Check the tag byte */
  101. if ( ( type != ASN1_ANY ) && ( type != asn1_type ( cursor ) ) ) {
  102. DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
  103. cursor, type, *( ( uint8_t * ) cursor->data ) );
  104. return -ENXIO;
  105. }
  106. cursor->data++;
  107. cursor->len--;
  108. /* Extract length of the length field and sanity check */
  109. len_len = *( ( uint8_t * ) cursor->data );
  110. if ( len_len & 0x80 ) {
  111. len_len = ( len_len & 0x7f );
  112. cursor->data++;
  113. cursor->len--;
  114. } else {
  115. len_len = 1;
  116. }
  117. if ( cursor->len < len_len ) {
  118. DBGC ( cursor, "ASN1 %p bad length field length %d (max "
  119. "%zd)\n", cursor, len_len, cursor->len );
  120. return -EINVAL_ASN1_LEN_LEN;
  121. }
  122. /* Extract the length and sanity check */
  123. for ( len = 0 ; len_len ; len_len-- ) {
  124. len <<= 8;
  125. len |= *( ( uint8_t * ) cursor->data );
  126. cursor->data++;
  127. cursor->len--;
  128. }
  129. if ( cursor->len < len ) {
  130. DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n",
  131. cursor, len, cursor->len );
  132. return -EINVAL_ASN1_LEN;
  133. }
  134. return len;
  135. }
  136. /**
  137. * Enter ASN.1 object
  138. *
  139. * @v cursor ASN.1 object cursor
  140. * @v type Expected type, or ASN1_ANY
  141. * @ret rc Return status code
  142. *
  143. * The object cursor will be updated to point to the body of the
  144. * current ASN.1 object. If any error occurs, the object cursor will
  145. * be invalidated.
  146. */
  147. int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) {
  148. int len;
  149. len = asn1_start ( cursor, type );
  150. if ( len < 0 ) {
  151. asn1_invalidate_cursor ( cursor );
  152. return len;
  153. }
  154. cursor->len = len;
  155. DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
  156. cursor, type, len );
  157. return 0;
  158. }
  159. /**
  160. * Skip ASN.1 object if present
  161. *
  162. * @v cursor ASN.1 object cursor
  163. * @v type Expected type, or ASN1_ANY
  164. * @ret rc Return status code
  165. *
  166. * The object cursor will be updated to point to the next ASN.1
  167. * object. If any error occurs, the object cursor will not be
  168. * modified.
  169. */
  170. int asn1_skip_if_exists ( struct asn1_cursor *cursor, unsigned int type ) {
  171. int len;
  172. len = asn1_start ( cursor, type );
  173. if ( len < 0 )
  174. return len;
  175. cursor->data += len;
  176. cursor->len -= len;
  177. DBGC ( cursor, "ASN1 %p skipped object type %02x (len %x)\n",
  178. cursor, type, len );
  179. if ( ! cursor->len ) {
  180. DBGC ( cursor, "ASN1 %p reached end of object\n", cursor );
  181. return -ENOENT;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * Skip ASN.1 object
  187. *
  188. * @v cursor ASN.1 object cursor
  189. * @v type Expected type, or ASN1_ANY
  190. * @ret rc Return status code
  191. *
  192. * The object cursor will be updated to point to the next ASN.1
  193. * object. If any error occurs, the object cursor will be
  194. * invalidated.
  195. */
  196. int asn1_skip ( struct asn1_cursor *cursor, unsigned int type ) {
  197. int rc;
  198. if ( ( rc = asn1_skip_if_exists ( cursor, type ) ) != 0 ) {
  199. asn1_invalidate_cursor ( cursor );
  200. return rc;
  201. }
  202. return 0;
  203. }
  204. /**
  205. * Shrink ASN.1 cursor to fit object
  206. *
  207. * @v cursor ASN.1 object cursor
  208. * @v type Expected type, or ASN1_ANY
  209. * @ret rc Return status code
  210. *
  211. * The object cursor will be shrunk to contain only the current ASN.1
  212. * object. If any error occurs, the object cursor will be
  213. * invalidated.
  214. */
  215. int asn1_shrink ( struct asn1_cursor *cursor, unsigned int type ) {
  216. struct asn1_cursor temp;
  217. const void *end;
  218. int len;
  219. /* Find end of object */
  220. memcpy ( &temp, cursor, sizeof ( temp ) );
  221. len = asn1_start ( &temp, type );
  222. if ( len < 0 ) {
  223. asn1_invalidate_cursor ( cursor );
  224. return len;
  225. }
  226. end = ( temp.data + len );
  227. /* Shrink original cursor to contain only its first object */
  228. cursor->len = ( end - cursor->data );
  229. return 0;
  230. }
  231. /**
  232. * Enter ASN.1 object of any type
  233. *
  234. * @v cursor ASN.1 object cursor
  235. * @ret rc Return status code
  236. */
  237. int asn1_enter_any ( struct asn1_cursor *cursor ) {
  238. return asn1_enter ( cursor, ASN1_ANY );
  239. }
  240. /**
  241. * Skip ASN.1 object of any type
  242. *
  243. * @v cursor ASN.1 object cursor
  244. * @ret rc Return status code
  245. */
  246. int asn1_skip_any ( struct asn1_cursor *cursor ) {
  247. return asn1_skip ( cursor, ASN1_ANY );
  248. }
  249. /**
  250. * Shrink ASN.1 object of any type
  251. *
  252. * @v cursor ASN.1 object cursor
  253. * @ret rc Return status code
  254. */
  255. int asn1_shrink_any ( struct asn1_cursor *cursor ) {
  256. return asn1_shrink ( cursor, ASN1_ANY );
  257. }
  258. /**
  259. * Parse value of ASN.1 boolean
  260. *
  261. * @v cursor ASN.1 object cursor
  262. * @ret value Value, or negative error
  263. */
  264. int asn1_boolean ( const struct asn1_cursor *cursor ) {
  265. struct asn1_cursor contents;
  266. const struct {
  267. uint8_t value;
  268. } __attribute__ (( packed )) *boolean;
  269. /* Enter boolean */
  270. memcpy ( &contents, cursor, sizeof ( contents ) );
  271. asn1_enter ( &contents, ASN1_BOOLEAN );
  272. if ( contents.len != sizeof ( *boolean ) )
  273. return -EINVAL_ASN1_BOOLEAN;
  274. /* Extract value */
  275. boolean = contents.data;
  276. return boolean->value;
  277. }
  278. /**
  279. * Parse value of ASN.1 integer
  280. *
  281. * @v cursor ASN.1 object cursor
  282. * @v value Value to fill in
  283. * @ret rc Return status code
  284. */
  285. int asn1_integer ( const struct asn1_cursor *cursor, int *value ) {
  286. struct asn1_cursor contents;
  287. uint8_t high_byte;
  288. int rc;
  289. /* Enter integer */
  290. memcpy ( &contents, cursor, sizeof ( contents ) );
  291. if ( ( rc = asn1_enter ( &contents, ASN1_INTEGER ) ) != 0 )
  292. return rc;
  293. if ( contents.len < 1 )
  294. return -EINVAL_ASN1_INTEGER;
  295. /* Initialise value according to sign byte */
  296. *value = *( ( int8_t * ) contents.data );
  297. contents.data++;
  298. contents.len--;
  299. /* Process value */
  300. while ( contents.len ) {
  301. high_byte = ( (*value) >> ( 8 * ( sizeof ( *value ) - 1 ) ) );
  302. if ( ( high_byte != 0x00 ) && ( high_byte != 0xff ) ) {
  303. DBGC ( cursor, "ASN1 %p integer overflow\n", cursor );
  304. return -EINVAL_ASN1_INTEGER;
  305. }
  306. *value = ( ( *value << 8 ) | *( ( uint8_t * ) contents.data ) );
  307. contents.data++;
  308. contents.len--;
  309. }
  310. return 0;
  311. }
  312. /**
  313. * Parse ASN.1 bit string
  314. *
  315. * @v cursor ASN.1 cursor
  316. * @v bits Bit string to fill in
  317. * @ret rc Return status code
  318. */
  319. int asn1_bit_string ( const struct asn1_cursor *cursor,
  320. struct asn1_bit_string *bits ) {
  321. struct asn1_cursor contents;
  322. const struct {
  323. uint8_t unused;
  324. uint8_t data[0];
  325. } __attribute__ (( packed )) *bit_string;
  326. size_t len;
  327. unsigned int unused;
  328. uint8_t unused_mask;
  329. const uint8_t *last;
  330. int rc;
  331. /* Enter bit string */
  332. memcpy ( &contents, cursor, sizeof ( contents ) );
  333. if ( ( rc = asn1_enter ( &contents, ASN1_BIT_STRING ) ) != 0 ) {
  334. DBGC ( cursor, "ASN1 %p cannot locate bit string:\n", cursor );
  335. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  336. return rc;
  337. }
  338. /* Validity checks */
  339. if ( contents.len < sizeof ( *bit_string ) ) {
  340. DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
  341. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  342. return -EINVAL_BIT_STRING;
  343. }
  344. bit_string = contents.data;
  345. len = ( contents.len - offsetof ( typeof ( *bit_string ), data ) );
  346. unused = bit_string->unused;
  347. unused_mask = ( 0xff >> ( 8 - unused ) );
  348. last = ( bit_string->data + len - 1 );
  349. if ( ( unused >= 8 ) ||
  350. ( ( unused > 0 ) && ( len == 0 ) ) ||
  351. ( ( *last & unused_mask ) != 0 ) ) {
  352. DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
  353. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  354. return -EINVAL_BIT_STRING;
  355. }
  356. /* Populate bit string */
  357. bits->data = &bit_string->data;
  358. bits->len = len;
  359. bits->unused = unused;
  360. return 0;
  361. }
  362. /**
  363. * Parse ASN.1 bit string that must be an integral number of bytes
  364. *
  365. * @v cursor ASN.1 cursor
  366. * @v bits Bit string to fill in
  367. * @ret rc Return status code
  368. */
  369. int asn1_integral_bit_string ( const struct asn1_cursor *cursor,
  370. struct asn1_bit_string *bits ) {
  371. int rc;
  372. /* Parse bit string */
  373. if ( ( rc = asn1_bit_string ( cursor, bits ) ) != 0 )
  374. return rc;
  375. /* Check that there are no unused bits at end of string */
  376. if ( bits->unused ) {
  377. DBGC ( cursor, "ASN1 %p invalid integral bit string:\n",
  378. cursor );
  379. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  380. return -EINVAL_BIT_STRING;
  381. }
  382. return 0;
  383. }
  384. /**
  385. * Compare two ASN.1 objects
  386. *
  387. * @v cursor1 ASN.1 object cursor
  388. * @v cursor2 ASN.1 object cursor
  389. * @ret difference Difference as returned by memcmp()
  390. *
  391. * Note that invalid and empty cursors will compare as equal with each
  392. * other.
  393. */
  394. int asn1_compare ( const struct asn1_cursor *cursor1,
  395. const struct asn1_cursor *cursor2 ) {
  396. int difference;
  397. difference = ( cursor2->len - cursor1->len );
  398. return ( difference ? difference :
  399. memcmp ( cursor1->data, cursor2->data, cursor1->len ) );
  400. }
  401. /**
  402. * Identify ASN.1 algorithm by OID
  403. *
  404. * @v cursor ASN.1 object cursor
  405. * @ret algorithm Algorithm, or NULL
  406. */
  407. static struct asn1_algorithm *
  408. asn1_find_algorithm ( const struct asn1_cursor *cursor ) {
  409. struct asn1_algorithm *algorithm;
  410. for_each_table_entry ( algorithm, ASN1_ALGORITHMS ) {
  411. if ( asn1_compare ( &algorithm->oid, cursor ) == 0 )
  412. return algorithm;
  413. }
  414. return NULL;
  415. }
  416. /**
  417. * Parse ASN.1 OID-identified algorithm
  418. *
  419. * @v cursor ASN.1 object cursor
  420. * @ret algorithm Algorithm
  421. * @ret rc Return status code
  422. */
  423. int asn1_algorithm ( const struct asn1_cursor *cursor,
  424. struct asn1_algorithm **algorithm ) {
  425. struct asn1_cursor contents;
  426. int rc;
  427. /* Enter signatureAlgorithm */
  428. memcpy ( &contents, cursor, sizeof ( contents ) );
  429. asn1_enter ( &contents, ASN1_SEQUENCE );
  430. /* Enter algorithm */
  431. if ( ( rc = asn1_enter ( &contents, ASN1_OID ) ) != 0 ) {
  432. DBGC ( cursor, "ASN1 %p cannot locate algorithm OID:\n",
  433. cursor );
  434. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  435. return -EINVAL_ASN1_ALGORITHM;
  436. }
  437. /* Identify algorithm */
  438. *algorithm = asn1_find_algorithm ( &contents );
  439. if ( ! *algorithm ) {
  440. DBGC ( cursor, "ASN1 %p unrecognised algorithm:\n", cursor );
  441. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  442. return -ENOTSUP_ALGORITHM;
  443. }
  444. return 0;
  445. }
  446. /**
  447. * Parse ASN.1 OID-identified public-key algorithm
  448. *
  449. * @v cursor ASN.1 object cursor
  450. * @ret algorithm Algorithm
  451. * @ret rc Return status code
  452. */
  453. int asn1_pubkey_algorithm ( const struct asn1_cursor *cursor,
  454. struct asn1_algorithm **algorithm ) {
  455. int rc;
  456. /* Parse algorithm */
  457. if ( ( rc = asn1_algorithm ( cursor, algorithm ) ) != 0 )
  458. return rc;
  459. /* Check algorithm has a public key */
  460. if ( ! (*algorithm)->pubkey ) {
  461. DBGC ( cursor, "ASN1 %p algorithm %s is not a public-key "
  462. "algorithm:\n", cursor, (*algorithm)->name );
  463. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  464. return -ENOTTY_ALGORITHM;
  465. }
  466. return 0;
  467. }
  468. /**
  469. * Parse ASN.1 OID-identified digest algorithm
  470. *
  471. * @v cursor ASN.1 object cursor
  472. * @ret algorithm Algorithm
  473. * @ret rc Return status code
  474. */
  475. int asn1_digest_algorithm ( const struct asn1_cursor *cursor,
  476. struct asn1_algorithm **algorithm ) {
  477. int rc;
  478. /* Parse algorithm */
  479. if ( ( rc = asn1_algorithm ( cursor, algorithm ) ) != 0 )
  480. return rc;
  481. /* Check algorithm has a digest */
  482. if ( ! (*algorithm)->digest ) {
  483. DBGC ( cursor, "ASN1 %p algorithm %s is not a digest "
  484. "algorithm:\n", cursor, (*algorithm)->name );
  485. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  486. return -ENOTTY_ALGORITHM;
  487. }
  488. return 0;
  489. }
  490. /**
  491. * Parse ASN.1 OID-identified signature algorithm
  492. *
  493. * @v cursor ASN.1 object cursor
  494. * @ret algorithm Algorithm
  495. * @ret rc Return status code
  496. */
  497. int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
  498. struct asn1_algorithm **algorithm ) {
  499. int rc;
  500. /* Parse algorithm */
  501. if ( ( rc = asn1_algorithm ( cursor, algorithm ) ) != 0 )
  502. return rc;
  503. /* Check algorithm has a public key */
  504. if ( ! (*algorithm)->pubkey ) {
  505. DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
  506. "algorithm:\n", cursor, (*algorithm)->name );
  507. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  508. return -ENOTTY_ALGORITHM;
  509. }
  510. /* Check algorithm has a digest */
  511. if ( ! (*algorithm)->digest ) {
  512. DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
  513. "algorithm:\n", cursor, (*algorithm)->name );
  514. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  515. return -ENOTTY_ALGORITHM;
  516. }
  517. return 0;
  518. }
  519. /**
  520. * Parse ASN.1 GeneralizedTime
  521. *
  522. * @v cursor ASN.1 cursor
  523. * @v time Time to fill in
  524. * @ret rc Return status code
  525. *
  526. * RFC 5280 section 4.1.2.5 places several restrictions on the allowed
  527. * formats for UTCTime and GeneralizedTime, and mandates the
  528. * interpretation of centuryless year values.
  529. */
  530. int asn1_generalized_time ( const struct asn1_cursor *cursor, time_t *time ) {
  531. struct asn1_cursor contents;
  532. unsigned int have_century;
  533. unsigned int type;
  534. union {
  535. struct {
  536. uint8_t century;
  537. uint8_t year;
  538. uint8_t month;
  539. uint8_t day;
  540. uint8_t hour;
  541. uint8_t minute;
  542. uint8_t second;
  543. } __attribute__ (( packed )) named;
  544. uint8_t raw[7];
  545. } pairs;
  546. struct tm tm;
  547. const uint8_t *data;
  548. size_t remaining;
  549. unsigned int tens;
  550. unsigned int units;
  551. unsigned int i;
  552. int rc;
  553. /* Determine time format utcTime/generalizedTime */
  554. memcpy ( &contents, cursor, sizeof ( contents ) );
  555. type = asn1_type ( &contents );
  556. switch ( type ) {
  557. case ASN1_UTC_TIME:
  558. have_century = 0;
  559. break;
  560. case ASN1_GENERALIZED_TIME:
  561. have_century = 1;
  562. break;
  563. default:
  564. DBGC ( cursor, "ASN1 %p invalid time type %02x\n",
  565. cursor, type );
  566. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  567. return -EINVAL_ASN1_TIME;
  568. }
  569. /* Enter utcTime/generalizedTime */
  570. if ( ( rc = asn1_enter ( &contents, type ) ) != 0 ) {
  571. DBGC ( cursor, "ASN1 %p cannot locate %s time:\n", cursor,
  572. ( ( type == ASN1_UTC_TIME ) ? "UTC" : "generalized" ) );
  573. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  574. return rc;
  575. }
  576. /* Parse digit string a pair at a time */
  577. memset ( &pairs, 0, sizeof ( pairs ) );
  578. data = contents.data;
  579. remaining = contents.len;
  580. for ( i = ( have_century ? 0 : 1 ) ; i < sizeof ( pairs.raw ) ; i++ ) {
  581. if ( remaining < 2 ) {
  582. /* Some certificates violate the X.509 RFC by
  583. * omitting the "seconds" value.
  584. */
  585. if ( i == ( sizeof ( pairs.raw ) - 1 ) )
  586. break;
  587. DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
  588. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  589. return -EINVAL_ASN1_TIME;
  590. }
  591. tens = data[0];
  592. units = data[1];
  593. if ( ! ( isdigit ( tens ) && isdigit ( units ) ) ) {
  594. DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
  595. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  596. return -EINVAL_ASN1_TIME;
  597. }
  598. pairs.raw[i] = ( ( 10 * ( tens - '0' ) ) + ( units - '0' ) );
  599. data += 2;
  600. remaining -= 2;
  601. }
  602. /* Determine century if applicable */
  603. if ( ! have_century )
  604. pairs.named.century = ( ( pairs.named.year >= 50 ) ? 19 : 20 );
  605. /* Check for trailing "Z" */
  606. if ( ( remaining != 1 ) || ( data[0] != 'Z' ) ) {
  607. DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
  608. DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
  609. return -EINVAL_ASN1_TIME;
  610. }
  611. /* Fill in time */
  612. tm.tm_year = ( ( ( pairs.named.century - 19 ) * 100 ) +
  613. pairs.named.year );
  614. tm.tm_mon = ( pairs.named.month - 1 );
  615. tm.tm_mday = pairs.named.day;
  616. tm.tm_hour = pairs.named.hour;
  617. tm.tm_min = pairs.named.minute;
  618. tm.tm_sec = pairs.named.second;
  619. /* Convert to seconds since the Epoch */
  620. *time = mktime ( &tm );
  621. return 0;
  622. }
  623. /**
  624. * Construct ASN.1 header
  625. *
  626. * @v header ASN.1 builder header
  627. * @v type Type
  628. * @v len Content length
  629. * @ret header_len Header length
  630. */
  631. static size_t asn1_header ( struct asn1_builder_header *header,
  632. unsigned int type, size_t len ) {
  633. unsigned int header_len = 2;
  634. unsigned int len_len = 0;
  635. size_t temp;
  636. /* Construct header */
  637. header->type = type;
  638. if ( len < 0x80 ) {
  639. header->length[0] = len;
  640. } else {
  641. for ( temp = len ; temp ; temp >>= 8 )
  642. len_len++;
  643. header->length[0] = ( 0x80 | len_len );
  644. header_len += len_len;
  645. for ( temp = len ; temp ; temp >>= 8 )
  646. header->length[len_len--] = ( temp & 0xff );
  647. }
  648. return header_len;
  649. }
  650. /**
  651. * Grow ASN.1 builder
  652. *
  653. * @v builder ASN.1 builder
  654. * @v extra Extra space to prepend
  655. * @ret rc Return status code
  656. */
  657. static int asn1_grow ( struct asn1_builder *builder, size_t extra ) {
  658. size_t new_len;
  659. void *new;
  660. /* As with the ASN1 parsing functions, make errors permanent */
  661. if ( builder->len && ! builder->data )
  662. return -ENOMEM;
  663. /* Reallocate data buffer */
  664. new_len = ( builder->len + extra );
  665. new = realloc ( builder->data, new_len );
  666. if ( ! new ) {
  667. free ( builder->data );
  668. builder->data = NULL;
  669. return -ENOMEM;
  670. }
  671. builder->data = new;
  672. /* Move existing data to end of buffer */
  673. memmove ( ( builder->data + extra ), builder->data, builder->len );
  674. builder->len = new_len;
  675. return 0;
  676. }
  677. /**
  678. * Prepend raw data to ASN.1 builder
  679. *
  680. * @v builder ASN.1 builder
  681. * @v data Data to prepend
  682. * @v len Length of data to prepend
  683. * @ret rc Return status code
  684. */
  685. int asn1_prepend_raw ( struct asn1_builder *builder, const void *data,
  686. size_t len ) {
  687. int rc;
  688. /* Grow buffer */
  689. if ( ( rc = asn1_grow ( builder, len ) ) != 0 )
  690. return rc;
  691. /* Populate data buffer */
  692. memcpy ( builder->data, data, len );
  693. return 0;
  694. }
  695. /**
  696. * Prepend data to ASN.1 builder
  697. *
  698. * @v builder ASN.1 builder
  699. * @v type Type
  700. * @v data Data to prepend
  701. * @v len Length of data to prepend
  702. * @ret rc Return status code
  703. */
  704. int asn1_prepend ( struct asn1_builder *builder, unsigned int type,
  705. const void *data, size_t len ) {
  706. struct asn1_builder_header header;
  707. size_t header_len;
  708. int rc;
  709. /* Construct header */
  710. header_len = asn1_header ( &header, type, len );
  711. /* Grow buffer */
  712. if ( ( rc = asn1_grow ( builder, header_len + len ) ) != 0 )
  713. return rc;
  714. /* Populate data buffer */
  715. memcpy ( builder->data, &header, header_len );
  716. memcpy ( ( builder->data + header_len ), data, len );
  717. return 0;
  718. }
  719. /**
  720. * Wrap ASN.1 builder
  721. *
  722. * @v builder ASN.1 builder
  723. * @v type Type
  724. * @ret rc Return status code
  725. */
  726. int asn1_wrap ( struct asn1_builder *builder, unsigned int type ) {
  727. struct asn1_builder_header header;
  728. size_t header_len;
  729. int rc;
  730. /* Construct header */
  731. header_len = asn1_header ( &header, type, builder->len );
  732. /* Grow buffer */
  733. if ( ( rc = asn1_grow ( builder, header_len ) ) != 0 )
  734. return rc;
  735. /* Populate data buffer */
  736. memcpy ( builder->data, &header, header_len );
  737. return 0;
  738. }