evp_pkey.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/asn1_int.h"
  15. #include "internal/evp_int.h"
  16. #include "internal/x509_int.h"
  17. /* Extract a private key from a PKCS8 structure */
  18. EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
  19. {
  20. EVP_PKEY *pkey = NULL;
  21. const ASN1_OBJECT *algoid;
  22. char obj_tmp[80];
  23. if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
  24. return NULL;
  25. if ((pkey = EVP_PKEY_new()) == NULL) {
  26. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
  27. return NULL;
  28. }
  29. if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
  30. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  31. i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
  32. ERR_add_error_data(2, "TYPE=", obj_tmp);
  33. goto error;
  34. }
  35. if (pkey->ameth->priv_decode) {
  36. if (!pkey->ameth->priv_decode(pkey, p8)) {
  37. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_PRIVATE_KEY_DECODE_ERROR);
  38. goto error;
  39. }
  40. } else {
  41. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_METHOD_NOT_SUPPORTED);
  42. goto error;
  43. }
  44. return pkey;
  45. error:
  46. EVP_PKEY_free(pkey);
  47. return NULL;
  48. }
  49. /* Turn a private key into a PKCS8 structure */
  50. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
  51. {
  52. PKCS8_PRIV_KEY_INFO *p8 = PKCS8_PRIV_KEY_INFO_new();
  53. if (p8 == NULL) {
  54. EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  55. return NULL;
  56. }
  57. if (pkey->ameth) {
  58. if (pkey->ameth->priv_encode) {
  59. if (!pkey->ameth->priv_encode(p8, pkey)) {
  60. EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
  61. goto error;
  62. }
  63. } else {
  64. EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED);
  65. goto error;
  66. }
  67. } else {
  68. EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  69. goto error;
  70. }
  71. return p8;
  72. error:
  73. PKCS8_PRIV_KEY_INFO_free(p8);
  74. return NULL;
  75. }
  76. /* EVP_PKEY attribute functions */
  77. int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
  78. {
  79. return X509at_get_attr_count(key->attributes);
  80. }
  81. int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
  82. {
  83. return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
  84. }
  85. int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
  86. int lastpos)
  87. {
  88. return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
  89. }
  90. X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
  91. {
  92. return X509at_get_attr(key->attributes, loc);
  93. }
  94. X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
  95. {
  96. return X509at_delete_attr(key->attributes, loc);
  97. }
  98. int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
  99. {
  100. if (X509at_add1_attr(&key->attributes, attr))
  101. return 1;
  102. return 0;
  103. }
  104. int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
  105. const ASN1_OBJECT *obj, int type,
  106. const unsigned char *bytes, int len)
  107. {
  108. if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
  109. return 1;
  110. return 0;
  111. }
  112. int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
  113. int nid, int type,
  114. const unsigned char *bytes, int len)
  115. {
  116. if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
  117. return 1;
  118. return 0;
  119. }
  120. int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
  121. const char *attrname, int type,
  122. const unsigned char *bytes, int len)
  123. {
  124. if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
  125. return 1;
  126. return 0;
  127. }