ecx_backend.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2020-2023 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 = key->haspubkey;
  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. memcpy(ret->pubkey, key->pubkey, sizeof(ret->pubkey));
  112. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  113. && key->privkey != NULL) {
  114. if (ossl_ecx_key_allocate_privkey(ret) == NULL) {
  115. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  116. goto err;
  117. }
  118. memcpy(ret->privkey, key->privkey, ret->keylen);
  119. }
  120. return ret;
  121. err:
  122. CRYPTO_FREE_REF(&ret->references);
  123. ossl_ecx_key_free(ret);
  124. return NULL;
  125. }
  126. #ifndef FIPS_MODULE
  127. ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
  128. const unsigned char *p, int plen,
  129. int id, ecx_key_op_t op,
  130. OSSL_LIB_CTX *libctx, const char *propq)
  131. {
  132. ECX_KEY *key = NULL;
  133. unsigned char *privkey, *pubkey;
  134. if (op != KEY_OP_KEYGEN) {
  135. if (palg != NULL) {
  136. int ptype;
  137. /* Algorithm parameters must be absent */
  138. X509_ALGOR_get0(NULL, &ptype, NULL, palg);
  139. if (ptype != V_ASN1_UNDEF) {
  140. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  141. return 0;
  142. }
  143. if (id == EVP_PKEY_NONE)
  144. id = OBJ_obj2nid(palg->algorithm);
  145. else if (id != OBJ_obj2nid(palg->algorithm)) {
  146. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  147. return 0;
  148. }
  149. }
  150. if (p == NULL || id == EVP_PKEY_NONE || plen != KEYLENID(id)) {
  151. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  152. return 0;
  153. }
  154. }
  155. key = ossl_ecx_key_new(libctx, KEYNID2TYPE(id), 1, propq);
  156. if (key == NULL) {
  157. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  158. return 0;
  159. }
  160. pubkey = key->pubkey;
  161. if (op == KEY_OP_PUBLIC) {
  162. memcpy(pubkey, p, plen);
  163. } else {
  164. privkey = ossl_ecx_key_allocate_privkey(key);
  165. if (privkey == NULL) {
  166. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  167. goto err;
  168. }
  169. if (op == KEY_OP_KEYGEN) {
  170. if (id != EVP_PKEY_NONE) {
  171. if (RAND_priv_bytes_ex(libctx, privkey, KEYLENID(id), 0) <= 0)
  172. goto err;
  173. if (id == EVP_PKEY_X25519) {
  174. privkey[0] &= 248;
  175. privkey[X25519_KEYLEN - 1] &= 127;
  176. privkey[X25519_KEYLEN - 1] |= 64;
  177. } else if (id == EVP_PKEY_X448) {
  178. privkey[0] &= 252;
  179. privkey[X448_KEYLEN - 1] |= 128;
  180. }
  181. }
  182. } else {
  183. memcpy(privkey, p, KEYLENID(id));
  184. }
  185. if (!ossl_ecx_public_from_private(key)) {
  186. ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
  187. goto err;
  188. }
  189. }
  190. return key;
  191. err:
  192. ossl_ecx_key_free(key);
  193. return NULL;
  194. }
  195. ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  196. OSSL_LIB_CTX *libctx, const char *propq)
  197. {
  198. ECX_KEY *ecx = NULL;
  199. const unsigned char *p;
  200. int plen;
  201. ASN1_OCTET_STRING *oct = NULL;
  202. const X509_ALGOR *palg;
  203. if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf))
  204. return 0;
  205. oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
  206. if (oct == NULL) {
  207. p = NULL;
  208. plen = 0;
  209. } else {
  210. p = ASN1_STRING_get0_data(oct);
  211. plen = ASN1_STRING_length(oct);
  212. }
  213. /*
  214. * EVP_PKEY_NONE means that ecx_key_op() has to figure out the key type
  215. * on its own.
  216. */
  217. ecx = ossl_ecx_key_op(palg, p, plen, EVP_PKEY_NONE, KEY_OP_PRIVATE,
  218. libctx, propq);
  219. ASN1_OCTET_STRING_free(oct);
  220. return ecx;
  221. }
  222. #endif