Debian Codegraph @

contrib/mod_auth_otp/crypto.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_auth_otp OpenSSL interface
3 * Copyright (c) 2015-2021 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_auth_otp.h"
26 #include "crypto.h"
27
28 int auth_otp_crypto_init(void) {
29 return 0;
30 }
31
32 void auth_otp_crypto_free(int flags) {
33 /* Only call EVP_cleanup() et al if other OpenSSL-using modules are not
34 * present. If we called EVP_cleanup() here during a restart,
35 * and other modules want to use OpenSSL, we may be depriving those modules
36 * of OpenSSL functionality.
37 *
38 * At the moment, the modules known to use OpenSSL are mod_ldap,
39 * mod_sftp, mod_sql, and mod_sql_passwd, and mod_tls.
40 */
41 if (pr_module_get("mod_digest.c") == NULL &&
42 pr_module_get("mod_ldap.c") == NULL &&
43 pr_module_get("mod_proxy.c") == NULL &&
44 pr_module_get("mod_radius.c") == NULL &&
45 pr_module_get("mod_sftp.c") == NULL &&
46 pr_module_get("mod_sql.c") == NULL &&
47 pr_module_get("mod_sql_passwd.c") == NULL &&
48 pr_module_get("mod_tls.c") == NULL) {
49
50 #if OPENSSL_VERSION_NUMBER < 0x10100000L
51 ERR_free_strings();
52 EVP_cleanup();
53 RAND_cleanup();
54 # if OPENSSL_VERSION_NUMBER >= 0x10000001L
55 ERR_remove_state(0);
56 # endif /* OpenSSL prior to 1.0.0-beta1 */
57 #endif
58 }
59 }
60
61 const char *auth_otp_crypto_get_errors(void) {
62 unsigned int count = 0;
63 unsigned long e = ERR_get_error();
64 BIO *bio = NULL;
65 char *data = NULL;
66 long datalen;
67 const char *str = "(unknown)";
68
69 /* Use ERR_print_errors() and a memory BIO to build up a string with
70 * all of the error messages from the error queue.
71 */
72
73 if (e) {
74 bio = BIO_new(BIO_s_mem());
75 }
76
77 while (e) {
78 pr_signals_handle();
79 BIO_printf(bio, "\n (%u) %s", ++count, ERR_error_string(e, NULL));
80 e = ERR_get_error();
81 }
82
83 datalen = BIO_get_mem_data(bio, &data);
84 if (data) {
85 data[datalen] = '\0';
86 str = pstrndup(auth_otp_pool, data, datalen-1);
87 }
88
89 if (bio) {
90 BIO_free(bio);
91 }
92
93 return str;
94 }
95
96 int auth_otp_hmac(const EVP_MD *md, const unsigned char *key, size_t key_len,
97 const unsigned char *data, size_t data_len, unsigned char *mac,
98 size_t *mac_len) {
99
100 if ((key == NULL || key_len == 0) ||
101 (data == NULL || data_len == 0) ||
102 (mac == NULL || mac_len == NULL)) {
103 errno = EINVAL;
104 return -1;
105 }
106
107 if (HMAC(md, key, key_len, data, data_len, mac,
108 (unsigned int *) mac_len) == NULL) {
109 (void) pr_log_writefile(auth_otp_logfd, MOD_AUTH_OTP_VERSION,
110 "HMAC error: %s", auth_otp_crypto_get_errors());
111 errno = EPERM;
112 return -1;
113 }
114
115 return 0;
116 }