a_digest.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 <time.h>
  11. #include <sys/types.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/err.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/buffer.h>
  16. #include <openssl/x509.h>
  17. #ifndef NO_ASN1_OLD
  18. int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
  19. unsigned char *md, unsigned int *len)
  20. {
  21. int i;
  22. unsigned char *str, *p;
  23. i = i2d(data, NULL);
  24. if ((str = OPENSSL_malloc(i)) == NULL) {
  25. ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_MALLOC_FAILURE);
  26. return 0;
  27. }
  28. p = str;
  29. i2d(data, &p);
  30. if (!EVP_Digest(str, i, md, len, type, NULL)) {
  31. OPENSSL_free(str);
  32. return 0;
  33. }
  34. OPENSSL_free(str);
  35. return 1;
  36. }
  37. #endif
  38. int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *asn,
  39. unsigned char *md, unsigned int *len)
  40. {
  41. int i;
  42. unsigned char *str = NULL;
  43. i = ASN1_item_i2d(asn, &str, it);
  44. if (!str)
  45. return 0;
  46. if (!EVP_Digest(str, i, md, len, type, NULL)) {
  47. OPENSSL_free(str);
  48. return 0;
  49. }
  50. OPENSSL_free(str);
  51. return 1;
  52. }