2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 #include <ipxe/tables.h>
34 #include <ipxe/asn1.h>
42 /* Disambiguate the various error causes */
43 #define EINVAL_ASN1_EMPTY \
44 __einfo_error ( EINFO_EINVAL_ASN1_EMPTY )
45 #define EINFO_EINVAL_ASN1_EMPTY \
46 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Empty or underlength cursor" )
47 #define EINVAL_ASN1_LEN_LEN \
48 __einfo_error ( EINFO_EINVAL_ASN1_LEN_LEN )
49 #define EINFO_EINVAL_ASN1_LEN_LEN \
50 __einfo_uniqify ( EINFO_EINVAL, 0x02, "Length field overruns cursor" )
51 #define EINVAL_ASN1_LEN \
52 __einfo_error ( EINFO_EINVAL_ASN1_LEN )
53 #define EINFO_EINVAL_ASN1_LEN \
54 __einfo_uniqify ( EINFO_EINVAL, 0x03, "Field overruns cursor" )
55 #define EINVAL_ASN1_BOOLEAN \
56 __einfo_error ( EINFO_EINVAL_ASN1_BOOLEAN )
57 #define EINFO_EINVAL_ASN1_BOOLEAN \
58 __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid boolean" )
59 #define EINVAL_ASN1_INTEGER \
60 __einfo_error ( EINFO_EINVAL_ASN1_INTEGER )
61 #define EINFO_EINVAL_ASN1_INTEGER \
62 __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid integer" )
63 #define EINVAL_ASN1_TIME \
64 __einfo_error ( EINFO_EINVAL_ASN1_TIME )
65 #define EINFO_EINVAL_ASN1_TIME \
66 __einfo_uniqify ( EINFO_EINVAL, 0x05, "Invalid time" )
67 #define EINVAL_ASN1_ALGORITHM \
68 __einfo_error ( EINFO_EINVAL_ASN1_ALGORITHM )
69 #define EINFO_EINVAL_ASN1_ALGORITHM \
70 __einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid algorithm" )
71 #define EINVAL_BIT_STRING \
72 __einfo_error ( EINFO_EINVAL_BIT_STRING )
73 #define EINFO_EINVAL_BIT_STRING \
74 __einfo_uniqify ( EINFO_EINVAL, 0x07, "Invalid bit string" )
75 #define ENOTSUP_ALGORITHM \
76 __einfo_error ( EINFO_ENOTSUP_ALGORITHM )
77 #define EINFO_ENOTSUP_ALGORITHM \
78 __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported algorithm" )
79 #define ENOTTY_ALGORITHM \
80 __einfo_error ( EINFO_ENOTTY_ALGORITHM )
81 #define EINFO_ENOTTY_ALGORITHM \
82 __einfo_uniqify ( EINFO_ENOTTY, 0x01, "Inappropriate algorithm" )
85 * Invalidate ASN.1 object cursor
87 * @v cursor ASN.1 object cursor
89 void asn1_invalidate_cursor ( struct asn1_cursor *cursor ) {
90 static uint8_t asn1_invalid_object[] = { ASN1_END, 0 };
92 cursor->data = asn1_invalid_object;
97 * Start parsing ASN.1 object
99 * @v cursor ASN.1 object cursor
100 * @v type Expected type, or ASN1_ANY
101 * @ret len Length of object body, or negative error
103 * The object cursor will be updated to point to the start of the
104 * object body (i.e. the first byte following the length byte(s)), and
105 * the length of the object body (i.e. the number of bytes until the
106 * following object tag, if any) is returned.
108 static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
109 unsigned int len_len;
113 if ( cursor->len < 2 /* Tag byte and first length byte */ ) {
115 DBGC ( cursor, "ASN1 %p too short\n", cursor );
116 return -EINVAL_ASN1_EMPTY;
119 /* Check the tag byte */
120 if ( ( type != ASN1_ANY ) && ( type != asn1_type ( cursor ) ) ) {
121 DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
122 cursor, type, *( ( uint8_t * ) cursor->data ) );
128 /* Extract length of the length field and sanity check */
129 len_len = *( ( uint8_t * ) cursor->data );
130 if ( len_len & 0x80 ) {
131 len_len = ( len_len & 0x7f );
137 if ( cursor->len < len_len ) {
138 DBGC ( cursor, "ASN1 %p bad length field length %d (max "
139 "%zd)\n", cursor, len_len, cursor->len );
140 return -EINVAL_ASN1_LEN_LEN;
143 /* Extract the length and sanity check */
144 for ( len = 0 ; len_len ; len_len-- ) {
146 len |= *( ( uint8_t * ) cursor->data );
150 if ( cursor->len < len ) {
151 DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n",
152 cursor, len, cursor->len );
153 return -EINVAL_ASN1_LEN;
162 * @v cursor ASN.1 object cursor
163 * @v type Expected type, or ASN1_ANY
164 * @ret rc Return status code
166 * The object cursor will be updated to point to the body of the
167 * current ASN.1 object. If any error occurs, the object cursor will
170 int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) {
173 len = asn1_start ( cursor, type );
175 asn1_invalidate_cursor ( cursor );
180 DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
187 * Skip ASN.1 object if present
189 * @v cursor ASN.1 object cursor
190 * @v type Expected type, or ASN1_ANY
191 * @ret rc Return status code
193 * The object cursor will be updated to point to the next ASN.1
194 * object. If any error occurs, the object cursor will not be
197 int asn1_skip_if_exists ( struct asn1_cursor *cursor, unsigned int type ) {
200 len = asn1_start ( cursor, type );
206 DBGC ( cursor, "ASN1 %p skipped object type %02x (len %x)\n",
209 if ( ! cursor->len ) {
210 DBGC ( cursor, "ASN1 %p reached end of object\n", cursor );
220 * @v cursor ASN.1 object cursor
221 * @v type Expected type, or ASN1_ANY
222 * @ret rc Return status code
224 * The object cursor will be updated to point to the next ASN.1
225 * object. If any error occurs, the object cursor will be
228 int asn1_skip ( struct asn1_cursor *cursor, unsigned int type ) {
231 if ( ( rc = asn1_skip_if_exists ( cursor, type ) ) != 0 ) {
232 asn1_invalidate_cursor ( cursor );
240 * Shrink ASN.1 cursor to fit object
242 * @v cursor ASN.1 object cursor
243 * @v type Expected type, or ASN1_ANY
244 * @ret rc Return status code
246 * The object cursor will be shrunk to contain only the current ASN.1
247 * object. If any error occurs, the object cursor will be
250 int asn1_shrink ( struct asn1_cursor *cursor, unsigned int type ) {
251 struct asn1_cursor temp;
255 /* Find end of object */
256 memcpy ( &temp, cursor, sizeof ( temp ) );
257 len = asn1_start ( &temp, type );
259 asn1_invalidate_cursor ( cursor );
262 end = ( temp.data + len );
264 /* Shrink original cursor to contain only its first object */
265 cursor->len = ( end - cursor->data );
271 * Enter ASN.1 object of any type
273 * @v cursor ASN.1 object cursor
274 * @ret rc Return status code
276 int asn1_enter_any ( struct asn1_cursor *cursor ) {
277 return asn1_enter ( cursor, ASN1_ANY );
281 * Skip ASN.1 object of any type
283 * @v cursor ASN.1 object cursor
284 * @ret rc Return status code
286 int asn1_skip_any ( struct asn1_cursor *cursor ) {
287 return asn1_skip ( cursor, ASN1_ANY );
291 * Shrink ASN.1 object of any type
293 * @v cursor ASN.1 object cursor
294 * @ret rc Return status code
296 int asn1_shrink_any ( struct asn1_cursor *cursor ) {
297 return asn1_shrink ( cursor, ASN1_ANY );
301 * Parse value of ASN.1 boolean
303 * @v cursor ASN.1 object cursor
304 * @ret value Value, or negative error
306 int asn1_boolean ( const struct asn1_cursor *cursor ) {
307 struct asn1_cursor contents;
310 } __attribute__ (( packed )) *boolean;
313 memcpy ( &contents, cursor, sizeof ( contents ) );
314 asn1_enter ( &contents, ASN1_BOOLEAN );
315 if ( contents.len != sizeof ( *boolean ) )
316 return -EINVAL_ASN1_BOOLEAN;
319 boolean = contents.data;
320 return boolean->value;
324 * Parse value of ASN.1 integer
326 * @v cursor ASN.1 object cursor
327 * @v value Value to fill in
328 * @ret rc Return status code
330 int asn1_integer ( const struct asn1_cursor *cursor, int *value ) {
331 struct asn1_cursor contents;
336 memcpy ( &contents, cursor, sizeof ( contents ) );
337 if ( ( rc = asn1_enter ( &contents, ASN1_INTEGER ) ) != 0 )
339 if ( contents.len < 1 )
340 return -EINVAL_ASN1_INTEGER;
342 /* Initialise value according to sign byte */
343 *value = *( ( int8_t * ) contents.data );
348 while ( contents.len ) {
349 high_byte = ( (*value) >> ( 8 * ( sizeof ( *value ) - 1 ) ) );
350 if ( ( high_byte != 0x00 ) && ( high_byte != 0xff ) ) {
351 DBGC ( cursor, "ASN1 %p integer overflow\n", cursor );
352 return -EINVAL_ASN1_INTEGER;
354 *value = ( ( *value << 8 ) | *( ( uint8_t * ) contents.data ) );
363 * Parse ASN.1 bit string
365 * @v cursor ASN.1 cursor
366 * @v bits Bit string to fill in
367 * @ret rc Return status code
369 int asn1_bit_string ( const struct asn1_cursor *cursor,
370 struct asn1_bit_string *bits ) {
371 struct asn1_cursor contents;
375 } __attribute__ (( packed )) *bit_string;
382 /* Enter bit string */
383 memcpy ( &contents, cursor, sizeof ( contents ) );
384 if ( ( rc = asn1_enter ( &contents, ASN1_BIT_STRING ) ) != 0 ) {
385 DBGC ( cursor, "ASN1 %p cannot locate bit string:\n", cursor );
386 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
390 /* Validity checks */
391 if ( contents.len < sizeof ( *bit_string ) ) {
392 DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
393 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
394 return -EINVAL_BIT_STRING;
396 bit_string = contents.data;
397 len = ( contents.len - offsetof ( typeof ( *bit_string ), data ) );
398 unused = bit_string->unused;
399 unused_mask = ( 0xff >> ( 8 - unused ) );
400 last = ( bit_string->data + len - 1 );
401 if ( ( unused >= 8 ) ||
402 ( ( unused > 0 ) && ( len == 0 ) ) ||
403 ( ( *last & unused_mask ) != 0 ) ) {
404 DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
405 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
406 return -EINVAL_BIT_STRING;
409 /* Populate bit string */
410 bits->data = &bit_string->data;
412 bits->unused = unused;
418 * Parse ASN.1 bit string that must be an integral number of bytes
420 * @v cursor ASN.1 cursor
421 * @v bits Bit string to fill in
422 * @ret rc Return status code
424 int asn1_integral_bit_string ( const struct asn1_cursor *cursor,
425 struct asn1_bit_string *bits ) {
428 /* Parse bit string */
429 if ( ( rc = asn1_bit_string ( cursor, bits ) ) != 0 )
432 /* Check that there are no unused bits at end of string */
433 if ( bits->unused ) {
434 DBGC ( cursor, "ASN1 %p invalid integral bit string:\n",
436 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
437 return -EINVAL_BIT_STRING;
444 * Compare two ASN.1 objects
446 * @v cursor1 ASN.1 object cursor
447 * @v cursor2 ASN.1 object cursor
448 * @ret difference Difference as returned by memcmp()
450 * Note that invalid and empty cursors will compare as equal with each
453 int asn1_compare ( const struct asn1_cursor *cursor1,
454 const struct asn1_cursor *cursor2 ) {
457 difference = ( cursor2->len - cursor1->len );
458 return ( difference ? difference :
459 memcmp ( cursor1->data, cursor2->data, cursor1->len ) );
463 * Identify ASN.1 algorithm by OID
465 * @v cursor ASN.1 object cursor
467 * @ret algorithm Algorithm, or NULL
469 static struct asn1_algorithm *
470 asn1_find_algorithm ( const struct asn1_cursor *cursor ) {
471 struct asn1_algorithm *algorithm;
473 for_each_table_entry ( algorithm, ASN1_ALGORITHMS ) {
474 if ( asn1_compare ( &algorithm->oid, cursor ) == 0 )
482 * Parse ASN.1 OID-identified algorithm
484 * @v cursor ASN.1 object cursor
485 * @ret algorithm Algorithm
486 * @ret rc Return status code
488 int asn1_algorithm ( const struct asn1_cursor *cursor,
489 struct asn1_algorithm **algorithm ) {
490 struct asn1_cursor contents;
493 /* Enter signatureAlgorithm */
494 memcpy ( &contents, cursor, sizeof ( contents ) );
495 asn1_enter ( &contents, ASN1_SEQUENCE );
497 /* Enter algorithm */
498 if ( ( rc = asn1_enter ( &contents, ASN1_OID ) ) != 0 ) {
499 DBGC ( cursor, "ASN1 %p cannot locate algorithm OID:\n",
501 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
502 return -EINVAL_ASN1_ALGORITHM;
505 /* Identify algorithm */
506 *algorithm = asn1_find_algorithm ( &contents );
507 if ( ! *algorithm ) {
508 DBGC ( cursor, "ASN1 %p unrecognised algorithm:\n", cursor );
509 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
510 return -ENOTSUP_ALGORITHM;
517 * Parse ASN.1 OID-identified public-key algorithm
519 * @v cursor ASN.1 object cursor
520 * @ret algorithm Algorithm
521 * @ret rc Return status code
523 int asn1_pubkey_algorithm ( const struct asn1_cursor *cursor,
524 struct asn1_algorithm **algorithm ) {
527 /* Parse algorithm */
528 if ( ( rc = asn1_algorithm ( cursor, algorithm ) ) != 0 )
531 /* Check algorithm has a public key */
532 if ( ! (*algorithm)->pubkey ) {
533 DBGC ( cursor, "ASN1 %p algorithm %s is not a public-key "
534 "algorithm:\n", cursor, (*algorithm)->name );
535 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
536 return -ENOTTY_ALGORITHM;
543 * Parse ASN.1 OID-identified digest algorithm
545 * @v cursor ASN.1 object cursor
546 * @ret algorithm Algorithm
547 * @ret rc Return status code
549 int asn1_digest_algorithm ( const struct asn1_cursor *cursor,
550 struct asn1_algorithm **algorithm ) {
553 /* Parse algorithm */
554 if ( ( rc = asn1_algorithm ( cursor, algorithm ) ) != 0 )
557 /* Check algorithm has a digest */
558 if ( ! (*algorithm)->digest ) {
559 DBGC ( cursor, "ASN1 %p algorithm %s is not a digest "
560 "algorithm:\n", cursor, (*algorithm)->name );
561 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
562 return -ENOTTY_ALGORITHM;
569 * Parse ASN.1 OID-identified signature algorithm
571 * @v cursor ASN.1 object cursor
572 * @ret algorithm Algorithm
573 * @ret rc Return status code
575 int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
576 struct asn1_algorithm **algorithm ) {
579 /* Parse algorithm */
580 if ( ( rc = asn1_algorithm ( cursor, algorithm ) ) != 0 )
583 /* Check algorithm has a public key */
584 if ( ! (*algorithm)->pubkey ) {
585 DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
586 "algorithm:\n", cursor, (*algorithm)->name );
587 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
588 return -ENOTTY_ALGORITHM;
591 /* Check algorithm has a digest */
592 if ( ! (*algorithm)->digest ) {
593 DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
594 "algorithm:\n", cursor, (*algorithm)->name );
595 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
596 return -ENOTTY_ALGORITHM;
603 * Parse ASN.1 GeneralizedTime
605 * @v cursor ASN.1 cursor
606 * @v time Time to fill in
607 * @ret rc Return status code
609 * RFC 5280 section 4.1.2.5 places several restrictions on the allowed
610 * formats for UTCTime and GeneralizedTime, and mandates the
611 * interpretation of centuryless year values.
613 int asn1_generalized_time ( const struct asn1_cursor *cursor, time_t *time ) {
614 struct asn1_cursor contents;
615 unsigned int have_century;
626 } __attribute__ (( packed )) named;
637 /* Determine time format utcTime/generalizedTime */
638 memcpy ( &contents, cursor, sizeof ( contents ) );
639 type = asn1_type ( &contents );
644 case ASN1_GENERALIZED_TIME:
648 DBGC ( cursor, "ASN1 %p invalid time type %02x\n",
650 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
651 return -EINVAL_ASN1_TIME;
654 /* Enter utcTime/generalizedTime */
655 if ( ( rc = asn1_enter ( &contents, type ) ) != 0 ) {
656 DBGC ( cursor, "ASN1 %p cannot locate %s time:\n", cursor,
657 ( ( type == ASN1_UTC_TIME ) ? "UTC" : "generalized" ) );
658 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
662 /* Parse digit string a pair at a time */
663 memset ( &pairs, 0, sizeof ( pairs ) );
664 data = contents.data;
665 remaining = contents.len;
666 for ( i = ( have_century ? 0 : 1 ) ; i < sizeof ( pairs.raw ) ; i++ ) {
667 if ( remaining < 2 ) {
668 /* Some certificates violate the X.509 RFC by
669 * omitting the "seconds" value.
671 if ( i == ( sizeof ( pairs.raw ) - 1 ) )
673 DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
674 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
675 return -EINVAL_ASN1_TIME;
679 if ( ! ( isdigit ( tens ) && isdigit ( units ) ) ) {
680 DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
681 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
682 return -EINVAL_ASN1_TIME;
684 pairs.raw[i] = ( ( 10 * ( tens - '0' ) ) + ( units - '0' ) );
689 /* Determine century if applicable */
690 if ( ! have_century )
691 pairs.named.century = ( ( pairs.named.year >= 50 ) ? 19 : 20 );
693 /* Check for trailing "Z" */
694 if ( ( remaining != 1 ) || ( data[0] != 'Z' ) ) {
695 DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
696 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
697 return -EINVAL_ASN1_TIME;
701 tm.tm_year = ( ( ( pairs.named.century - 19 ) * 100 ) +
703 tm.tm_mon = ( pairs.named.month - 1 );
704 tm.tm_mday = pairs.named.day;
705 tm.tm_hour = pairs.named.hour;
706 tm.tm_min = pairs.named.minute;
707 tm.tm_sec = pairs.named.second;
709 /* Convert to seconds since the Epoch */
710 *time = mktime ( &tm );
716 * Construct ASN.1 header
718 * @v header ASN.1 builder header
720 * @v len Content length
721 * @ret header_len Header length
723 static size_t asn1_header ( struct asn1_builder_header *header,
724 unsigned int type, size_t len ) {
725 unsigned int header_len = 2;
726 unsigned int len_len = 0;
729 /* Construct header */
732 header->length[0] = len;
734 for ( temp = len ; temp ; temp >>= 8 )
736 header->length[0] = ( 0x80 | len_len );
737 header_len += len_len;
738 for ( temp = len ; temp ; temp >>= 8 )
739 header->length[len_len--] = ( temp & 0xff );
748 * @v builder ASN.1 builder
749 * @v extra Extra space to prepend
750 * @ret rc Return status code
752 static int asn1_grow ( struct asn1_builder *builder, size_t extra ) {
756 /* As with the ASN1 parsing functions, make errors permanent */
757 if ( builder->len && ! builder->data )
760 /* Reallocate data buffer */
761 new_len = ( builder->len + extra );
762 new = realloc ( builder->data, new_len );
764 free ( builder->data );
765 builder->data = NULL;
770 /* Move existing data to end of buffer */
771 memmove ( ( builder->data + extra ), builder->data, builder->len );
772 builder->len = new_len;
778 * Prepend raw data to ASN.1 builder
780 * @v builder ASN.1 builder
781 * @v data Data to prepend
782 * @v len Length of data to prepend
783 * @ret rc Return status code
785 int asn1_prepend_raw ( struct asn1_builder *builder, const void *data,
790 if ( ( rc = asn1_grow ( builder, len ) ) != 0 )
793 /* Populate data buffer */
794 memcpy ( builder->data, data, len );
800 * Prepend data to ASN.1 builder
802 * @v builder ASN.1 builder
804 * @v data Data to prepend
805 * @v len Length of data to prepend
806 * @ret rc Return status code
808 int asn1_prepend ( struct asn1_builder *builder, unsigned int type,
809 const void *data, size_t len ) {
810 struct asn1_builder_header header;
814 /* Construct header */
815 header_len = asn1_header ( &header, type, len );
818 if ( ( rc = asn1_grow ( builder, header_len + len ) ) != 0 )
821 /* Populate data buffer */
822 memcpy ( builder->data, &header, header_len );
823 memcpy ( ( builder->data + header_len ), data, len );
831 * @v builder ASN.1 builder
833 * @ret rc Return status code
835 int asn1_wrap ( struct asn1_builder *builder, unsigned int type ) {
836 struct asn1_builder_header header;
840 /* Construct header */
841 header_len = asn1_header ( &header, type, builder->len );
844 if ( ( rc = asn1_grow ( builder, header_len ) ) != 0 )
847 /* Populate data buffer */
848 memcpy ( builder->data, &header, header_len );