ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/rsa.c
Revision: 1015
Committed: Sun Oct 25 00:08:06 2009 UTC (16 years, 11 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/rsa.c
File size: 6139 byte(s)
Log Message:
- fixed broken cryptlinks as reported by henri
- removed deprecated inet_misc.c
- removed __attribute__ format, which sometimes really is usefull, even if
  unportable unless gcc, but for now they only give false warnings

File Contents

# Content
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
27 #include <openssl/pem.h>
28 #include <openssl/rand.h>
29 #include <openssl/rsa.h>
30 #include <openssl/md5.h>
31 #include <openssl/bn.h>
32 #include <openssl/evp.h>
33 #include <openssl/err.h>
34 #include <openssl/opensslv.h>
35
36 #include "memory.h"
37 #include "rsa.h"
38 #include "s_conf.h"
39 #include "s_log.h"
40 #include "client.h" /* CIPHERKEYLEN .. eww */
41 #include "ircd.h" /* bio_spare_fd */
42
43
44 /*
45 * report_crypto_errors - Dump crypto error list to log
46 */
47 void
48 report_crypto_errors(void)
49 {
50 unsigned long e = 0;
51 unsigned long cnt = 0;
52
53 ERR_load_crypto_strings();
54
55 while ((cnt < 100) && (e = ERR_get_error()))
56 {
57 ilog(L_CRIT, "SSL error: %s", ERR_error_string(e, 0));
58 cnt++;
59 }
60 }
61
62 /*
63 * verify_private_key - reread private key and verify against inmem key
64 */
65 int
66 verify_private_key(void)
67 {
68 BIO *file;
69 RSA *key;
70 RSA *mkey;
71
72 /* If the rsa_private_key directive isn't found, error out. */
73 if (ServerInfo.rsa_private_key == NULL)
74 {
75 ilog(L_NOTICE, "rsa_private_key in serverinfo{} is not defined.");
76 return -1;
77 }
78
79 /* If rsa_private_key_file isn't available, error out. */
80 if (ServerInfo.rsa_private_key_file == NULL)
81 {
82 ilog(L_NOTICE, "Internal error: rsa_private_key_file isn't defined.");
83 return -1;
84 }
85
86 if (bio_spare_fd > -1)
87 close(bio_spare_fd);
88
89 file = BIO_new_file(ServerInfo.rsa_private_key_file, "r");
90
91 /*
92 * If BIO_new_file returned NULL (according to OpenSSL docs), then
93 * an error occurred.
94 */
95 if (file == NULL)
96 {
97 bio_spare_fd = save_spare_fd("SSL private key validation");
98 ilog(L_NOTICE, "Failed to open private key file - can't validate it");
99 return -1;
100 }
101
102 /*
103 * jdc -- Let's do this a little differently. According to the
104 * OpenSSL documentation, you need to METHOD_free(key) before
105 * assigning it. Don't believe me? Check out the following
106 * URL: http://www.openssl.org/docs/crypto/pem.html#BUGS
107 * P.S. -- I have no idea why the key= assignment has to be typecasted.
108 * For some reason the system thinks PEM_read_bio_RSAPrivateKey
109 * is returning an int, not a RSA *.
110 * androsyn -- Thats because you didn't have a prototype and including
111 * pem.h breaks things for some reason..
112 */
113 key = (RSA *) PEM_read_bio_RSAPrivateKey(file, NULL, 0, NULL);
114
115 if (key == NULL)
116 {
117 ilog(L_NOTICE, "PEM_read_bio_RSAPrivateKey() failed; possibly not RSA?");
118 report_crypto_errors();
119 return -1;
120 }
121
122 BIO_set_close(file, BIO_CLOSE);
123 BIO_free(file);
124 bio_spare_fd = save_spare_fd("SSL private key validation");
125
126 mkey = ServerInfo.rsa_private_key;
127
128 /*
129 * Compare the in-memory key to the key we just loaded above. If
130 * any of the portions don't match, then logically we have a different
131 * in-memory key vs. the one we just loaded. This is bad, mmmkay?
132 */
133 if (mkey->pad != key->pad)
134 ilog(L_CRIT, "Private key corrupted: pad %i != pad %i",
135 mkey->pad, key->pad);
136
137 if (mkey->version != key->version)
138 ilog(L_CRIT, "Private key corrupted: version %li != version %li",
139 mkey->version, key->version);
140
141 if (BN_cmp(mkey->n, key->n))
142 ilog(L_CRIT, "Private key corrupted: n differs");
143 if (BN_cmp(mkey->e, key->e))
144 ilog(L_CRIT, "Private key corrupted: e differs");
145 if (BN_cmp(mkey->d, key->d))
146 ilog(L_CRIT, "Private key corrupted: d differs");
147 if (BN_cmp(mkey->p, key->p))
148 ilog(L_CRIT, "Private key corrupted: p differs");
149 if (BN_cmp(mkey->q, key->q))
150 ilog(L_CRIT, "Private key corrupted: q differs");
151 if (BN_cmp(mkey->dmp1, key->dmp1))
152 ilog(L_CRIT, "Private key corrupted: dmp1 differs");
153 if (BN_cmp(mkey->dmq1, key->dmq1))
154 ilog(L_CRIT, "Private key corrupted: dmq1 differs");
155 if (BN_cmp(mkey->iqmp, key->iqmp))
156 ilog(L_CRIT, "Private key corrupted: iqmp differs");
157
158 RSA_free(key);
159 return 0;
160 }
161
162
163 static void
164 binary_to_hex(unsigned char *bin, char *hex, int length)
165 {
166 static const char trans[] = "0123456789ABCDEF";
167 int i;
168
169 for (i = 0; i < length; i++)
170 {
171 hex[i << 1] = trans[bin[i] >> 4];
172 hex[(i << 1) + 1] = trans[bin[i] & 0xf];
173 }
174
175 hex[i << 1] = '\0';
176 }
177
178 int
179 get_randomness(unsigned char *buf, int length)
180 {
181 /* Seed OpenSSL PRNG with EGD enthropy pool -kre */
182 if (ConfigFileEntry.use_egd &&
183 (ConfigFileEntry.egdpool_path != NULL))
184 {
185 if (RAND_egd(ConfigFileEntry.egdpool_path) == -1)
186 return -1;
187 }
188
189 if (RAND_status())
190 return (RAND_bytes(buf, length));
191 else /* XXX - abort? */
192 return (RAND_pseudo_bytes(buf, length));
193 }
194
195 int
196 generate_challenge(char **r_challenge, char **r_response, RSA *rsa)
197 {
198 unsigned char secret[32], *tmp;
199 unsigned long length;
200 int ret = -1;
201
202 if (!rsa)
203 return -1;
204 get_randomness(secret, 32);
205 *r_response = MyMalloc(65);
206 binary_to_hex(secret, *r_response, 32);
207
208 length = RSA_size(rsa);
209 tmp = MyMalloc(length);
210 ret = RSA_public_encrypt(32, secret, tmp, rsa, RSA_PKCS1_PADDING);
211
212 *r_challenge = MyMalloc((length << 1) + 1);
213 binary_to_hex( tmp, *r_challenge, length);
214 (*r_challenge)[length<<1] = 0;
215 MyFree(tmp);
216
217 if (ret < 0)
218 {
219 report_crypto_errors();
220 return -1;
221 }
222 return 0;
223 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision