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:411
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:599
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  int rc;
80 
81  /* Sanity check: no realistic DER image can be smaller than this */
82  if ( image->len < sizeof ( buf ) )
83  return -ENOEXEC;
84 
85  /* Prepare partial cursor */
86  cursor.data = buf;
87  cursor.len = sizeof ( buf );
88  copy_from_user ( buf, image->data, 0, sizeof ( buf ) );
89  extra = ( image->len - sizeof ( buf ) );
90 
91  /* Check that image begins with an ASN.1 sequence object */
92  if ( ( rc = asn1_enter_partial ( &cursor, ASN1_SEQUENCE,
93  &extra ) ) != 0 ) {
94  DBGC ( image, "DER %s is not valid ASN.1: %s\n",
95  image->name, strerror ( rc ) );
96  return rc;
97  }
98 
99  /* Check that image comprises a single well-formed ASN.1 object */
100  if ( extra != ( image->len - sizeof ( buf ) ) ) {
101  DBGC ( image, "DER %s is not single ASN.1\n", image->name );
102  return -ENOEXEC;
103  }
104 
105  return 0;
106 }
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:411
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
int asn1_enter_partial(struct asn1_cursor *cursor, unsigned int type, size_t *extra)
Enter ASN.1 partial object.
Definition: asn1.c:171
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
char * name
Name.
Definition: image.h:34
An ASN.1 object cursor.
Definition: asn1.h:20

References asn1_enter_partial(), ASN1_SEQUENCE, copy_from_user(), asn1_cursor::data, image::data, DBGC, ENOEXEC, extra, 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.