You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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