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"
20 typedef struct private_pubkey_cert_t private_pubkey_cert_t
;
23 * private data of pubkey_cert
25 struct private_pubkey_cert_t
{
38 * dummy issuer id, ID_ANY
40 identification_t
*issuer
;
43 * subject, ID_KEY_ID of the public key
45 identification_t
*subject
;
54 * Implementation of certificate_t.get_type
56 static certificate_type_t
get_type(private_pubkey_cert_t
*this)
58 return CERT_TRUSTED_PUBKEY
;
62 * Implementation of certificate_t.get_subject
64 static identification_t
* get_subject(private_pubkey_cert_t
*this)
70 * Implementation of certificate_t.get_issuer
72 static identification_t
* get_issuer(private_pubkey_cert_t
*this)
78 * Implementation of certificate_t.has_subject.
80 static id_match_t
has_subject(private_pubkey_cert_t
*this,
81 identification_t
*subject
)
83 if (subject
->get_type(subject
) == ID_KEY_ID
)
85 cred_encoding_type_t type
;
88 for (type
= 0; type
< CRED_ENCODING_MAX
; type
++)
90 if (this->key
->get_fingerprint(this->key
, type
, &fingerprint
) &&
91 chunk_equals(fingerprint
, subject
->get_encoding(subject
)))
93 return ID_MATCH_PERFECT
;
101 * Implementation of certificate_t.has_subject.
103 static id_match_t
has_issuer(private_pubkey_cert_t
*this,
104 identification_t
*issuer
)
106 return ID_MATCH_NONE
;
110 * Implementation of certificate_t.equals.
112 static bool equals(private_pubkey_cert_t
*this, certificate_t
*other
)
114 public_key_t
*other_key
;
116 other_key
= other
->get_public_key(other
);
119 if (public_key_equals(this->key
, other_key
))
121 other_key
->destroy(other_key
);
124 other_key
->destroy(other_key
);
130 * Implementation of certificate_t.issued_by
132 static bool issued_by(private_pubkey_cert_t
*this, certificate_t
*issuer
)
134 return equals(this, issuer
);
138 * Implementation of certificate_t.get_public_key
140 static public_key_t
* get_public_key(private_pubkey_cert_t
*this)
142 this->key
->get_ref(this->key
);
147 * Implementation of certificate_t.get_validity.
149 static bool get_validity(private_pubkey_cert_t
*this, time_t *when
,
150 time_t *not_before
, time_t *not_after
)
164 * Implementation of certificate_t.get_encoding.
166 static bool get_encoding(private_pubkey_cert_t
*this, cred_encoding_type_t type
,
169 return this->key
->get_encoding(this->key
, type
, encoding
);
173 * Implementation of certificate_t.get_ref
175 static private_pubkey_cert_t
* get_ref(private_pubkey_cert_t
*this)
182 * Implementation of pubkey_cert_t.destroy
184 static void destroy(private_pubkey_cert_t
*this)
186 if (ref_put(&this->ref
))
188 this->subject
->destroy(this->subject
);
189 this->issuer
->destroy(this->issuer
);
190 this->key
->destroy(this->key
);
198 static pubkey_cert_t
*pubkey_cert_create(public_key_t
*key
)
200 private_pubkey_cert_t
*this = malloc_thing(private_pubkey_cert_t
);
203 this->public.interface
.get_type
= (certificate_type_t (*)(certificate_t
*this))get_type
;
204 this->public.interface
.get_subject
= (identification_t
* (*)(certificate_t
*this))get_subject
;
205 this->public.interface
.get_issuer
= (identification_t
* (*)(certificate_t
*this))get_issuer
;
206 this->public.interface
.has_subject
= (id_match_t (*)(certificate_t
*, identification_t
*subject
))has_subject
;
207 this->public.interface
.has_issuer
= (id_match_t (*)(certificate_t
*, identification_t
*issuer
))has_issuer
;
208 this->public.interface
.issued_by
= (bool (*)(certificate_t
*this, certificate_t
*issuer
))issued_by
;
209 this->public.interface
.get_public_key
= (public_key_t
* (*)(certificate_t
*this))get_public_key
;
210 this->public.interface
.get_validity
= (bool (*)(certificate_t
*, time_t *when
, time_t *, time_t*))get_validity
;
211 this->public.interface
.get_encoding
= (bool (*)(certificate_t
*,cred_encoding_type_t
,chunk_t
*))get_encoding
;
212 this->public.interface
.equals
= (bool (*)(certificate_t
*, certificate_t
*other
))equals
;
213 this->public.interface
.get_ref
= (certificate_t
* (*)(certificate_t
*this))get_ref
;
214 this->public.interface
.destroy
= (void (*)(certificate_t
*this))destroy
;
218 this->issuer
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
219 if (key
->get_fingerprint(key
, KEYID_PUBKEY_INFO_SHA1
, &fingerprint
))
221 this->subject
= identification_create_from_encoding(ID_KEY_ID
, fingerprint
);
225 this->subject
= identification_create_from_encoding(ID_ANY
, chunk_empty
);
228 return &this->public;
234 pubkey_cert_t
*pubkey_cert_wrap(certificate_type_t type
, va_list args
)
236 public_key_t
*key
= NULL
;
237 chunk_t blob
= chunk_empty
;
241 switch (va_arg(args
, builder_part_t
))
243 case BUILD_BLOB_ASN1_DER
:
244 blob
= va_arg(args
, chunk_t
);
246 case BUILD_PUBLIC_KEY
:
247 key
= va_arg(args
, public_key_t
*);
262 key
= lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
263 BUILD_BLOB_ASN1_DER
, blob
, BUILD_END
);
267 return pubkey_cert_create(key
);