evp_pkey.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. RAND_add(p8->pkey->data, p8->pkey->length, 0.0);
  72. return p8;
  73. error:
  74. PKCS8_PRIV_KEY_INFO_free(p8);
  75. return NULL;
  76. }
  77. /* EVP_PKEY attribute functions */
  78. int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
  79. {
  80. return X509at_get_attr_count(key->attributes);
  81. }
  82. int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
  83. {
  84. return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
  85. }
  86. int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
  87. int lastpos)
  88. {
  89. return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
  90. }
  91. X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
  92. {
  93. return X509at_get_attr(key->attributes, loc);
  94. }
  95. X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
  96. {
  97. return X509at_delete_attr(key->attributes, loc);
  98. }
  99. int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
  100. {
  101. if (X509at_add1_attr(&key->attributes, attr))
  102. return 1;
  103. return 0;
  104. }
  105. int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
  106. const ASN1_OBJECT *obj, int type,
  107. const unsigned char *bytes, int len)
  108. {
  109. if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
  110. return 1;
  111. return 0;
  112. }
  113. int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
  114. int nid, int type,
  115. const unsigned char *bytes, int len)
  116. {
  117. if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
  118. return 1;
  119. return 0;
  120. }
  121. int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
  122. const char *attrname, int type,
  123. const unsigned char *bytes, int len)
  124. {
  125. if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
  126. return 1;
  127. return 0;
  128. }