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)
 
 FILE_SECBOOT (PERMITTED)
 
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 18 of file pem.h.

◆ PEM_END

#define PEM_END   "-----END"

Post-encapsulation boundary marker.

Definition at line 21 of file pem.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ 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 104 of file pem.c.

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

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  )