evp_pkey.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_MALLOC_FAILURE);
  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. size_t len;
  64. OSSL_DECODER_CTX *dctx = NULL;
  65. if ((encoded_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &encoded_data)) <= 0
  66. || encoded_data == NULL)
  67. return NULL;
  68. p8_data = encoded_data;
  69. len = encoded_len;
  70. dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", "PrivateKeyInfo",
  71. EVP_PKEY_NONE, 0, libctx, propq);
  72. if (dctx == NULL
  73. || !OSSL_DECODER_from_data(dctx, &p8_data, &len))
  74. /* try legacy */
  75. pkey = evp_pkcs82pkey_legacy(p8, libctx, propq);
  76. OPENSSL_clear_free(encoded_data, encoded_len);
  77. OSSL_DECODER_CTX_free(dctx);
  78. return pkey;
  79. }
  80. EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
  81. {
  82. return EVP_PKCS82PKEY_ex(p8, NULL, NULL);
  83. }
  84. /* Turn a private key into a PKCS8 structure */
  85. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
  86. {
  87. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  88. OSSL_ENCODER_CTX *ctx = NULL;
  89. /*
  90. * The implementation for provider-native keys is to encode the
  91. * key to a DER encoded PKCS#8 structure, then convert it to a
  92. * PKCS8_PRIV_KEY_INFO with good old d2i functions.
  93. */
  94. if (evp_pkey_is_provided(pkey)) {
  95. int selection = OSSL_KEYMGMT_SELECT_ALL;
  96. unsigned char *der = NULL;
  97. size_t derlen = 0;
  98. const unsigned char *pp;
  99. if ((ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
  100. "DER", "PrivateKeyInfo",
  101. NULL)) == NULL
  102. || !OSSL_ENCODER_to_data(ctx, &der, &derlen))
  103. goto error;
  104. pp = der;
  105. p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pp, (long)derlen);
  106. OPENSSL_free(der);
  107. if (p8 == NULL)
  108. goto error;
  109. } else {
  110. p8 = PKCS8_PRIV_KEY_INFO_new();
  111. if (p8 == NULL) {
  112. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  113. return NULL;
  114. }
  115. if (pkey->ameth != NULL) {
  116. if (pkey->ameth->priv_encode != NULL) {
  117. if (!pkey->ameth->priv_encode(p8, pkey)) {
  118. ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
  119. goto error;
  120. }
  121. } else {
  122. ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
  123. goto error;
  124. }
  125. } else {
  126. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  127. goto error;
  128. }
  129. }
  130. goto end;
  131. error:
  132. PKCS8_PRIV_KEY_INFO_free(p8);
  133. p8 = NULL;
  134. end:
  135. OSSL_ENCODER_CTX_free(ctx);
  136. return p8;
  137. }
  138. /* EVP_PKEY attribute functions */
  139. int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
  140. {
  141. return X509at_get_attr_count(key->attributes);
  142. }
  143. int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
  144. {
  145. return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
  146. }
  147. int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
  148. int lastpos)
  149. {
  150. return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
  151. }
  152. X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
  153. {
  154. return X509at_get_attr(key->attributes, loc);
  155. }
  156. X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
  157. {
  158. return X509at_delete_attr(key->attributes, loc);
  159. }
  160. int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
  161. {
  162. if (X509at_add1_attr(&key->attributes, attr))
  163. return 1;
  164. return 0;
  165. }
  166. int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
  167. const ASN1_OBJECT *obj, int type,
  168. const unsigned char *bytes, int len)
  169. {
  170. if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
  171. return 1;
  172. return 0;
  173. }
  174. int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
  175. int nid, int type,
  176. const unsigned char *bytes, int len)
  177. {
  178. if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
  179. return 1;
  180. return 0;
  181. }
  182. int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
  183. const char *attrname, int type,
  184. const unsigned char *bytes, int len)
  185. {
  186. if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
  187. return 1;
  188. return 0;
  189. }
  190. const char *EVP_PKEY_get0_type_name(const EVP_PKEY *key)
  191. {
  192. const EVP_PKEY_ASN1_METHOD *ameth;
  193. const char *name = NULL;
  194. if (key->keymgmt != NULL)
  195. return EVP_KEYMGMT_get0_name(key->keymgmt);
  196. /* Otherwise fallback to legacy */
  197. ameth = EVP_PKEY_get0_asn1(key);
  198. if (ameth != NULL)
  199. EVP_PKEY_asn1_get0_info(NULL, NULL,
  200. NULL, NULL, &name, ameth);
  201. return name;
  202. }