iPXE
pem.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <ipxe/asn1.h>
30 #include <ipxe/base64.h>
31 #include <ipxe/uaccess.h>
32 #include <ipxe/image.h>
33 #include <ipxe/pem.h>
34 
35 /** @file
36  *
37  * PEM-encoded ASN.1 data
38  *
39  */
40 
41 /**
42  * Locate next line
43  *
44  * @v data PEM data
45  * @v len Length of PEM data
46  * @v offset Starting offset
47  * @ret next Offset to next line
48  */
49 static size_t pem_next ( userptr_t data, size_t len, size_t offset ) {
50  off_t eol;
51 
52  /* Find and skip next newline character, if any */
53  eol = memchr_user ( data, offset, '\n', ( len - offset ) );
54  if ( eol < 0 )
55  return len;
56  return ( eol + 1 );
57 }
58 
59 /**
60  * Locate boundary marker line
61  *
62  * @v data PEM data
63  * @v len Length of PEM data
64  * @v offset Starting offset
65  * @v marker Boundary marker
66  * @ret offset Offset to boundary marker line, or negative error
67  */
68 static int pem_marker ( userptr_t data, size_t len, size_t offset,
69  const char *marker ) {
70  char buf[ strlen ( marker ) ];
71 
72  /* Sanity check */
73  assert ( offset <= len );
74 
75  /* Scan for marker at start of line */
76  while ( offset < len ) {
77 
78  /* Check for marker */
79  if ( ( len - offset ) < sizeof ( buf ) )
80  break;
81  copy_from_user ( buf, data, offset, sizeof ( buf ) );
82  if ( memcmp ( buf, marker, sizeof ( buf ) ) == 0 )
83  return offset;
84 
85  /* Move to next line */
86  offset = pem_next ( data, len, offset );
87  assert ( offset <= len );
88  }
89 
90  return -ENOENT;
91 }
92 
93 /**
94  * Extract ASN.1 object from PEM data
95  *
96  * @v data PEM data
97  * @v len Length of PEM data
98  * @v offset Offset within data
99  * @v cursor ASN.1 cursor to fill in
100  * @ret next Offset to next object, or negative error
101  *
102  * The caller is responsible for eventually calling free() on the
103  * allocated ASN.1 cursor.
104  */
105 int pem_asn1 ( userptr_t data, size_t len, size_t offset,
106  struct asn1_cursor **cursor ) {
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 }
186 
187 /**
188  * Probe PEM image
189  *
190  * @v image PEM image
191  * @ret rc Return status code
192  */
193 static int pem_image_probe ( struct image *image ) {
194  int offset;
195  int rc;
196 
197  /* Check that image contains a BEGIN marker */
198  if ( ( offset = pem_marker ( image->data, image->len, 0,
199  PEM_BEGIN ) ) < 0 ) {
200  rc = offset;
201  DBGC ( image, "PEM %s has no BEGIN marker: %s\n",
202  image->name, strerror ( rc ) );
203  return rc;
204  }
205 
206  return 0;
207 }
208 
209 /**
210  * Extract ASN.1 object from image
211  *
212  * @v image PEM image
213  * @v offset Offset within image
214  * @v cursor ASN.1 cursor to fill in
215  * @ret next Offset to next image, or negative error
216  *
217  * The caller is responsible for eventually calling free() on the
218  * allocated ASN.1 cursor.
219  */
220 static int pem_image_asn1 ( struct image *image, size_t offset,
221  struct asn1_cursor **cursor ) {
222  int next;
223  int rc;
224 
225  /* Extract ASN.1 object */
226  if ( ( next = pem_asn1 ( image->data, image->len, offset,
227  cursor ) ) < 0 ) {
228  rc = next;
229  DBGC ( image, "PEM %s could not extract ASN.1: %s\n",
230  image->name, strerror ( rc ) );
231  return rc;
232  }
233 
234  return next;
235 }
236 
237 /** PEM image type */
238 struct image_type pem_image_type __image_type ( PROBE_NORMAL ) = {
239  .name = "PEM",
240  .probe = pem_image_probe,
241  .asn1 = pem_image_asn1,
242 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
userptr_t data
Raw file image.
Definition: image.h:41
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint32_t next
Next descriptor address.
Definition: myson.h:18
Error codes.
off_t memchr_user(userptr_t userptr, off_t offset, int c, size_t len)
Find character in user buffer.
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
An executable image type.
Definition: image.h:76
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define PROBE_NORMAL
Normal image probe priority.
Definition: image.h:137
int base64_decode(const char *encoded, void *data, size_t len)
Base64-decode string.
Definition: base64.c:91
An executable image.
Definition: image.h:24
Access to external ("user") memory.
struct image_type pem_image_type __image_type(PROBE_NORMAL)
PEM image type.
char * name
Name of this image type.
Definition: image.h:78
#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
static int pem_image_asn1(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: pem.c:220
Assertions.
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
Executable images.
ASN.1 encoding.
PEM-encoded ASN.1 data.
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
size_t len
Length of raw file image.
Definition: image.h:43
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
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
Base64 encoding.
signed long off_t
Definition: stdint.h:8
uint32_t len
Length.
Definition: ena.h:14
#define PEM_END
Post-encapsulation boundary marker.
Definition: pem.h:21
int pem_asn1(userptr_t data, size_t len, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from PEM data.
Definition: pem.c:105
uint32_t end
Ending offset.
Definition: netvsc.h:18
uint8_t data[48]
Additional event data.
Definition: ena.h:22
static int pem_image_probe(struct image *image)
Probe PEM image.
Definition: pem.c:193
#define PEM_BEGIN
Pre-encapsulation boundary marker.
Definition: pem.h:18
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An ASN.1 object cursor.
Definition: asn1.h:20
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33
struct eth_slow_marker_tlv marker
Marker information.
Definition: eth_slow.h:14