evp_pkey.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 1999-2021 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 <stdio.h>
  10. #include <stdlib.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/x509.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/encoder.h>
  15. #include <openssl/decoder.h>
  16. #include "internal/provider.h"
  17. #include "crypto/asn1.h"
  18. #include "crypto/evp.h"
  19. #include "crypto/x509.h"
  20. /* Extract a private key from a PKCS8 structure */
  21. EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
  22. const char *propq)
  23. {
  24. EVP_PKEY *pkey = NULL;
  25. const ASN1_OBJECT *algoid;
  26. char obj_tmp[80];
  27. if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
  28. return NULL;
  29. if ((pkey = EVP_PKEY_new()) == NULL) {
  30. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  31. return NULL;
  32. }
  33. if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
  34. i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
  35. ERR_raise_data(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM,
  36. "TYPE=%s", obj_tmp);
  37. goto error;
  38. }
  39. if (pkey->ameth->priv_decode_ex != NULL) {
  40. if (!pkey->ameth->priv_decode_ex(pkey, p8, libctx, propq))
  41. goto error;
  42. } else if (pkey->ameth->priv_decode != NULL) {
  43. if (!pkey->ameth->priv_decode(pkey, p8)) {
  44. ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_DECODE_ERROR);
  45. goto error;
  46. }
  47. } else {
  48. ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
  49. goto error;
  50. }
  51. return pkey;
  52. error:
  53. EVP_PKEY_free(pkey);
  54. return NULL;
  55. }
  56. EVP_PKEY *EVP_PKCS82PKEY_ex(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
  57. const char *propq)
  58. {
  59. EVP_PKEY *pkey = NULL;
  60. const unsigned char *p8_data = NULL;
  61. unsigned char *encoded_data = NULL;
  62. int encoded_len;
  63. int selection;
  64. size_t len;
  65. OSSL_DECODER_CTX *dctx = NULL;
  66. if ((encoded_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &encoded_data)) <= 0
  67. || encoded_data == NULL)
  68. return NULL;
  69. p8_data = encoded_data;
  70. len = encoded_len;
  71. selection = EVP_PKEY_KEYPAIR | EVP_PKEY_KEY_PARAMETERS;
  72. dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", "PrivateKeyInfo",
  73. NULL, selection, libctx, propq);
  74. if (dctx == NULL
  75. || !OSSL_DECODER_from_data(dctx, &p8_data, &len))
  76. /* try legacy */
  77. pkey = evp_pkcs82pkey_legacy(p8, libctx, propq);
  78. OPENSSL_clear_free(encoded_data, encoded_len);
  79. OSSL_DECODER_CTX_free(dctx);
  80. return pkey;
  81. }
  82. EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
  83. {
  84. return EVP_PKCS82PKEY_ex(p8, NULL, NULL);
  85. }
  86. /* Turn a private key into a PKCS8 structure */
  87. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
  88. {
  89. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  90. OSSL_ENCODER_CTX *ctx = NULL;
  91. /*
  92. * The implementation for provider-native keys is to encode the
  93. * key to a DER encoded PKCS#8 structure, then convert it to a
  94. * PKCS8_PRIV_KEY_INFO with good old d2i functions.
  95. */
  96. if (evp_pkey_is_provided(pkey)) {
  97. int selection = OSSL_KEYMGMT_SELECT_ALL;
  98. unsigned char *der = NULL;
  99. size_t derlen = 0;
  100. const unsigned char *pp;
  101. if ((ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
  102. "DER", "PrivateKeyInfo",
  103. NULL)) == NULL
  104. || !OSSL_ENCODER_to_data(ctx, &der, &derlen))
  105. goto error;
  106. pp = der;
  107. p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pp, (long)derlen);
  108. OPENSSL_free(der);
  109. if (p8 == NULL)
  110. goto error;
  111. } else {
  112. p8 = PKCS8_PRIV_KEY_INFO_new();
  113. if (p8 == NULL) {
  114. ERR_raise(ERR_LIB_EVP, ERR_R_ASN1_LIB);
  115. return NULL;
  116. }
  117. if (pkey->ameth != NULL) {
  118. if (pkey->ameth->priv_encode != NULL) {
  119. if (!pkey->ameth->priv_encode(p8, pkey)) {
  120. ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
  121. goto error;
  122. }
  123. } else {
  124. ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
  125. goto error;
  126. }
  127. } else {
  128. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  129. goto error;
  130. }
  131. }
  132. goto end;
  133. error:
  134. PKCS8_PRIV_KEY_INFO_free(p8);
  135. p8 = NULL;
  136. end:
  137. OSSL_ENCODER_CTX_free(ctx);
  138. return p8;
  139. }
  140. /* EVP_PKEY attribute functions */
  141. int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
  142. {
  143. return X509at_get_attr_count(key->attributes);
  144. }
  145. int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
  146. {
  147. return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
  148. }
  149. int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
  150. int lastpos)
  151. {
  152. return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
  153. }
  154. X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
  155. {
  156. return X509at_get_attr(key->attributes, loc);
  157. }
  158. X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
  159. {
  160. return X509at_delete_attr(key->attributes, loc);
  161. }
  162. int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
  163. {
  164. if (X509at_add1_attr(&key->attributes, attr))
  165. return 1;
  166. return 0;
  167. }
  168. int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
  169. const ASN1_OBJECT *obj, int type,
  170. const unsigned char *bytes, int len)
  171. {
  172. if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
  173. return 1;
  174. return 0;
  175. }
  176. int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
  177. int nid, int type,
  178. const unsigned char *bytes, int len)
  179. {
  180. if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
  181. return 1;
  182. return 0;
  183. }
  184. int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
  185. const char *attrname, int type,
  186. const unsigned char *bytes, int len)
  187. {
  188. if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
  189. return 1;
  190. return 0;
  191. }
  192. const char *EVP_PKEY_get0_type_name(const EVP_PKEY *key)
  193. {
  194. const EVP_PKEY_ASN1_METHOD *ameth;
  195. const char *name = NULL;
  196. if (key->keymgmt != NULL)
  197. return EVP_KEYMGMT_get0_name(key->keymgmt);
  198. /* Otherwise fallback to legacy */
  199. ameth = EVP_PKEY_get0_asn1(key);
  200. if (ameth != NULL)
  201. EVP_PKEY_asn1_get0_info(NULL, NULL,
  202. NULL, NULL, &name, ameth);
  203. return name;
  204. }
  205. const OSSL_PROVIDER *EVP_PKEY_get0_provider(const EVP_PKEY *key)
  206. {
  207. if (evp_pkey_is_provided(key))
  208. return EVP_KEYMGMT_get0_provider(key->keymgmt);
  209. return NULL;
  210. }