d2i_pu.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /*
  10. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/bn.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/asn1.h>
  20. #include <openssl/rsa.h>
  21. #include <openssl/dsa.h>
  22. #include <openssl/ec.h>
  23. #include "crypto/evp.h"
  24. EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
  25. long length)
  26. {
  27. EVP_PKEY *ret;
  28. if ((a == NULL) || (*a == NULL)) {
  29. if ((ret = EVP_PKEY_new()) == NULL) {
  30. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  31. return NULL;
  32. }
  33. } else
  34. ret = *a;
  35. if (type != EVP_PKEY_get_id(ret) && !EVP_PKEY_set_type(ret, type)) {
  36. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  37. goto err;
  38. }
  39. switch (EVP_PKEY_get_id(ret)) {
  40. case EVP_PKEY_RSA:
  41. if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
  42. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  43. goto err;
  44. }
  45. break;
  46. #ifndef OPENSSL_NO_DSA
  47. case EVP_PKEY_DSA:
  48. /* TMP UGLY CAST */
  49. if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
  50. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  51. goto err;
  52. }
  53. break;
  54. #endif
  55. #ifndef OPENSSL_NO_EC
  56. case EVP_PKEY_EC:
  57. if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
  58. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  59. goto err;
  60. }
  61. break;
  62. #endif
  63. default:
  64. ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
  65. goto err;
  66. }
  67. if (a != NULL)
  68. (*a) = ret;
  69. return ret;
  70. err:
  71. if (a == NULL || *a != ret)
  72. EVP_PKEY_free(ret);
  73. return NULL;
  74. }