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