2016-05-17 14:24:46 -04:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2018-12-06 13:40:06 +01:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 14:24:46 -04: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 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 16:56:48 +02:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
2019-09-28 00:45:33 +02:00
|
|
|
#include "crypto/evp.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2020-09-24 10:42:23 +01:00
|
|
|
int EVP_SignFinal_ex(EVP_MD_CTX *ctx, unsigned char *sigret,
|
2020-10-15 12:55:50 +03:00
|
|
|
unsigned int *siglen, EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
|
2020-09-24 10:42:23 +01:00
|
|
|
const char *propq)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned char m[EVP_MAX_MD_SIZE];
|
2015-05-06 10:16:55 +01:00
|
|
|
unsigned int m_len = 0;
|
2015-12-02 13:57:04 +00:00
|
|
|
int i = 0;
|
|
|
|
size_t sltmp;
|
2015-01-22 03:40:55 +00:00
|
|
|
EVP_PKEY_CTX *pkctx = NULL;
|
2006-04-19 17:05:59 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
*siglen = 0;
|
2015-11-27 14:17:50 +01:00
|
|
|
if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_FINALISE)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, m, &m_len))
|
|
|
|
goto err;
|
|
|
|
} else {
|
2015-05-06 10:16:55 +01:00
|
|
|
int rv = 0;
|
2015-12-02 00:49:35 +01:00
|
|
|
EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
|
2020-07-26 17:32:05 +10:00
|
|
|
|
2015-11-27 14:17:50 +01:00
|
|
|
if (tmp_ctx == NULL) {
|
2020-07-26 17:32:05 +10:00
|
|
|
EVPerr(0, ERR_R_MALLOC_FAILURE);
|
2015-11-27 14:17:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rv = EVP_MD_CTX_copy_ex(tmp_ctx, ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (rv)
|
2015-11-27 14:17:50 +01:00
|
|
|
rv = EVP_DigestFinal_ex(tmp_ctx, m, &m_len);
|
2015-12-02 00:49:35 +01:00
|
|
|
EVP_MD_CTX_free(tmp_ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!rv)
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-19 17:05:59 +00:00
|
|
|
|
2015-12-02 13:57:04 +00:00
|
|
|
sltmp = (size_t)EVP_PKEY_size(pkey);
|
|
|
|
i = 0;
|
2020-07-26 17:32:05 +10:00
|
|
|
pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
|
2015-12-02 13:57:04 +00:00
|
|
|
if (pkctx == NULL)
|
|
|
|
goto err;
|
|
|
|
if (EVP_PKEY_sign_init(pkctx) <= 0)
|
|
|
|
goto err;
|
2015-11-30 10:25:36 +01:00
|
|
|
if (EVP_PKEY_CTX_set_signature_md(pkctx, EVP_MD_CTX_md(ctx)) <= 0)
|
2015-12-02 13:57:04 +00:00
|
|
|
goto err;
|
|
|
|
if (EVP_PKEY_sign(pkctx, sigret, &sltmp, m, m_len) <= 0)
|
|
|
|
goto err;
|
|
|
|
*siglen = sltmp;
|
|
|
|
i = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
2015-11-27 14:17:50 +01:00
|
|
|
EVP_PKEY_CTX_free(pkctx);
|
|
|
|
return i;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2020-07-26 17:32:05 +10:00
|
|
|
|
|
|
|
int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
|
|
|
|
unsigned int *siglen, EVP_PKEY *pkey)
|
|
|
|
{
|
2020-09-24 10:42:23 +01:00
|
|
|
return EVP_SignFinal_ex(ctx, sigret, siglen, pkey, NULL, NULL);
|
2020-07-26 17:32:05 +10:00
|
|
|
}
|