2016-05-18 02:24:46 +08:00
|
|
|
/*
|
2021-06-17 20:24:59 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2015-01-22 11:40:55 +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/x509.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/evp.h>
|
2001-05-07 07:19:37 +08:00
|
|
|
#include <openssl/ui.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-05-10 03:29:48 +08:00
|
|
|
#ifndef BUFSIZ
|
|
|
|
# define BUFSIZ 256
|
|
|
|
#endif
|
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
/* should be init to zeros. */
|
|
|
|
static char prompt_string[80];
|
|
|
|
|
2004-03-16 07:15:26 +08:00
|
|
|
void EVP_set_pw_prompt(const char *prompt)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (prompt == NULL)
|
|
|
|
prompt_string[0] = '\0';
|
|
|
|
else {
|
|
|
|
strncpy(prompt_string, prompt, 79);
|
|
|
|
prompt_string[79] = '\0';
|
|
|
|
}
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
char *EVP_get_pw_prompt(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (prompt_string[0] == '\0')
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
else
|
2017-10-17 22:04:09 +08:00
|
|
|
return prompt_string;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* For historical reasons, the standard function for reading passwords is in
|
|
|
|
* the DES library -- if someone ever wants to disable DES, this function
|
|
|
|
* will fail
|
|
|
|
*/
|
1999-04-20 05:31:43 +08:00
|
|
|
int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
|
|
|
|
}
|
2001-05-07 07:19:37 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
|
|
|
|
int verify)
|
|
|
|
{
|
2017-07-05 16:26:25 +08:00
|
|
|
int ret = -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
char buff[BUFSIZ];
|
|
|
|
UI *ui;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((prompt == NULL) && (prompt_string[0] != '\0'))
|
|
|
|
prompt = prompt_string;
|
|
|
|
ui = UI_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (ui == NULL)
|
2017-07-05 16:26:25 +08:00
|
|
|
return ret;
|
|
|
|
if (UI_add_input_string(ui, prompt, 0, buf, min,
|
|
|
|
(len >= BUFSIZ) ? BUFSIZ - 1 : len) < 0
|
|
|
|
|| (verify
|
|
|
|
&& UI_add_verify_string(ui, prompt, 0, buff, min,
|
|
|
|
(len >= BUFSIZ) ? BUFSIZ - 1 : len,
|
|
|
|
buf) < 0))
|
|
|
|
goto end;
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = UI_process(ui);
|
|
|
|
OPENSSL_cleanse(buff, BUFSIZ);
|
2017-07-05 16:26:25 +08:00
|
|
|
end:
|
|
|
|
UI_free(ui);
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
|
|
|
|
const unsigned char *salt, const unsigned char *data,
|
|
|
|
int datal, int count, unsigned char *key,
|
|
|
|
unsigned char *iv)
|
|
|
|
{
|
2015-11-27 21:17:50 +08:00
|
|
|
EVP_MD_CTX *c;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char md_buf[EVP_MAX_MD_SIZE];
|
|
|
|
int niv, nkey, addmd = 0;
|
|
|
|
unsigned int mds = 0, i;
|
|
|
|
int rv = 0;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
nkey = EVP_CIPHER_get_key_length(type);
|
|
|
|
niv = EVP_CIPHER_get_iv_length(type);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
|
|
|
|
OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (data == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return nkey;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-12-02 07:49:35 +08:00
|
|
|
c = EVP_MD_CTX_new();
|
2015-11-27 21:17:50 +08:00
|
|
|
if (c == NULL)
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
for (;;) {
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestInit_ex(c, md, NULL))
|
2015-10-08 20:38:57 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (addmd++)
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestUpdate(c, data, datal))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (salt != NULL)
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (i = 1; i < (unsigned int)count; i++) {
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestInit_ex(c, md, NULL))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-11-27 21:17:50 +08:00
|
|
|
if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
if (nkey) {
|
|
|
|
for (;;) {
|
|
|
|
if (nkey == 0)
|
|
|
|
break;
|
|
|
|
if (i == mds)
|
|
|
|
break;
|
|
|
|
if (key != NULL)
|
|
|
|
*(key++) = md_buf[i];
|
|
|
|
nkey--;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (niv && (i != mds)) {
|
|
|
|
for (;;) {
|
|
|
|
if (niv == 0)
|
|
|
|
break;
|
|
|
|
if (i == mds)
|
|
|
|
break;
|
|
|
|
if (iv != NULL)
|
|
|
|
*(iv++) = md_buf[i];
|
|
|
|
niv--;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((nkey == 0) && (niv == 0))
|
|
|
|
break;
|
|
|
|
}
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
rv = EVP_CIPHER_get_key_length(type);
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(c);
|
2015-10-08 20:38:57 +08:00
|
|
|
OPENSSL_cleanse(md_buf, sizeof(md_buf));
|
2015-01-22 11:40:55 +08:00
|
|
|
return rv;
|
|
|
|
}
|