ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/rsa.c
(Generate patch)

Comparing:
ircd-hybrid/src/rsa.c (file contents), Revision 30 by adx, Sun Oct 2 20:03:27 2005 UTC vs.
ircd-hybrid/trunk/src/rsa.c (file contents), Revision 4254 by michael, Fri Jul 18 19:07:27 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  rsa.c: Functions for use with RSA public key cryptography.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 2000-2014 ircd-hybrid development team
5   *
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
# Line 18 | Line 17
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
21 *
22 *  $Id: rsa.c,v 7.34 2005/07/26 03:33:05 adx Exp $
20   */
21  
22 < #include "stdinc.h"
22 > /*! \file rsa.c
23 > * \brief Functions for use with RSA public key cryptography.
24 > * \version $Id$
25 > */
26  
27 + #include "stdinc.h"
28 + #ifdef HAVE_LIBCRYPTO
29   #include <openssl/pem.h>
30   #include <openssl/rand.h>
31   #include <openssl/rsa.h>
30 #include <openssl/md5.h>
32   #include <openssl/bn.h>
33   #include <openssl/evp.h>
34   #include <openssl/err.h>
35 + #include <openssl/opensslv.h>
36  
37   #include "memory.h"
38   #include "rsa.h"
39 < #include "tools.h"
40 < #include "s_conf.h"
39 < #include "s_log.h"
40 < #include "client.h" /* CIPHERKEYLEN .. eww */
41 < #include "ircd.h" /* bio_spare_fd */
39 > #include "conf.h"
40 > #include "log.h"
41  
43 static void binary_to_hex(unsigned char *bin, char *hex, int length);
42  
43   /*
44   * report_crypto_errors - Dump crypto error list to log
# Line 48 | Line 46 | static void binary_to_hex(unsigned char
46   void
47   report_crypto_errors(void)
48   {
49 <  unsigned long e   = 0;
52 <  unsigned long cnt = 0;
53 <
54 <  ERR_load_crypto_strings();
55 <
56 <  while ((cnt < 100) && (e = ERR_get_error()))
57 <  {
58 <    ilog(L_CRIT, "SSL error: %s", ERR_error_string(e, 0));
59 <    cnt++;
60 <  }
61 < }
62 <
63 < /*
64 < * verify_private_key - reread private key and verify against inmem key
65 < */
66 < int
67 < verify_private_key(void)
68 < {
69 <  BIO *file;
70 <  RSA *key;
71 <  RSA *mkey;
72 <
73 <  /* If the rsa_private_key directive isn't found, error out. */
74 <  if (ServerInfo.rsa_private_key == NULL)
75 <  {
76 <    ilog(L_NOTICE, "rsa_private_key in serverinfo{} is not defined.");
77 <    return -1;
78 <  }
79 <
80 <  /* If rsa_private_key_file isn't available, error out. */
81 <  if (ServerInfo.rsa_private_key_file == NULL)
82 <  {
83 <    ilog(L_NOTICE, "Internal error: rsa_private_key_file isn't defined.");
84 <    return -1;
85 <  }
86 <
87 <  if (bio_spare_fd > -1)
88 <    close(bio_spare_fd);
89 <
90 <  file = BIO_new_file(ServerInfo.rsa_private_key_file, "r");
91 <
92 <  /*
93 <   * If BIO_new_file returned NULL (according to OpenSSL docs), then
94 <   * an error occurred.
95 <   */
96 <  if (file == NULL)
97 <  {
98 <    bio_spare_fd = save_spare_fd("SSL private key validation");
99 <    ilog(L_NOTICE, "Failed to open private key file - can't validate it");
100 <    return -1;
101 <  }
102 <
103 <  /*
104 <   * jdc -- Let's do this a little differently.  According to the
105 <   *        OpenSSL documentation, you need to METHOD_free(key) before
106 <   *        assigning it.  Don't believe me?  Check out the following
107 <   *        URL:  http://www.openssl.org/docs/crypto/pem.html#BUGS
108 <   * P.S. -- I have no idea why the key= assignment has to be typecasted.
109 <   *         For some reason the system thinks PEM_read_bio_RSAPrivateKey
110 <   *         is returning an int, not a RSA *.
111 <   * androsyn -- Thats because you didn't have a prototype and including
112 <   *             pem.h breaks things for some reason..
113 <   */
114 <  key = (RSA *) PEM_read_bio_RSAPrivateKey(file, NULL, 0, NULL);
115 <
116 <  if (key == NULL)
117 <  {
118 <    ilog(L_NOTICE, "PEM_read_bio_RSAPrivateKey() failed; possibly not RSA?");
119 <    report_crypto_errors();
120 <    return -1;
121 <  }
122 <
123 <  BIO_set_close(file, BIO_CLOSE);
124 <  BIO_free(file);
125 <  bio_spare_fd = save_spare_fd("SSL private key validation");
126 <
127 <  mkey = ServerInfo.rsa_private_key;
128 <
129 <  /*
130 <   * Compare the in-memory key to the key we just loaded above.  If
131 <   * any of the portions don't match, then logically we have a different
132 <   * in-memory key vs. the one we just loaded.  This is bad, mmmkay?
133 <   */
134 <  if (mkey->pad != key->pad)
135 <    ilog(L_CRIT, "Private key corrupted: pad %i != pad %i",
136 <                 mkey->pad, key->pad);
137 <  
138 <  if (mkey->version != key->version)
139 <  {
140 < #if (OPENSSL_VERSION_NUMBER & 0x00907000) == 0x00907000
141 <    ilog(L_CRIT, "Private key corrupted: version %li != version %li",
142 <                 mkey->version, key->version);
143 < #else
144 <    ilog(L_CRIT, "Private key corrupted: version %i != version %i",
145 <                 mkey->version, key->version);
146 < #endif
147 <  }    
148 <
149 <  if (BN_cmp(mkey->n, key->n))
150 <    ilog(L_CRIT, "Private key corrupted: n differs");
151 <  if (BN_cmp(mkey->e, key->e))
152 <    ilog(L_CRIT, "Private key corrupted: e differs");
153 <  if (BN_cmp(mkey->d, key->d))
154 <    ilog(L_CRIT, "Private key corrupted: d differs");
155 <  if (BN_cmp(mkey->p, key->p))
156 <    ilog(L_CRIT, "Private key corrupted: p differs");
157 <  if (BN_cmp(mkey->q, key->q))
158 <    ilog(L_CRIT, "Private key corrupted: q differs");
159 <  if (BN_cmp(mkey->dmp1, key->dmp1))
160 <    ilog(L_CRIT, "Private key corrupted: dmp1 differs");
161 <  if (BN_cmp(mkey->dmq1, key->dmq1))
162 <    ilog(L_CRIT, "Private key corrupted: dmq1 differs");
163 <  if (BN_cmp(mkey->iqmp, key->iqmp))
164 <    ilog(L_CRIT, "Private key corrupted: iqmp differs");
49 >  unsigned long e = 0;
50  
51 <  RSA_free(key);
52 <  return 0;
51 >  while ((e = ERR_get_error()))
52 >    ilog(LOG_TYPE_IRCD, "SSL error: %s", ERR_error_string(e, 0));
53   }
54  
55 <
56 < static void
172 < binary_to_hex(unsigned char *bin, char *hex, int length)
55 > void
56 > binary_to_hex(const unsigned char *bin, char *hex, unsigned int length)
57   {
58    static const char trans[] = "0123456789ABCDEF";
175  int i;
59  
60 <  for(i = 0; i < length; i++)
60 >  for (const unsigned char *end = bin + length; bin < end; ++bin)
61    {
62 <    hex[i  << 1]      = trans[bin[i] >> 4];
63 <    hex[(i << 1) + 1] = trans[bin[i] & 0xf];
62 >    *hex++ = trans[*bin >>  4];
63 >    *hex++ = trans[*bin & 0xf];
64    }
65  
66 <  hex[i << 1] = '\0';
66 >  *hex = '\0';
67   }
68  
69   int
70   get_randomness(unsigned char *buf, int length)
71   {
72 <    /* Seed OpenSSL PRNG with EGD enthropy pool -kre */
190 <    if (ConfigFileEntry.use_egd &&
191 <        (ConfigFileEntry.egdpool_path != NULL))
192 <    {
193 <      if (RAND_egd(ConfigFileEntry.egdpool_path) == -1)
194 <            return -1;
195 <    }
196 <
197 <  if (RAND_status())
198 <    return (RAND_bytes(buf, length));
199 <  else /* XXX - abort? */
200 <    return (RAND_pseudo_bytes(buf, length));
72 >  return RAND_bytes(buf, length);
73   }
74  
75   int
76   generate_challenge(char **r_challenge, char **r_response, RSA *rsa)
77   {
78 <  unsigned char secret[32], *tmp;
79 <  unsigned long length;
78 >  unsigned char secret[32], *tmp = NULL;
79 >  unsigned long length = 0;
80    int ret = -1;
81  
82    if (!rsa)
83 <        return -1;
84 <  get_randomness(secret, 32);
85 <  *r_response = MyMalloc(65);
83 >    return -1;
84 >
85 >  if (!get_randomness(secret, 32))
86 >  {
87 >    report_crypto_errors();
88 >    return -1;
89 >  }
90 >
91 >  *r_response = MyCalloc(65);
92    binary_to_hex(secret, *r_response, 32);
93  
94    length = RSA_size(rsa);
95 <  tmp = MyMalloc(length);
95 >  tmp = MyCalloc(length);
96    ret = RSA_public_encrypt(32, secret, tmp, rsa, RSA_PKCS1_PADDING);
97  
98 <  *r_challenge = MyMalloc((length << 1) + 1);
99 <  binary_to_hex( tmp, *r_challenge, length);
222 <  (*r_challenge)[length<<1] = 0;
98 >  *r_challenge = MyCalloc((length << 1) + 1);
99 >  binary_to_hex(tmp, *r_challenge, length);
100    MyFree(tmp);
101  
102    if (ret < 0)
# Line 227 | Line 104 | generate_challenge(char **r_challenge, c
104      report_crypto_errors();
105      return -1;
106    }
107 +
108    return 0;
109   }
110 + #endif

Comparing:
ircd-hybrid/src/rsa.c (property svn:keywords), Revision 30 by adx, Sun Oct 2 20:03:27 2005 UTC vs.
ircd-hybrid/trunk/src/rsa.c (property svn:keywords), Revision 4254 by michael, Fri Jul 18 19:07:27 2014 UTC

# Line 1 | Line 1
1 < "Id Revision"
1 > Id Revision

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)