1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/**
* @file der_encoder.c
*
* @brief Implementation of der_encoder_t.
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <gmp.h>
#include "der_encoder.h"
#include <utils/allocator.h>
#include <daemon.h>
typedef struct private_der_encoder_t private_der_encoder_t;
/**
* Private data of a der_encoder_t object.
*/
struct private_der_encoder_t {
/**
* Public interface for this signer.
*/
der_encoder_t public;
asn1_rule_t *rule;
asn1_rule_t *first_rule;
void *output;
logger_t *logger;
};
static status_t read_hdr(private_der_encoder_t *this, chunk_t *data);
static status_t read_sequence(private_der_encoder_t *this, chunk_t data)
{
while (this->rule->type != ASN1_END)
{
read_hdr(this, &data);
}
return SUCCESS;
}
static status_t read_int(private_der_encoder_t *this, chunk_t data)
{
this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_INTEGER", data);
u_int *integ = (u_int*)((u_int8_t*)this->output + this->rule->data_offset);
*integ = 0;
while (data.len-- > 0)
{
*integ = 256 * (*integ) + *data.ptr++;
}
return SUCCESS;
}
static status_t read_mpz(private_der_encoder_t *this, chunk_t data)
{
this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_INTEGER as mpz", data);
mpz_t *mpz = (mpz_t*)((u_int8_t*)this->output + this->rule->data_offset);
mpz_import(*mpz, data.len, 1, 1, 1, 0, data.ptr);
return SUCCESS;
}
static u_int32_t read_length(chunk_t *data)
{
u_int8_t n;
size_t len;
/* read first octet of length field */
n = *data->ptr++;
if ((n & 0x80) == 0)
{
/* single length octet */
return n;
}
/* composite length, determine number of length octets */
n &= 0x7f;
if (n > data->len)
{
/* length longer than available bytes */
return -1;
}
if (n > sizeof(len))
{
/* larger than size_t can hold */
return -1;
}
len = 0;
while (n-- > 0)
{
len = 256 * len + *data->ptr++;
}
return len;
}
static status_t read_hdr(private_der_encoder_t *this, chunk_t *data)
{
chunk_t inner;
/* advance to the next rule */
this->rule++;
if (this->rule->type == ASN1_END)
{
return SUCCESS;
}
this->logger->log(this->logger, CONTROL|LEVEL2, "reading header of rule %s",
mapping_find(asn1_type_m, this->rule->type));
this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "reading from:", *data);
/* read type, advance in data */
if (*(data->ptr) != this->rule->type)
{
this->logger->log(this->logger, CONTROL|LEVEL2, "Bad byte found (%x)", *data->ptr);
return PARSE_ERROR;
}
data->ptr++;
data->len--;
/* read length, advance in data */
inner.len = read_length(data);
if (inner.len == -1)
{
this->logger->log(this->logger, CONTROL|LEVEL2, "Error reading length");
return PARSE_ERROR;
}
this->logger->log(this->logger, CONTROL|LEVEL2, "Length is %d",
inner.len);
inner.ptr = data->ptr;
/* advance in data */
data->ptr += inner.len;
data->len -= inner.len;
/* process inner */
switch (this->rule->type)
{
case ASN1_INTEGER:
if (this->rule->flags & ASN1_MPZ)
{
read_mpz(this, inner);
}
else
{
read_int(this, inner);
}
break;
case ASN1_SEQUENCE:
read_sequence(this, inner);
break;
default:
break;
}
return SUCCESS;
}
static status_t decode(private_der_encoder_t *this, chunk_t input, void *output)
{
this->rule = this->first_rule - 1;
this->output = output;
return read_hdr(this, &input);
}
/**
* Implementation of der_encoder.destroy.
*/
static void destroy(private_der_encoder_t *this)
{
allocator_free(this);
}
/*
* Described in header.
*/
der_encoder_t *der_encoder_create(asn1_rule_t *rules)
{
private_der_encoder_t *this = allocator_alloc_thing(private_der_encoder_t);
/* public functions */
this->public.decode = (status_t (*) (der_encoder_t*,chunk_t,void*))decode;
this->public.destroy = (void (*) (der_encoder_t*))destroy;
this->first_rule = rules;
this->logger = charon->logger_manager->get_logger(charon->logger_manager, DER_DECODER);
return &(this->public);
}
|