iPXE
Macros | Functions
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 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)
 
static int asn1_start (struct asn1_cursor *cursor, unsigned int type, size_t extra)
 Start parsing ASN.1 object. More...
 
int asn1_enter_partial (struct asn1_cursor *cursor, unsigned int type, size_t *extra)
 Enter ASN.1 partial object. More...
 
int asn1_enter (struct asn1_cursor *cursor, unsigned int type)
 Enter ASN.1 object. More...
 
int asn1_skip_if_exists (struct asn1_cursor *cursor, unsigned int type)
 Skip ASN.1 object if present. More...
 
int asn1_skip (struct asn1_cursor *cursor, unsigned int type)
 Skip ASN.1 object. More...
 
int asn1_shrink (struct asn1_cursor *cursor, unsigned int type)
 Shrink ASN.1 cursor to fit object. More...
 
int asn1_enter_any (struct asn1_cursor *cursor)
 Enter ASN.1 object of any type. More...
 
int asn1_skip_any (struct asn1_cursor *cursor)
 Skip ASN.1 object of any type. More...
 
int asn1_shrink_any (struct asn1_cursor *cursor)
 Shrink ASN.1 object of any type. More...
 
int asn1_boolean (const struct asn1_cursor *cursor)
 Parse value of ASN.1 boolean. More...
 
int asn1_integer (const struct asn1_cursor *cursor, int *value)
 Parse value of ASN.1 integer. More...
 
int asn1_bit_string (const struct asn1_cursor *cursor, struct asn1_bit_string *bits)
 Parse ASN.1 bit string. More...
 
int asn1_integral_bit_string (const struct asn1_cursor *cursor, struct asn1_bit_string *bits)
 Parse ASN.1 bit string that must be an integral number of bytes. More...
 
int asn1_compare (const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
 Compare two ASN.1 objects. More...
 
static struct asn1_algorithmasn1_find_algorithm (const struct asn1_cursor *cursor)
 Identify ASN.1 algorithm by OID. More...
 
int asn1_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm, struct asn1_cursor *params)
 Parse ASN.1 OID-identified algorithm. More...
 
int asn1_pubkey_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
 Parse ASN.1 OID-identified public-key algorithm. More...
 
int asn1_digest_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
 Parse ASN.1 OID-identified digest algorithm. More...
 
int asn1_cipher_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm, struct asn1_cursor *params)
 Parse ASN.1 OID-identified cipher algorithm. More...
 
int asn1_signature_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm **algorithm)
 Parse ASN.1 OID-identified signature algorithm. More...
 
int asn1_check_algorithm (const struct asn1_cursor *cursor, struct asn1_algorithm *expected)
 Check ASN.1 OID-identified algorithm. More...
 
int asn1_parse_cbc (struct asn1_algorithm *algorithm, struct asn1_cursor *params)
 Parse ASN.1 CBC cipher parameters. More...
 
int asn1_parse_gcm (struct asn1_algorithm *algorithm __unused, struct asn1_cursor *params)
 Parse ASN.1 GCM cipher parameters. More...
 
int asn1_generalized_time (const struct asn1_cursor *cursor, time_t *time)
 Parse ASN.1 GeneralizedTime. More...
 
static size_t asn1_header (struct asn1_builder_header *header, unsigned int type, size_t len)
 Construct ASN.1 header. More...
 
int asn1_grow (struct asn1_builder *builder, size_t extra)
 Grow ASN.1 builder. More...
 
int asn1_prepend_raw (struct asn1_builder *builder, const void *data, size_t len)
 Prepend raw data to ASN.1 builder. More...
 
int asn1_prepend (struct asn1_builder *builder, unsigned int type, const void *data, size_t len)
 Prepend data to ASN.1 builder. More...
 
int asn1_wrap (struct asn1_builder *builder, unsigned int type)
 Wrap ASN.1 builder. More...
 
int image_asn1 (struct image *image, size_t offset, struct asn1_cursor **cursor)
 Extract ASN.1 object from image. More...
 
 REQUIRING_SYMBOL (image_asn1)
 
 REQUIRE_OBJECT (config_asn1)
 

Detailed Description

ASN.1 encoding.

Definition in file asn1.c.

Macro Definition Documentation

◆ EINVAL_ASN1_EMPTY

#define EINVAL_ASN1_EMPTY   __einfo_error ( EINFO_EINVAL_ASN1_EMPTY )

Definition at line 45 of file asn1.c.

◆ EINFO_EINVAL_ASN1_EMPTY

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

Definition at line 47 of file asn1.c.

◆ EINVAL_ASN1_LEN_LEN

#define EINVAL_ASN1_LEN_LEN   __einfo_error ( EINFO_EINVAL_ASN1_LEN_LEN )

Definition at line 49 of file asn1.c.

◆ EINFO_EINVAL_ASN1_LEN_LEN

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

Definition at line 51 of file asn1.c.

◆ EINVAL_ASN1_LEN

#define EINVAL_ASN1_LEN   __einfo_error ( EINFO_EINVAL_ASN1_LEN )

Definition at line 53 of file asn1.c.

◆ EINFO_EINVAL_ASN1_LEN

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

Definition at line 55 of file asn1.c.

◆ EINVAL_ASN1_BOOLEAN

#define EINVAL_ASN1_BOOLEAN   __einfo_error ( EINFO_EINVAL_ASN1_BOOLEAN )

Definition at line 57 of file asn1.c.

◆ EINFO_EINVAL_ASN1_BOOLEAN

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

Definition at line 59 of file asn1.c.

◆ EINVAL_ASN1_INTEGER

#define EINVAL_ASN1_INTEGER   __einfo_error ( EINFO_EINVAL_ASN1_INTEGER )

Definition at line 61 of file asn1.c.

◆ EINFO_EINVAL_ASN1_INTEGER

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

Definition at line 63 of file asn1.c.

◆ EINVAL_ASN1_TIME

#define EINVAL_ASN1_TIME   __einfo_error ( EINFO_EINVAL_ASN1_TIME )

Definition at line 65 of file asn1.c.

◆ EINFO_EINVAL_ASN1_TIME

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

Definition at line 67 of file asn1.c.

◆ EINVAL_ASN1_ALGORITHM

#define EINVAL_ASN1_ALGORITHM   __einfo_error ( EINFO_EINVAL_ASN1_ALGORITHM )

Definition at line 69 of file asn1.c.

◆ EINFO_EINVAL_ASN1_ALGORITHM

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

Definition at line 71 of file asn1.c.

◆ EINVAL_BIT_STRING

#define EINVAL_BIT_STRING   __einfo_error ( EINFO_EINVAL_BIT_STRING )

Definition at line 73 of file asn1.c.

◆ EINFO_EINVAL_BIT_STRING

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

Definition at line 75 of file asn1.c.

◆ ENOTSUP_ALGORITHM

#define ENOTSUP_ALGORITHM   __einfo_error ( EINFO_ENOTSUP_ALGORITHM )

Definition at line 77 of file asn1.c.

◆ EINFO_ENOTSUP_ALGORITHM

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

Definition at line 79 of file asn1.c.

◆ ENOTTY_ALGORITHM

#define ENOTTY_ALGORITHM   __einfo_error ( EINFO_ENOTTY_ALGORITHM )

Definition at line 81 of file asn1.c.

◆ EINFO_ENOTTY_ALGORITHM

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

Definition at line 83 of file asn1.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ asn1_start()

static int asn1_start ( struct asn1_cursor cursor,
unsigned int  type,
size_t  extra 
)
static

Start parsing ASN.1 object.

Parameters
cursorASN.1 object cursor
typeExpected type, or ASN1_ANY
extraAdditional length not present within partial cursor
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 103 of file asn1.c.

104  {
105  unsigned int len_len;
106  unsigned int len;
107 
108  /* Sanity check */
109  if ( cursor->len < 2 /* Tag byte and first length byte */ ) {
110  if ( cursor->len )
111  DBGC ( cursor, "ASN1 %p too short\n", cursor );
112  asn1_invalidate_cursor ( cursor );
113  return -EINVAL_ASN1_EMPTY;
114  }
115 
116  /* Check the tag byte */
117  if ( ( type != ASN1_ANY ) && ( type != asn1_type ( cursor ) ) ) {
118  DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
119  cursor, type, *( ( uint8_t * ) cursor->data ) );
120  return -ENXIO;
121  }
122  cursor->data++;
123  cursor->len--;
124 
125  /* Extract length of the length field and sanity check */
126  len_len = *( ( uint8_t * ) cursor->data );
127  if ( len_len & 0x80 ) {
128  len_len = ( len_len & 0x7f );
129  cursor->data++;
130  cursor->len--;
131  } else {
132  len_len = 1;
133  }
134  if ( cursor->len < len_len ) {
135  DBGC ( cursor, "ASN1 %p bad length field length %d (max "
136  "%zd)\n", cursor, len_len, cursor->len );
137  asn1_invalidate_cursor ( cursor );
138  return -EINVAL_ASN1_LEN_LEN;
139  }
140 
141  /* Extract the length and sanity check */
142  for ( len = 0 ; len_len ; len_len-- ) {
143  len <<= 8;
144  len |= *( ( uint8_t * ) cursor->data );
145  cursor->data++;
146  cursor->len--;
147  }
148  if ( ( cursor->len + extra ) < len ) {
149  DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n",
150  cursor, len, ( cursor->len + extra ) );
151  asn1_invalidate_cursor ( cursor );
152  return -EINVAL_ASN1_LEN;
153  }
154 
155  return len;
156 }
#define EINVAL_ASN1_LEN_LEN
Definition: asn1.c:49
uint8_t extra
Signature extra byte.
Definition: smbios.h:17
uint32_t type
Operating system type.
Definition: ena.h:12
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
#define EINVAL_ASN1_LEN
Definition: asn1.c:53
static unsigned int asn1_type(const struct asn1_cursor *cursor)
Extract ASN.1 type.
Definition: asn1.h:446
#define ASN1_ANY
ASN.1 "any tag" magic value.
Definition: asn1.h:101
size_t len
Length of data.
Definition: asn1.h:24
static void asn1_invalidate_cursor(struct asn1_cursor *cursor)
Invalidate ASN.1 object cursor.
Definition: asn1.h:435
unsigned char uint8_t
Definition: stdint.h:10
#define ENXIO
No such device or address.
Definition: errno.h:599
uint32_t len
Length.
Definition: ena.h:14
#define EINVAL_ASN1_EMPTY
Definition: asn1.c:45

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

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

◆ asn1_enter_partial()

int asn1_enter_partial ( struct asn1_cursor cursor,
unsigned int  type,
size_t extra 
)

Enter ASN.1 partial object.

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

The object cursor and additional length 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 171 of file asn1.c.

172  {
173  int len;
174 
175  /* Parse current object */
176  len = asn1_start ( cursor, type, *extra );
177  if ( len < 0 ) {
178  asn1_invalidate_cursor ( cursor );
179  return len;
180  }
181 
182  /* Update cursor and additional length */
183  if ( ( ( size_t ) len ) <= cursor->len )
184  cursor->len = len;
185  assert ( ( len - cursor->len ) <= *extra );
186  *extra = ( len - cursor->len );
187 
188  DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
189  cursor, type, len );
190  return 0;
191 }
uint8_t extra
Signature extra byte.
Definition: smbios.h:17
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
static int asn1_start(struct asn1_cursor *cursor, unsigned int type, size_t extra)
Start parsing ASN.1 object.
Definition: asn1.c:103
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void asn1_invalidate_cursor(struct asn1_cursor *cursor)
Invalidate ASN.1 object cursor.
Definition: asn1.h:435
uint32_t len
Length.
Definition: ena.h:14

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

Referenced by asn1_enter(), and der_probe().

◆ 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 205 of file asn1.c.

205  {
206  static size_t no_extra = 0;
207 
208  return asn1_enter_partial ( cursor, type, &no_extra );
209 }
uint32_t type
Operating system type.
Definition: ena.h:12
int asn1_enter_partial(struct asn1_cursor *cursor, unsigned int type, size_t *extra)
Enter ASN.1 partial object.
Definition: asn1.c:171

References asn1_enter_partial(), and type.

Referenced by asn1_algorithm(), asn1_bit_string(), asn1_boolean(), asn1_enter_any(), 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(), 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_integer(), 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 225 of file asn1.c.

225  {
226  int len;
227 
228  /* Parse current object */
229  len = asn1_start ( cursor, type, 0 );
230  if ( len < 0 )
231  return len;
232 
233  /* Update cursor */
234  cursor->data += len;
235  cursor->len -= len;
236 
237  DBGC ( cursor, "ASN1 %p skipped object type %02x (len %x)\n",
238  cursor, type, len );
239  return 0;
240 }
uint32_t type
Operating system type.
Definition: ena.h:12
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
static int asn1_start(struct asn1_cursor *cursor, unsigned int type, size_t extra)
Start parsing ASN.1 object.
Definition: asn1.c:103
uint32_t len
Length.
Definition: ena.h:14

References asn1_start(), asn1_cursor::data, DBGC, len, asn1_cursor::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 254 of file asn1.c.

254  {
255  int rc;
256 
257  if ( ( rc = asn1_skip_if_exists ( cursor, type ) ) != 0 ) {
258  asn1_invalidate_cursor ( cursor );
259  return rc;
260  }
261 
262  return 0;
263 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint32_t type
Operating system type.
Definition: ena.h:12
static void asn1_invalidate_cursor(struct asn1_cursor *cursor)
Invalidate ASN.1 object cursor.
Definition: asn1.h:435
int asn1_skip_if_exists(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object if present.
Definition: asn1.c:225

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(), 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 277 of file asn1.c.

277  {
278  struct asn1_cursor temp;
279  const void *end;
280  int len;
281 
282  /* Find end of object */
283  memcpy ( &temp, cursor, sizeof ( temp ) );
284  len = asn1_start ( &temp, type, 0 );
285  if ( len < 0 ) {
286  asn1_invalidate_cursor ( cursor );
287  return len;
288  }
289  end = ( temp.data + len );
290 
291  /* Shrink original cursor to contain only its first object */
292  cursor->len = ( end - cursor->data );
293 
294  return 0;
295 }
uint32_t type
Operating system type.
Definition: ena.h:12
const void * data
Start of data.
Definition: asn1.h:22
size_t len
Length of data.
Definition: asn1.h:24
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static int asn1_start(struct asn1_cursor *cursor, unsigned int type, size_t extra)
Start parsing ASN.1 object.
Definition: asn1.c:103
static void asn1_invalidate_cursor(struct asn1_cursor *cursor)
Invalidate ASN.1 object cursor.
Definition: asn1.h:435
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint32_t len
Length.
Definition: ena.h:14
An ASN.1 object cursor.
Definition: asn1.h:20

References asn1_invalidate_cursor(), asn1_start(), asn1_cursor::data, end, len, asn1_cursor::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 303 of file asn1.c.

303  {
304  return asn1_enter ( cursor, ASN1_ANY );
305 }
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
#define ASN1_ANY
ASN.1 "any tag" magic value.
Definition: asn1.h:101

References ASN1_ANY, and asn1_enter().

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

◆ asn1_skip_any()

int asn1_skip_any ( struct asn1_cursor cursor)

◆ 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 323 of file asn1.c.

323  {
324  return asn1_shrink ( cursor, ASN1_ANY );
325 }
#define ASN1_ANY
ASN.1 "any tag" magic value.
Definition: asn1.h:101
int asn1_shrink(struct asn1_cursor *cursor, unsigned int type)
Shrink ASN.1 cursor to fit object.
Definition: asn1.c:277

References ASN1_ANY, and asn1_shrink().

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

◆ 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 333 of file asn1.c.

333  {
334  struct asn1_cursor contents;
335  const struct {
336  uint8_t value;
337  } __attribute__ (( packed )) *boolean;
338 
339  /* Enter boolean */
340  memcpy ( &contents, cursor, sizeof ( contents ) );
341  asn1_enter ( &contents, ASN1_BOOLEAN );
342  if ( contents.len != sizeof ( *boolean ) )
343  return -EINVAL_ASN1_BOOLEAN;
344 
345  /* Extract value */
346  boolean = contents.data;
347  return boolean->value;
348 }
#define __attribute__(x)
Definition: compiler.h:10
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
#define ASN1_BOOLEAN
ASN.1 boolean.
Definition: asn1.h:59
void * memcpy(void *dest, const void *src, size_t len) __nonnull
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
unsigned char uint8_t
Definition: stdint.h:10
#define EINVAL_ASN1_BOOLEAN
Definition: asn1.c:57
An ASN.1 object cursor.
Definition: asn1.h:20

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 357 of file asn1.c.

357  {
358  struct asn1_cursor contents;
359  uint8_t high_byte;
360  int rc;
361 
362  /* Enter integer */
363  memcpy ( &contents, cursor, sizeof ( contents ) );
364  if ( ( rc = asn1_enter ( &contents, ASN1_INTEGER ) ) != 0 )
365  return rc;
366  if ( contents.len < 1 )
367  return -EINVAL_ASN1_INTEGER;
368 
369  /* Initialise value according to sign byte */
370  *value = *( ( int8_t * ) contents.data );
371  contents.data++;
372  contents.len--;
373 
374  /* Process value */
375  while ( contents.len ) {
376  high_byte = ( (*value) >> ( 8 * ( sizeof ( *value ) - 1 ) ) );
377  if ( ( high_byte != 0x00 ) && ( high_byte != 0xff ) ) {
378  DBGC ( cursor, "ASN1 %p integer overflow\n", cursor );
379  return -EINVAL_ASN1_INTEGER;
380  }
381  *value = ( ( *value << 8 ) | *( ( uint8_t * ) contents.data ) );
382  contents.data++;
383  contents.len--;
384  }
385 
386  return 0;
387 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
#define DBGC(...)
Definition: compiler.h:505
void * memcpy(void *dest, const void *src, size_t len) __nonnull
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define EINVAL_ASN1_INTEGER
Definition: asn1.c:61
signed char int8_t
Definition: stdint.h:15
unsigned char uint8_t
Definition: stdint.h:10
#define ASN1_INTEGER
ASN.1 integer.
Definition: asn1.h:62
An ASN.1 object cursor.
Definition: asn1.h:20

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_bit_string()

int asn1_bit_string ( const struct asn1_cursor cursor,
struct asn1_bit_string bits 
)

Parse ASN.1 bit string.

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

Definition at line 396 of file asn1.c.

397  {
398  struct asn1_cursor contents;
399  const struct {
400  uint8_t unused;
401  uint8_t data[0];
402  } __attribute__ (( packed )) *bit_string;
403  size_t len;
404  unsigned int unused;
405  uint8_t unused_mask;
406  const uint8_t *last;
407  int rc;
408 
409  /* Enter bit string */
410  memcpy ( &contents, cursor, sizeof ( contents ) );
411  if ( ( rc = asn1_enter ( &contents, ASN1_BIT_STRING ) ) != 0 ) {
412  DBGC ( cursor, "ASN1 %p cannot locate bit string:\n", cursor );
413  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
414  return rc;
415  }
416 
417  /* Validity checks */
418  if ( contents.len < sizeof ( *bit_string ) ) {
419  DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
420  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
421  return -EINVAL_BIT_STRING;
422  }
423  bit_string = contents.data;
424  len = ( contents.len - offsetof ( typeof ( *bit_string ), data ) );
425  unused = bit_string->unused;
426  unused_mask = ( 0xff >> ( 8 - unused ) );
427  last = ( bit_string->data + len - 1 );
428  if ( ( unused >= 8 ) ||
429  ( ( unused > 0 ) && ( len == 0 ) ) ||
430  ( ( *last & unused_mask ) != 0 ) ) {
431  DBGC ( cursor, "ASN1 %p invalid bit string:\n", cursor );
432  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
433  return -EINVAL_BIT_STRING;
434  }
435 
436  /* Populate bit string */
437  bits->data = &bit_string->data;
438  bits->len = len;
439  bits->unused = unused;
440 
441  return 0;
442 }
#define __attribute__(x)
Definition: compiler.h:10
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EINVAL_BIT_STRING
Definition: asn1.c:73
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
size_t len
Length of data.
Definition: asn1.h:24
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define DBGC_HDA(...)
Definition: compiler.h:506
unsigned char uint8_t
Definition: stdint.h:10
uint8_t unused
Unused.
Definition: librm.h:254
static volatile void * bits
Definition: bitops.h:27
uint8_t data[48]
Additional event data.
Definition: ena.h:22
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
uint32_t len
Length.
Definition: ena.h:14
An ASN.1 object cursor.
Definition: asn1.h:20
#define ASN1_BIT_STRING
ASN.1 bit string.
Definition: asn1.h:65

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

◆ asn1_integral_bit_string()

int asn1_integral_bit_string ( const struct asn1_cursor cursor,
struct asn1_bit_string bits 
)

Parse ASN.1 bit string that must be an integral number of bytes.

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

Definition at line 451 of file asn1.c.

452  {
453  int rc;
454 
455  /* Parse bit string */
456  if ( ( rc = asn1_bit_string ( cursor, bits ) ) != 0 )
457  return rc;
458 
459  /* Check that there are no unused bits at end of string */
460  if ( bits->unused ) {
461  DBGC ( cursor, "ASN1 %p invalid integral bit string:\n",
462  cursor );
463  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
464  return -EINVAL_BIT_STRING;
465  }
466 
467  return 0;
468 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EINVAL_BIT_STRING
Definition: asn1.c:73
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
#define DBGC_HDA(...)
Definition: compiler.h:506
static volatile void * bits
Definition: bitops.h:27
An ASN.1 bit string.
Definition: asn1.h:420

References bits, asn1_cursor::data, DBGC, DBGC_HDA, EINVAL_BIT_STRING, asn1_cursor::len, and rc.

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

◆ 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 480 of file asn1.c.

481  {
482  int difference;
483 
484  difference = ( cursor2->len - cursor1->len );
485  return ( difference ? difference :
486  memcmp ( cursor1->data, cursor2->data, cursor1->len ) );
487 }
const void * data
Start of data.
Definition: asn1.h:22
size_t len
Length of data.
Definition: asn1.h:24
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114

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(), 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()

static 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 497 of file asn1.c.

497  {
498  struct asn1_algorithm *algorithm;
499 
501  if ( asn1_compare ( &algorithm->oid, cursor ) == 0 )
502  return algorithm;
503  }
504 
505  return NULL;
506 }
An ASN.1 OID-identified algorithm.
Definition: asn1.h:366
int asn1_compare(const struct asn1_cursor *cursor1, const struct asn1_cursor *cursor2)
Compare two ASN.1 objects.
Definition: asn1.c:480
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1030
#define ASN1_ALGORITHMS
ASN.1 OID-identified algorithms.
Definition: asn1.h:391
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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

Referenced by asn1_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 516 of file asn1.c.

518  {
519  struct asn1_cursor contents;
520  int rc;
521 
522  /* Enter algorithm */
523  memcpy ( &contents, cursor, sizeof ( contents ) );
524  asn1_enter ( &contents, ASN1_SEQUENCE );
525 
526  /* Get raw parameters, if applicable */
527  if ( params ) {
528  memcpy ( params, &contents, sizeof ( *params ) );
529  asn1_skip_any ( params );
530  }
531 
532  /* Enter algorithm identifier */
533  if ( ( rc = asn1_enter ( &contents, ASN1_OID ) ) != 0 ) {
534  DBGC ( cursor, "ASN1 %p cannot locate algorithm OID:\n",
535  cursor );
536  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
537  return -EINVAL_ASN1_ALGORITHM;
538  }
539 
540  /* Identify algorithm */
541  *algorithm = asn1_find_algorithm ( &contents );
542  if ( ! *algorithm ) {
543  DBGC ( cursor, "ASN1 %p unrecognised algorithm:\n", cursor );
544  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
545  return -ENOTSUP_ALGORITHM;
546  }
547 
548  /* Parse parameters, if applicable */
549  if ( params && (*algorithm)->parse &&
550  ( ( rc = (*algorithm)->parse ( *algorithm, params ) ) != 0 ) ) {
551  DBGC ( cursor, "ASN1 %p cannot parse %s parameters: %s\n",
552  cursor, (*algorithm)->name, strerror ( rc ) );
553  return rc;
554  }
555 
556  return 0;
557 }
#define EINVAL_ASN1_ALGORITHM
Definition: asn1.c:69
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
int asn1_skip_any(struct asn1_cursor *cursor)
Skip ASN.1 object of any type.
Definition: asn1.c:313
size_t len
Length of data.
Definition: asn1.h:24
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define DBGC_HDA(...)
Definition: compiler.h:506
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:89
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1030
static struct asn1_algorithm * asn1_find_algorithm(const struct asn1_cursor *cursor)
Identify ASN.1 algorithm by OID.
Definition: asn1.c:497
#define ASN1_OID
ASN.1 object identifier.
Definition: asn1.h:74
#define ENOTSUP_ALGORITHM
Definition: asn1.c:77
An ASN.1 object cursor.
Definition: asn1.h:20

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 566 of file asn1.c.

567  {
568  int rc;
569 
570  /* Parse algorithm */
571  if ( ( rc = asn1_algorithm ( cursor, algorithm, NULL ) ) != 0 )
572  return rc;
573 
574  /* Check algorithm has a public key */
575  if ( ! (*algorithm)->pubkey ) {
576  DBGC ( cursor, "ASN1 %p algorithm %s is not a public-key "
577  "algorithm:\n", cursor, (*algorithm)->name );
578  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
579  return -ENOTTY_ALGORITHM;
580  }
581 
582  return 0;
583 }
An ASN.1 OID-identified algorithm.
Definition: asn1.h:366
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define ENOTTY_ALGORITHM
Definition: asn1.c:81
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
#define DBGC_HDA(...)
Definition: compiler.h:506
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1030
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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 592 of file asn1.c.

593  {
594  int rc;
595 
596  /* Parse algorithm */
597  if ( ( rc = asn1_algorithm ( cursor, algorithm, NULL ) ) != 0 )
598  return rc;
599 
600  /* Check algorithm has a digest */
601  if ( ! (*algorithm)->digest ) {
602  DBGC ( cursor, "ASN1 %p algorithm %s is not a digest "
603  "algorithm:\n", cursor, (*algorithm)->name );
604  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
605  return -ENOTTY_ALGORITHM;
606  }
607 
608  return 0;
609 }
An ASN.1 OID-identified algorithm.
Definition: asn1.h:366
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define ENOTTY_ALGORITHM
Definition: asn1.c:81
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
#define DBGC_HDA(...)
Definition: compiler.h:506
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1030
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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 619 of file asn1.c.

621  {
622  int rc;
623 
624  /* Parse algorithm */
625  if ( ( rc = asn1_algorithm ( cursor, algorithm, params ) ) != 0 )
626  return rc;
627 
628  /* Check algorithm has a cipher */
629  if ( ! (*algorithm)->cipher ) {
630  DBGC ( cursor, "ASN1 %p algorithm %s is not a cipher "
631  "algorithm:\n", cursor, (*algorithm)->name );
632  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
633  return -ENOTTY_ALGORITHM;
634  }
635 
636  return 0;
637 }
An ASN.1 OID-identified algorithm.
Definition: asn1.h:366
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define ENOTTY_ALGORITHM
Definition: asn1.c:81
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
#define DBGC_HDA(...)
Definition: compiler.h:506
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1030

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 646 of file asn1.c.

647  {
648  int rc;
649 
650  /* Parse algorithm */
651  if ( ( rc = asn1_algorithm ( cursor, algorithm, NULL ) ) != 0 )
652  return rc;
653 
654  /* Check algorithm has a public key */
655  if ( ! (*algorithm)->pubkey ) {
656  DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
657  "algorithm:\n", cursor, (*algorithm)->name );
658  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
659  return -ENOTTY_ALGORITHM;
660  }
661 
662  /* Check algorithm has a digest */
663  if ( ! (*algorithm)->digest ) {
664  DBGC ( cursor, "ASN1 %p algorithm %s is not a signature "
665  "algorithm:\n", cursor, (*algorithm)->name );
666  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
667  return -ENOTTY_ALGORITHM;
668  }
669 
670  return 0;
671 }
An ASN.1 OID-identified algorithm.
Definition: asn1.h:366
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define ENOTTY_ALGORITHM
Definition: asn1.c:81
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
size_t len
Length of data.
Definition: asn1.h:24
#define DBGC_HDA(...)
Definition: compiler.h:506
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1030
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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_check_algorithm()

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

Check ASN.1 OID-identified algorithm.

Parameters
cursorASN.1 object cursor
expectedExpected algorithm
Return values
rcReturn status code

Definition at line 680 of file asn1.c.

681  {
682  struct asn1_algorithm *actual;
683  int rc;
684 
685  /* Parse algorithm */
686  if ( ( rc = asn1_algorithm ( cursor, &actual, NULL ) ) != 0 )
687  return rc;
688 
689  /* Check algorithm matches */
690  if ( actual != expected ) {
691  DBGC ( cursor, "ASN1 %p algorithm %s does not match %s\n",
692  cursor, actual->name, expected->name );
693  return -ENOTTY_ALGORITHM;
694  }
695 
696  return 0;
697 }
An ASN.1 OID-identified algorithm.
Definition: asn1.h:366
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define ENOTTY_ALGORITHM
Definition: asn1.c:81
#define DBGC(...)
Definition: compiler.h:505
const char * name
Name.
Definition: asn1.h:368
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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

Referenced by 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 706 of file asn1.c.

707  {
708  struct cipher_algorithm *cipher = algorithm->cipher;
709 
710  /* Sanity check */
711  assert ( cipher != NULL );
712 
713  /* Enter parameters */
714  asn1_enter ( params, ASN1_OCTET_STRING );
715 
716  /* Check length */
717  if ( params->len != cipher->blocksize )
718  return -EINVAL;
719 
720  return 0;
721 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
size_t blocksize
Block size.
Definition: crypto.h:60
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
size_t len
Length of data.
Definition: asn1.h:24
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
u16 algorithm
Authentication algorithm (Open System or Shared Key)
Definition: ieee80211.h:1030
A cipher algorithm.
Definition: crypto.h:50
#define ASN1_OCTET_STRING
ASN.1 octet string.
Definition: asn1.h:68
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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 730 of file asn1.c.

731  {
732 
733  /* Enter parameters */
734  asn1_enter ( params, ASN1_SEQUENCE );
735 
736  /* Enter nonce */
737  return asn1_enter ( params, ASN1_OCTET_STRING );
738 }
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:89
#define ASN1_OCTET_STRING
ASN.1 octet string.
Definition: asn1.h:68

References 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 751 of file asn1.c.

751  {
752  struct asn1_cursor contents;
753  unsigned int have_century;
754  unsigned int type;
755  union {
756  struct {
758  uint8_t year;
759  uint8_t month;
760  uint8_t day;
761  uint8_t hour;
762  uint8_t minute;
763  uint8_t second;
764  } __attribute__ (( packed )) named;
765  uint8_t raw[7];
766  } pairs;
767  struct tm tm;
768  const uint8_t *data;
769  size_t remaining;
770  unsigned int tens;
771  unsigned int units;
772  unsigned int i;
773  int rc;
774 
775  /* Determine time format utcTime/generalizedTime */
776  memcpy ( &contents, cursor, sizeof ( contents ) );
777  type = asn1_type ( &contents );
778  switch ( type ) {
779  case ASN1_UTC_TIME:
780  have_century = 0;
781  break;
783  have_century = 1;
784  break;
785  default:
786  DBGC ( cursor, "ASN1 %p invalid time type %02x\n",
787  cursor, type );
788  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
789  return -EINVAL_ASN1_TIME;
790  }
791 
792  /* Enter utcTime/generalizedTime */
793  if ( ( rc = asn1_enter ( &contents, type ) ) != 0 ) {
794  DBGC ( cursor, "ASN1 %p cannot locate %s time:\n", cursor,
795  ( ( type == ASN1_UTC_TIME ) ? "UTC" : "generalized" ) );
796  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
797  return rc;
798  }
799 
800  /* Parse digit string a pair at a time */
801  memset ( &pairs, 0, sizeof ( pairs ) );
802  data = contents.data;
803  remaining = contents.len;
804  for ( i = ( have_century ? 0 : 1 ) ; i < sizeof ( pairs.raw ) ; i++ ) {
805  if ( remaining < 2 ) {
806  /* Some certificates violate the X.509 RFC by
807  * omitting the "seconds" value.
808  */
809  if ( i == ( sizeof ( pairs.raw ) - 1 ) )
810  break;
811  DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
812  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
813  return -EINVAL_ASN1_TIME;
814  }
815  tens = data[0];
816  units = data[1];
817  if ( ! ( isdigit ( tens ) && isdigit ( units ) ) ) {
818  DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
819  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
820  return -EINVAL_ASN1_TIME;
821  }
822  pairs.raw[i] = ( ( 10 * ( tens - '0' ) ) + ( units - '0' ) );
823  data += 2;
824  remaining -= 2;
825  }
826 
827  /* Determine century if applicable */
828  if ( ! have_century )
829  pairs.named.century = ( ( pairs.named.year >= 50 ) ? 19 : 20 );
830 
831  /* Check for trailing "Z" */
832  if ( ( remaining != 1 ) || ( data[0] != 'Z' ) ) {
833  DBGC ( cursor, "ASN1 %p invalid time:\n", cursor );
834  DBGC_HDA ( cursor, 0, cursor->data, cursor->len );
835  return -EINVAL_ASN1_TIME;
836  }
837 
838  /* Fill in time */
839  tm.tm_year = ( ( ( pairs.named.century - 19 ) * 100 ) +
840  pairs.named.year );
841  tm.tm_mon = ( pairs.named.month - 1 );
842  tm.tm_mday = pairs.named.day;
843  tm.tm_hour = pairs.named.hour;
844  tm.tm_min = pairs.named.minute;
845  tm.tm_sec = pairs.named.second;
846 
847  /* Convert to seconds since the Epoch */
848  *time = mktime ( &tm );
849 
850  return 0;
851 }
#define __attribute__(x)
Definition: compiler.h:10
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int tm_min
Minutes [0,59].
Definition: time.h:19
#define EINVAL_ASN1_TIME
Definition: asn1.c:65
int asn1_enter(struct asn1_cursor *cursor, unsigned int type)
Enter ASN.1 object.
Definition: asn1.c:205
uint32_t type
Operating system type.
Definition: ena.h:12
int tm_mday
Day of month [1,31].
Definition: time.h:23
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
uint8_t year
Year (BCD)
Definition: ucode.h:12
int tm_year
Years since 1900.
Definition: time.h:27
static unsigned int asn1_type(const struct asn1_cursor *cursor)
Extract ASN.1 type.
Definition: asn1.h:446
size_t len
Length of data.
Definition: asn1.h:24
static int isdigit(int character)
Check if character is a decimal digit.
Definition: ctype.h:29
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define DBGC_HDA(...)
Definition: compiler.h:506
int tm_mon
Month of year [0,11].
Definition: time.h:25
time_t mktime(struct tm *tm)
Calculate seconds since the Epoch.
Definition: time.c:117
#define ASN1_GENERALIZED_TIME
ASN.1 generalized time.
Definition: asn1.h:86
unsigned char uint8_t
Definition: stdint.h:10
Broken-down time.
Definition: time.h:15
uint8_t century
Century (BCD)
Definition: ucode.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint8_t day
Day (BCD)
Definition: ucode.h:16
int tm_sec
Seconds [0,60].
Definition: time.h:17
__be32 raw[7]
Definition: CIB_PRM.h:28
#define ASN1_UTC_TIME
ASN.1 UTC time.
Definition: asn1.h:83
int tm_hour
Hour [0,23].
Definition: time.h:21
uint8_t month
Month (BCD)
Definition: ucode.h:18
static __always_inline int off_t userptr_t second
Definition: librm.h:166
An ASN.1 object cursor.
Definition: asn1.h:20
void * memset(void *dest, int character, size_t len) __nonnull

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, second, 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()

static 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 861 of file asn1.c.

862  {
863  unsigned int header_len = 2;
864  unsigned int len_len = 0;
865  size_t temp;
866 
867  /* Construct header */
868  header->type = type;
869  if ( len < 0x80 ) {
870  header->length[0] = len;
871  } else {
872  for ( temp = len ; temp ; temp >>= 8 )
873  len_len++;
874  header->length[0] = ( 0x80 | len_len );
875  header_len += len_len;
876  for ( temp = len ; temp ; temp >>= 8 )
877  header->length[len_len--] = ( temp & 0xff );
878  }
879 
880  return header_len;
881 }
uint32_t type
Operating system type.
Definition: ena.h:12
struct ena_aq_header header
Header.
Definition: ena.h:12
uint32_t len
Length.
Definition: ena.h:14

References header, len, and type.

Referenced by asn1_prepend(), and asn1_wrap().

◆ 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 890 of file asn1.c.

890  {
891  size_t new_len;
892  void *new;
893 
894  /* As with the ASN1 parsing functions, make errors permanent */
895  if ( builder->len && ! builder->data )
896  return -ENOMEM;
897 
898  /* Reallocate data buffer */
899  new_len = ( builder->len + extra );
900  new = realloc ( builder->data, new_len );
901  if ( ! new ) {
902  free ( builder->data );
903  builder->data = NULL;
904  return -ENOMEM;
905  }
906  builder->data = new;
907 
908  /* Move existing data to end of buffer */
909  memmove ( ( builder->data + extra ), builder->data, builder->len );
910  builder->len = new_len;
911 
912  return 0;
913 }
void * data
Data.
Definition: asn1.h:35
uint8_t extra
Signature extra byte.
Definition: smbios.h:17
#define ENOMEM
Not enough space.
Definition: errno.h:534
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
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:521
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
size_t len
Length of data.
Definition: asn1.h:37

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

Referenced by asn1_prepend(), asn1_prepend_raw(), asn1_wrap(), and icert_cert().

◆ 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 923 of file asn1.c.

924  {
925  int rc;
926 
927  /* Grow buffer */
928  if ( ( rc = asn1_grow ( builder, len ) ) != 0 )
929  return rc;
930 
931  /* Populate data buffer */
932  memcpy ( builder->data, data, len );
933 
934  return 0;
935 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void * data
Data.
Definition: asn1.h:35
void * memcpy(void *dest, const void *src, size_t len) __nonnull
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
Definition: asn1.c:890
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t len
Length.
Definition: ena.h:14

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

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

◆ 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 946 of file asn1.c.

947  {
949  size_t header_len;
950  int rc;
951 
952  /* Construct header */
953  header_len = asn1_header ( &header, type, len );
954 
955  /* Grow buffer */
956  if ( ( rc = asn1_grow ( builder, header_len + len ) ) != 0 )
957  return rc;
958 
959  /* Populate data buffer */
960  memcpy ( builder->data, &header, header_len );
961  memcpy ( ( builder->data + header_len ), data, len );
962 
963  return 0;
964 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void * data
Data.
Definition: asn1.h:35
uint32_t type
Operating system type.
Definition: ena.h:12
void * memcpy(void *dest, const void *src, size_t len) __nonnull
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
Definition: asn1.c:890
static size_t asn1_header(struct asn1_builder_header *header, unsigned int type, size_t len)
Construct ASN.1 header.
Definition: asn1.c:861
An ASN.1 header.
Definition: asn1.h:48
struct ena_aq_header header
Header.
Definition: ena.h:12
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t len
Length.
Definition: ena.h:14

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

Referenced by 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 973 of file asn1.c.

973  {
975  size_t header_len;
976  int rc;
977 
978  /* Construct header */
979  header_len = asn1_header ( &header, type, builder->len );
980 
981  /* Grow buffer */
982  if ( ( rc = asn1_grow ( builder, header_len ) ) != 0 )
983  return rc;
984 
985  /* Populate data buffer */
986  memcpy ( builder->data, &header, header_len );
987 
988  return 0;
989 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void * data
Data.
Definition: asn1.h:35
uint32_t type
Operating system type.
Definition: ena.h:12
void * memcpy(void *dest, const void *src, size_t len) __nonnull
int asn1_grow(struct asn1_builder *builder, size_t extra)
Grow ASN.1 builder.
Definition: asn1.c:890
static size_t asn1_header(struct asn1_builder_header *header, unsigned int type, size_t len)
Construct ASN.1 header.
Definition: asn1.c:861
An ASN.1 header.
Definition: asn1.h:48
struct ena_aq_header header
Header.
Definition: ena.h:12
size_t len
Length of data.
Definition: asn1.h:37

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

Referenced by 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 1002 of file asn1.c.

1003  {
1004  int next;
1005  int rc;
1006 
1007  /* Sanity check */
1008  assert ( offset <= image->len );
1009 
1010  /* Check that this image can be used to extract an ASN.1 object */
1011  if ( ! ( image->type && image->type->asn1 ) )
1012  return -ENOTSUP;
1013 
1014  /* Try creating ASN.1 cursor */
1015  next = image->type->asn1 ( image, offset, cursor );
1016  if ( next < 0 ) {
1017  rc = next;
1018  DBGC ( image, "IMAGE %s could not extract ASN.1 object: %s\n",
1019  image->name, strerror ( rc ) );
1020  return rc;
1021  }
1022 
1023  return next;
1024 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint32_t next
Next descriptor address.
Definition: myson.h:18
struct image_type * type
Image type, if known.
Definition: image.h:46
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
int(* asn1)(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: image.h:114
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
char * name
Name.
Definition: image.h:34
uint32_t len
Length.
Definition: ena.h:14

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

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

◆ REQUIRING_SYMBOL()

REQUIRING_SYMBOL ( image_asn1  )

◆ REQUIRE_OBJECT()

REQUIRE_OBJECT ( config_asn1  )