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