iPXE
Functions
der.c File Reference

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

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

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int der_asn1 (const void *data, size_t len, size_t offset, struct asn1_cursor **cursor)
 Extract ASN.1 object from DER data. More...
 
static int der_image_probe (struct image *image)
 Probe DER image. More...
 
static int der_image_asn1 (struct image *image, size_t offset, struct asn1_cursor **cursor)
 Extract ASN.1 object from 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()

int der_asn1 ( const void *  data,
size_t  len,
size_t  offset,
struct asn1_cursor **  cursor 
)

Extract ASN.1 object from DER data.

Parameters
dataDER data
lenLength of DER data
offsetOffset within data
cursorASN.1 cursor to fill in
Return values
nextOffset to next object, or negative error

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

Definition at line 52 of file der.c.

53  {
54  size_t remaining;
55  void *raw;
56 
57  /* Sanity check */
58  assert ( offset <= len );
59  remaining = ( len - offset );
60 
61  /* Allocate cursor and data buffer */
62  *cursor = malloc ( sizeof ( **cursor ) + remaining );
63  if ( ! *cursor )
64  return -ENOMEM;
65  raw = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
66 
67  /* Populate cursor and data buffer */
68  (*cursor)->data = raw;
69  (*cursor)->len = remaining;
70  memcpy ( raw, ( data + offset ), remaining );
71 
72  /* Shrink cursor */
73  asn1_shrink_any ( *cursor );
74 
75  return ( offset + (*cursor)->len );
76 }
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
int asn1_shrink_any(struct asn1_cursor *cursor)
Shrink ASN.1 object of any type.
Definition: asn1.c:312
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:620
uint8_t data[48]
Additional event data.
Definition: ena.h:22
__be32 raw[7]
Definition: CIB_PRM.h:28
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

References asn1_shrink_any(), assert(), data, ENOMEM, len, malloc(), memcpy(), offset, and raw.

Referenced by der_image_asn1(), and efisig_asn1().

◆ der_image_probe()

static int der_image_probe ( struct image image)
static

Probe DER image.

Parameters
imageDER image
Return values
rcReturn status code

Definition at line 84 of file der.c.

84  {
85  struct asn1_cursor cursor;
86  int rc;
87 
88  /* Prepare cursor */
89  cursor.data = image->data;
90  cursor.len = image->len;
91 
92  /* Check that image begins with an ASN.1 sequence object */
93  if ( ( rc = asn1_skip ( &cursor, ASN1_SEQUENCE ) ) != 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 ( cursor.len ) {
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
const void * data
Read-only data.
Definition: image.h:50
#define ENOEXEC
Exec format error.
Definition: errno.h:519
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
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:55
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:89
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
Definition: asn1.c:243
struct arbelprm_wqe_segment_data_ptr data[ARBEL_MAX_GATHER]
Definition: arbel.h:236
char * name
Name.
Definition: image.h:37
An ASN.1 object cursor.
Definition: asn1.h:20

References ASN1_SEQUENCE, asn1_skip(), asn1_cursor::data, image::data, DBGC, ENOEXEC, asn1_cursor::len, image::len, image::name, rc, and strerror().

◆ der_image_asn1()

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

Extract ASN.1 object from DER 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 119 of file der.c.

120  {
121  int next;
122  int rc;
123 
124  /* Extract ASN.1 object */
125  if ( ( next = der_asn1 ( image->data, image->len, offset,
126  cursor ) ) < 0 ) {
127  rc = next;
128  DBGC ( image, "DER %s could not extract ASN.1: %s\n",
129  image->name, strerror ( rc ) );
130  return rc;
131  }
132 
133  return next;
134 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const void * data
Read-only data.
Definition: image.h:50
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
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:55
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
int der_asn1(const void *data, size_t len, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from DER data.
Definition: der.c:52
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
char * name
Name.
Definition: image.h:37

References image::data, DBGC, der_asn1(), image::len, image::name, next, offset, rc, and strerror().

◆ __image_type()

struct image_type zimg_image_type __image_type ( PROBE_NORMAL  )
Initial value:
= {
.name = "zimg",
.probe = zimg_probe,
.extract = zimg_extract,
}
static int zimg_probe(struct image *image)
Probe compressed kernel image.
Definition: lkrn.c:349
static int zimg_extract(struct image *image, struct image *extracted)
Extract compresed kernel image.
Definition: lkrn.c:320
int image_extract_exec(struct image *image)
Extract and execute image.
Definition: archive.c:107

DER image type.

Linux kernel compressed image type.