2
0

a_dup.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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/asn1.h>
  12. #ifndef NO_OLD_ASN1
  13. void *ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *x)
  14. {
  15. unsigned char *b, *p;
  16. const unsigned char *p2;
  17. int i;
  18. char *ret;
  19. if (x == NULL)
  20. return NULL;
  21. i = i2d(x, NULL);
  22. b = OPENSSL_malloc(i + 10);
  23. if (b == NULL) {
  24. ASN1err(ASN1_F_ASN1_DUP, ERR_R_MALLOC_FAILURE);
  25. return NULL;
  26. }
  27. p = b;
  28. i = i2d(x, &p);
  29. p2 = b;
  30. ret = d2i(NULL, &p2, i);
  31. OPENSSL_free(b);
  32. return ret;
  33. }
  34. #endif
  35. /*
  36. * ASN1_ITEM version of dup: this follows the model above except we don't
  37. * need to allocate the buffer. At some point this could be rewritten to
  38. * directly dup the underlying structure instead of doing and encode and
  39. * decode.
  40. */
  41. void *ASN1_item_dup(const ASN1_ITEM *it, void *x)
  42. {
  43. unsigned char *b = NULL;
  44. const unsigned char *p;
  45. long i;
  46. void *ret;
  47. if (x == NULL)
  48. return NULL;
  49. i = ASN1_item_i2d(x, &b, it);
  50. if (b == NULL) {
  51. ASN1err(ASN1_F_ASN1_ITEM_DUP, ERR_R_MALLOC_FAILURE);
  52. return NULL;
  53. }
  54. p = b;
  55. ret = ASN1_item_d2i(NULL, &p, i, it);
  56. OPENSSL_free(b);
  57. return ret;
  58. }