d2i_pr.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 1995-2020 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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/bn.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/engine.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/asn1.h>
  19. #include "crypto/asn1.h"
  20. #include "crypto/evp.h"
  21. EVP_PKEY *d2i_PrivateKey_ex(int type, EVP_PKEY **a, const unsigned char **pp,
  22. long length, OSSL_LIB_CTX *libctx,
  23. const char *propq)
  24. {
  25. EVP_PKEY *ret;
  26. const unsigned char *p = *pp;
  27. if ((a == NULL) || (*a == NULL)) {
  28. if ((ret = EVP_PKEY_new()) == NULL) {
  29. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  30. return NULL;
  31. }
  32. } else {
  33. ret = *a;
  34. #ifndef OPENSSL_NO_ENGINE
  35. ENGINE_finish(ret->engine);
  36. ret->engine = NULL;
  37. #endif
  38. }
  39. if (!EVP_PKEY_set_type(ret, type)) {
  40. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
  41. goto err;
  42. }
  43. ERR_set_mark();
  44. if (!ret->ameth->old_priv_decode ||
  45. !ret->ameth->old_priv_decode(ret, &p, length)) {
  46. if (ret->ameth->priv_decode != NULL
  47. || ret->ameth->priv_decode_ex != NULL) {
  48. EVP_PKEY *tmp;
  49. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  50. p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
  51. if (p8 == NULL) {
  52. ERR_clear_last_mark();
  53. goto err;
  54. }
  55. tmp = EVP_PKCS82PKEY_ex(p8, libctx, propq);
  56. PKCS8_PRIV_KEY_INFO_free(p8);
  57. if (tmp == NULL) {
  58. ERR_clear_last_mark();
  59. goto err;
  60. }
  61. EVP_PKEY_free(ret);
  62. ret = tmp;
  63. ERR_pop_to_mark();
  64. if (EVP_PKEY_type(type) != EVP_PKEY_base_id(ret))
  65. goto err;
  66. } else {
  67. ERR_clear_last_mark();
  68. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  69. goto err;
  70. }
  71. } else {
  72. ERR_clear_last_mark();
  73. }
  74. *pp = p;
  75. if (a != NULL)
  76. (*a) = ret;
  77. return ret;
  78. err:
  79. if (a == NULL || *a != ret)
  80. EVP_PKEY_free(ret);
  81. return NULL;
  82. }
  83. EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
  84. long length)
  85. {
  86. return d2i_PrivateKey_ex(type, a, pp, length, NULL, NULL);
  87. }
  88. /*
  89. * This works like d2i_PrivateKey() except it automatically works out the
  90. * type
  91. */
  92. EVP_PKEY *d2i_AutoPrivateKey_ex(EVP_PKEY **a, const unsigned char **pp,
  93. long length, OSSL_LIB_CTX *libctx,
  94. const char *propq)
  95. {
  96. STACK_OF(ASN1_TYPE) *inkey;
  97. const unsigned char *p;
  98. int keytype;
  99. p = *pp;
  100. /*
  101. * Dirty trick: read in the ASN1 data into a STACK_OF(ASN1_TYPE): by
  102. * analyzing it we can determine the passed structure: this assumes the
  103. * input is surrounded by an ASN1 SEQUENCE.
  104. */
  105. inkey = d2i_ASN1_SEQUENCE_ANY(NULL, &p, length);
  106. p = *pp;
  107. /*
  108. * Since we only need to discern "traditional format" RSA and DSA keys we
  109. * can just count the elements.
  110. */
  111. if (sk_ASN1_TYPE_num(inkey) == 6) {
  112. keytype = EVP_PKEY_DSA;
  113. } else if (sk_ASN1_TYPE_num(inkey) == 4) {
  114. keytype = EVP_PKEY_EC;
  115. } else if (sk_ASN1_TYPE_num(inkey) == 3) { /* This seems to be PKCS8, not
  116. * traditional format */
  117. PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, length);
  118. EVP_PKEY *ret;
  119. sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
  120. if (p8 == NULL) {
  121. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
  122. return NULL;
  123. }
  124. ret = EVP_PKCS82PKEY_ex(p8, libctx, propq);
  125. PKCS8_PRIV_KEY_INFO_free(p8);
  126. if (ret == NULL)
  127. return NULL;
  128. *pp = p;
  129. if (a) {
  130. *a = ret;
  131. }
  132. return ret;
  133. } else {
  134. keytype = EVP_PKEY_RSA;
  135. }
  136. sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
  137. return d2i_PrivateKey_ex(keytype, a, pp, length, libctx, propq);
  138. }
  139. EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
  140. long length)
  141. {
  142. return d2i_AutoPrivateKey_ex(a, pp, length, NULL, NULL);
  143. }