2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "pubkey_cert.h"
22 typedef struct private_pubkey_cert_t private_pubkey_cert_t
;
25 * private data of pubkey_cert
27 struct private_pubkey_cert_t
{
40 * dummy issuer id, ID_ANY
42 identification_t
*issuer
;
45 * subject, ID_KEY_ID of the public key
47 identification_t
*subject
;
65 METHOD(certificate_t
, get_type
, certificate_type_t
,
66 private_pubkey_cert_t
*this)
68 return CERT_TRUSTED_PUBKEY
;
71 METHOD(certificate_t
, get_subject
, identification_t
*,
72 private_pubkey_cert_t
*this)
77 METHOD(certificate_t
, get_issuer
, identification_t
*,
78 private_pubkey_cert_t
*this)
83 METHOD(certificate_t
, has_subject
, id_match_t
,
84 private_pubkey_cert_t
*this, identification_t
*subject
)
86 if (subject
->get_type(subject
) == ID_KEY_ID
)
88 cred_encoding_type_t type
;
91 for (type
= 0; type
< CRED_ENCODING_MAX
; type
++)
93 if (this->key
->get_fingerprint(this->key
, type
, &fingerprint
) &&
94 chunk_equals(fingerprint
, subject
->get_encoding(subject
)))
96 return ID_MATCH_PERFECT
;
101 return this->subject
->matches(this->subject
, subject
);
104 METHOD(certificate_t
, has_issuer
, id_match_t
,
105 private_pubkey_cert_t
*this, identification_t
*issuer
)
107 return ID_MATCH_NONE
;
110 METHOD(certificate_t
, equals
, bool,
111 private_pubkey_cert_t
*this, certificate_t
*other
)
113 public_key_t
*other_key
;
115 other_key
= other
->get_public_key(other
);
118 if (public_key_equals(this->key
, other_key
))
120 other_key
->destroy(other_key
);
123 other_key
->destroy(other_key
);
128 METHOD(certificate_t
, issued_by
, bool,
129 private_pubkey_cert_t
*this, certificate_t
*issuer
)
131 return equals(this, issuer
);
134 METHOD(certificate_t
, get_public_key
, public_key_t
*,
135 private_pubkey_cert_t
*this)
137 this->key
->get_ref(this->key
);
141 METHOD(certificate_t
, get_validity
, bool,
142 private_pubkey_cert_t
*this, time_t *when
, time_t *not_before
,
145 time_t t
= when ?
*when
: time(NULL
);
149 *not_before
= this->notBefore
;
153 *not_after
= this->notAfter
;
155 return ((this->notBefore
== UNDEFINED_TIME
|| t
>= this->notBefore
) &&
156 (this->notAfter
== UNDEFINED_TIME
|| t
<= this->notAfter
));
159 METHOD(certificate_t
, get_encoding
, bool,
160 private_pubkey_cert_t
*this, cred_encoding_type_t type
, chunk_t
*encoding
)
162 return this->key
->get_encoding(this->key
, type
, encoding
);
165 METHOD(certificate_t
, get_ref
, certificate_t
*,
166 private_pubkey_cert_t
*this)
169 return &this->public.interface
;
172 METHOD(certificate_t
, destroy
, void,
173 private_pubkey_cert_t
*this)
175 if (ref_put(&this->ref
))
177 this->subject
->destroy(this->subject
);
178 this->issuer
->destroy(this->issuer
);
179 this->key
->destroy(this->key
);
187 static pubkey_cert_t
*pubkey_cert_create(public_key_t
*key
,
188 time_t notBefore
, time_t notAfter
,
189 identification_t
*subject
)
191 private_pubkey_cert_t
*this;
197 .get_type
= _get_type
,
198 .get_subject
= _get_subject
,
199 .get_issuer
= _get_issuer
,
200 .has_subject
= _has_subject
,
201 .has_issuer
= _has_issuer
,
202 .issued_by
= _issued_by
,
203 .get_public_key
= _get_public_key
,
204 .get_validity
= _get_validity
,
205 .get_encoding
= _get_encoding
,
213 .notBefore
= notBefore
,
214 .notAfter
= notAfter
,
215 .issuer
= identification_create_from_encoding(ID_ANY
, chunk_empty
),
220 this->subject
= subject
->clone(subject
);
222 else if (key
->get_fingerprint(key
, KEYID_PUBKEY_INFO_SHA1
, &fingerprint
))
224 this->subject
= identification_create_from_encoding(ID_KEY_ID
, fingerprint
);
228 this->subject
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
231 return &this->public;
237 pubkey_cert_t
*pubkey_cert_wrap(certificate_type_t type
, va_list args
)
239 public_key_t
*key
= NULL
;
240 chunk_t blob
= chunk_empty
;
241 identification_t
*subject
= NULL
;
242 time_t notBefore
= UNDEFINED_TIME
, notAfter
= UNDEFINED_TIME
;
246 switch (va_arg(args
, builder_part_t
))
248 case BUILD_BLOB_ASN1_DER
:
249 blob
= va_arg(args
, chunk_t
);
251 case BUILD_PUBLIC_KEY
:
252 key
= va_arg(args
, public_key_t
*);
254 case BUILD_NOT_BEFORE_TIME
:
255 notBefore
= va_arg(args
, time_t);
257 case BUILD_NOT_AFTER_TIME
:
258 notAfter
= va_arg(args
, time_t);
261 subject
= va_arg(args
, identification_t
*);
276 key
= lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
277 BUILD_BLOB_ASN1_DER
, blob
, BUILD_END
);
281 return pubkey_cert_create(key
, notBefore
, notAfter
, subject
);