2 * @file certreq_payload.c
4 * @brief Implementation of certreq_payload_t.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 #include <crypto/hashers/hasher.h>
30 #include "certreq_payload.h"
33 typedef struct private_certreq_payload_t private_certreq_payload_t
;
36 * Private data of an certreq_payload_t object.
39 struct private_certreq_payload_t
{
41 * Public certreq_payload_t interface.
43 certreq_payload_t
public;
48 u_int8_t next_payload
;
56 * Length of this payload.
58 u_int16_t payload_length
;
61 * Encoding of the CERT Data.
63 u_int8_t cert_encoding
;
66 * The contained certreq data value.
72 * Encoding rules to parse or generate a CERTREQ payload
74 * The defined offsets are the positions in a object of type
75 * private_certreq_payload_t.
78 encoding_rule_t certreq_payload_encodings
[] = {
79 /* 1 Byte next payload type, stored in the field next_payload */
80 { U_INT_8
, offsetof(private_certreq_payload_t
, next_payload
) },
81 /* the critical bit */
82 { FLAG
, offsetof(private_certreq_payload_t
, critical
) },
83 /* 7 Bit reserved bits, nowhere stored */
91 /* Length of the whole payload*/
92 { PAYLOAD_LENGTH
, offsetof(private_certreq_payload_t
, payload_length
)},
93 /* 1 Byte CERTREQ type*/
94 { U_INT_8
, offsetof(private_certreq_payload_t
, cert_encoding
)},
95 /* some certreq data bytes, length is defined in PAYLOAD_LENGTH */
96 { CERTREQ_DATA
, offsetof(private_certreq_payload_t
, certreq_data
)}
101 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
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 ! Next Payload !C! RESERVED ! Payload Length !
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 ~ Certification Authority ~
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 * Implementation of payload_t.verify.
115 static status_t
verify(private_certreq_payload_t
*this)
117 if ((this->cert_encoding
== 0) ||
118 ((this->cert_encoding
>= 14) && (this->cert_encoding
<= 200)))
127 * Implementation of certreq_payload_t.get_encoding_rules.
129 static void get_encoding_rules(private_certreq_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
131 *rules
= certreq_payload_encodings
;
132 *rule_count
= sizeof(certreq_payload_encodings
) / sizeof(encoding_rule_t
);
136 * Implementation of payload_t.get_type.
138 static payload_type_t
get_payload_type(private_certreq_payload_t
*this)
140 return CERTIFICATE_REQUEST
;
144 * Implementation of payload_t.get_next_type.
146 static payload_type_t
get_next_type(private_certreq_payload_t
*this)
148 return (this->next_payload
);
152 * Implementation of payload_t.set_next_type.
154 static void set_next_type(private_certreq_payload_t
*this,payload_type_t type
)
156 this->next_payload
= type
;
160 * Implementation of payload_t.get_length.
162 static size_t get_length(private_certreq_payload_t
*this)
164 return this->payload_length
;
168 * Implementation of certreq_payload_t.set_cert_encoding.
170 static void set_cert_encoding (private_certreq_payload_t
*this, cert_encoding_t encoding
)
172 this->cert_encoding
= encoding
;
176 * Implementation of certreq_payload_t.get_cert_encoding.
178 static cert_encoding_t
get_cert_encoding (private_certreq_payload_t
*this)
180 return (this->cert_encoding
);
184 * Implementation of certreq_payload_t.set_data.
186 static void set_data (private_certreq_payload_t
*this, chunk_t data
)
188 if (this->certreq_data
.ptr
!= NULL
)
190 chunk_free(&(this->certreq_data
));
192 this->certreq_data
.ptr
= clalloc(data
.ptr
,data
.len
);
193 this->certreq_data
.len
= data
.len
;
194 this->payload_length
= CERTREQ_PAYLOAD_HEADER_LENGTH
+ this->certreq_data
.len
;
198 * Implementation of certreq_payload_t.get_data.
200 static chunk_t
get_data (private_certreq_payload_t
*this)
202 return (this->certreq_data
);
206 * Implementation of certreq_payload_t.get_data_clone.
208 static chunk_t
get_data_clone (private_certreq_payload_t
*this)
211 if (this->certreq_data
.ptr
== NULL
)
213 return (this->certreq_data
);
215 cloned_data
.ptr
= clalloc(this->certreq_data
.ptr
,this->certreq_data
.len
);
216 cloned_data
.len
= this->certreq_data
.len
;
221 * Implementation of payload_t.destroy and certreq_payload_t.destroy.
223 static void destroy(private_certreq_payload_t
*this)
225 if (this->certreq_data
.ptr
!= NULL
)
227 chunk_free(&(this->certreq_data
));
234 * Described in header
236 certreq_payload_t
*certreq_payload_create()
238 private_certreq_payload_t
*this = malloc_thing(private_certreq_payload_t
);
240 /* interface functions */
241 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
242 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*,encoding_rule_t
**,size_t*))get_encoding_rules
;
243 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*))get_length
;
244 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*))get_next_type
;
245 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
))set_next_type
;
246 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*))get_payload_type
;
247 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
249 /* public functions */
250 this->public.destroy
= (void (*) (certreq_payload_t
*)) destroy
;
251 this->public.set_cert_encoding
= (void (*) (certreq_payload_t
*,cert_encoding_t
))set_cert_encoding
;
252 this->public.get_cert_encoding
= (cert_encoding_t (*) (certreq_payload_t
*))get_cert_encoding
;
253 this->public.set_data
= (void (*) (certreq_payload_t
*,chunk_t
))set_data
;
254 this->public.get_data_clone
= (chunk_t (*) (certreq_payload_t
*))get_data_clone
;
255 this->public.get_data
= (chunk_t (*) (certreq_payload_t
*))get_data
;
257 /* private variables */
258 this->critical
= FALSE
;
259 this->next_payload
= NO_PAYLOAD
;
260 this->payload_length
=CERTREQ_PAYLOAD_HEADER_LENGTH
;
261 this->certreq_data
= CHUNK_INITIALIZER
;
263 return (&(this->public));
267 * Described in header
269 certreq_payload_t
*certreq_payload_create_from_cacert(identification_t
*id
)
271 x509_t
*cacert
= charon
->credentials
->get_ca_certificate(charon
->credentials
, id
);
272 rsa_public_key_t
*pubkey
= cacert
->get_public_key(cacert
);
273 chunk_t keyid
= pubkey
->get_keyid(pubkey
);
275 certreq_payload_t
*this = certreq_payload_create();
277 DBG1(DBG_IKE
, "request certificate issued by '%D'", id
);
278 DBG2(DBG_IKE
, " with keyid %#B", &keyid
);
280 this->set_cert_encoding(this, CERT_X509_SIGNATURE
);
281 this->set_data(this, keyid
);
286 * Described in header
288 certreq_payload_t
*certreq_payload_create_from_cacerts(void)
290 certreq_payload_t
*this;
295 iterator_t
*iterator
= charon
->credentials
->create_cacert_iterator(charon
->credentials
);
296 int count
= iterator
->get_count(iterator
);
301 this = certreq_payload_create();
302 keyids
= chunk_alloc(count
* HASH_SIZE_SHA1
);
305 while (iterator
->iterate(iterator
, (void**)&cacert
))
307 rsa_public_key_t
*pubkey
= cacert
->get_public_key(cacert
);
308 chunk_t keyid
= pubkey
->get_keyid(pubkey
);
310 DBG1(DBG_IKE
, "request certificate issued by '%D'", cacert
->get_subject(cacert
));
311 DBG2(DBG_IKE
, " with keyid %#B", &keyid
);
312 memcpy(pos
, keyid
.ptr
, keyid
.len
);
313 pos
+= HASH_SIZE_SHA1
;
315 iterator
->destroy(iterator
);
317 this->set_cert_encoding(this, CERT_X509_SIGNATURE
);
318 this->set_data(this, keyids
);