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.

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