2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2008 Tobias Brunner
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "openssl_ec_public_key.h"
18 #include "openssl_util.h"
22 #include <openssl/evp.h>
23 #include <openssl/ecdsa.h>
24 #include <openssl/x509.h>
26 typedef struct private_openssl_ec_public_key_t private_openssl_ec_public_key_t
;
29 * Private data structure with signing context.
31 struct private_openssl_ec_public_key_t
{
33 * Public interface for this signer.
35 openssl_ec_public_key_t
public;
49 * Verification of a signature as in RFC 4754
51 static bool verify_signature(private_openssl_ec_public_key_t
*this,
52 int hash_type
, chunk_t data
, chunk_t signature
)
59 if (hash_type
!= NID_undef
)
61 if (!openssl_hash_chunk(hash_type
, data
, &hash
))
66 sig
= ECDSA_SIG_new();
69 /* split the signature chunk in r and s */
70 if (openssl_bn_split(signature
, sig
->r
, sig
->s
))
72 valid
= (ECDSA_do_verify(hash
.ptr
, hash
.len
, sig
, this->ec
) == 1);
76 if (hash_type
!= NID_undef
)
84 * Verification of a DER encoded signature as in RFC 3279
86 static bool verify_der_signature(private_openssl_ec_public_key_t
*this,
87 int nid
, chunk_t data
, chunk_t signature
)
92 /* remove any preceding 0-bytes from signature */
93 while (signature
.len
&& signature
.ptr
[0] == 0x00)
95 signature
= chunk_skip(signature
, 1);
97 if (openssl_hash_chunk(nid
, data
, &hash
))
99 valid
= ECDSA_verify(0, hash
.ptr
, hash
.len
,
100 signature
.ptr
, signature
.len
, this->ec
);
107 * Implementation of public_key_t.get_type.
109 static key_type_t
get_type(private_openssl_ec_public_key_t
*this)
115 * Implementation of public_key_t.verify.
117 static bool verify(private_openssl_ec_public_key_t
*this, signature_scheme_t scheme
,
118 chunk_t data
, chunk_t signature
)
122 case SIGN_ECDSA_WITH_SHA1_DER
:
123 return verify_der_signature(this, NID_sha1
, data
, signature
);
124 case SIGN_ECDSA_WITH_SHA256_DER
:
125 return verify_der_signature(this, NID_sha256
, data
, signature
);
126 case SIGN_ECDSA_WITH_SHA384_DER
:
127 return verify_der_signature(this, NID_sha384
, data
, signature
);
128 case SIGN_ECDSA_WITH_SHA512_DER
:
129 return verify_der_signature(this, NID_sha512
, data
, signature
);
130 case SIGN_ECDSA_WITH_NULL
:
131 return verify_signature(this, NID_undef
, data
, signature
);
133 return verify_signature(this, NID_sha256
, data
, signature
);
135 return verify_signature(this, NID_sha384
, data
, signature
);
137 return verify_signature(this, NID_sha512
, data
, signature
);
139 DBG1("signature scheme %N not supported in EC",
140 signature_scheme_names
, scheme
);
146 * Implementation of public_key_t.get_keysize.
148 static bool encrypt_(private_openssl_ec_public_key_t
*this,
149 chunk_t crypto
, chunk_t
*plain
)
151 DBG1("EC public key encryption not implemented");
156 * Implementation of public_key_t.get_keysize.
158 static size_t get_keysize(private_openssl_ec_public_key_t
*this)
160 return EC_FIELD_ELEMENT_LEN(EC_KEY_get0_group(this->ec
));
164 * Calculate fingerprint from a EC_KEY, also used in ec private key.
166 bool openssl_ec_fingerprint(EC_KEY
*ec
, key_encoding_type_t type
, chunk_t
*fp
)
172 if (lib
->encoding
->get_cache(lib
->encoding
, type
, ec
, fp
))
178 case KEY_ID_PUBKEY_SHA1
:
179 key
= chunk_alloc(i2o_ECPublicKey(ec
, NULL
));
181 i2o_ECPublicKey(ec
, &p
);
183 case KEY_ID_PUBKEY_INFO_SHA1
:
184 key
= chunk_alloc(i2d_EC_PUBKEY(ec
, NULL
));
186 i2d_EC_PUBKEY(ec
, &p
);
191 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
194 DBG1("SHA1 hash algorithm not supported, fingerprinting failed");
198 hasher
->allocate_hash(hasher
, key
, fp
);
199 hasher
->destroy(hasher
);
201 lib
->encoding
->cache(lib
->encoding
, type
, ec
, *fp
);
206 * Implementation of private_key_t.get_fingerprint.
208 static bool get_fingerprint(private_openssl_ec_public_key_t
*this,
209 key_encoding_type_t type
, chunk_t
*fingerprint
)
211 return openssl_ec_fingerprint(this->ec
, type
, fingerprint
);
215 * Implementation of private_key_t.get_encoding.
217 static bool get_encoding(private_openssl_ec_public_key_t
*this,
218 key_encoding_type_t type
, chunk_t
*encoding
)
224 case KEY_PUB_SPKI_ASN1_DER
:
226 *encoding
= chunk_alloc(i2d_EC_PUBKEY(this->ec
, NULL
));
228 i2d_EC_PUBKEY(this->ec
, &p
);
237 * Implementation of public_key_t.get_ref.
239 static public_key_t
* get_ref(private_openssl_ec_public_key_t
*this)
242 return &this->public.interface
;
246 * Implementation of openssl_ec_public_key.destroy.
248 static void destroy(private_openssl_ec_public_key_t
*this)
250 if (ref_put(&this->ref
))
254 lib
->encoding
->clear_cache(lib
->encoding
, this->ec
);
255 EC_KEY_free(this->ec
);
262 * Generic private constructor
264 static private_openssl_ec_public_key_t
*create_empty()
266 private_openssl_ec_public_key_t
*this = malloc_thing(private_openssl_ec_public_key_t
);
268 this->public.interface
.get_type
= (key_type_t (*)(public_key_t
*this))get_type
;
269 this->public.interface
.verify
= (bool (*)(public_key_t
*this, signature_scheme_t scheme
, chunk_t data
, chunk_t signature
))verify
;
270 this->public.interface
.encrypt
= (bool (*)(public_key_t
*this, chunk_t crypto
, chunk_t
*plain
))encrypt_
;
271 this->public.interface
.get_keysize
= (size_t (*) (public_key_t
*this))get_keysize
;
272 this->public.interface
.equals
= public_key_equals
;
273 this->public.interface
.get_fingerprint
= (bool(*)(public_key_t
*, key_encoding_type_t type
, chunk_t
*fp
))get_fingerprint
;
274 this->public.interface
.get_encoding
= (bool(*)(public_key_t
*, key_encoding_type_t type
, chunk_t
*encoding
))get_encoding
;
275 this->public.interface
.get_ref
= (public_key_t
* (*)(public_key_t
*this))get_ref
;
276 this->public.interface
.destroy
= (void (*)(public_key_t
*this))destroy
;
285 * Load a public key from an ASN1 encoded blob
287 static openssl_ec_public_key_t
*load(chunk_t blob
)
289 private_openssl_ec_public_key_t
*this = create_empty();
290 u_char
*p
= blob
.ptr
;
292 this->ec
= d2i_EC_PUBKEY(NULL
, (const u_char
**)&p
, blob
.len
);
299 return &this->public;
302 typedef struct private_builder_t private_builder_t
;
305 * Builder implementation for key loading
307 struct private_builder_t
{
308 /** implements the builder interface */
310 /** loaded public key */
311 openssl_ec_public_key_t
*key
;
315 * Implementation of builder_t.build
317 static openssl_ec_public_key_t
*build(private_builder_t
*this)
319 openssl_ec_public_key_t
*key
= this->key
;
326 * Implementation of builder_t.add
328 static void add(private_builder_t
*this, builder_part_t part
, ...)
336 case BUILD_BLOB_ASN1_DER
:
338 va_start(args
, part
);
339 this->key
= load(va_arg(args
, chunk_t
));
349 destroy((private_openssl_ec_public_key_t
*)this->key
);
351 builder_cancel(&this->public);
355 * Builder construction function
357 builder_t
*openssl_ec_public_key_builder(key_type_t type
)
359 private_builder_t
*this;
361 if (type
!= KEY_ECDSA
)
366 this = malloc_thing(private_builder_t
);
369 this->public.add
= (void(*)(builder_t
*this, builder_part_t part
, ...))add
;
370 this->public.build
= (void*(*)(builder_t
*this))build
;
372 return &this->public;