/src/openssl/crypto/evp/evp_key.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
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  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include <stdio.h>  | 
11  |  | #include "internal/cryptlib.h"  | 
12  |  | #include <openssl/x509.h>  | 
13  |  | #include <openssl/objects.h>  | 
14  |  | #include <openssl/evp.h>  | 
15  |  | #include <openssl/ui.h>  | 
16  |  |  | 
17  |  | #ifndef BUFSIZ  | 
18  |  | # define BUFSIZ 256  | 
19  |  | #endif  | 
20  |  |  | 
21  |  | /* should be init to zeros. */  | 
22  |  | static char prompt_string[80];  | 
23  |  |  | 
24  |  | void EVP_set_pw_prompt(const char *prompt)  | 
25  | 0  | { | 
26  | 0  |     if (prompt == NULL)  | 
27  | 0  |         prompt_string[0] = '\0';  | 
28  | 0  |     else { | 
29  | 0  |         strncpy(prompt_string, prompt, 79);  | 
30  | 0  |         prompt_string[79] = '\0';  | 
31  | 0  |     }  | 
32  | 0  | }  | 
33  |  |  | 
34  |  | char *EVP_get_pw_prompt(void)  | 
35  | 0  | { | 
36  | 0  |     if (prompt_string[0] == '\0')  | 
37  | 0  |         return NULL;  | 
38  | 0  |     else  | 
39  | 0  |         return prompt_string;  | 
40  | 0  | }  | 
41  |  |  | 
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  |  |  */  | 
47  |  | int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)  | 
48  | 0  | { | 
49  | 0  |     return EVP_read_pw_string_min(buf, 0, len, prompt, verify);  | 
50  | 0  | }  | 
51  |  |  | 
52  |  | int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,  | 
53  |  |                            int verify)  | 
54  | 0  | { | 
55  | 0  |     int ret = -1;  | 
56  | 0  |     char buff[BUFSIZ];  | 
57  | 0  |     UI *ui;  | 
58  |  | 
  | 
59  | 0  |     if ((prompt == NULL) && (prompt_string[0] != '\0'))  | 
60  | 0  |         prompt = prompt_string;  | 
61  | 0  |     ui = UI_new();  | 
62  | 0  |     if (ui == NULL)  | 
63  | 0  |         return ret;  | 
64  | 0  |     if (UI_add_input_string(ui, prompt, 0, buf, min,  | 
65  | 0  |                             (len >= BUFSIZ) ? BUFSIZ - 1 : len) < 0  | 
66  | 0  |         || (verify  | 
67  | 0  |             && UI_add_verify_string(ui, prompt, 0, buff, min,  | 
68  | 0  |                                     (len >= BUFSIZ) ? BUFSIZ - 1 : len,  | 
69  | 0  |                                     buf) < 0))  | 
70  | 0  |         goto end;  | 
71  | 0  |     ret = UI_process(ui);  | 
72  | 0  |     OPENSSL_cleanse(buff, BUFSIZ);  | 
73  | 0  |  end:  | 
74  | 0  |     UI_free(ui);  | 
75  | 0  |     return ret;  | 
76  | 0  | }  | 
77  |  |  | 
78  |  | int 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  | 0  | { | 
83  | 0  |     EVP_MD_CTX *c;  | 
84  | 0  |     unsigned char md_buf[EVP_MAX_MD_SIZE];  | 
85  | 0  |     int niv, nkey, addmd = 0;  | 
86  | 0  |     unsigned int mds = 0, i;  | 
87  | 0  |     int rv = 0;  | 
88  | 0  |     nkey = EVP_CIPHER_get_key_length(type);  | 
89  | 0  |     niv = EVP_CIPHER_get_iv_length(type);  | 
90  | 0  |     OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);  | 
91  | 0  |     OPENSSL_assert(niv >= 0 && niv <= EVP_MAX_IV_LENGTH);  | 
92  |  | 
  | 
93  | 0  |     if (data == NULL)  | 
94  | 0  |         return nkey;  | 
95  |  |  | 
96  | 0  |     c = EVP_MD_CTX_new();  | 
97  | 0  |     if (c == NULL)  | 
98  | 0  |         goto err;  | 
99  | 0  |     for (;;) { | 
100  | 0  |         if (!EVP_DigestInit_ex(c, md, NULL))  | 
101  | 0  |             goto err;  | 
102  | 0  |         if (addmd++)  | 
103  | 0  |             if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))  | 
104  | 0  |                 goto err;  | 
105  | 0  |         if (!EVP_DigestUpdate(c, data, datal))  | 
106  | 0  |             goto err;  | 
107  | 0  |         if (salt != NULL)  | 
108  | 0  |             if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))  | 
109  | 0  |                 goto err;  | 
110  | 0  |         if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))  | 
111  | 0  |             goto err;  | 
112  |  |  | 
113  | 0  |         for (i = 1; i < (unsigned int)count; i++) { | 
114  | 0  |             if (!EVP_DigestInit_ex(c, md, NULL))  | 
115  | 0  |                 goto err;  | 
116  | 0  |             if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))  | 
117  | 0  |                 goto err;  | 
118  | 0  |             if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))  | 
119  | 0  |                 goto err;  | 
120  | 0  |         }  | 
121  | 0  |         i = 0;  | 
122  | 0  |         if (nkey) { | 
123  | 0  |             for (;;) { | 
124  | 0  |                 if (nkey == 0)  | 
125  | 0  |                     break;  | 
126  | 0  |                 if (i == mds)  | 
127  | 0  |                     break;  | 
128  | 0  |                 if (key != NULL)  | 
129  | 0  |                     *(key++) = md_buf[i];  | 
130  | 0  |                 nkey--;  | 
131  | 0  |                 i++;  | 
132  | 0  |             }  | 
133  | 0  |         }  | 
134  | 0  |         if (niv && (i != mds)) { | 
135  | 0  |             for (;;) { | 
136  | 0  |                 if (niv == 0)  | 
137  | 0  |                     break;  | 
138  | 0  |                 if (i == mds)  | 
139  | 0  |                     break;  | 
140  | 0  |                 if (iv != NULL)  | 
141  | 0  |                     *(iv++) = md_buf[i];  | 
142  | 0  |                 niv--;  | 
143  | 0  |                 i++;  | 
144  | 0  |             }  | 
145  | 0  |         }  | 
146  | 0  |         if ((nkey == 0) && (niv == 0))  | 
147  | 0  |             break;  | 
148  | 0  |     }  | 
149  | 0  |     rv = EVP_CIPHER_get_key_length(type);  | 
150  | 0  |  err:  | 
151  | 0  |     EVP_MD_CTX_free(c);  | 
152  | 0  |     OPENSSL_cleanse(md_buf, sizeof(md_buf));  | 
153  | 0  |     return rv;  | 
154  | 0  | }  |