openssl_ed_public_key.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2018 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  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>.
  9. *
  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
  13. * for more details.
  14. */
  15. #include <openssl/evp.h>
  16. #if OPENSSL_VERSION_NUMBER >= 0x1010100fL && !defined(OPENSSL_NO_EC)
  17. #include <openssl/x509.h>
  18. #include "openssl_ed_public_key.h"
  19. #include <utils/debug.h>
  20. typedef struct private_public_key_t private_public_key_t;
  21. /**
  22. * Private data
  23. */
  24. struct private_public_key_t {
  25. /**
  26. * Public interface
  27. */
  28. public_key_t public;
  29. /**
  30. * Key object
  31. */
  32. EVP_PKEY *key;
  33. /**
  34. * Key type
  35. */
  36. key_type_t type;
  37. /**
  38. * Reference counter
  39. */
  40. refcount_t ref;
  41. };
  42. /**
  43. * Map a key type to an EVP key type
  44. */
  45. int openssl_ed_key_type(key_type_t type)
  46. {
  47. switch (type)
  48. {
  49. case KEY_ED25519:
  50. return EVP_PKEY_ED25519;
  51. case KEY_ED448:
  52. return EVP_PKEY_ED448;
  53. default:
  54. return 0;
  55. }
  56. }
  57. /**
  58. * Map a key type to a key size
  59. */
  60. int openssl_ed_keysize(key_type_t type)
  61. {
  62. switch (type)
  63. {
  64. case KEY_ED25519:
  65. return 32 * 8;
  66. case KEY_ED448:
  67. return 57 * 8;
  68. default:
  69. return 0;
  70. }
  71. }
  72. METHOD(public_key_t, get_type, key_type_t,
  73. private_public_key_t *this)
  74. {
  75. return this->type;
  76. }
  77. METHOD(public_key_t, verify, bool,
  78. private_public_key_t *this, signature_scheme_t scheme,
  79. void *params, chunk_t data, chunk_t signature)
  80. {
  81. EVP_MD_CTX *ctx;
  82. if ((this->type == KEY_ED25519 && scheme != SIGN_ED25519) ||
  83. (this->type == KEY_ED448 && scheme != SIGN_ED448))
  84. {
  85. DBG1(DBG_LIB, "signature scheme %N not supported by %N key",
  86. signature_scheme_names, scheme, key_type_names, this->type);
  87. return FALSE;
  88. }
  89. ctx = EVP_MD_CTX_new();
  90. if (!ctx ||
  91. EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, this->key) <= 0 ||
  92. EVP_DigestVerify(ctx, signature.ptr, signature.len,
  93. data.ptr, data.len) <= 0)
  94. {
  95. EVP_MD_CTX_free(ctx);
  96. return FALSE;
  97. }
  98. EVP_MD_CTX_free(ctx);
  99. return TRUE;
  100. }
  101. METHOD(public_key_t, encrypt, bool,
  102. private_public_key_t *this, encryption_scheme_t scheme,
  103. chunk_t crypto, chunk_t *plain)
  104. {
  105. DBG1(DBG_LIB, "encryption scheme %N not supported", encryption_scheme_names,
  106. scheme);
  107. return FALSE;
  108. }
  109. METHOD(public_key_t, get_keysize, int,
  110. private_public_key_t *this)
  111. {
  112. return openssl_ed_keysize(this->type);
  113. }
  114. /**
  115. * Calculate fingerprint from an EdDSA key, also used in ed private key.
  116. */
  117. bool openssl_ed_fingerprint(EVP_PKEY *key, cred_encoding_type_t type,
  118. chunk_t *fp)
  119. {
  120. hasher_t *hasher;
  121. chunk_t blob;
  122. u_char *p;
  123. if (lib->encoding->get_cache(lib->encoding, type, key, fp))
  124. {
  125. return TRUE;
  126. }
  127. switch (type)
  128. {
  129. case KEYID_PUBKEY_SHA1:
  130. if (!EVP_PKEY_get_raw_public_key(key, NULL, &blob.len))
  131. {
  132. return FALSE;
  133. }
  134. blob = chunk_alloca(blob.len);
  135. if (!EVP_PKEY_get_raw_public_key(key, blob.ptr, &blob.len))
  136. {
  137. return FALSE;
  138. }
  139. break;
  140. case KEYID_PUBKEY_INFO_SHA1:
  141. blob = chunk_alloca(i2d_PUBKEY(key, NULL));
  142. p = blob.ptr;
  143. i2d_PUBKEY(key, &p);
  144. break;
  145. default:
  146. return FALSE;
  147. }
  148. hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
  149. if (!hasher || !hasher->allocate_hash(hasher, blob, fp))
  150. {
  151. DBG1(DBG_LIB, "SHA1 not supported, fingerprinting failed");
  152. DESTROY_IF(hasher);
  153. return FALSE;
  154. }
  155. hasher->destroy(hasher);
  156. lib->encoding->cache(lib->encoding, type, key, *fp);
  157. return TRUE;
  158. }
  159. METHOD(public_key_t, get_fingerprint, bool,
  160. private_public_key_t *this, cred_encoding_type_t type, chunk_t *fingerprint)
  161. {
  162. return openssl_ed_fingerprint(this->key, type, fingerprint);
  163. }
  164. METHOD(public_key_t, get_encoding, bool,
  165. private_public_key_t *this, cred_encoding_type_t type, chunk_t *encoding)
  166. {
  167. bool success = TRUE;
  168. u_char *p;
  169. *encoding = chunk_alloc(i2d_PUBKEY(this->key, NULL));
  170. p = encoding->ptr;
  171. i2d_PUBKEY(this->key, &p);
  172. if (type != PUBKEY_SPKI_ASN1_DER)
  173. {
  174. chunk_t asn1_encoding = *encoding;
  175. success = lib->encoding->encode(lib->encoding, type,
  176. NULL, encoding, CRED_PART_EDDSA_PUB_ASN1_DER,
  177. asn1_encoding, CRED_PART_END);
  178. chunk_clear(&asn1_encoding);
  179. }
  180. return success;
  181. }
  182. METHOD(public_key_t, get_ref, public_key_t*,
  183. private_public_key_t *this)
  184. {
  185. ref_get(&this->ref);
  186. return &this->public;
  187. }
  188. METHOD(public_key_t, destroy, void,
  189. private_public_key_t *this)
  190. {
  191. if (ref_put(&this->ref))
  192. {
  193. lib->encoding->clear_cache(lib->encoding, this->key);
  194. EVP_PKEY_free(this->key);
  195. free(this);
  196. }
  197. }
  198. /**
  199. * Generic private constructor
  200. */
  201. static private_public_key_t *create_empty(key_type_t type)
  202. {
  203. private_public_key_t *this;
  204. INIT(this,
  205. .public = {
  206. .get_type = _get_type,
  207. .verify = _verify,
  208. .encrypt = _encrypt,
  209. .get_keysize = _get_keysize,
  210. .equals = public_key_equals,
  211. .get_fingerprint = _get_fingerprint,
  212. .has_fingerprint = public_key_has_fingerprint,
  213. .get_encoding = _get_encoding,
  214. .get_ref = _get_ref,
  215. .destroy = _destroy,
  216. },
  217. .type = type,
  218. .ref = 1,
  219. );
  220. return this;
  221. }
  222. /*
  223. * Described in header
  224. */
  225. public_key_t *openssl_ed_public_key_load(key_type_t type, va_list args)
  226. {
  227. private_public_key_t *this;
  228. chunk_t blob = chunk_empty, pub = chunk_empty;
  229. EVP_PKEY *key = NULL;
  230. while (TRUE)
  231. {
  232. switch (va_arg(args, builder_part_t))
  233. {
  234. case BUILD_BLOB_ASN1_DER:
  235. blob = va_arg(args, chunk_t);
  236. continue;
  237. case BUILD_EDDSA_PUB:
  238. pub = va_arg(args, chunk_t);
  239. continue;
  240. case BUILD_END:
  241. break;
  242. default:
  243. return NULL;
  244. }
  245. break;
  246. }
  247. if (pub.len)
  248. {
  249. key = EVP_PKEY_new_raw_public_key(openssl_ed_key_type(type), NULL,
  250. pub.ptr, pub.len);
  251. }
  252. else if (blob.len)
  253. {
  254. key = d2i_PUBKEY(NULL, (const u_char**)&blob.ptr, blob.len);
  255. if (key && EVP_PKEY_base_id(key) != openssl_ed_key_type(type))
  256. {
  257. EVP_PKEY_free(key);
  258. return NULL;
  259. }
  260. }
  261. if (!key)
  262. {
  263. return NULL;
  264. }
  265. this = create_empty(type);
  266. this->key = key;
  267. return &this->public;
  268. }
  269. #endif /* OPENSSL_VERSION_NUMBER */