]> git.ipfire.org Git - people/ms/strongswan.git/blob - programs/charon/charon/encoding/payloads/cert_payload.c
- renamed get_block_size of hasher
[people/ms/strongswan.git] / programs / charon / charon / encoding / payloads / cert_payload.c
1 /**
2 * @file cert_payload.c
3 *
4 * @brief Implementation of cert_payload_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stddef.h>
24
25 #include "cert_payload.h"
26
27
28 /**
29 * String mappings for cert_encoding_t.
30 */
31 mapping_t cert_encoding_m[] = {
32 {PKCS7_WRAPPED_X509_CERTIFICATE, "PKCS7_WRAPPED_X509_CERTIFICATE"},
33 {PGP_CERTIFICATE, "PGP_CERTIFICATE"},
34 {DNS_SIGNED_KEY, "DNS_SIGNED_KEY"},
35 {X509_CERTIFICATE_SIGNATURE, "X509_CERTIFICATE_SIGNATURE"},
36 {KERBEROS_TOKEN, "KERBEROS_TOKEN"},
37 {CERTIFICATE_REVOCATION_LIST, "CERTIFICATE_REVOCATION_LIST"},
38 {AUTHORITY_REVOCATION_LIST, "AUTHORITY_REVOCATION_LIST"},
39 {SPKI_CERTIFICATE, "SPKI_CERTIFICATE"},
40 {X509_CERTIFICATE_ATTRIBUTE, "X509_CERTIFICATE_ATTRIBUTE"},
41 {RAW_SA_KEY, "RAW_SA_KEY"},
42 {HASH_AND_URL_X509_CERTIFICATE, "HASH_AND_URL_X509_CERTIFICATE"},
43 {HASH_AND_URL_X509_BUNDLE, "HASH_AND_URL_X509_BUNDLE"},
44 {MAPPING_END, NULL}
45 };
46
47
48 typedef struct private_cert_payload_t private_cert_payload_t;
49
50 /**
51 * Private data of an cert_payload_t object.
52 *
53 */
54 struct private_cert_payload_t {
55 /**
56 * Public cert_payload_t interface.
57 */
58 cert_payload_t public;
59
60 /**
61 * Next payload type.
62 */
63 u_int8_t next_payload;
64
65 /**
66 * Critical flag.
67 */
68 bool critical;
69
70 /**
71 * Length of this payload.
72 */
73 u_int16_t payload_length;
74
75 /**
76 * Encoding of the CERT Data.
77 */
78 u_int8_t cert_encoding;
79
80 /**
81 * The contained cert data value.
82 */
83 chunk_t cert_data;
84 };
85
86 /**
87 * Encoding rules to parse or generate a CERT payload
88 *
89 * The defined offsets are the positions in a object of type
90 * private_cert_payload_t.
91 *
92 */
93 encoding_rule_t cert_payload_encodings[] = {
94 /* 1 Byte next payload type, stored in the field next_payload */
95 { U_INT_8, offsetof(private_cert_payload_t, next_payload) },
96 /* the critical bit */
97 { FLAG, offsetof(private_cert_payload_t, critical) },
98 /* 7 Bit reserved bits, nowhere stored */
99 { RESERVED_BIT, 0 },
100 { RESERVED_BIT, 0 },
101 { RESERVED_BIT, 0 },
102 { RESERVED_BIT, 0 },
103 { RESERVED_BIT, 0 },
104 { RESERVED_BIT, 0 },
105 { RESERVED_BIT, 0 },
106 /* Length of the whole payload*/
107 { PAYLOAD_LENGTH, offsetof(private_cert_payload_t, payload_length)},
108 /* 1 Byte CERT type*/
109 { U_INT_8, offsetof(private_cert_payload_t, cert_encoding) },
110 /* some cert data bytes, length is defined in PAYLOAD_LENGTH */
111 { CERT_DATA, offsetof(private_cert_payload_t, cert_data) }
112 };
113
114 /*
115 1 2 3
116 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 ! Next Payload !C! RESERVED ! Payload Length !
119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 ! Cert Encoding ! !
121 +-+-+-+-+-+-+-+-+ !
122 ~ Certificate Data ~
123 ! !
124 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 */
126
127 /**
128 * Implementation of payload_t.verify.
129 */
130 static status_t verify(private_cert_payload_t *this)
131 {
132 if ((this->cert_encoding == 0) ||
133 ((this->cert_encoding >= 14) && (this->cert_encoding <= 200)))
134 {
135 /* reserved IDs */
136 return FAILED;
137 }
138 return SUCCESS;
139 }
140
141 /**
142 * Implementation of cert_payload_t.get_encoding_rules.
143 */
144 static void get_encoding_rules(private_cert_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
145 {
146 *rules = cert_payload_encodings;
147 *rule_count = sizeof(cert_payload_encodings) / sizeof(encoding_rule_t);
148 }
149
150 /**
151 * Implementation of payload_t.get_type.
152 */
153 static payload_type_t get_payload_type(private_cert_payload_t *this)
154 {
155 return CERTIFICATE;
156 }
157
158 /**
159 * Implementation of payload_t.get_next_type.
160 */
161 static payload_type_t get_next_type(private_cert_payload_t *this)
162 {
163 return (this->next_payload);
164 }
165
166 /**
167 * Implementation of payload_t.set_next_type.
168 */
169 static void set_next_type(private_cert_payload_t *this,payload_type_t type)
170 {
171 this->next_payload = type;
172 }
173
174 /**
175 * Implementation of payload_t.get_length.
176 */
177 static size_t get_length(private_cert_payload_t *this)
178 {
179 return this->payload_length;
180 }
181
182 /**
183 * Implementation of cert_payload_t.set_cert_encoding.
184 */
185 static void set_cert_encoding (private_cert_payload_t *this, cert_encoding_t encoding)
186 {
187 this->cert_encoding = encoding;
188 }
189
190 /**
191 * Implementation of cert_payload_t.get_cert_encoding.
192 */
193 static cert_encoding_t get_cert_encoding (private_cert_payload_t *this)
194 {
195 return (this->cert_encoding);
196 }
197
198 /**
199 * Implementation of cert_payload_t.set_data.
200 */
201 static void set_data (private_cert_payload_t *this, chunk_t data)
202 {
203 if (this->cert_data.ptr != NULL)
204 {
205 chunk_free(&(this->cert_data));
206 }
207 this->cert_data.ptr = clalloc(data.ptr,data.len);
208 this->cert_data.len = data.len;
209 this->payload_length = CERT_PAYLOAD_HEADER_LENGTH + this->cert_data.len;
210 }
211
212 /**
213 * Implementation of cert_payload_t.get_data.
214 */
215 static chunk_t get_data (private_cert_payload_t *this)
216 {
217 return (this->cert_data);
218 }
219
220 /**
221 * Implementation of cert_payload_t.get_data_clone.
222 */
223 static chunk_t get_data_clone (private_cert_payload_t *this)
224 {
225 chunk_t cloned_data;
226 if (this->cert_data.ptr == NULL)
227 {
228 return (this->cert_data);
229 }
230 cloned_data.ptr = clalloc(this->cert_data.ptr,this->cert_data.len);
231 cloned_data.len = this->cert_data.len;
232 return cloned_data;
233 }
234
235 /**
236 * Implementation of payload_t.destroy and cert_payload_t.destroy.
237 */
238 static void destroy(private_cert_payload_t *this)
239 {
240 if (this->cert_data.ptr != NULL)
241 {
242 chunk_free(&(this->cert_data));
243 }
244
245 free(this);
246 }
247
248 /*
249 * Described in header
250 */
251 cert_payload_t *cert_payload_create()
252 {
253 private_cert_payload_t *this = malloc_thing(private_cert_payload_t);
254
255 /* interface functions */
256 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
257 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
258 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
259 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
260 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
261 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
262 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
263
264 /* public functions */
265 this->public.destroy = (void (*) (cert_payload_t *)) destroy;
266 this->public.set_cert_encoding = (void (*) (cert_payload_t *,cert_encoding_t)) set_cert_encoding;
267 this->public.get_cert_encoding = (cert_encoding_t (*) (cert_payload_t *)) get_cert_encoding;
268 this->public.set_data = (void (*) (cert_payload_t *,chunk_t)) set_data;
269 this->public.get_data_clone = (chunk_t (*) (cert_payload_t *)) get_data_clone;
270 this->public.get_data = (chunk_t (*) (cert_payload_t *)) get_data;
271
272 /* private variables */
273 this->critical = FALSE;
274 this->next_payload = NO_PAYLOAD;
275 this->payload_length =CERT_PAYLOAD_HEADER_LENGTH;
276 this->cert_data = CHUNK_INITIALIZER;
277
278 return (&(this->public));
279 }