[fdt] Add ability to parse a MAC address from a flattened device tree
[ipxe.git] / src / image / pem.c
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 };