2
0

d2i_pu.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 1995-2016 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/asn1.h>
  15. #include <openssl/rsa.h>
  16. #include <openssl/dsa.h>
  17. #include <openssl/ec.h>
  18. #include "internal/evp_int.h"
  19. EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
  20. long length)
  21. {
  22. EVP_PKEY *ret;
  23. if ((a == NULL) || (*a == NULL)) {
  24. if ((ret = EVP_PKEY_new()) == NULL) {
  25. ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_EVP_LIB);
  26. return NULL;
  27. }
  28. } else
  29. ret = *a;
  30. if (type != EVP_PKEY_id(ret) && !EVP_PKEY_set_type(ret, type)) {
  31. ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_EVP_LIB);
  32. goto err;
  33. }
  34. switch (EVP_PKEY_id(ret)) {
  35. #ifndef OPENSSL_NO_RSA
  36. case EVP_PKEY_RSA:
  37. if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
  38. ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
  39. goto err;
  40. }
  41. break;
  42. #endif
  43. #ifndef OPENSSL_NO_DSA
  44. case EVP_PKEY_DSA:
  45. /* TMP UGLY CAST */
  46. if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
  47. ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
  48. goto err;
  49. }
  50. break;
  51. #endif
  52. #ifndef OPENSSL_NO_EC
  53. case EVP_PKEY_EC:
  54. if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
  55. ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
  56. goto err;
  57. }
  58. break;
  59. #endif
  60. default:
  61. ASN1err(ASN1_F_D2I_PUBLICKEY, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
  62. goto err;
  63. }
  64. if (a != NULL)
  65. (*a) = ret;
  66. return ret;
  67. err:
  68. if (a == NULL || *a != ret)
  69. EVP_PKEY_free(ret);
  70. return NULL;
  71. }