Changeset 7862
- Timestamp:
- Feb 10, 2009, 3:54:47 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/crypto.c
r7658 r7862 1 /* 2 * This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com> 1 /* * This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com> 3 2 * 4 3 * This file is licensed by the GPL version 2. Works owned by the … … 22 21 #include <openssl/bn.h> 23 22 #include <openssl/dh.h> 23 #include <openssl/err.h> 24 24 #include <openssl/rc4.h> 25 25 #include <openssl/sha.h> … … 30 30 #include "crypto.h" 31 31 #include "utils.h" 32 33 #define MY_NAME "tr_crypto" 32 34 33 35 /** … … 98 100 **/ 99 101 102 #define logErrorFromSSL( ... ) \ 103 do { \ 104 if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) { \ 105 char buf[512]; \ 106 ERR_error_string_n( ERR_get_error( ), buf, sizeof( buf ) ); \ 107 tr_msg( __FILE__, __LINE__, TR_MSG_ERR, MY_NAME, "%s", buf ); \ 108 } \ 109 } while( 0 ) 110 100 111 static DH* 101 112 getSharedDH( void ) … … 106 117 { 107 118 dh = DH_new( ); 119 108 120 dh->p = BN_bin2bn( dh_P, sizeof( dh_P ), NULL ); 121 if( dh->p == NULL ) 122 logErrorFromSSL( ); 123 109 124 dh->g = BN_bin2bn( dh_G, sizeof( dh_G ), NULL ); 110 DH_generate_key( dh ); 125 if( dh->g == NULL ) 126 logErrorFromSSL( ); 127 128 if( !DH_generate_key( dh ) ) 129 logErrorFromSSL( ); 111 130 } 112 131 … … 152 171 const uint8_t * peerPublicKey ) 153 172 { 154 int len , offset;173 int len; 155 174 uint8_t secret[KEY_LEN]; 156 175 BIGNUM * bn = BN_bin2bn( peerPublicKey, KEY_LEN, NULL ); … … 160 179 161 180 len = DH_compute_key( secret, bn, dh ); 162 assert( len <= KEY_LEN ); 163 offset = KEY_LEN - len; 164 memset( crypto->mySecret, 0, offset ); 165 memcpy( crypto->mySecret + offset, secret, len ); 166 crypto->mySecretIsSet = 1; 181 if( len == -1 ) 182 logErrorFromSSL( ); 183 else { 184 int offset; 185 assert( len <= KEY_LEN ); 186 offset = KEY_LEN - len; 187 memset( crypto->mySecret, 0, offset ); 188 memcpy( crypto->mySecret + offset, secret, len ); 189 crypto->mySecretIsSet = 1; 190 } 167 191 168 192 BN_free( bn ); 169 170 193 return crypto->mySecret; 171 194 } … … 194 217 assert( crypto->mySecretIsSet ); 195 218 196 SHA1_Init( &sha ); 197 SHA1_Update( &sha, key, 4 ); 198 SHA1_Update( &sha, crypto->mySecret, KEY_LEN ); 199 SHA1_Update( &sha, crypto->torrentHash, SHA_DIGEST_LENGTH ); 200 SHA1_Final( buf, &sha ); 201 RC4_set_key( setme, SHA_DIGEST_LENGTH, buf ); 219 if( SHA1_Init( &sha ) 220 && SHA1_Update( &sha, key, 4 ) 221 && SHA1_Update( &sha, crypto->mySecret, KEY_LEN ) 222 && SHA1_Update( &sha, crypto->torrentHash, SHA_DIGEST_LENGTH ) 223 && SHA1_Final( buf, &sha ) ) 224 { 225 RC4_set_key( setme, SHA_DIGEST_LENGTH, buf ); 226 } 227 else 228 { 229 logErrorFromSSL( ); 230 } 202 231 } 203 232 … … 307 336 size_t len ) 308 337 { 309 RAND_pseudo_bytes ( buf, len ); 310 } 311 338 if( RAND_pseudo_bytes ( buf, len ) != 1 ) 339 logErrorFromSSL( ); 340 } 341
Note: See TracChangeset
for help on using the changeset viewer.