iPXE
Macros | Functions
pem.h File Reference

PEM-encoded ASN.1 data. More...

#include <stdint.h>
#include <ipxe/asn1.h>
#include <ipxe/image.h>

Go to the source code of this file.

Macros

#define PEM_BEGIN   "-----BEGIN"
 Pre-encapsulation boundary marker. More...
 
#define PEM_END   "-----END"
 Post-encapsulation boundary marker. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int pem_asn1 (const void *data, size_t len, size_t offset, struct asn1_cursor **cursor)
 Extract ASN.1 object from PEM data. More...
 
struct image_type pem_image_type __image_type (PROBE_NORMAL)
 

Detailed Description

PEM-encoded ASN.1 data.

Definition in file pem.h.

Macro Definition Documentation

◆ PEM_BEGIN

#define PEM_BEGIN   "-----BEGIN"

Pre-encapsulation boundary marker.

Definition at line 17 of file pem.h.

◆ PEM_END

#define PEM_END   "-----END"

Post-encapsulation boundary marker.

Definition at line 20 of file pem.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ pem_asn1()

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

Extract ASN.1 object from PEM data.

Parameters
dataPEM data
lenLength of PEM 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 103 of file pem.c.

104  {
105  size_t encoded_len;
106  size_t decoded_max_len;
107  char *encoded;
108  void *decoded;
109  int decoded_len;
110  int begin;
111  int end;
112  int rc;
113 
114  /* Locate and skip BEGIN marker */
115  begin = pem_marker ( data, len, offset, PEM_BEGIN );
116  if ( begin < 0 ) {
117  rc = begin;
118  DBGC ( data, "PEM [%#zx,%#zx) missing BEGIN marker: %s\n",
119  offset, len, strerror ( rc ) );
120  goto err_begin;
121  }
122  begin = pem_next ( data, len, begin );
123 
124  /* Locate and skip END marker */
125  end = pem_marker ( data, len, begin, PEM_END );
126  if ( end < 0 ) {
127  rc = end;
128  DBGC ( data, "PEM [%#zx,%#zx) missing END marker: %s\n",
129  offset, len, strerror ( rc ) );
130  goto err_end;
131  }
132  encoded_len = ( end - begin );
133  end = pem_next ( data, len, end );
134 
135  /* Extract Base64-encoded data */
136  encoded = malloc ( encoded_len + 1 /* NUL */ );
137  if ( ! encoded ) {
138  rc = -ENOMEM;
139  goto err_alloc_encoded;
140  }
141  memcpy ( encoded, ( data + begin ), encoded_len );
142  encoded[encoded_len] = '\0';
143 
144  /* Allocate cursor and data buffer */
145  decoded_max_len = base64_decoded_max_len ( encoded );
146  *cursor = malloc ( sizeof ( **cursor ) + decoded_max_len );
147  if ( ! *cursor ) {
148  rc = -ENOMEM;
149  goto err_alloc_cursor;
150  }
151  decoded = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
152 
153  /* Decode Base64-encoded data */
154  decoded_len = base64_decode ( encoded, decoded, decoded_max_len );
155  if ( decoded_len < 0 ) {
156  rc = decoded_len;
157  DBGC ( data, "PEM could not decode: %s\n", strerror ( rc ) );
158  goto err_decode;
159  }
160  (*cursor)->data = decoded;
161  (*cursor)->len = decoded_len;
162  assert ( (*cursor)->len <= decoded_max_len );
163 
164  /* Free Base64-encoded data */
165  free ( encoded );
166 
167  /* Update offset and skip any unencapsulated trailer */
168  offset = end;
169  if ( pem_marker ( data, len, offset, PEM_BEGIN ) < 0 )
170  offset = len;
171 
172  return offset;
173 
174  err_decode:
175  free ( *cursor );
176  *cursor = NULL;
177  err_alloc_cursor:
178  free ( encoded );
179  err_alloc_encoded:
180  err_end:
181  err_begin:
182  return rc;
183 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int pem_marker(const void *data, size_t len, size_t offset, const char *marker)
Locate boundary marker line.
Definition: pem.c:67
#define DBGC(...)
Definition: compiler.h:505
static size_t pem_next(const void *data, size_t len, size_t offset)
Locate next line.
Definition: pem.c:48
int base64_decode(const char *encoded, void *data, size_t len)
Base64-decode string.
Definition: base64.c:91
#define ENOMEM
Not enough space.
Definition: errno.h:534
static size_t base64_decoded_max_len(const char *encoded)
Calculate maximum length of base64-decoded string.
Definition: base64.h:34
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
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:620
#define PEM_END
Post-encapsulation boundary marker.
Definition: pem.h:20
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define PEM_BEGIN
Pre-encapsulation boundary marker.
Definition: pem.h:17
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), base64_decode(), base64_decoded_max_len(), data, DBGC, end, ENOMEM, free, len, malloc(), memcpy(), NULL, offset, PEM_BEGIN, PEM_END, pem_marker(), pem_next(), rc, and strerror().

Referenced by efisig_asn1(), ipair_rx_pubkey(), and pem_image_asn1().

◆ __image_type()

struct image_type pem_image_type __image_type ( PROBE_NORMAL  )