]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_key.c
Check DSA parameters for excessive sizes before validating
[thirdparty/openssl.git] / crypto / evp / evp_key.c
CommitLineData
62867571 1/*
b6461792 2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/x509.h>
13#include <openssl/objects.h>
14#include <openssl/evp.h>
a63d5eaa 15#include <openssl/ui.h>
d02b48c6 16
2e9d61ec
LE
17#ifndef BUFSIZ
18# define BUFSIZ 256
19#endif
20
d02b48c6
RE
21/* should be init to zeros. */
22static char prompt_string[80];
23
875a644a 24void EVP_set_pw_prompt(const char *prompt)
0f113f3e
MC
25{
26 if (prompt == NULL)
27 prompt_string[0] = '\0';
28 else {
29 strncpy(prompt_string, prompt, 79);
30 prompt_string[79] = '\0';
31 }
32}
d02b48c6 33
6b691a5c 34char *EVP_get_pw_prompt(void)
0f113f3e
MC
35{
36 if (prompt_string[0] == '\0')
26a7d938 37 return NULL;
0f113f3e 38 else
26a7d938 39 return prompt_string;
0f113f3e 40}
d02b48c6 41
0f113f3e
MC
42/*
43 * For historical reasons, the standard function for reading passwords is in
44 * the DES library -- if someone ever wants to disable DES, this function
45 * will fail
46 */
6b691a5c 47int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
0f113f3e
MC
48{
49 return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
50}
a63d5eaa 51
0f113f3e
MC
52int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
53 int verify)
54{
b96dba9e 55 int ret = -1;
0f113f3e
MC
56 char buff[BUFSIZ];
57 UI *ui;
d02b48c6 58
0f113f3e
MC
59 if ((prompt == NULL) && (prompt_string[0] != '\0'))
60 prompt = prompt_string;
61 ui = UI_new();
90945fa3 62 if (ui == NULL)
b96dba9e
RL
63 return ret;
64 if (UI_add_input_string(ui, prompt, 0, buf, min,
65 (len >= BUFSIZ) ? BUFSIZ - 1 : len) < 0
66 || (verify
67 && UI_add_verify_string(ui, prompt, 0, buff, min,
68 (len >= BUFSIZ) ? BUFSIZ - 1 : len,
69 buf) < 0))
70 goto end;
0f113f3e 71 ret = UI_process(ui);
0f113f3e 72 OPENSSL_cleanse(buff, BUFSIZ);
b96dba9e
RL
73 end:
74 UI_free(ui);
0f113f3e
MC
75 return ret;
76}
d02b48c6 77
0f113f3e
MC
78int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
79 const unsigned char *salt, const unsigned char *data,
80 int datal, int count, unsigned char *key,
81 unsigned char *iv)
82{
77a01145 83 EVP_MD_CTX *c;
0f113f3e
MC
84 unsigned char md_buf[EVP_MAX_MD_SIZE];
85 int niv, nkey, addmd = 0;
86 unsigned int mds = 0, i;
87 int rv = 0;
ed576acd
TM
88 nkey = EVP_CIPHER_get_key_length(type);
89 niv = EVP_CIPHER_get_iv_length(type);
0f113f3e 90 OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
6f22bcd6 91 OPENSSL_assert(niv >= 0 && niv <= EVP_MAX_IV_LENGTH);
d02b48c6 92
0f113f3e 93 if (data == NULL)
26a7d938 94 return nkey;
d02b48c6 95
bfb0641f 96 c = EVP_MD_CTX_new();
77a01145
RL
97 if (c == NULL)
98 goto err;
0f113f3e 99 for (;;) {
77a01145 100 if (!EVP_DigestInit_ex(c, md, NULL))
3f6c7691 101 goto err;
0f113f3e 102 if (addmd++)
77a01145 103 if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
0f113f3e 104 goto err;
77a01145 105 if (!EVP_DigestUpdate(c, data, datal))
0f113f3e
MC
106 goto err;
107 if (salt != NULL)
77a01145 108 if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))
0f113f3e 109 goto err;
77a01145 110 if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
0f113f3e 111 goto err;
d02b48c6 112
0f113f3e 113 for (i = 1; i < (unsigned int)count; i++) {
77a01145 114 if (!EVP_DigestInit_ex(c, md, NULL))
0f113f3e 115 goto err;
77a01145 116 if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
0f113f3e 117 goto err;
77a01145 118 if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
0f113f3e
MC
119 goto err;
120 }
121 i = 0;
122 if (nkey) {
123 for (;;) {
124 if (nkey == 0)
125 break;
126 if (i == mds)
127 break;
128 if (key != NULL)
129 *(key++) = md_buf[i];
130 nkey--;
131 i++;
132 }
133 }
134 if (niv && (i != mds)) {
135 for (;;) {
136 if (niv == 0)
137 break;
138 if (i == mds)
139 break;
140 if (iv != NULL)
141 *(iv++) = md_buf[i];
142 niv--;
143 i++;
144 }
145 }
146 if ((nkey == 0) && (niv == 0))
147 break;
148 }
ed576acd 149 rv = EVP_CIPHER_get_key_length(type);
0f113f3e 150 err:
bfb0641f 151 EVP_MD_CTX_free(c);
3f6c7691 152 OPENSSL_cleanse(md_buf, sizeof(md_buf));
0f113f3e
MC
153 return rv;
154}