d2i_pr.c 4.1 KB

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