1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 2000-2014 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file rsa.c |
23 |
|
|
* \brief Functions for use with RSA public key cryptography. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
michael |
1307 |
#ifdef HAVE_LIBCRYPTO |
29 |
adx |
30 |
#include <openssl/pem.h> |
30 |
|
|
#include <openssl/rand.h> |
31 |
|
|
#include <openssl/rsa.h> |
32 |
|
|
#include <openssl/bn.h> |
33 |
|
|
#include <openssl/evp.h> |
34 |
|
|
#include <openssl/err.h> |
35 |
michael |
1013 |
#include <openssl/opensslv.h> |
36 |
adx |
30 |
|
37 |
|
|
#include "memory.h" |
38 |
|
|
#include "rsa.h" |
39 |
michael |
1309 |
#include "conf.h" |
40 |
|
|
#include "log.h" |
41 |
adx |
30 |
|
42 |
|
|
|
43 |
|
|
/* |
44 |
|
|
* report_crypto_errors - Dump crypto error list to log |
45 |
|
|
*/ |
46 |
|
|
void |
47 |
|
|
report_crypto_errors(void) |
48 |
|
|
{ |
49 |
michael |
1752 |
unsigned long e = 0; |
50 |
adx |
30 |
|
51 |
michael |
1752 |
while ((e = ERR_get_error())) |
52 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "SSL error: %s", ERR_error_string(e, 0)); |
53 |
adx |
30 |
} |
54 |
|
|
|
55 |
michael |
4113 |
void |
56 |
michael |
4110 |
binary_to_hex(const unsigned char *bin, char *hex, unsigned int length) |
57 |
adx |
30 |
{ |
58 |
|
|
static const char trans[] = "0123456789ABCDEF"; |
59 |
|
|
|
60 |
michael |
4110 |
for (const unsigned char *end = bin + length; bin < end; ++bin) |
61 |
adx |
30 |
{ |
62 |
michael |
4110 |
*hex++ = trans[*bin >> 4]; |
63 |
|
|
*hex++ = trans[*bin & 0xf]; |
64 |
adx |
30 |
} |
65 |
|
|
|
66 |
michael |
4110 |
*hex = '\0'; |
67 |
adx |
30 |
} |
68 |
|
|
|
69 |
|
|
int |
70 |
|
|
get_randomness(unsigned char *buf, int length) |
71 |
|
|
{ |
72 |
michael |
2134 |
/* Seed OpenSSL PRNG with EGD enthropy pool -kre */ |
73 |
michael |
3335 |
if (ConfigFileEntry.use_egd && ConfigFileEntry.egdpool_path) |
74 |
michael |
2134 |
if (RAND_egd(ConfigFileEntry.egdpool_path) == -1) |
75 |
|
|
return -1; |
76 |
adx |
30 |
|
77 |
|
|
if (RAND_status()) |
78 |
michael |
2134 |
return RAND_bytes(buf, length); |
79 |
|
|
/* XXX - abort? */ |
80 |
|
|
return RAND_pseudo_bytes(buf, length); |
81 |
adx |
30 |
} |
82 |
|
|
|
83 |
|
|
int |
84 |
|
|
generate_challenge(char **r_challenge, char **r_response, RSA *rsa) |
85 |
|
|
{ |
86 |
michael |
3335 |
unsigned char secret[32], *tmp = NULL; |
87 |
|
|
unsigned long length = 0; |
88 |
adx |
30 |
int ret = -1; |
89 |
|
|
|
90 |
|
|
if (!rsa) |
91 |
michael |
1307 |
return -1; |
92 |
|
|
|
93 |
adx |
30 |
get_randomness(secret, 32); |
94 |
michael |
3504 |
*r_response = MyCalloc(65); |
95 |
adx |
30 |
binary_to_hex(secret, *r_response, 32); |
96 |
|
|
|
97 |
|
|
length = RSA_size(rsa); |
98 |
michael |
3504 |
tmp = MyCalloc(length); |
99 |
adx |
30 |
ret = RSA_public_encrypt(32, secret, tmp, rsa, RSA_PKCS1_PADDING); |
100 |
|
|
|
101 |
michael |
3504 |
*r_challenge = MyCalloc((length << 1) + 1); |
102 |
adx |
30 |
binary_to_hex( tmp, *r_challenge, length); |
103 |
michael |
3335 |
(*r_challenge)[length << 1] = 0; |
104 |
adx |
30 |
MyFree(tmp); |
105 |
|
|
|
106 |
|
|
if (ret < 0) |
107 |
|
|
{ |
108 |
|
|
report_crypto_errors(); |
109 |
|
|
return -1; |
110 |
|
|
} |
111 |
michael |
1307 |
|
112 |
adx |
30 |
return 0; |
113 |
|
|
} |
114 |
michael |
1307 |
#endif |