iPXE
der.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 <string.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <ipxe/asn1.h>
31 #include <ipxe/der.h>
32 #include <ipxe/image.h>
33 
34 /** @file
35  *
36  * DER-encoded ASN.1 data
37  *
38  */
39 
40 /**
41  * Extract ASN.1 object from DER data
42  *
43  * @v data DER data
44  * @v len Length of DER data
45  * @v offset Offset within data
46  * @v cursor ASN.1 cursor to fill in
47  * @ret next Offset to next object, or negative error
48  *
49  * The caller is responsible for eventually calling free() on the
50  * allocated ASN.1 cursor.
51  */
52 int der_asn1 ( const void *data, size_t len, size_t offset,
53  struct asn1_cursor **cursor ) {
54  size_t remaining;
55  void *raw;
56 
57  /* Sanity check */
58  assert ( offset <= len );
59  remaining = ( len - offset );
60 
61  /* Allocate cursor and data buffer */
62  *cursor = malloc ( sizeof ( **cursor ) + remaining );
63  if ( ! *cursor )
64  return -ENOMEM;
65  raw = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
66 
67  /* Populate cursor and data buffer */
68  (*cursor)->data = raw;
69  (*cursor)->len = remaining;
70  memcpy ( raw, ( data + offset ), remaining );
71 
72  /* Shrink cursor */
73  asn1_shrink_any ( *cursor );
74 
75  return ( offset + (*cursor)->len );
76 }
77 
78 /**
79  * Probe DER image
80  *
81  * @v image DER image
82  * @ret rc Return status code
83  */
84 static int der_image_probe ( struct image *image ) {
85  struct asn1_cursor cursor;
86  int rc;
87 
88  /* Prepare cursor */
89  cursor.data = image->data;
90  cursor.len = image->len;
91 
92  /* Check that image begins with an ASN.1 sequence object */
93  if ( ( rc = asn1_skip ( &cursor, ASN1_SEQUENCE ) ) != 0 ) {
94  DBGC ( image, "DER %s is not valid ASN.1: %s\n",
95  image->name, strerror ( rc ) );
96  return rc;
97  }
98 
99  /* Check that image comprises a single well-formed ASN.1 object */
100  if ( cursor.len ) {
101  DBGC ( image, "DER %s is not single ASN.1\n", image->name );
102  return -ENOEXEC;
103  }
104 
105  return 0;
106 }
107 
108 /**
109  * Extract ASN.1 object from DER image
110  *
111  * @v image DER image
112  * @v offset Offset within image
113  * @v cursor ASN.1 cursor to fill in
114  * @ret next Offset to next image, or negative error
115  *
116  * The caller is responsible for eventually calling free() on the
117  * allocated ASN.1 cursor.
118  */
119 static int der_image_asn1 ( struct image *image, size_t offset,
120  struct asn1_cursor **cursor ) {
121  int next;
122  int rc;
123 
124  /* Extract ASN.1 object */
125  if ( ( next = der_asn1 ( image->data, image->len, offset,
126  cursor ) ) < 0 ) {
127  rc = next;
128  DBGC ( image, "DER %s could not extract ASN.1: %s\n",
129  image->name, strerror ( rc ) );
130  return rc;
131  }
132 
133  return next;
134 }
135 
136 /** DER image type */
137 struct image_type der_image_type __image_type ( PROBE_NORMAL ) = {
138  .name = "DER",
139  .probe = der_image_probe,
140  .asn1 = der_image_asn1,
141 };
static int der_image_asn1(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from DER image.
Definition: der.c:119
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Error codes.
const void * data
Read-only data.
Definition: image.h:50
#define ENOEXEC
Exec format error.
Definition: errno.h:519
const void * data
Start of data.
Definition: asn1.h:22
#define DBGC(...)
Definition: compiler.h:505
An executable image type.
Definition: image.h:94
#define PROBE_NORMAL
Normal image probe priority.
Definition: image.h:155
An executable image.
Definition: image.h:23
char * name
Name of this image type.
Definition: image.h:96
size_t len
Length of data.
Definition: asn1.h:24
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
Executable images.
ASN.1 encoding.
ring len
Length.
Definition: dwmac.h:231
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length of raw file image.
Definition: image.h:55
int asn1_shrink_any(struct asn1_cursor *cursor)
Shrink ASN.1 object of any type.
Definition: asn1.c:312
DER image format.
#define ASN1_SEQUENCE
ASN.1 sequence.
Definition: asn1.h:89
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:620
static int der_image_probe(struct image *image)
Probe DER image.
Definition: der.c:84
int asn1_skip(struct asn1_cursor *cursor, unsigned int type)
Skip ASN.1 object.
Definition: asn1.c:243
uint8_t data[48]
Additional event data.
Definition: ena.h:22
__be32 raw[7]
Definition: CIB_PRM.h:28
int der_asn1(const void *data, size_t len, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from DER data.
Definition: der.c:52
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
char * name
Name.
Definition: image.h:37
String functions.
An ASN.1 object cursor.
Definition: asn1.h:20
struct image_type der_image_type __image_type(PROBE_NORMAL)
DER image type.