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.

asn1.c 22KB

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