Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

asn1.c 21KB

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