2016-05-18 02:51:34 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:34 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
2020-07-24 20:53:27 +08:00
|
|
|
/* We need to use some engine deprecated APIs */
|
|
|
|
#define OPENSSL_SUPPRESS_DEPRECATED
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2017-08-25 20:51:45 +08:00
|
|
|
#include <sys/types.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-09-12 01:54:18 +08:00
|
|
|
|
2020-07-24 20:53:27 +08:00
|
|
|
#include <openssl/engine.h>
|
2004-12-05 09:03:15 +08:00
|
|
|
#include <openssl/err.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/buffer.h>
|
1999-08-08 19:25:32 +08:00
|
|
|
#include <openssl/x509.h>
|
2020-07-24 20:53:27 +08:00
|
|
|
#include "crypto/x509.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-02-25 05:33:52 +08:00
|
|
|
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
2000-12-29 06:24:50 +08:00
|
|
|
|
2005-03-31 21:57:54 +08:00
|
|
|
int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *md, unsigned int *len)
|
|
|
|
{
|
2018-10-01 04:39:38 +08:00
|
|
|
int inl;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *str, *p;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2018-10-01 04:39:38 +08:00
|
|
|
inl = i2d(data, NULL);
|
|
|
|
if (inl <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
|
2018-10-01 04:39:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((str = OPENSSL_malloc(inl)) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
p = str;
|
|
|
|
i2d(data, &p);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2018-10-01 04:39:38 +08:00
|
|
|
if (!EVP_Digest(str, inl, md, len, type, NULL)) {
|
2017-02-02 01:29:47 +08:00
|
|
|
OPENSSL_free(str);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2017-02-02 01:29:47 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(str);
|
2017-10-09 19:05:58 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-12-29 06:24:50 +08:00
|
|
|
#endif
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
int asn1_item_digest_ex(const ASN1_ITEM *it, const EVP_MD *md, void *asn,
|
|
|
|
unsigned char *data, unsigned int *len,
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-07-24 20:53:27 +08:00
|
|
|
int i, ret = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *str = NULL;
|
2020-07-24 20:53:27 +08:00
|
|
|
EVP_MD *fetched_md = (EVP_MD *)md;
|
2000-12-29 03:18:48 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
i = ASN1_item_i2d(asn, &str, it);
|
2020-08-11 13:41:54 +08:00
|
|
|
if (i < 0 || str == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2000-12-29 03:18:48 +08:00
|
|
|
|
2020-07-24 20:53:27 +08:00
|
|
|
if (EVP_MD_provider(md) == NULL) {
|
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2020-07-30 22:15:05 +08:00
|
|
|
ENGINE *tmpeng = ENGINE_get_digest_engine(EVP_MD_type(md));
|
|
|
|
|
|
|
|
if (tmpeng != NULL)
|
|
|
|
ENGINE_finish(tmpeng);
|
|
|
|
else
|
2020-07-24 20:53:27 +08:00
|
|
|
#endif
|
|
|
|
fetched_md = EVP_MD_fetch(libctx, EVP_MD_name(md), propq);
|
2017-02-02 01:29:47 +08:00
|
|
|
}
|
2020-07-24 20:53:27 +08:00
|
|
|
if (fetched_md == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = EVP_Digest(str, i, data, len, fetched_md, NULL);
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(str);
|
2020-07-24 20:53:27 +08:00
|
|
|
if (fetched_md != md)
|
|
|
|
EVP_MD_free(fetched_md);
|
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2020-07-24 20:53:27 +08:00
|
|
|
|
|
|
|
int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *md, void *asn,
|
|
|
|
unsigned char *data, unsigned int *len)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return asn1_item_digest_ex(it, md, asn, data, len, NULL, NULL);
|
2020-07-24 20:53:27 +08:00
|
|
|
}
|
|
|
|
|