| 23 |
|
*/ |
| 24 |
|
|
| 25 |
|
#include "stdinc.h" |
| 26 |
< |
|
| 26 |
> |
#ifdef HAVE_LIBCRYPTO |
| 27 |
|
#include <openssl/pem.h> |
| 28 |
|
#include <openssl/rand.h> |
| 29 |
|
#include <openssl/rsa.h> |
| 30 |
– |
#include <openssl/md5.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 "tools.h" |
| 37 |
|
#include "s_conf.h" |
| 38 |
|
#include "s_log.h" |
| 40 |
– |
#include "client.h" /* CIPHERKEYLEN .. eww */ |
| 41 |
– |
#include "ircd.h" /* bio_spare_fd */ |
| 39 |
|
|
| 43 |
– |
static void binary_to_hex(unsigned char *bin, char *hex, int length); |
| 40 |
|
|
| 41 |
|
/* |
| 42 |
|
* report_crypto_errors - Dump crypto error list to log |
| 47 |
|
unsigned long e = 0; |
| 48 |
|
unsigned long cnt = 0; |
| 49 |
|
|
| 54 |
– |
ERR_load_crypto_strings(); |
| 55 |
– |
|
| 50 |
|
while ((cnt < 100) && (e = ERR_get_error())) |
| 51 |
|
{ |
| 52 |
< |
ilog(L_CRIT, "SSL error: %s", ERR_error_string(e, 0)); |
| 52 |
> |
ilog(LOG_TYPE_IRCD, "SSL error: %s", ERR_error_string(e, 0)); |
| 53 |
|
cnt++; |
| 54 |
|
} |
| 55 |
|
} |
| 56 |
|
|
| 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"); |
| 165 |
– |
|
| 166 |
– |
RSA_free(key); |
| 167 |
– |
return 0; |
| 168 |
– |
} |
| 169 |
– |
|
| 170 |
– |
|
| 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++) |
| 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]; |
| 94 |
|
int ret = -1; |
| 95 |
|
|
| 96 |
|
if (!rsa) |
| 97 |
< |
return -1; |
| 97 |
> |
return -1; |
| 98 |
> |
|
| 99 |
|
get_randomness(secret, 32); |
| 100 |
|
*r_response = MyMalloc(65); |
| 101 |
|
binary_to_hex(secret, *r_response, 32); |
| 114 |
|
report_crypto_errors(); |
| 115 |
|
return -1; |
| 116 |
|
} |
| 117 |
+ |
|
| 118 |
|
return 0; |
| 119 |
|
} |
| 120 |
+ |
#endif |