iPXE
Functions
der.c File Reference

DER-encoded ASN.1 data. More...

#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/asn1.h>
#include <ipxe/der.h>
#include <ipxe/uaccess.h>
#include <ipxe/image.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int der_asn1 (struct image *image, size_t offset __unused, struct asn1_cursor **cursor)
 Extract ASN.1 object from image. More...
 
static int der_probe (struct image *image)
 Probe DER image. More...
 
struct image_type der_image_type __image_type (PROBE_NORMAL)
 DER image type. More...
 

Detailed Description

DER-encoded ASN.1 data.

Definition in file der.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ der_asn1()

static int der_asn1 ( struct image image,
size_t offset  __unused,
struct asn1_cursor **  cursor 
)
static

Extract ASN.1 object from image.

Parameters
imageDER image
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 51 of file der.c.

52  {
53  void *data;
54 
55  /* Allocate cursor and data buffer */
56  *cursor = malloc ( sizeof ( **cursor ) + image->len );
57  if ( ! *cursor )
58  return -ENOMEM;
59  data = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
60 
61  /* Populate cursor and data buffer */
62  (*cursor)->data = data;
63  (*cursor)->len = image->len;
65 
66  return image->len;
67 }
userptr_t data
Raw file image.
Definition: image.h:41
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
An executable image.
Definition: image.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
size_t len
Length of raw file image.
Definition: image.h:43
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References copy_from_user(), data, image::data, ENOMEM, image::len, and malloc().

◆ der_probe()

static int der_probe ( struct image image)
static

Probe DER image.

Parameters
imageDER image
Return values
rcReturn status code

Definition at line 75 of file der.c.

75  {
76  struct asn1_cursor cursor;
77  uint8_t buf[8];
78  size_t extra;
79  size_t total;
80  int len;
81  int rc;
82 
83  /* Sanity check: no realistic DER image can be smaller than this */
84  if ( image->len < sizeof ( buf ) )
85  return -ENOEXEC;
86 
87  /* Prepare partial cursor */
88  cursor.data = buf;
89  cursor.len = sizeof ( buf );
90  copy_from_user ( buf, image->data, 0, sizeof ( buf ) );
91  extra = ( image->len - sizeof ( buf ) );
92 
93  /* Get length of ASN.1 sequence */
94  len = asn1_start ( &cursor, ASN1_SEQUENCE, extra );
95  if ( len < 0 ) {
96  rc = len;
97  DBGC ( image, "DER %s is not valid ASN.1: %s\n",
98  image->name, strerror ( rc ) );
99  return rc;
100  }
101 
102  /* Add length of tag and length bytes consumed by asn1_start() */
103  total = ( len + ( cursor.data - ( ( void * ) buf ) ) );
104  assert ( total <= image->len );
105 
106  /* Check that image comprises a single well-formed ASN.1 object */
107  if ( total != image->len ) {
108  DBGC ( image, "DER %s is not single ASN.1\n", image->name );
109  return -ENOEXEC;
110  }
111 
112  return 0;
113 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
uint8_t extra
Signature extra byte.
Definition: smbios.h:17
#define ENOEXEC
Exec format error.
Definition: errno.h:519
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
int asn1_start(struct asn1_cursor *cursor, unsigned int type, size_t extra)
Start parsing ASN.1 object.
Definition: asn1.c:98
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length of raw file image.
Definition: image.h:43
unsigned char uint8_t
Definition: stdint.h:10
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:89
uint32_t len
Length.
Definition: ena.h:14
char * name
Name.
Definition: image.h:34
An ASN.1 object cursor.
Definition: asn1.h:20

References ASN1_SEQUENCE, asn1_start(), assert(), copy_from_user(), asn1_cursor::data, image::data, DBGC, ENOEXEC, extra, len, asn1_cursor::len, image::len, image::name, rc, and strerror().

◆ __image_type()

struct image_type der_image_type __image_type ( PROBE_NORMAL  )

DER image type.