iPXE
Macros | Functions
pem.h File Reference

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

#include <stdint.h>
#include <ipxe/uaccess.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 (userptr_t 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  )

◆ pem_asn1()

int pem_asn1 ( userptr_t  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 105 of file pem.c.

106  {
107  size_t encoded_len;
108  size_t decoded_max_len;
109  char *encoded;
110  void *decoded;
111  int decoded_len;
112  int begin;
113  int end;
114  int rc;
115 
116  /* Locate and skip BEGIN marker */
117  begin = pem_marker ( data, len, offset, PEM_BEGIN );
118  if ( begin < 0 ) {
119  rc = begin;
120  DBGC ( data, "PEM [%#zx,%#zx) missing BEGIN marker: %s\n",
121  offset, len, strerror ( rc ) );
122  goto err_begin;
123  }
124  begin = pem_next ( data, len, begin );
125 
126  /* Locate and skip END marker */
127  end = pem_marker ( data, len, begin, PEM_END );
128  if ( end < 0 ) {
129  rc = end;
130  DBGC ( data, "PEM [%#zx,%#zx) missing END marker: %s\n",
131  offset, len, strerror ( rc ) );
132  goto err_end;
133  }
134  encoded_len = ( end - begin );
135  end = pem_next ( data, len, end );
136 
137  /* Extract Base64-encoded data */
138  encoded = malloc ( encoded_len + 1 /* NUL */ );
139  if ( ! encoded ) {
140  rc = -ENOMEM;
141  goto err_alloc_encoded;
142  }
143  copy_from_user ( encoded, data, begin, encoded_len );
144  encoded[encoded_len] = '\0';
145 
146  /* Allocate cursor and data buffer */
147  decoded_max_len = base64_decoded_max_len ( encoded );
148  *cursor = malloc ( sizeof ( **cursor ) + decoded_max_len );
149  if ( ! *cursor ) {
150  rc = -ENOMEM;
151  goto err_alloc_cursor;
152  }
153  decoded = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
154 
155  /* Decode Base64-encoded data */
156  decoded_len = base64_decode ( encoded, decoded, decoded_max_len );
157  if ( decoded_len < 0 ) {
158  rc = decoded_len;
159  DBGC ( data, "PEM could not decode: %s\n", strerror ( rc ) );
160  goto err_decode;
161  }
162  (*cursor)->data = decoded;
163  (*cursor)->len = decoded_len;
164  assert ( (*cursor)->len <= decoded_max_len );
165 
166  /* Free Base64-encoded data */
167  free ( encoded );
168 
169  /* Update offset and skip any unencapsulated trailer */
170  offset = end;
171  if ( pem_marker ( data, len, offset, PEM_BEGIN ) < 0 )
172  offset = len;
173 
174  return offset;
175 
176  err_decode:
177  free ( *cursor );
178  *cursor = NULL;
179  err_alloc_cursor:
180  free ( encoded );
181  err_alloc_encoded:
182  err_end:
183  err_begin:
184  return rc;
185 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
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
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
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static int pem_marker(userptr_t data, size_t len, size_t offset, const char *marker)
Locate boundary marker line.
Definition: pem.c:68
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
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:583
static size_t pem_next(userptr_t data, size_t len, size_t offset)
Locate next line.
Definition: pem.c:49
uint32_t len
Length.
Definition: ena.h:14
#define PEM_END
Post-encapsulation boundary marker.
Definition: pem.h:21
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define PEM_BEGIN
Pre-encapsulation boundary marker.
Definition: pem.h:18
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

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

Referenced by pem_image_asn1().

◆ __image_type()

struct image_type pem_image_type __image_type ( PROBE_NORMAL  )