/src/openssl/crypto/evp/evp_key.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | /* should be init to zeros. */ |
18 | | static char prompt_string[80]; |
19 | | |
20 | | void EVP_set_pw_prompt(const char *prompt) |
21 | 0 | { |
22 | 0 | if (prompt == NULL) |
23 | 0 | prompt_string[0] = '\0'; |
24 | 0 | else { |
25 | 0 | strncpy(prompt_string, prompt, 79); |
26 | 0 | prompt_string[79] = '\0'; |
27 | 0 | } |
28 | 0 | } |
29 | | |
30 | | char *EVP_get_pw_prompt(void) |
31 | 0 | { |
32 | 0 | if (prompt_string[0] == '\0') |
33 | 0 | return NULL; |
34 | 0 | else |
35 | 0 | return prompt_string; |
36 | 0 | } |
37 | | |
38 | | /* |
39 | | * For historical reasons, the standard function for reading passwords is in |
40 | | * the DES library -- if someone ever wants to disable DES, this function |
41 | | * will fail |
42 | | */ |
43 | | int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify) |
44 | 0 | { |
45 | 0 | return EVP_read_pw_string_min(buf, 0, len, prompt, verify); |
46 | 0 | } |
47 | | |
48 | | int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt, |
49 | | int verify) |
50 | 0 | { |
51 | 0 | int ret = -1; |
52 | 0 | char buff[BUFSIZ]; |
53 | 0 | UI *ui; |
54 | 0 |
|
55 | 0 | if ((prompt == NULL) && (prompt_string[0] != '\0')) |
56 | 0 | prompt = prompt_string; |
57 | 0 | ui = UI_new(); |
58 | 0 | if (ui == NULL) |
59 | 0 | return ret; |
60 | 0 | if (UI_add_input_string(ui, prompt, 0, buf, min, |
61 | 0 | (len >= BUFSIZ) ? BUFSIZ - 1 : len) < 0 |
62 | 0 | || (verify |
63 | 0 | && UI_add_verify_string(ui, prompt, 0, buff, min, |
64 | 0 | (len >= BUFSIZ) ? BUFSIZ - 1 : len, |
65 | 0 | buf) < 0)) |
66 | 0 | goto end; |
67 | 0 | ret = UI_process(ui); |
68 | 0 | OPENSSL_cleanse(buff, BUFSIZ); |
69 | 0 | end: |
70 | 0 | UI_free(ui); |
71 | 0 | return ret; |
72 | 0 | } |
73 | | |
74 | | int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, |
75 | | const unsigned char *salt, const unsigned char *data, |
76 | | int datal, int count, unsigned char *key, |
77 | | unsigned char *iv) |
78 | 0 | { |
79 | 0 | EVP_MD_CTX *c; |
80 | 0 | unsigned char md_buf[EVP_MAX_MD_SIZE]; |
81 | 0 | int niv, nkey, addmd = 0; |
82 | 0 | unsigned int mds = 0, i; |
83 | 0 | int rv = 0; |
84 | 0 | nkey = EVP_CIPHER_key_length(type); |
85 | 0 | niv = EVP_CIPHER_iv_length(type); |
86 | 0 | OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH); |
87 | 0 | OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH); |
88 | 0 |
|
89 | 0 | if (data == NULL) |
90 | 0 | return nkey; |
91 | 0 | |
92 | 0 | c = EVP_MD_CTX_new(); |
93 | 0 | if (c == NULL) |
94 | 0 | goto err; |
95 | 0 | for (;;) { |
96 | 0 | if (!EVP_DigestInit_ex(c, md, NULL)) |
97 | 0 | goto err; |
98 | 0 | if (addmd++) |
99 | 0 | if (!EVP_DigestUpdate(c, &(md_buf[0]), mds)) |
100 | 0 | goto err; |
101 | 0 | if (!EVP_DigestUpdate(c, data, datal)) |
102 | 0 | goto err; |
103 | 0 | if (salt != NULL) |
104 | 0 | if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN)) |
105 | 0 | goto err; |
106 | 0 | if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds)) |
107 | 0 | goto err; |
108 | 0 | |
109 | 0 | for (i = 1; i < (unsigned int)count; i++) { |
110 | 0 | if (!EVP_DigestInit_ex(c, md, NULL)) |
111 | 0 | goto err; |
112 | 0 | if (!EVP_DigestUpdate(c, &(md_buf[0]), mds)) |
113 | 0 | goto err; |
114 | 0 | if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds)) |
115 | 0 | goto err; |
116 | 0 | } |
117 | 0 | i = 0; |
118 | 0 | if (nkey) { |
119 | 0 | for (;;) { |
120 | 0 | if (nkey == 0) |
121 | 0 | break; |
122 | 0 | if (i == mds) |
123 | 0 | break; |
124 | 0 | if (key != NULL) |
125 | 0 | *(key++) = md_buf[i]; |
126 | 0 | nkey--; |
127 | 0 | i++; |
128 | 0 | } |
129 | 0 | } |
130 | 0 | if (niv && (i != mds)) { |
131 | 0 | for (;;) { |
132 | 0 | if (niv == 0) |
133 | 0 | break; |
134 | 0 | if (i == mds) |
135 | 0 | break; |
136 | 0 | if (iv != NULL) |
137 | 0 | *(iv++) = md_buf[i]; |
138 | 0 | niv--; |
139 | 0 | i++; |
140 | 0 | } |
141 | 0 | } |
142 | 0 | if ((nkey == 0) && (niv == 0)) |
143 | 0 | break; |
144 | 0 | } |
145 | 0 | rv = EVP_CIPHER_key_length(type); |
146 | 0 | err: |
147 | 0 | EVP_MD_CTX_free(c); |
148 | 0 | OPENSSL_cleanse(md_buf, sizeof(md_buf)); |
149 | 0 | return rv; |
150 | 0 | } |