2
0

a_digest.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <sys/types.h>
  14. #include "internal/cryptlib.h"
  15. #include <openssl/engine.h>
  16. #include <openssl/err.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/buffer.h>
  19. #include <openssl/x509.h>
  20. #include "crypto/x509.h"
  21. #ifndef OPENSSL_NO_DEPRECATED_3_0
  22. int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
  23. unsigned char *md, unsigned int *len)
  24. {
  25. int inl;
  26. unsigned char *str, *p;
  27. inl = i2d(data, NULL);
  28. if (inl <= 0) {
  29. ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_INTERNAL_ERROR);
  30. return 0;
  31. }
  32. if ((str = OPENSSL_malloc(inl)) == NULL) {
  33. ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_MALLOC_FAILURE);
  34. return 0;
  35. }
  36. p = str;
  37. i2d(data, &p);
  38. if (!EVP_Digest(str, inl, md, len, type, NULL)) {
  39. OPENSSL_free(str);
  40. return 0;
  41. }
  42. OPENSSL_free(str);
  43. return 1;
  44. }
  45. #endif
  46. int asn1_item_digest_with_libctx(const ASN1_ITEM *it, const EVP_MD *md,
  47. void *asn, unsigned char *data,
  48. unsigned int *len, OPENSSL_CTX *libctx,
  49. const char *propq)
  50. {
  51. int i, ret = 0;
  52. unsigned char *str = NULL;
  53. EVP_MD *fetched_md = (EVP_MD *)md;
  54. i = ASN1_item_i2d(asn, &str, it);
  55. if (i < 0 || str == NULL)
  56. return 0;
  57. if (EVP_MD_provider(md) == NULL) {
  58. #if !defined(OPENSSL_NO_ENGINE)
  59. ENGINE *tmpeng = ENGINE_get_digest_engine(EVP_MD_type(md));
  60. if (tmpeng != NULL)
  61. ENGINE_finish(tmpeng);
  62. else
  63. #endif
  64. fetched_md = EVP_MD_fetch(libctx, EVP_MD_name(md), propq);
  65. }
  66. if (fetched_md == NULL)
  67. goto err;
  68. ret = EVP_Digest(str, i, data, len, fetched_md, NULL);
  69. err:
  70. OPENSSL_free(str);
  71. if (fetched_md != md)
  72. EVP_MD_free(fetched_md);
  73. return ret;
  74. }
  75. int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *md, void *asn,
  76. unsigned char *data, unsigned int *len)
  77. {
  78. return asn1_item_digest_with_libctx(it, md, asn, data, len, NULL, NULL);
  79. }