d2i_param.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2019 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/evp.h>
  12. #include <openssl/asn1.h>
  13. #include "internal/evp_int.h"
  14. #include "internal/asn1_int.h"
  15. EVP_PKEY *d2i_KeyParams(int type, EVP_PKEY **a, const unsigned char **pp,
  16. long length)
  17. {
  18. EVP_PKEY *ret = NULL;
  19. const unsigned char *p = *pp;
  20. if ((a == NULL) || (*a == NULL)) {
  21. if ((ret = EVP_PKEY_new()) == NULL)
  22. return NULL;
  23. } else
  24. ret = *a;
  25. if (type != EVP_PKEY_id(ret) && !EVP_PKEY_set_type(ret, type))
  26. goto err;
  27. if (ret->ameth == NULL || ret->ameth->param_decode == NULL) {
  28. ASN1err(ASN1_F_D2I_KEYPARAMS, ASN1_R_UNSUPPORTED_TYPE);
  29. goto err;
  30. }
  31. if (!ret->ameth->param_decode(ret, &p, length))
  32. goto err;
  33. if (a != NULL)
  34. (*a) = ret;
  35. return ret;
  36. err:
  37. if (a == NULL || *a != ret)
  38. EVP_PKEY_free(ret);
  39. return NULL;
  40. }
  41. EVP_PKEY *d2i_KeyParams_bio(int type, EVP_PKEY **a, BIO *in)
  42. {
  43. BUF_MEM *b = NULL;
  44. const unsigned char *p;
  45. void *ret = NULL;
  46. int len;
  47. len = asn1_d2i_read_bio(in, &b);
  48. if (len < 0)
  49. goto err;
  50. p = (unsigned char *)b->data;
  51. ret = d2i_KeyParams(type, a, &p, len);
  52. err:
  53. BUF_MEM_free(b);
  54. return ret;
  55. }