ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/rsa.c
Revision: 69
Committed: Tue Oct 4 16:09:51 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 6335 byte(s)
Log Message:
- splitted ircd/libio, all headers connected with libio sources have been
  moved for internal use only. To use libio interface, include "libio.h"
  (which is already done in "stdinc.h")


File Contents

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

Properties

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