evp_pkey.c 7.7 KB

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