2016-05-18 02:24:46 +08:00
|
|
|
/*
|
2021-04-22 21:38:44 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:40:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:24:46 +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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/evp.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
int EVP_SignFinal_ex(EVP_MD_CTX *ctx, unsigned char *sigret,
|
2020-10-15 17:55:50 +08:00
|
|
|
unsigned int *siglen, EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
|
2020-09-24 17:42:23 +08:00
|
|
|
const char *propq)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned char m[EVP_MAX_MD_SIZE];
|
2015-05-06 17:16:55 +08:00
|
|
|
unsigned int m_len = 0;
|
2015-12-02 21:57:04 +08:00
|
|
|
int i = 0;
|
|
|
|
size_t sltmp;
|
2015-01-22 11:40:55 +08:00
|
|
|
EVP_PKEY_CTX *pkctx = NULL;
|
2006-04-20 01:05:59 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
*siglen = 0;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_FINALISE)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, m, &m_len))
|
|
|
|
goto err;
|
|
|
|
} else {
|
2015-05-06 17:16:55 +08:00
|
|
|
int rv = 0;
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
|
2020-07-26 15:32:05 +08:00
|
|
|
|
2015-11-27 21:17:50 +08:00
|
|
|
if (tmp_ctx == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2015-11-27 21:17:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rv = EVP_MD_CTX_copy_ex(tmp_ctx, ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (rv)
|
2015-11-27 21:17:50 +08:00
|
|
|
rv = EVP_DigestFinal_ex(tmp_ctx, m, &m_len);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(tmp_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!rv)
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-20 01:05:59 +08:00
|
|
|
|
2015-12-02 21:57:04 +08:00
|
|
|
sltmp = (size_t)EVP_PKEY_size(pkey);
|
|
|
|
i = 0;
|
2020-07-26 15:32:05 +08:00
|
|
|
pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
|
2015-12-02 21:57:04 +08:00
|
|
|
if (pkctx == NULL)
|
|
|
|
goto err;
|
|
|
|
if (EVP_PKEY_sign_init(pkctx) <= 0)
|
|
|
|
goto err;
|
Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new. Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field. The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md(). Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md(). Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
if (flags & x) --> if ((flags & x) != 0)
if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
2021-02-17 06:51:56 +08:00
|
|
|
if (EVP_PKEY_CTX_set_signature_md(pkctx, EVP_MD_CTX_get0_md(ctx)) <= 0)
|
2015-12-02 21:57:04 +08:00
|
|
|
goto err;
|
|
|
|
if (EVP_PKEY_sign(pkctx, sigret, &sltmp, m, m_len) <= 0)
|
|
|
|
goto err;
|
|
|
|
*siglen = sltmp;
|
|
|
|
i = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-11-27 21:17:50 +08:00
|
|
|
EVP_PKEY_CTX_free(pkctx);
|
|
|
|
return i;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2020-07-26 15:32:05 +08:00
|
|
|
|
|
|
|
int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
|
|
|
|
unsigned int *siglen, EVP_PKEY *pkey)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return EVP_SignFinal_ex(ctx, sigret, siglen, pkey, NULL, NULL);
|
2020-07-26 15:32:05 +08:00
|
|
|
}
|