ecx_backend.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/ec.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/err.h>
  15. #ifndef FIPS_MODULE
  16. # include <openssl/x509.h>
  17. #endif
  18. #include "crypto/ecx.h"
  19. #include "ecx_backend.h"
  20. /*
  21. * The intention with the "backend" source file is to offer backend support
  22. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  23. * implementations alike.
  24. */
  25. int ossl_ecx_public_from_private(ECX_KEY *key)
  26. {
  27. switch (key->type) {
  28. case ECX_KEY_TYPE_X25519:
  29. ossl_x25519_public_from_private(key->pubkey, key->privkey);
  30. break;
  31. case ECX_KEY_TYPE_ED25519:
  32. if (!ossl_ed25519_public_from_private(key->libctx, key->pubkey,
  33. key->privkey, key->propq)) {
  34. ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
  35. return 0;
  36. }
  37. break;
  38. case ECX_KEY_TYPE_X448:
  39. ossl_x448_public_from_private(key->pubkey, key->privkey);
  40. break;
  41. case ECX_KEY_TYPE_ED448:
  42. if (!ossl_ed448_public_from_private(key->libctx, key->pubkey,
  43. key->privkey, key->propq)) {
  44. ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
  45. return 0;
  46. }
  47. break;
  48. }
  49. return 1;
  50. }
  51. int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
  52. int include_private)
  53. {
  54. size_t privkeylen = 0, pubkeylen = 0;
  55. const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
  56. unsigned char *pubkey;
  57. if (ecx == NULL)
  58. return 0;
  59. param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  60. if (include_private)
  61. param_priv_key =
  62. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  63. if (param_pub_key == NULL && param_priv_key == NULL)
  64. return 0;
  65. if (param_priv_key != NULL) {
  66. if (!OSSL_PARAM_get_octet_string(param_priv_key,
  67. (void **)&ecx->privkey, ecx->keylen,
  68. &privkeylen))
  69. return 0;
  70. if (privkeylen != ecx->keylen) {
  71. /*
  72. * Invalid key length. We will clear what we've received now. We
  73. * can't leave it to ossl_ecx_key_free() because that will call
  74. * OPENSSL_secure_clear_free() and assume the correct key length
  75. */
  76. OPENSSL_secure_clear_free(ecx->privkey, privkeylen);
  77. ecx->privkey = NULL;
  78. return 0;
  79. }
  80. }
  81. pubkey = ecx->pubkey;
  82. if (param_pub_key != NULL
  83. && !OSSL_PARAM_get_octet_string(param_pub_key,
  84. (void **)&pubkey,
  85. sizeof(ecx->pubkey), &pubkeylen))
  86. return 0;
  87. if ((param_pub_key != NULL && pubkeylen != ecx->keylen))
  88. return 0;
  89. if (param_pub_key == NULL && !ossl_ecx_public_from_private(ecx))
  90. return 0;
  91. ecx->haspubkey = 1;
  92. return 1;
  93. }
  94. ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection)
  95. {
  96. ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
  97. if (ret == NULL)
  98. return NULL;
  99. ret->libctx = key->libctx;
  100. ret->haspubkey = 0;
  101. ret->keylen = key->keylen;
  102. ret->type = key->type;
  103. if (!CRYPTO_NEW_REF(&ret->references, 1))
  104. goto err;
  105. if (key->propq != NULL) {
  106. ret->propq = OPENSSL_strdup(key->propq);
  107. if (ret->propq == NULL)
  108. goto err;
  109. }
  110. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
  111. && key->haspubkey == 1) {
  112. memcpy(ret->pubkey, key->pubkey, sizeof(ret->pubkey));
  113. ret->haspubkey = 1;
  114. }
  115. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  116. && key->privkey != NULL) {
  117. if (ossl_ecx_key_allocate_privkey(ret) == NULL) {
  118. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  119. goto err;
  120. }
  121. memcpy(ret->privkey, key->privkey, ret->keylen);
  122. }
  123. return ret;
  124. err:
  125. CRYPTO_FREE_REF(&ret->references);
  126. ossl_ecx_key_free(ret);
  127. return NULL;
  128. }
  129. #ifndef FIPS_MODULE
  130. ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
  131. const unsigned char *p, int plen,
  132. int id, ecx_key_op_t op,
  133. OSSL_LIB_CTX *libctx, const char *propq)
  134. {
  135. ECX_KEY *key = NULL;
  136. unsigned char *privkey, *pubkey;
  137. if (op != KEY_OP_KEYGEN) {
  138. if (palg != NULL) {
  139. int ptype;
  140. /* Algorithm parameters must be absent */
  141. X509_ALGOR_get0(NULL, &ptype, NULL, palg);
  142. if (ptype != V_ASN1_UNDEF) {
  143. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  144. return 0;
  145. }
  146. if (id == EVP_PKEY_NONE)
  147. id = OBJ_obj2nid(palg->algorithm);
  148. else if (id != OBJ_obj2nid(palg->algorithm)) {
  149. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  150. return 0;
  151. }
  152. }
  153. if (p == NULL || id == EVP_PKEY_NONE || plen != KEYLENID(id)) {
  154. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  155. return 0;
  156. }
  157. }
  158. key = ossl_ecx_key_new(libctx, KEYNID2TYPE(id), 1, propq);
  159. if (key == NULL) {
  160. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  161. return 0;
  162. }
  163. pubkey = key->pubkey;
  164. if (op == KEY_OP_PUBLIC) {
  165. memcpy(pubkey, p, plen);
  166. } else {
  167. privkey = ossl_ecx_key_allocate_privkey(key);
  168. if (privkey == NULL) {
  169. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  170. goto err;
  171. }
  172. if (op == KEY_OP_KEYGEN) {
  173. if (id != EVP_PKEY_NONE) {
  174. if (RAND_priv_bytes_ex(libctx, privkey, KEYLENID(id), 0) <= 0)
  175. goto err;
  176. if (id == EVP_PKEY_X25519) {
  177. privkey[0] &= 248;
  178. privkey[X25519_KEYLEN - 1] &= 127;
  179. privkey[X25519_KEYLEN - 1] |= 64;
  180. } else if (id == EVP_PKEY_X448) {
  181. privkey[0] &= 252;
  182. privkey[X448_KEYLEN - 1] |= 128;
  183. }
  184. }
  185. } else {
  186. memcpy(privkey, p, KEYLENID(id));
  187. }
  188. if (!ossl_ecx_public_from_private(key)) {
  189. ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
  190. goto err;
  191. }
  192. }
  193. return key;
  194. err:
  195. ossl_ecx_key_free(key);
  196. return NULL;
  197. }
  198. ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  199. OSSL_LIB_CTX *libctx, const char *propq)
  200. {
  201. ECX_KEY *ecx = NULL;
  202. const unsigned char *p;
  203. int plen;
  204. ASN1_OCTET_STRING *oct = NULL;
  205. const X509_ALGOR *palg;
  206. if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf))
  207. return 0;
  208. oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
  209. if (oct == NULL) {
  210. p = NULL;
  211. plen = 0;
  212. } else {
  213. p = ASN1_STRING_get0_data(oct);
  214. plen = ASN1_STRING_length(oct);
  215. }
  216. /*
  217. * EVP_PKEY_NONE means that ecx_key_op() has to figure out the key type
  218. * on its own.
  219. */
  220. ecx = ossl_ecx_key_op(palg, p, plen, EVP_PKEY_NONE, KEY_OP_PRIVATE,
  221. libctx, propq);
  222. ASN1_OCTET_STRING_free(oct);
  223. return ecx;
  224. }
  225. #endif