iPXE
asn1.c File Reference

ASN.1 encoding. More...

#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <ipxe/tables.h>
#include <ipxe/image.h>
#include <ipxe/crypto.h>
#include <ipxe/asn1.h>

Go to the source code of this file.

Macros

#define EINVAL_ASN1_EMPTY   __einfo_error ( EINFO_EINVAL_ASN1_EMPTY )
#define EINFO_EINVAL_ASN1_EMPTY   __einfo_uniqify ( EINFO_EINVAL, 0x01, "Empty or underlength cursor" )
#define EINVAL_ASN1_LEN_LEN   __einfo_error ( EINFO_EINVAL_ASN1_LEN_LEN )
#define EINFO_EINVAL_ASN1_LEN_LEN   __einfo_uniqify ( EINFO_EINVAL, 0x02, "Length field overruns cursor" )
#define EINVAL_ASN1_LEN   __einfo_error ( EINFO_EINVAL_ASN1_LEN )
#define EINFO_EINVAL_ASN1_LEN   __einfo_uniqify ( EINFO_EINVAL, 0x03, "Field overruns cursor" )
#define EINVAL_ASN1_BOOLEAN   __einfo_error ( EINFO_EINVAL_ASN1_BOOLEAN )
#define EINFO_EINVAL_ASN1_BOOLEAN   __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid boolean" )
#define EINVAL_ASN1_INTEGER   __einfo_error ( EINFO_EINVAL_ASN1_INTEGER )
#define EINFO_EINVAL_ASN1_INTEGER   __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid integer" )
#define EINVAL_ASN1_TIME   __einfo_error ( EINFO_EINVAL_ASN1_TIME )
#define EINFO_EINVAL_ASN1_TIME   __einfo_uniqify ( EINFO_EINVAL, 0x05, "Invalid time" )
#define EINVAL_ASN1_ALGORITHM   __einfo_error ( EINFO_EINVAL_ASN1_ALGORITHM )
#define EINFO_EINVAL_ASN1_ALGORITHM   __einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid algorithm" )
#define EINVAL_BIT_STRING   __einfo_error ( EINFO_EINVAL_BIT_STRING )
#define EINFO_EINVAL_BIT_STRING   __einfo_uniqify ( EINFO_EINVAL, 0x07, "Invalid bit string" )
#define ENOTSUP_ALGORITHM   __einfo_error ( EINFO_ENOTSUP_ALGORITHM )
#define EINFO_ENOTSUP_ALGORITHM   __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported algorithm" )
#define ENOTSUP_HIGH   __einfo_error ( EINFO_ENOTSUP_HIGH )
#define EINFO_ENOTSUP_HIGH   __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported high tag number" )
#define ENOTTY_ALGORITHM   __einfo_error ( EINFO_ENOTTY_ALGORITHM )
#define EINFO_ENOTTY_ALGORITHM   __einfo_uniqify ( EINFO_ENOTTY, 0x01, "Inappropriate algorithm" )

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static int asn1_start (struct asn1_cursor *cursor, unsigned int type)
 Start parsing ASN.1 object.
int asn1_enter (struct asn1_cursor *cursor, unsigned int type)
 Enter ASN.1 object.
int asn1_skip_if_exists (struct asn1_cursor *cursor, unsigned int type)
 Skip ASN.1 object if present.
int asn1_skip (struct asn1_cursor *cursor, unsigned int type)
 Skip ASN.1 object.
int asn1_shrink (struct asn1_cursor *cursor, unsigned int type)
 Shrink ASN.1 cursor to fit object.
int asn1_enter_any (struct asn1_cursor *cursor)
 Enter ASN.1 object of any type.
int asn1_skip_any (struct asn1_cursor *cursor)
 Skip ASN.1 object of any type.
int asn1_shrink_any (struct asn1_cursor *cursor)
 Shrink ASN.1 object of any type.
int asn1_enter_bits (struct asn1_cursor *cursor, unsigned int *unused)
 Enter ASN.1 bit string.
int asn1_enter_unsigned (struct asn1_cursor *cursor)
 Enter ASN.1 unsigned integer.
int asn1_boolean (const struct asn1_cursor *cursor)
 Parse value of ASN.1 boolean.
int asn1_integer (const struct asn1_cursor *cursor, int *value)
 Parse value of ASN.1 integer.
int asn1_compare (const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
 Compare two ASN.1 objects.
static struct asn1_algorithmasn1_find_algorithm (const struct asn1_cursor *cursor)
 Identify ASN.1 algorithm by OID.
int asn1_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm, struct asn1_cursor *params)
 Parse ASN.1 OID-identified algorithm.
int asn1_pubkey_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
 Parse ASN.1 OID-identified public-key algorithm.
int asn1_digest_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
 Parse ASN.1 OID-identified digest algorithm.
int asn1_cipher_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm, struct asn1_cursor *params)
 Parse ASN.1 OID-identified cipher algorithm.
int asn1_signature_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
 Parse ASN.1 OID-identified signature algorithm.
int asn1_curve_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm *wrapper, struct asn1_algorithm **algorithm)
 Parse ASN.1 OID-identified elliptic curve algorithm.
int asn1_check_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm *expected, struct asn1_cursor *params)
 Check ASN.1 OID-identified algorithm.
int asn1_parse_cbc (struct asn1_algorithm *algorithm, struct asn1_cursor *params)
 Parse ASN.1 CBC cipher parameters.
int asn1_parse_gcm (struct asn1_algorithm *algorithm __unused, struct asn1_cursor *params)
 Parse ASN.1 GCM cipher parameters.
int asn1_generalized_time (const struct asn1_cursor *cursor, time_t *time)
 Parse ASN.1 GeneralizedTime.
static size_t asn1_header (struct asn1_builder_header *header, unsigned int type, size_t len)
 Construct ASN.1 header.
size_t asn1_header_len (size_t len)
 Calculate ASN.1 header length.
int asn1_grow (struct asn1_builder *builder, size_t extra)
 Grow ASN.1 builder.
int asn1_prepend_raw (struct asn1_builder *builder, const void *data, size_t len)
 Prepend raw data to ASN.1 builder.
int asn1_prepend (struct asn1_builder *builder, unsigned int type, const void *data, size_t len)
 Prepend data to ASN.1 builder.
int asn1_wrap (struct asn1_builder *builder, unsigned int type)
 Wrap ASN.1 builder.
int image_asn1 (struct image *image, size_t offset, struct asn1_cursor **cursor)
 Extract ASN.1 object from image.
 REQUIRING_SYMBOL (image_asn1)
 REQUIRE_OBJECT (config_asn1)

Detailed Description

ASN.1 encoding.

The ASN.1 parsing helper functions are designed to be safe to use on untrusted input, including malformed input. Any parsing error will cause the function to invalidate the cursor by setting its length field to zero before returning.

All parsing helper functions will strictly respect the cursor's bounds for all purposes (including for determining the tag type and the tag length). An invalidated cursor (or any cursor that happens to naturally end up with a zero length, e.g. by skipping the only existent object) may therefore safely be passed in to any parsing helper function.

A consequence of this design is that it is not necessary to check the return status from all parsing helper functions in a sequence, only to check the return status from the last. The standard C comma operator may safely be used to connect a sequence of ASN.1 parsing operations. For example:

if ( ( rc = ( asn1_enter ( cursor, ASN1_SEQUENCE ),
asn1_skip ( cursor, ASN1_INTEGER ),
asn1_enter ( cursor, ASN1_SEQUENCE ) ) ) != 0 ) {
return rc;
}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition asn1.c:261
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
Definition asn1.c:323
#define ASN1_INTEGER
ASN.1 integer.
Definition asn1.h:63
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition asn1.h:93

or equivalently, just:

asn1_skip ( cursor, ASN1_INTEGER );
if ( ( rc = asn1_enter ( cursor, ASN1_SEQUENCE ) ) != 0 )
return rc;

The caller may not need to check the return status from the parsing helper functions at all. For example, a caller that is trying to extract a fixed-length octet string from within a deeply nested ASN.1 structure may simply check that the resulting cursor length is correct (which it would have to check anyway). Since any error will produce a zero-length cursor, this check would also suffice to catch any parsing errors.

The cursor navigation functions such as asn1_enter() and asn1_skip() will validate the type and length bytes (which they consume themselves), but will not otherwise validate the contents of the cursor. Callers must perform their own validation after navigating to the expected data structure (e.g. by checking that the resulting length is correct), or use the higher-level helpers such as asn1_boolean() and asn1_integer() that validate and extract the semantic contents of cursors.

The type ASN1_ANY may be used as a wildcard type to enter or skip an object of any type.

The function asn1_skip_if_exists() may be useful when parsing objects where an encountered type is not knowable in advance (e.g. for sequences containing optional elements). If the potential type is not found, then asn1_skip_if_exists() will return an error but will not invalidate the cursor (since a missing optional object is not a parsing error).

The function asn1_type() may be used to determine the type of an unknown object. Calling asn1_type() on an invalidated cursor will return ASN1_END (and the cursor will remain invalidated).

Definition in file asn1.c.

Macro Definition Documentation

◆ EINVAL_ASN1_EMPTY

#define EINVAL_ASN1_EMPTY   __einfo_error ( EINFO_EINVAL_ASN1_EMPTY )

Definition at line 118 of file asn1.c.

118#define EINVAL_ASN1_EMPTY \
119 __einfo_error ( EINFO_EINVAL_ASN1_EMPTY )

Referenced by asn1_start().

◆ EINFO_EINVAL_ASN1_EMPTY

#define EINFO_EINVAL_ASN1_EMPTY   __einfo_uniqify ( EINFO_EINVAL, 0x01, "Empty or underlength cursor" )

Definition at line 120 of file asn1.c.

120#define EINFO_EINVAL_ASN1_EMPTY \
121 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Empty or underlength cursor" )

◆ EINVAL_ASN1_LEN_LEN

#define EINVAL_ASN1_LEN_LEN   __einfo_error ( EINFO_EINVAL_ASN1_LEN_LEN )

Definition at line 122 of file asn1.c.

122#define EINVAL_ASN1_LEN_LEN \
123 __einfo_error ( EINFO_EINVAL_ASN1_LEN_LEN )

Referenced by asn1_start().

◆ EINFO_EINVAL_ASN1_LEN_LEN

#define EINFO_EINVAL_ASN1_LEN_LEN   __einfo_uniqify ( EINFO_EINVAL, 0x02, "Length field overruns cursor" )

Definition at line 124 of file asn1.c.

124#define EINFO_EINVAL_ASN1_LEN_LEN \
125 __einfo_uniqify ( EINFO_EINVAL, 0x02, "Length field overruns cursor" )

◆ EINVAL_ASN1_LEN

#define EINVAL_ASN1_LEN   __einfo_error ( EINFO_EINVAL_ASN1_LEN )

Definition at line 126 of file asn1.c.

126#define EINVAL_ASN1_LEN \
127 __einfo_error ( EINFO_EINVAL_ASN1_LEN )

Referenced by asn1_start().

◆ EINFO_EINVAL_ASN1_LEN

#define EINFO_EINVAL_ASN1_LEN   __einfo_uniqify ( EINFO_EINVAL, 0x03, "Field overruns cursor" )

Definition at line 128 of file asn1.c.

128#define EINFO_EINVAL_ASN1_LEN \
129 __einfo_uniqify ( EINFO_EINVAL, 0x03, "Field overruns cursor" )

◆ EINVAL_ASN1_BOOLEAN

#define EINVAL_ASN1_BOOLEAN   __einfo_error ( EINFO_EINVAL_ASN1_BOOLEAN )

Definition at line 130 of file asn1.c.

130#define EINVAL_ASN1_BOOLEAN \
131 __einfo_error ( EINFO_EINVAL_ASN1_BOOLEAN )

Referenced by asn1_boolean().

◆ EINFO_EINVAL_ASN1_BOOLEAN

#define EINFO_EINVAL_ASN1_BOOLEAN   __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid boolean" )

Definition at line 132 of file asn1.c.

132#define EINFO_EINVAL_ASN1_BOOLEAN \
133 __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid boolean" )

◆ EINVAL_ASN1_INTEGER

#define EINVAL_ASN1_INTEGER   __einfo_error ( EINFO_EINVAL_ASN1_INTEGER )

Definition at line 134 of file asn1.c.

134#define EINVAL_ASN1_INTEGER \
135 __einfo_error ( EINFO_EINVAL_ASN1_INTEGER )

Referenced by asn1_integer().

◆ EINFO_EINVAL_ASN1_INTEGER

#define EINFO_EINVAL_ASN1_INTEGER   __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid integer" )

Definition at line 136 of file asn1.c.

136#define EINFO_EINVAL_ASN1_INTEGER \
137 __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid integer" )

◆ EINVAL_ASN1_TIME

#define EINVAL_ASN1_TIME   __einfo_error ( EINFO_EINVAL_ASN1_TIME )

Definition at line 138 of file asn1.c.

138#define EINVAL_ASN1_TIME \
139 __einfo_error ( EINFO_EINVAL_ASN1_TIME )

Referenced by asn1_generalized_time().

◆ EINFO_EINVAL_ASN1_TIME

#define EINFO_EINVAL_ASN1_TIME   __einfo_uniqify ( EINFO_EINVAL, 0x05, "Invalid time" )

Definition at line 140 of file asn1.c.

140#define EINFO_EINVAL_ASN1_TIME \
141 __einfo_uniqify ( EINFO_EINVAL, 0x05, "Invalid time" )

◆ EINVAL_ASN1_ALGORITHM

#define EINVAL_ASN1_ALGORITHM   __einfo_error ( EINFO_EINVAL_ASN1_ALGORITHM )

Definition at line 142 of file asn1.c.

142#define EINVAL_ASN1_ALGORITHM \
143 __einfo_error ( EINFO_EINVAL_ASN1_ALGORITHM )

Referenced by asn1_algorithm().

◆ EINFO_EINVAL_ASN1_ALGORITHM

#define EINFO_EINVAL_ASN1_ALGORITHM   __einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid algorithm" )

Definition at line 144 of file asn1.c.

144#define EINFO_EINVAL_ASN1_ALGORITHM \
145 __einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid algorithm" )

◆ EINVAL_BIT_STRING

#define EINVAL_BIT_STRING   __einfo_error ( EINFO_EINVAL_BIT_STRING )

Definition at line 146 of file asn1.c.

146#define EINVAL_BIT_STRING \
147 __einfo_error ( EINFO_EINVAL_BIT_STRING )

Referenced by asn1_enter_bits().

◆ EINFO_EINVAL_BIT_STRING

#define EINFO_EINVAL_BIT_STRING   __einfo_uniqify ( EINFO_EINVAL, 0x07, "Invalid bit string" )

Definition at line 148 of file asn1.c.

148#define EINFO_EINVAL_BIT_STRING \
149 __einfo_uniqify ( EINFO_EINVAL, 0x07, "Invalid bit string" )

◆ ENOTSUP_ALGORITHM

#define ENOTSUP_ALGORITHM   __einfo_error ( EINFO_ENOTSUP_ALGORITHM )

Definition at line 150 of file asn1.c.

150#define ENOTSUP_ALGORITHM \
151 __einfo_error ( EINFO_ENOTSUP_ALGORITHM )

Referenced by asn1_algorithm(), and asn1_curve_algorithm().

◆ EINFO_ENOTSUP_ALGORITHM

#define EINFO_ENOTSUP_ALGORITHM   __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported algorithm" )

Definition at line 152 of file asn1.c.

152#define EINFO_ENOTSUP_ALGORITHM \
153 __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported algorithm" )

◆ ENOTSUP_HIGH

#define ENOTSUP_HIGH   __einfo_error ( EINFO_ENOTSUP_HIGH )

Definition at line 154 of file asn1.c.

154#define ENOTSUP_HIGH \
155 __einfo_error ( EINFO_ENOTSUP_HIGH )

Referenced by asn1_start().

◆ EINFO_ENOTSUP_HIGH

#define EINFO_ENOTSUP_HIGH   __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported high tag number" )

Definition at line 156 of file asn1.c.

156#define EINFO_ENOTSUP_HIGH \
157 __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported high tag number" )

◆ ENOTTY_ALGORITHM

#define ENOTTY_ALGORITHM   __einfo_error ( EINFO_ENOTTY_ALGORITHM )

Definition at line 158 of file asn1.c.

158#define ENOTTY_ALGORITHM \
159 __einfo_error ( EINFO_ENOTTY_ALGORITHM )

Referenced by asn1_check_algorithm(), asn1_cipher_algorithm(), asn1_curve_algorithm(), asn1_digest_algorithm(), asn1_pubkey_algorithm(), and asn1_signature_algorithm().

◆ EINFO_ENOTTY_ALGORITHM

#define EINFO_ENOTTY_ALGORITHM   __einfo_uniqify ( EINFO_ENOTTY, 0x01, "Inappropriate algorithm" )

Definition at line 160 of file asn1.c.

160#define EINFO_ENOTTY_ALGORITHM \
161 __einfo_uniqify ( EINFO_ENOTTY, 0x01, "Inappropriate algorithm" )

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ asn1_start()

int asn1_start ( struct asn1_cursor * cursor,
unsigned int type )
static

Start parsing ASN.1 object.

Parameters
cursorASN.1 object cursor
typeExpected type, or ASN1_ANY
Return values
lenLength of object body, or negative error

The object cursor will be updated to point to the start of the object body (i.e. the first byte following the length byte(s)), and the length of the object body (i.e. the number of bytes until the following object tag, if any) is returned.

If the expected type is not found, the object cursor will not be modified. If any other error occurs, the object cursor will be invalidated.

Definition at line 179 of file asn1.c.

179 {
180 unsigned int len_len;
181 unsigned int len;
182 uint8_t high_byte;
183
184 /* Sanity check */
185 if ( cursor->len < 2 /* Tag byte and first length byte */ ) {
186 if ( cursor->len )
187 DBGC ( cursor, "ASN1 %p too short\n", cursor );
188 asn1_invalidate_cursor ( cursor );
189 return -EINVAL_ASN1_EMPTY;
190 }
191
192 /* Refuse to handle multi-byte tags */
193 if ( ( asn1_type ( cursor ) & ASN1_HIGH ) == ASN1_HIGH ) {
194 DBGC ( cursor, "ASN1 %p has unsupported high tag number\n",
195 cursor );
196 asn1_invalidate_cursor ( cursor );
197 return -ENOTSUP_HIGH;
198 }
199
200 /* Check the tag byte */
201 if ( ( type != ASN1_ANY ) && ( type != asn1_type ( cursor ) ) ) {
202 DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
203 cursor, type, *( ( uint8_t * ) cursor->data ) );
204 return -ENXIO;
205 }
206 cursor->data++;
207 cursor->len--;
208
209 /* Extract length of the length field and sanity check */
210 len_len = *( ( uint8_t * ) cursor->data );
211 if ( len_len & 0x80 ) {
212 len_len = ( len_len & 0x7f );
213 cursor->data++;
214 cursor->len--;
215 } else {
216 len_len = 1;
217 }
218 if ( ( len_len == 0 ) || ( cursor->len < len_len ) ) {
219 DBGC ( cursor, "ASN1 %p bad length field length %d (min 0, "
220 "max %zd)\n", cursor, len_len, cursor->len );
221 asn1_invalidate_cursor ( cursor );
222 return -EINVAL_ASN1_LEN_LEN;
223 }
224
225 /* Extract the length and sanity check */
226 for ( len = 0 ; len_len ; len_len-- ) {
227 high_byte = ( len >> ( 8 * ( sizeof ( len ) - 1 ) ) );
228 if ( high_byte ) {
229 DBGC ( cursor, "ASN1 %p unrepresentable length\n",
230 cursor );
231 asn1_invalidate_cursor ( cursor );
232 return -EINVAL_ASN1_LEN;
233 }
234 len <<= 8;
235 len |= *( ( uint8_t * ) cursor->data );
236 cursor->data++;
237 cursor->len--;
238 }
239 if ( ( cursor->len < len ) || ( ( ( int ) len ) < 0 ) ) {
240 DBGC ( cursor, "ASN1 %p bad length %d (min 0, max %zd)\n",
241 cursor, len, cursor->len );
242 asn1_invalidate_cursor ( cursor );
243 return -EINVAL_ASN1_LEN;
244 }
245
246 return len;
247}
unsigned char uint8_t
Definition stdint.h:10
#define EINVAL_ASN1_LEN_LEN
Definition asn1.c:122
#define ENOTSUP_HIGH
Definition asn1.c:154
#define EINVAL_ASN1_EMPTY
Definition asn1.c:118
#define EINVAL_ASN1_LEN
Definition asn1.c:126
#define ASN1_HIGH
ASN.1 high tag number.
Definition asn1.h:90
#define ASN1_ANY
ASN.1 "any tag" magic value.
Definition asn1.h:105
static void asn1_invalidate_cursor(struct asn1_cursor *cursor)
Invalidate ASN.1 object cursor.
Definition asn1.h:494
static unsigned int asn1_type(const struct asn1_cursor *cursor)
Extract ASN.1 type.
Definition asn1.h:505
ring len
Length.
Definition dwmac.h:226
uint32_t type
Operating system type.
Definition ena.h:1
#define DBGC(...)
Definition compiler.h:530
#define ENXIO
No such device or address.
Definition errno.h:643
const void * data
Start of data.
Definition asn1.h:23
size_t len
Length of data.
Definition asn1.h:25

References ASN1_ANY, ASN1_HIGH, asn1_invalidate_cursor(), asn1_type(), asn1_cursor::data, DBGC, EINVAL_ASN1_EMPTY, EINVAL_ASN1_LEN, EINVAL_ASN1_LEN_LEN, ENOTSUP_HIGH, ENXIO, asn1_cursor::len, len, and type.

Referenced by asn1_enter(), asn1_shrink(), and asn1_skip_if_exists().

◆ asn1_enter()

int asn1_enter ( struct asn1_cursor * cursor,
unsigned int type )

Enter ASN.1 object.

Parameters
cursorASN.1 object cursor
typeExpected type, or ASN1_ANY
Return values
rcReturn status code

The object cursor will be updated to point to the body of the current ASN.1 object.

If any error occurs, the object cursor will be invalidated.

Definition at line 261 of file asn1.c.

261 {
262 int len;
263
264 /* Parse current object */
265 len = asn1_start ( cursor, type );
266 if ( len < 0 ) {
267 asn1_invalidate_cursor ( cursor );
268 return len;
269 }
270
271 /* Update cursor */
272 if ( ( ( size_t ) len ) <= cursor->len )
273 cursor->len = len;
274
275 DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
276 cursor, type, len );
277 return 0;
278}
static int asn1_start(struct asn1_cursor *cursor, unsigned int type)
Start parsing ASN.1 object.
Definition asn1.c:179

References asn1_invalidate_cursor(), asn1_start(), DBGC, asn1_cursor::len, len, and type.

Referenced by asn1_algorithm(), asn1_boolean(), asn1_curve_algorithm(), asn1_enter_any(), asn1_enter_bits(), asn1_enter_unsigned(), asn1_generalized_time(), asn1_integer(), asn1_parse_cbc(), asn1_parse_gcm(), cms_parse(), cms_parse_certificates(), cms_parse_content_type(), cms_parse_encrypted(), cms_parse_enveloped(), cms_parse_identifier(), cms_parse_mac(), cms_parse_participant(), cms_parse_participants(), cms_parse_signed(), cms_parse_value(), ecdsa_parse_key(), ecdsa_verify(), ocsp_compare_responder_key_hash(), ocsp_parse_basic_response(), ocsp_parse_cert_id(), ocsp_parse_certs(), ocsp_parse_response(), ocsp_parse_response_bytes(), ocsp_parse_response_status(), ocsp_parse_response_type(), ocsp_parse_responses(), ocsp_parse_tbs_response_data(), ocsp_request(), rsa_parse_mod_exp(), validator_append(), x509_parse(), x509_parse_access_description(), x509_parse_authority_info_access(), x509_parse_basic_constraints(), x509_parse_common_name(), x509_parse_extended_key_usage(), x509_parse_extension(), x509_parse_extensions(), x509_parse_key_purpose(), x509_parse_ocsp(), x509_parse_public_key(), x509_parse_subject_alt_name(), x509_parse_tbscertificate(), x509_parse_validity(), and x509_parse_version().

◆ asn1_skip_if_exists()

int asn1_skip_if_exists ( struct asn1_cursor * cursor,
unsigned int type )

Skip ASN.1 object if present.

Parameters
cursorASN.1 object cursor
typeExpected type, or ASN1_ANY
Return values
rcReturn status code

The object cursor will be updated to point to the next ASN.1 object.

If the expected type is not found, the object cursor will not be modified. If any other error occurs, the object cursor will be invalidated.

Definition at line 294 of file asn1.c.

294 {
295 int len;
296
297 /* Parse current object */
298 len = asn1_start ( cursor, type );
299 if ( len < 0 )
300 return len;
301
302 /* Update cursor */
303 cursor->data += len;
304 cursor->len -= len;
305
306 DBGC ( cursor, "ASN1 %p skipped object type %02x (len %x)\n",
307 cursor, type, len );
308 return 0;
309}

References asn1_start(), asn1_cursor::data, DBGC, asn1_cursor::len, len, and type.

Referenced by asn1_skip(), cms_parse_enveloped(), cms_parse_participant(), cms_parse_signed(), and ocsp_parse_tbs_response_data().

◆ asn1_skip()

int asn1_skip ( struct asn1_cursor * cursor,
unsigned int type )

Skip ASN.1 object.

Parameters
cursorASN.1 object cursor
typeExpected type, or ASN1_ANY
Return values
rcReturn status code

The object cursor will be updated to point to the next ASN.1 object.

If any error occurs, the object cursor will be invalidated.

Definition at line 323 of file asn1.c.

323 {
324 int rc;
325
326 if ( ( rc = asn1_skip_if_exists ( cursor, type ) ) != 0 ) {
327 asn1_invalidate_cursor ( cursor );
328 return rc;
329 }
330
331 return 0;
332}
int asn1_skip_if_exists(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object if present.
Definition asn1.c:294

References asn1_invalidate_cursor(), asn1_skip_if_exists(), rc, and type.

Referenced by asn1_skip_any(), cms_parse_encrypted(), cms_parse_enveloped(), cms_parse_participant(), cms_parse_signed(), der_image_probe(), ecdsa_parse_key(), ocsp_parse_cert_id(), ocsp_request(), and rsa_parse_mod_exp().

◆ asn1_shrink()

int asn1_shrink ( struct asn1_cursor * cursor,
unsigned int type )

Shrink ASN.1 cursor to fit object.

Parameters
cursorASN.1 object cursor
typeExpected type, or ASN1_ANY
Return values
rcReturn status code

The object cursor will be shrunk to contain only the current ASN.1 object.

If any error occurs, the object cursor will be invalidated.

Definition at line 346 of file asn1.c.

346 {
347 struct asn1_cursor temp;
348 const void *end;
349 int len;
350
351 /* Find end of object */
352 memcpy ( &temp, cursor, sizeof ( temp ) );
353 len = asn1_start ( &temp, type );
354 if ( len < 0 ) {
355 asn1_invalidate_cursor ( cursor );
356 return len;
357 }
358 end = ( temp.data + len );
359
360 /* Shrink original cursor to contain only its first object */
361 cursor->len = ( end - cursor->data );
362
363 return 0;
364}
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint32_t end
Ending offset.
Definition netvsc.h:7
An ASN.1 object cursor.
Definition asn1.h:21

References asn1_invalidate_cursor(), asn1_start(), asn1_cursor::data, end, asn1_cursor::len, len, memcpy(), and type.

Referenced by asn1_shrink_any(), cms_parse_identifier(), ocsp_parse_cert_id(), x509_parse_issuer(), and x509_parse_serial().

◆ asn1_enter_any()

int asn1_enter_any ( struct asn1_cursor * cursor)

Enter ASN.1 object of any type.

Parameters
cursorASN.1 object cursor
Return values
rcReturn status code

Definition at line 372 of file asn1.c.

372 {
373 return asn1_enter ( cursor, ASN1_ANY );
374}

References ASN1_ANY, and asn1_enter().

Referenced by ecdsa_parse_key(), ocsp_parse_responder_id(), x509_check_alt_name(), and x509_parse_common_name().

◆ asn1_skip_any()

◆ asn1_shrink_any()

int asn1_shrink_any ( struct asn1_cursor * cursor)

Shrink ASN.1 object of any type.

Parameters
cursorASN.1 object cursor
Return values
rcReturn status code

Definition at line 392 of file asn1.c.

392 {
393 return asn1_shrink ( cursor, ASN1_ANY );
394}
int asn1_shrink(struct asn1_cursor *cursor, unsigned int type)
Shrink ASN.1 cursor to fit object.
Definition asn1.c:346

References ASN1_ANY, and asn1_shrink().

Referenced by cms_message(), der_asn1(), ocsp_parse_tbs_response_data(), x509_certificate(), x509_parse_public_key(), x509_parse_subject(), and x509_parse_tbscertificate().

◆ asn1_enter_bits()

int asn1_enter_bits ( struct asn1_cursor * cursor,
unsigned int * unused )

Enter ASN.1 bit string.

Parameters
cursorASN.1 cursor
unusedUnused bits to fill in (or NULL to require all used)
Return values
rcReturn status code

Definition at line 403 of file asn1.c.

403 {
404 const struct {
406 uint8_t data[0];
407 } __attribute__ (( packed )) *bit_string;
408 const uint8_t *last;
409 unsigned int unused_bits;
410 int rc;
411
412 /* Enter bit string */
413 if ( ( rc = asn1_enter ( cursor, ASN1_BIT_STRING ) ) != 0 )
414 return rc;
415
416 /* Check that bit string header exists */
417 if ( cursor->len < sizeof ( *bit_string ) ) {
418 DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
419 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
420 asn1_invalidate_cursor ( cursor );
421 return -EINVAL_BIT_STRING;
422 }
423 bit_string = cursor->data;
424 last = ( cursor->data + cursor->len - 1 );
425 cursor->data = &bit_string->data;
426 cursor->len -= offsetof ( typeof ( *bit_string ), data );
427 unused_bits = bit_string->unused;
428
429 /* Check validity of unused bits */
430 if ( ( unused_bits >= 8 ) ||
431 ( ( unused_bits > 0 ) && ( cursor->len == 0 ) ) ||
432 ( ( *last & ( 0xffU >> ( 8 - unused_bits ) ) ) != 0 ) ) {
433 DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
434 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
435 asn1_invalidate_cursor ( cursor );
436 return -EINVAL_BIT_STRING;
437 }
438
439 /* Record or check number of unused bits, as applicable */
440 if ( unused ) {
441 *unused = unused_bits;
442 } else if ( unused_bits ) {
443 DBGC ( cursor, "ASN1 %p invalid integral bit string:\n",
444 cursor );
445 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
446 asn1_invalidate_cursor ( cursor );
447 return -EINVAL_BIT_STRING;
448 }
449
450 return 0;
451}
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
#define EINVAL_BIT_STRING
Definition asn1.c:146
#define ASN1_BIT_STRING
ASN.1 bit string.
Definition asn1.h:66
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC_HDA(...)
Definition compiler.h:531
#define __attribute__(x)
Definition compiler.h:10
uint8_t unused
Unused.
Definition librm.h:5
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25

References __attribute__, ASN1_BIT_STRING, asn1_enter(), asn1_invalidate_cursor(), asn1_cursor::data, data, DBGC, DBGC_HDA, EINVAL_BIT_STRING, asn1_cursor::len, offsetof, rc, typeof(), and unused.

Referenced by ecdsa_parse_key(), ocsp_parse_basic_response(), rsa_parse_mod_exp(), x509_parse(), x509_parse_key_usage(), and x509_parse_public_key().

◆ asn1_enter_unsigned()

int asn1_enter_unsigned ( struct asn1_cursor * cursor)

Enter ASN.1 unsigned integer.

Parameters
cursorASN.1 object cursor
Return values
rcReturn status code

Definition at line 459 of file asn1.c.

459 {
460 uint8_t msb;
461 int rc;
462
463 /* Enter integer */
464 if ( ( rc = asn1_enter ( cursor, ASN1_INTEGER ) ) != 0 )
465 return rc;
466
467 /* Reject empty integers */
468 if ( ! cursor->len ) {
469 DBGC ( cursor, "ASN1 %p empty unsigned integer\n", cursor );
470 /* Cursor is already invalid */
471 return -EINVAL;
472 }
473
474 /* Reject negative values */
475 msb = *( ( uint8_t * ) cursor->data );
476 if ( msb & 0x80 ) {
477 DBGC ( cursor, "ASN1 %p negative unsigned integer:\n",
478 cursor );
479 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
480 asn1_invalidate_cursor ( cursor );
481 return -EINVAL;
482 }
483
484 /* Skip any initial positive sign byte(s) */
485 while ( ( cursor->len > 1 ) && ( msb == 0x00 ) ) {
486 cursor->data++;
487 cursor->len--;
488 msb = *( ( uint8_t * ) cursor->data );
489 }
490
491 return 0;
492}
#define EINVAL
Invalid argument.
Definition errno.h:472

References asn1_enter(), ASN1_INTEGER, asn1_invalidate_cursor(), asn1_cursor::data, DBGC, DBGC_HDA, EINVAL, asn1_cursor::len, and rc.

Referenced by ecdsa_parse_signature(), and rsa_parse_mod_exp().

◆ asn1_boolean()

int asn1_boolean ( const struct asn1_cursor * cursor)

Parse value of ASN.1 boolean.

Parameters
cursorASN.1 object cursor
Return values
valueValue, or negative error

Definition at line 500 of file asn1.c.

500 {
501 struct asn1_cursor contents;
502 const struct {
504 } __attribute__ (( packed )) *boolean;
505
506 /* Enter boolean */
507 memcpy ( &contents, cursor, sizeof ( contents ) );
508 asn1_enter ( &contents, ASN1_BOOLEAN );
509 if ( contents.len != sizeof ( *boolean ) )
510 return -EINVAL_ASN1_BOOLEAN;
511
512 /* Extract value */
513 boolean = contents.data;
514 return boolean->value;
515}
pseudo_bit_t value[0x00020]
Definition arbel.h:2
#define EINVAL_ASN1_BOOLEAN
Definition asn1.c:130
#define ASN1_BOOLEAN
ASN.1 boolean.
Definition asn1.h:60

References __attribute__, ASN1_BOOLEAN, asn1_enter(), asn1_cursor::data, EINVAL_ASN1_BOOLEAN, asn1_cursor::len, memcpy(), and value.

Referenced by x509_parse_basic_constraints(), and x509_parse_extension().

◆ asn1_integer()

int asn1_integer ( const struct asn1_cursor * cursor,
int * value )

Parse value of ASN.1 integer.

Parameters
cursorASN.1 object cursor
valueValue to fill in
Return values
rcReturn status code

Definition at line 524 of file asn1.c.

524 {
525 struct asn1_cursor contents;
526 uint8_t high_byte;
527 int rc;
528
529 /* Enter integer */
530 memcpy ( &contents, cursor, sizeof ( contents ) );
531 if ( ( rc = asn1_enter ( &contents, ASN1_INTEGER ) ) != 0 )
532 return rc;
533 if ( contents.len < 1 )
534 return -EINVAL_ASN1_INTEGER;
535
536 /* Initialise value according to sign byte */
537 *value = *( ( int8_t * ) contents.data );
538 contents.data++;
539 contents.len--;
540
541 /* Process value */
542 while ( contents.len ) {
543 high_byte = ( (*value) >> ( 8 * ( sizeof ( *value ) - 1 ) ) );
544 if ( ( high_byte != 0x00 ) && ( high_byte != 0xff ) ) {
545 DBGC ( cursor, "ASN1 %p integer overflow\n", cursor );
546 return -EINVAL_ASN1_INTEGER;
547 }
548 *value = ( ( *value << 8 ) | *( ( uint8_t * ) contents.data ) );
549 contents.data++;
550 contents.len--;
551 }
552
553 return 0;
554}
signed char int8_t
Definition stdint.h:15
#define EINVAL_ASN1_INTEGER
Definition asn1.c:134

References asn1_enter(), ASN1_INTEGER, asn1_cursor::data, DBGC, EINVAL_ASN1_INTEGER, asn1_cursor::len, memcpy(), rc, and value.

Referenced by x509_parse_basic_constraints(), and x509_parse_version().

◆ asn1_compare()

int asn1_compare ( const struct asn1_cursor * cursor1,
const struct asn1_cursor * cursor2 )

Compare two ASN.1 objects.

Parameters
cursor1ASN.1 object cursor
cursor2ASN.1 object cursor
Return values
differenceDifference as returned by memcmp()

Note that invalid and empty cursors will compare as equal with each other.

Definition at line 566 of file asn1.c.

567 {
568 int difference;
569
570 difference = ( cursor2->len - cursor1->len );
571 return ( difference ? difference :
572 memcmp ( cursor1->data, cursor2->data, cursor1->len ) );
573}
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115

References asn1_cursor::data, asn1_cursor::len, and memcmp().

Referenced by asn1_find_algorithm(), cms_parse_content_type(), ocsp_compare_responder_name(), ocsp_parse_cert_id(), ocsp_parse_response_type(), pubkey_decrypt_okx(), pubkey_encrypt_decrypt_okx(), pubkey_encrypt_okx(), pubkey_sign_okx(), rsa_match(), x509_check_issuer(), x509_find(), x509_find_access_method(), x509_find_extension(), x509_find_issuer_serial(), x509_find_subject(), x509_is_self_signed(), x509_parse_common_name(), and x509_parse_key_purpose().

◆ asn1_find_algorithm()

struct asn1_algorithm * asn1_find_algorithm ( const struct asn1_cursor * cursor)
static

Identify ASN.1 algorithm by OID.

Parameters
cursorASN.1 object cursor
Return values
algorithmAlgorithm, or NULL

Definition at line 583 of file asn1.c.

583 {
585
587 if ( asn1_compare ( &algorithm->oid, cursor ) == 0 )
588 return algorithm;
589 }
590
591 return NULL;
592}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition asn1.c:566
#define ASN1_ALGORITHMS
ASN.1 OID-identified algorithms.
Definition asn1.h:454
u16 algorithm
Authentication algorithm (Open System or Shared Key).
Definition ieee80211.h:1
An ASN.1 OID-identified algorithm.
Definition asn1.h:429
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386

References algorithm, ASN1_ALGORITHMS, asn1_compare(), for_each_table_entry, and NULL.

Referenced by asn1_algorithm(), and asn1_curve_algorithm().

◆ asn1_algorithm()

int asn1_algorithm ( const struct asn1_cursor * cursor,
struct asn1_algorithm ** algorithm,
struct asn1_cursor * params )

Parse ASN.1 OID-identified algorithm.

Parameters
cursorASN.1 object cursor
Return values
algorithmAlgorithm
paramsAlgorithm parameters, or NULL
rcReturn status code

Definition at line 602 of file asn1.c.

604 {
605 struct asn1_cursor contents;
606 int rc;
607
608 /* Enter algorithm */
609 memcpy ( &contents, cursor, sizeof ( contents ) );
610 asn1_enter ( &contents, ASN1_SEQUENCE );
611
612 /* Get raw parameters, if applicable */
613 if ( params ) {
614 memcpy ( params, &contents, sizeof ( *params ) );
615 asn1_skip_any ( params );
616 }
617
618 /* Enter algorithm identifier */
619 if ( ( rc = asn1_enter ( &contents, ASN1_OID ) ) != 0 ) {
620 DBGC ( cursor, "ASN1 %p cannot locate algorithm OID:\n",
621 cursor );
622 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
623 return -EINVAL_ASN1_ALGORITHM;
624 }
625
626 /* Identify algorithm */
627 *algorithm = asn1_find_algorithm ( &contents );
628 if ( ! *algorithm ) {
629 DBGC ( cursor, "ASN1 %p unrecognised algorithm:\n", cursor );
630 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
631 return -ENOTSUP_ALGORITHM;
632 }
633
634 /* Parse parameters, if applicable */
635 if ( params && (*algorithm)->parse &&
636 ( ( rc = (*algorithm)->parse ( *algorithm, params ) ) != 0 ) ) {
637 DBGC ( cursor, "ASN1 %p cannot parse %s parameters: %s\n",
638 cursor, (*algorithm)->name, strerror ( rc ) );
639 return rc;
640 }
641
642 return 0;
643}
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition asn1.c:382
#define ENOTSUP_ALGORITHM
Definition asn1.c:150
#define EINVAL_ASN1_ALGORITHM
Definition asn1.c:142
static struct asn1_algorithm * asn1_find_algorithm(const struct asn1_cursor *cursor)
Identify ASN.1 algorithm by OID.
Definition asn1.c:583
#define ASN1_OID
ASN.1 object identifier.
Definition asn1.h:75
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79

References algorithm, asn1_enter(), asn1_find_algorithm(), ASN1_OID, ASN1_SEQUENCE, asn1_skip_any(), asn1_cursor::data, DBGC, DBGC_HDA, EINVAL_ASN1_ALGORITHM, ENOTSUP_ALGORITHM, asn1_cursor::len, memcpy(), rc, and strerror().

◆ asn1_pubkey_algorithm()

int asn1_pubkey_algorithm ( const struct asn1_cursor * cursor,
struct asn1_algorithm ** algorithm )

Parse ASN.1 OID-identified public-key algorithm.

Parameters
cursorASN.1 object cursor
Return values
algorithmAlgorithm
rcReturn status code

Definition at line 652 of file asn1.c.

653 {
654 int rc;
655
656 /* Parse algorithm */
657 if ( ( rc = asn1_algorithm ( cursor, algorithm, NULL ) ) != 0 )
658 return rc;
659
660 /* Check algorithm has a public key */
661 if ( ! (*algorithm)->pubkey ) {
662 DBGC ( cursor, "ASN1 %p algorithm %s is not a public-key "
663 "algorithm:\n", cursor, (*algorithm)->name );
664 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
665 return -ENOTTY_ALGORITHM;
666 }
667
668 return 0;
669}
#define ENOTTY_ALGORITHM
Definition asn1.c:158

References algorithm, asn1_cursor::data, DBGC, DBGC_HDA, ENOTTY_ALGORITHM, asn1_cursor::len, NULL, and rc.

Referenced by cms_parse_pubkey_algorithm(), and x509_parse_public_key().

◆ asn1_digest_algorithm()

int asn1_digest_algorithm ( const struct asn1_cursor * cursor,
struct asn1_algorithm ** algorithm )

Parse ASN.1 OID-identified digest algorithm.

Parameters
cursorASN.1 object cursor
Return values
algorithmAlgorithm
rcReturn status code

Definition at line 678 of file asn1.c.

679 {
680 int rc;
681
682 /* Parse algorithm */
683 if ( ( rc = asn1_algorithm ( cursor, algorithm, NULL ) ) != 0 )
684 return rc;
685
686 /* Check algorithm has a digest */
687 if ( ! (*algorithm)->digest ) {
688 DBGC ( cursor, "ASN1 %p algorithm %s is not a digest "
689 "algorithm:\n", cursor, (*algorithm)->name );
690 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
691 return -ENOTTY_ALGORITHM;
692 }
693
694 return 0;
695}

References algorithm, asn1_cursor::data, DBGC, DBGC_HDA, ENOTTY_ALGORITHM, asn1_cursor::len, NULL, and rc.

Referenced by cms_parse_digest_algorithm().

◆ asn1_cipher_algorithm()

int asn1_cipher_algorithm ( const struct asn1_cursor * cursor,
struct asn1_algorithm ** algorithm,
struct asn1_cursor * params )

Parse ASN.1 OID-identified cipher algorithm.

Parameters
cursorASN.1 object cursor
Return values
algorithmAlgorithm
paramsAlgorithm parameters, or NULL
rcReturn status code

Definition at line 705 of file asn1.c.

707 {
708 int rc;
709
710 /* Parse algorithm */
711 if ( ( rc = asn1_algorithm ( cursor, algorithm, params ) ) != 0 )
712 return rc;
713
714 /* Check algorithm has a cipher */
715 if ( ! (*algorithm)->cipher ) {
716 DBGC ( cursor, "ASN1 %p algorithm %s is not a cipher "
717 "algorithm:\n", cursor, (*algorithm)->name );
718 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
719 return -ENOTTY_ALGORITHM;
720 }
721
722 return 0;
723}

References algorithm, asn1_cursor::data, DBGC, DBGC_HDA, ENOTTY_ALGORITHM, asn1_cursor::len, and rc.

Referenced by cms_parse_cipher_algorithm().

◆ asn1_signature_algorithm()

int asn1_signature_algorithm ( const struct asn1_cursor * cursor,
struct asn1_algorithm ** algorithm )

Parse ASN.1 OID-identified signature algorithm.

Parameters
cursorASN.1 object cursor
Return values
algorithmAlgorithm
rcReturn status code

Definition at line 732 of file asn1.c.

733 {
734 int rc;
735
736 /* Parse algorithm */
737 if ( ( rc = asn1_algorithm ( cursor, algorithm, NULL ) ) != 0 )
738 return rc;
739
740 /* Check algorithm has a public key */
741 if ( ! (*algorithm)->pubkey ) {
742 DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
743 "algorithm:\n", cursor, (*algorithm)->name );
744 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
745 return -ENOTTY_ALGORITHM;
746 }
747
748 /* Check algorithm has a digest */
749 if ( ! (*algorithm)->digest ) {
750 DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
751 "algorithm:\n", cursor, (*algorithm)->name );
752 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
753 return -ENOTTY_ALGORITHM;
754 }
755
756 return 0;
757}

References algorithm, asn1_cursor::data, DBGC, DBGC_HDA, ENOTTY_ALGORITHM, asn1_cursor::len, NULL, and rc.

Referenced by ocsp_parse_basic_response(), x509_parse(), and x509_parse_tbscertificate().

◆ asn1_curve_algorithm()

int asn1_curve_algorithm ( const struct asn1_cursor * cursor,
struct asn1_algorithm * wrapper,
struct asn1_algorithm ** algorithm )

Parse ASN.1 OID-identified elliptic curve algorithm.

Parameters
cursorASN.1 object cursor
wrapperOptional wrapper algorithm, or NULL
Return values
algorithmAlgorithm
rcReturn status code

Definition at line 767 of file asn1.c.

769 {
770 struct asn1_cursor curve;
771
772 /* Elliptic curves are identified as either:
773 *
774 * - a wrapper algorithm "id-ecPublicKey" with the actual
775 * curve specified in the algorithm parameters, or
776 *
777 * - a standalone object identifier for the curve
778 */
779 if ( ( wrapper == NULL ) ||
780 ( asn1_check_algorithm ( cursor, wrapper, &curve ) != 0 ) ) {
781 memcpy ( &curve, cursor, sizeof ( curve ) );
782 }
783
784 /* Identify curve */
785 asn1_enter ( &curve, ASN1_OID );
786 *algorithm = asn1_find_algorithm ( &curve );
787 if ( ! *algorithm ) {
788 DBGC ( cursor, "ASN1 %p unrecognised EC algorithm:\n",
789 cursor );
790 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
791 return -ENOTSUP_ALGORITHM;
792 }
793
794 /* Check algorithm has an elliptic curve */
795 if ( ! (*algorithm)->curve ) {
796 DBGC ( cursor, "ASN1 %p algorithm %s is not an elliptic curve "
797 "algorithm:\n", cursor, (*algorithm)->name );
798 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
799 return -ENOTTY_ALGORITHM;
800 }
801
802 return 0;
803}
int asn1_check_algorithm(const struct asn1_cursor *cursor, struct asn1_algorithm *expected, struct asn1_cursor *params)
Check ASN.1 OID-identified algorithm.
Definition asn1.c:813

References algorithm, asn1_check_algorithm(), asn1_enter(), asn1_find_algorithm(), ASN1_OID, asn1_cursor::data, DBGC, DBGC_HDA, ENOTSUP_ALGORITHM, ENOTTY_ALGORITHM, asn1_cursor::len, memcpy(), and NULL.

Referenced by ecdsa_parse_key().

◆ asn1_check_algorithm()

int asn1_check_algorithm ( const struct asn1_cursor * cursor,
struct asn1_algorithm * expected,
struct asn1_cursor * params )

Check ASN.1 OID-identified algorithm.

Parameters
cursorASN.1 object cursor
expectedExpected algorithm
Return values
paramsAlgorithm parameters, or NULL
rcReturn status code

Definition at line 813 of file asn1.c.

815 {
816 struct asn1_algorithm *actual;
817 int rc;
818
819 /* Parse algorithm */
820 if ( ( rc = asn1_algorithm ( cursor, &actual, params ) ) != 0 )
821 return rc;
822
823 /* Check algorithm matches */
824 if ( actual != expected ) {
825 DBGC ( cursor, "ASN1 %p algorithm %s does not match %s\n",
826 cursor, actual->name, expected->name );
827 return -ENOTTY_ALGORITHM;
828 }
829
830 return 0;
831}
const char * name
Name.
Definition asn1.h:431

References DBGC, ENOTTY_ALGORITHM, asn1_algorithm::name, and rc.

Referenced by asn1_curve_algorithm(), and rsa_parse_mod_exp().

◆ asn1_parse_cbc()

int asn1_parse_cbc ( struct asn1_algorithm * algorithm,
struct asn1_cursor * params )

Parse ASN.1 CBC cipher parameters.

Parameters
algorithmAlgorithm
paramParameters to parse
Return values
rcReturn status code

Definition at line 840 of file asn1.c.

841 {
842 struct cipher_algorithm *cipher = algorithm->cipher;
843
844 /* Sanity check */
845 assert ( cipher != NULL );
846
847 /* Enter parameters */
848 asn1_enter ( params, ASN1_OCTET_STRING );
849
850 /* Check length */
851 if ( params->len != cipher->blocksize )
852 return -EINVAL;
853
854 return 0;
855}
#define ASN1_OCTET_STRING
ASN.1 octet string.
Definition asn1.h:69
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
A cipher algorithm.
Definition crypto.h:58
size_t blocksize
Block size.
Definition crypto.h:68

References algorithm, asn1_enter(), ASN1_OCTET_STRING, assert, cipher_algorithm::blocksize, EINVAL, asn1_cursor::len, and NULL.

◆ asn1_parse_gcm()

int asn1_parse_gcm ( struct asn1_algorithm *algorithm __unused,
struct asn1_cursor * params )

Parse ASN.1 GCM cipher parameters.

Parameters
algorithmAlgorithm
paramParameters to parse
Return values
rcReturn status code

Definition at line 864 of file asn1.c.

865 {
866
867 /* Enter parameters */
868 asn1_enter ( params, ASN1_SEQUENCE );
869
870 /* Enter nonce */
871 return asn1_enter ( params, ASN1_OCTET_STRING );
872}

References __unused, algorithm, asn1_enter(), ASN1_OCTET_STRING, and ASN1_SEQUENCE.

◆ asn1_generalized_time()

int asn1_generalized_time ( const struct asn1_cursor * cursor,
time_t * time )

Parse ASN.1 GeneralizedTime.

Parameters
cursorASN.1 cursor
timeTime to fill in
Return values
rcReturn status code

RFC 5280 section 4.1.2.5 places several restrictions on the allowed formats for UTCTime and GeneralizedTime, and mandates the interpretation of centuryless year values.

Definition at line 885 of file asn1.c.

885 {
886 struct asn1_cursor contents;
887 unsigned int have_century;
888 unsigned int type;
889 union {
890 struct {
894 uint8_t day;
895 uint8_t hour;
896 uint8_t minute;
897 uint8_t second;
898 } __attribute__ (( packed )) named;
899 uint8_t raw[7];
900 } pairs;
901 struct tm tm;
902 const uint8_t *data;
903 size_t remaining;
904 unsigned int tens;
905 unsigned int units;
906 unsigned int i;
907 int rc;
908
909 /* Determine time format utcTime/generalizedTime */
910 memcpy ( &contents, cursor, sizeof ( contents ) );
911 type = asn1_type ( &contents );
912 switch ( type ) {
913 case ASN1_UTC_TIME:
914 have_century = 0;
915 break;
917 have_century = 1;
918 break;
919 default:
920 DBGC ( cursor, "ASN1 %p invalid time type %02x\n",
921 cursor, type );
922 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
923 return -EINVAL_ASN1_TIME;
924 }
925
926 /* Enter utcTime/generalizedTime */
927 if ( ( rc = asn1_enter ( &contents, type ) ) != 0 ) {
928 DBGC ( cursor, "ASN1 %p cannot locate %s time:\n", cursor,
929 ( ( type == ASN1_UTC_TIME ) ? "UTC" : "generalized" ) );
930 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
931 return rc;
932 }
933
934 /* Parse digit string a pair at a time */
935 memset ( &pairs, 0, sizeof ( pairs ) );
936 data = contents.data;
937 remaining = contents.len;
938 for ( i = ( have_century ? 0 : 1 ) ; i < sizeof ( pairs.raw ) ; i++ ) {
939 if ( remaining < 2 ) {
940 /* Some certificates violate the X.509 RFC by
941 * omitting the "seconds" value.
942 */
943 if ( i == ( sizeof ( pairs.raw ) - 1 ) )
944 break;
945 DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
946 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
947 return -EINVAL_ASN1_TIME;
948 }
949 tens = data[0];
950 units = data[1];
951 if ( ! ( isdigit ( tens ) && isdigit ( units ) ) ) {
952 DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
953 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
954 return -EINVAL_ASN1_TIME;
955 }
956 pairs.raw[i] = ( ( 10 * ( tens - '0' ) ) + ( units - '0' ) );
957 data += 2;
958 remaining -= 2;
959 }
960
961 /* Determine century if applicable */
962 if ( ! have_century )
963 pairs.named.century = ( ( pairs.named.year >= 50 ) ? 19 : 20 );
964
965 /* Check for trailing "Z" */
966 if ( ( remaining != 1 ) || ( data[0] != 'Z' ) ) {
967 DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
968 DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
969 return -EINVAL_ASN1_TIME;
970 }
971
972 /* Fill in time */
973 tm.tm_year = ( ( ( pairs.named.century - 19 ) * 100 ) +
974 pairs.named.year );
975 tm.tm_mon = ( pairs.named.month - 1 );
976 tm.tm_mday = pairs.named.day;
977 tm.tm_hour = pairs.named.hour;
978 tm.tm_min = pairs.named.minute;
979 tm.tm_sec = pairs.named.second;
980
981 /* Convert to seconds since the Epoch */
982 *time = mktime ( &tm );
983
984 return 0;
985}
__be32 raw[7]
Definition CIB_PRM.h:0
#define EINVAL_ASN1_TIME
Definition asn1.c:138
#define ASN1_UTC_TIME
ASN.1 UTC time.
Definition asn1.h:84
#define ASN1_GENERALIZED_TIME
ASN.1 generalized time.
Definition asn1.h:87
static int isdigit(int character)
Check if character is a decimal digit.
Definition ctype.h:30
void * memset(void *dest, int character, size_t len) __nonnull
Broken-down time.
Definition time.h:16
int tm_mon
Month of year [0,11].
Definition time.h:26
int tm_year
Years since 1900.
Definition time.h:28
int tm_hour
Hour [0,23].
Definition time.h:22
int tm_sec
Seconds [0,60].
Definition time.h:18
int tm_mday
Day of month [1,31].
Definition time.h:24
int tm_min
Minutes [0,59].
Definition time.h:20
time_t mktime(struct tm *tm)
Calculate seconds since the Epoch.
Definition time.c:119
uint8_t month
Month (BCD).
Definition ucode.h:7
uint8_t day
Day (BCD).
Definition ucode.h:5
uint8_t year
Year (BCD).
Definition ucode.h:1
uint8_t century
Century (BCD).
Definition ucode.h:3

References __attribute__, asn1_enter(), ASN1_GENERALIZED_TIME, asn1_type(), ASN1_UTC_TIME, century, asn1_cursor::data, data, day, DBGC, DBGC_HDA, EINVAL_ASN1_TIME, isdigit(), asn1_cursor::len, memcpy(), memset(), mktime(), month, raw, rc, tm::tm_hour, tm::tm_mday, tm::tm_min, tm::tm_mon, tm::tm_sec, tm::tm_year, type, and year.

Referenced by ocsp_parse_responses(), and x509_parse_validity().

◆ asn1_header()

size_t asn1_header ( struct asn1_builder_header * header,
unsigned int type,
size_t len )
static

Construct ASN.1 header.

Parameters
headerASN.1 builder header
typeType
lenContent length
Return values
header_lenHeader length

Definition at line 995 of file asn1.c.

996 {
997 unsigned int header_len = 2;
998 unsigned int len_len = 0;
999 size_t temp;
1000
1001 /* Construct header */
1002 header->type = type;
1003 if ( len < 0x80 ) {
1004 header->length[0] = len;
1005 } else {
1006 for ( temp = len ; temp ; temp >>= 8 )
1007 len_len++;
1008 header->length[0] = ( 0x80 | len_len );
1009 header_len += len_len;
1010 for ( temp = len ; temp ; temp >>= 8 )
1011 header->length[len_len--] = ( temp & 0xff );
1012 }
1013
1014 return header_len;
1015}
struct ena_llq_option header
Header locations.
Definition ena.h:5

References header, len, and type.

Referenced by asn1_header_len(), asn1_prepend(), and asn1_wrap().

◆ asn1_header_len()

size_t asn1_header_len ( size_t len)

Calculate ASN.1 header length.

Parameters
lenContent length
Return values
header_lenHeader length

Definition at line 1023 of file asn1.c.

1023 {
1025
1026 return asn1_header ( &header, ASN1_ANY, len );
1027}
static size_t asn1_header(struct asn1_builder_header *header, unsigned int type, size_t len)
Construct ASN.1 header.
Definition asn1.c:995
An ASN.1 header.
Definition asn1.h:49

References ASN1_ANY, asn1_header(), header, and len.

Referenced by ecdsa_parse_signature(), and ecdsa_verify().

◆ asn1_grow()

int asn1_grow ( struct asn1_builder * builder,
size_t extra )

Grow ASN.1 builder.

Parameters
builderASN.1 builder
extraExtra space to prepend
Return values
rcReturn status code

Definition at line 1036 of file asn1.c.

1036 {
1037 size_t new_len;
1038 void *new;
1039
1040 /* As with the ASN1 parsing functions, make errors permanent */
1041 if ( builder->len && ! builder->data )
1042 return -ENOMEM;
1043
1044 /* Reallocate data buffer */
1045 new_len = ( builder->len + extra );
1046 new = realloc ( builder->data, new_len );
1047 if ( ! new ) {
1048 zfree ( builder->data );
1049 builder->data = NULL;
1050 return -ENOMEM;
1051 }
1052 builder->data = new;
1053
1054 /* Move existing data to end of buffer */
1055 memmove ( ( builder->data + extra ), builder->data, builder->len );
1056 builder->len = new_len;
1057
1058 return 0;
1059}
#define ENOMEM
Not enough space.
Definition errno.h:578
uint8_t extra
Signature extra byte.
Definition smbios.h:6
void * memmove(void *dest, const void *src, size_t len) __nonnull
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:663
void zfree(void *ptr)
Clear and free memory.
Definition malloc.c:738
void * data
Data.
Definition asn1.h:36
size_t len
Length of data.
Definition asn1.h:38

References asn1_builder::data, ENOMEM, extra, asn1_builder::len, memmove(), NULL, realloc(), and zfree().

Referenced by asn1_prepend(), asn1_prepend_raw(), asn1_wrap(), rsa_pkcs1_decrypt(), rsa_pkcs1_encrypt(), rsa_sign(), and tls_key_build().

◆ asn1_prepend_raw()

int asn1_prepend_raw ( struct asn1_builder * builder,
const void * data,
size_t len )

Prepend raw data to ASN.1 builder.

Parameters
builderASN.1 builder
dataData to prepend
lenLength of data to prepend
Return values
rcReturn status code

Definition at line 1069 of file asn1.c.

1070 {
1071 int rc;
1072
1073 /* Grow buffer */
1074 if ( ( rc = asn1_grow ( builder, len ) ) != 0 )
1075 return rc;
1076
1077 /* Populate data buffer */
1078 memcpy ( builder->data, data, len );
1079
1080 return 0;
1081}
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
Definition asn1.c:1036

References asn1_grow(), asn1_builder::data, data, len, memcpy(), and rc.

Referenced by icert_cert(), icert_certs(), ocsp_request(), tls_send_certificate_verify(), and tls_send_client_key_exchange().

◆ asn1_prepend()

int asn1_prepend ( struct asn1_builder * builder,
unsigned int type,
const void * data,
size_t len )

Prepend data to ASN.1 builder.

Parameters
builderASN.1 builder
typeType
dataData to prepend
lenLength of data to prepend
Return values
rcReturn status code

Definition at line 1092 of file asn1.c.

1093 {
1095 size_t header_len;
1096 int rc;
1097
1098 /* Construct header */
1099 header_len = asn1_header ( &header, type, len );
1100
1101 /* Grow buffer */
1102 if ( ( rc = asn1_grow ( builder, header_len + len ) ) != 0 )
1103 return rc;
1104
1105 /* Populate data buffer */
1106 memcpy ( builder->data, &header, header_len );
1107 memcpy ( ( builder->data + header_len ), data, len );
1108
1109 return 0;
1110}

References asn1_grow(), asn1_header(), asn1_builder::data, data, header, len, memcpy(), rc, and type.

Referenced by ecdsa_prepend_signature(), icert_certs(), and ocsp_request().

◆ asn1_wrap()

int asn1_wrap ( struct asn1_builder * builder,
unsigned int type )

Wrap ASN.1 builder.

Parameters
builderASN.1 builder
typeType
Return values
rcReturn status code

Definition at line 1119 of file asn1.c.

1119 {
1121 size_t header_len;
1122 int rc;
1123
1124 /* Construct header */
1125 header_len = asn1_header ( &header, type, builder->len );
1126
1127 /* Grow buffer */
1128 if ( ( rc = asn1_grow ( builder, header_len ) ) != 0 )
1129 return rc;
1130
1131 /* Populate data buffer */
1132 memcpy ( builder->data, &header, header_len );
1133
1134 return 0;
1135}

References asn1_grow(), asn1_header(), asn1_builder::data, header, asn1_builder::len, memcpy(), rc, and type.

Referenced by ecdsa_sign(), icert_cert(), icert_certs(), and ocsp_request().

◆ image_asn1()

int image_asn1 ( struct image * image,
size_t offset,
struct asn1_cursor ** cursor )

Extract ASN.1 object from image.

Parameters
imageImage
offsetOffset within image
cursorASN.1 cursor to fill in
Return values
nextOffset to next image, or negative error

The caller is responsible for eventually calling free() on the allocated ASN.1 cursor.

Definition at line 1148 of file asn1.c.

1149 {
1150 int next;
1151 int rc;
1152
1153 /* Sanity check */
1155
1156 /* Check that this image can be used to extract an ASN.1 object */
1157 if ( ! ( image->type && image->type->asn1 ) )
1158 return -ENOTSUP;
1159
1160 /* Try creating ASN.1 cursor */
1161 next = image->type->asn1 ( image, offset, cursor );
1162 if ( next < 0 ) {
1163 rc = next;
1164 DBGC ( image, "IMAGE %s could not extract ASN.1 object: %s\n",
1165 image->name, strerror ( rc ) );
1166 return rc;
1167 }
1168
1169 return next;
1170}
uint16_t offset
Offset to command line.
Definition bzimage.h:3
uint32_t next
Next descriptor address.
Definition dwmac.h:11
#define ENOTSUP
Operation not supported.
Definition errno.h:633
int(* asn1)(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition image.h:140
An executable image.
Definition image.h:24
struct image_type * type
Image type, if known.
Definition image.h:66
char * name
Name.
Definition image.h:38

References image_type::asn1, assert, DBGC, ENOTSUP, len, image::name, next, offset, rc, strerror(), and image::type.

Referenced by asn1_okx(), cms_message(), image_x509(), and REQUIRING_SYMBOL().

◆ REQUIRING_SYMBOL()

REQUIRING_SYMBOL ( image_asn1 )

References image_asn1().

◆ REQUIRE_OBJECT()

REQUIRE_OBJECT ( config_asn1 )