From c3b49855e02b8525223c133858e2417deabb5779 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 20 Feb 2017 21:44:48 +0100 Subject: [PATCH] compilation fix for openssl-1.1.0 (gxssecurity+gxstunnel part) --- libretroshare/src/gxs/gxssecurity.cc | 353 +++++++++++---------- libretroshare/src/gxstunnel/p3gxstunnel.cc | 16 + 2 files changed, 202 insertions(+), 167 deletions(-) diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 20ddc1462..9591b3f9e 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -41,20 +41,31 @@ static const uint32_t MULTI_ENCRYPTION_FORMAT_v001_ENCRYPTED_KEY_SIZE = 256 ; static RsGxsId getRsaKeyFingerprint_old_insecure_method(RSA *pubkey) { +#if OPENSSL_VERSION_NUMBER < 0x10100000L int lenn = BN_num_bytes(pubkey -> n); RsTemporaryMemory tmp(lenn) ; BN_bn2bin(pubkey -> n, tmp); +#else + const BIGNUM *nn=NULL,*ee=NULL ; + RSA_get0_key(pubkey,&nn,&ee,NULL) ; + + int lenn = BN_num_bytes(nn); + + RsTemporaryMemory tmp(lenn) ; + BN_bn2bin(nn, tmp); +#endif // Copy first CERTSIGNLEN bytes from the hash of the public modulus and exponent - // We should not be using strings here, but a real ID. To be done later. + // We should not be using strings here, but a real ID. To be done later. - assert(lenn >= CERTSIGNLEN) ; + assert(lenn >= CERTSIGNLEN) ; - return RsGxsId((unsigned char*)tmp) ; + return RsGxsId((unsigned char*)tmp) ; } static RsGxsId getRsaKeyFingerprint(RSA *pubkey) { +#if OPENSSL_VERSION_NUMBER < 0x10100000L int lenn = BN_num_bytes(pubkey -> n); int lene = BN_num_bytes(pubkey -> e); @@ -62,6 +73,18 @@ static RsGxsId getRsaKeyFingerprint(RSA *pubkey) BN_bn2bin(pubkey -> n, tmp); BN_bn2bin(pubkey -> e, &tmp[lenn]); +#else + const BIGNUM *nn=NULL,*ee=NULL ; + RSA_get0_key(pubkey,&nn,&ee,NULL) ; + + int lenn = BN_num_bytes(nn); + int lene = BN_num_bytes(ee); + + RsTemporaryMemory tmp(lenn+lene) ; + + BN_bn2bin(nn, tmp); + BN_bn2bin(ee, &tmp[lenn]); +#endif Sha1CheckSum s = RsDirUtil::sha1sum(tmp,lenn+lene) ; @@ -530,11 +553,10 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u return false; } - EVP_CIPHER_CTX ctx; + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); int eklen, net_ekl; unsigned char *ek; unsigned char iv[EVP_MAX_IV_LENGTH]; - EVP_CIPHER_CTX_init(&ctx); int out_currOffset = 0; int out_offset = 0; @@ -551,7 +573,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u int max_outlen = inlen + cipher_block_size + EVP_MAX_IV_LENGTH + max_evp_key_size + size_net_ekl; // intialize context and send store encrypted cipher in ek - if(!EVP_SealInit(&ctx, EVP_aes_128_cbc(), &ek, &eklen, iv, &public_key, 1)) return false; + if(!EVP_SealInit(ctx, EVP_aes_128_cbc(), &ek, &eklen, iv, &public_key, 1)) return false; // now assign memory to out accounting for data, and cipher block size, key length, and key length val out = (uint8_t*)rs_malloc(inlen + cipher_block_size + size_net_ekl + eklen + EVP_MAX_IV_LENGTH) ; @@ -570,7 +592,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u out_offset += EVP_MAX_IV_LENGTH; // now encrypt actual data - if(!EVP_SealUpdate(&ctx, (unsigned char*) out + out_offset, &out_currOffset, (unsigned char*) in, inlen)) + if(!EVP_SealUpdate(ctx, (unsigned char*) out + out_offset, &out_currOffset, (unsigned char*) in, inlen)) { free(out) ; out = NULL ; @@ -581,7 +603,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u out_offset += out_currOffset; // add padding - if(!EVP_SealFinal(&ctx, (unsigned char*) out + out_offset, &out_currOffset)) + if(!EVP_SealFinal(ctx, (unsigned char*) out + out_offset, &out_currOffset)) { free(out) ; out = NULL ; @@ -602,7 +624,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u // free encrypted key data free(ek); - EVP_CIPHER_CTX_cleanup(&ctx); + EVP_CIPHER_CTX_free(ctx); outlen = out_offset; return true; @@ -613,152 +635,151 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u #ifdef DISTRIB_DEBUG std::cerr << "GxsSecurity::encrypt() " << std::endl; #endif - // Encrypts (in,inlen) into (out,outlen) using the given RSA public key. - // The format of the encrypted data is: - // - // [--- ID ---|--- number of encrypted keys---| n * (--- Encrypted session keys ---) |--- IV ---|---- Encrypted data ---] - // 2 bytes 2 byte = n 256 bytes EVP_MAX_IV_LENGTH Rest of packet - // + // Encrypts (in,inlen) into (out,outlen) using the given RSA public key. + // The format of the encrypted data is: + // + // [--- ID ---|--- number of encrypted keys---| n * (--- Encrypted session keys ---) |--- IV ---|---- Encrypted data ---] + // 2 bytes 2 byte = n 256 bytes EVP_MAX_IV_LENGTH Rest of packet + // - out = NULL ; - EVP_CIPHER_CTX ctx; - EVP_CIPHER_CTX_init(&ctx); - std::vector public_keys(keys.size(),NULL); - - try - { - for(uint32_t i=0;i ek(keys.size(),NULL) ; - std::vector eklen(keys.size(),0) ; - - for(uint32_t i=0;i> 8) & 0xff ; - - // number of keys - - out[out_offset++] = keys.size() & 0xff ; - out[out_offset++] = (keys.size() >> 8) & 0xff ; - - // encrypted keys, each preceeded with its length - - for(uint32_t i=0;i max_outlen) - throw std::runtime_error("Memory used by encryption exceeds allocated memory block") ; - - // free encrypted key data - - for(uint32_t i=0;i public_keys(keys.size(),NULL); + + try + { + for(uint32_t i=0;i ek(keys.size(),NULL) ; + std::vector eklen(keys.size(),0) ; + + for(uint32_t i=0;i> 8) & 0xff ; + + // number of keys + + out[out_offset++] = keys.size() & 0xff ; + out[out_offset++] = (keys.size() >> 8) & 0xff ; + + // encrypted keys, each preceeded with its length + + for(uint32_t i=0;i max_outlen) + throw std::runtime_error("Memory used by encryption exceeds allocated memory block") ; + + // free encrypted key data + + for(uint32_t i=0;ipublic_key = BN_dup(dh->pub_key) ; +#else + const BIGNUM *pub_key=NULL ; + DH_get0_key(dh,&pub_key,NULL) ; + dhitem->public_key = BN_dup(pub_key) ; +#endif // we should also sign the data and check the signature on the other end. // @@ -1133,8 +1139,18 @@ bool p3GxsTunnelService::locked_initDHSessionKey(DH *& dh) return false ; } +#if OPENSSL_VERSION_NUMBER < 0x10100000L BN_hex2bn(&dh->p,dh_prime_2048_hex.c_str()) ; BN_hex2bn(&dh->g,"5") ; +#else + BIGNUM *pp=NULL ; + BIGNUM *gg=NULL ; + + BN_hex2bn(&pp,dh_prime_2048_hex.c_str()) ; + BN_hex2bn(&gg,"5") ; + + DH_set0_pqg(dh,pp,NULL,gg) ; +#endif int codes = 0 ;