Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 02e3992

Browse files
committed
Fixed PR Comments and Synced code
1 parent 86dec92 commit 02e3992

File tree

3 files changed

+58
-64
lines changed

3 files changed

+58
-64
lines changed

src/Common/src/Interop/Unix/libssl/SslConnectionInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ internal SslConnectionInfo(SafeSslHandle sslContext)
3131
{
3232
throw Interop.OpenSsl.CreateSslException(SR.net_ssl_get_connection_info_failed);
3333
}
34-
// TODO (Issue #3362) map key sizes
34+
35+
//Openssl does not provide a way to return a exchange key size.
36+
//It internally does calculate the key size before generating key to exchange
37+
//It is not a constant (Algorthim specific) either that we can hardcode and return.
3538
}
3639

3740
private SslProtocols MapProtocolVersion(string protocolVersion)

src/Native/System.Security.Cryptography.Native/pal_ssl.cpp

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -364,57 +364,7 @@ enum class SSL_DataHashAlgorithm : int64_t
364364
#endif
365365
};
366366

367-
class SSL_DataHashSize
368-
{
369-
public:
370-
static const int32_t MD5_HashKeySize = 128;
371-
static const int32_t SHA1_HashKeySize = 160;
372-
static const int32_t SHA256_HashKeySize = 256;
373-
static const int32_t SHA384_HashKeySize = 384;
374-
static const int32_t GOST_HashKeySize = 256;
375-
};
376-
377-
static HashAlgorithmType MapHashAlgorithmType(const SSL_CIPHER* cipher)
378-
{
379-
unsigned long mac;
380-
#if HAVE_SSL_CIPHER_SPLIT_ALGORITHMS
381-
mac = cipher->algorithm_mac;
382-
#else
383-
const unsigned long SSL_MAC_MASK = 0x00c00000L;
384-
mac = cipher->algorithms & SSL_MAC_MASK;
385-
#endif
386-
387-
SSL_DataHashAlgorithm sslMac = static_cast<SSL_DataHashAlgorithm>(mac);
388-
switch (sslMac)
389-
{
390-
case SSL_DataHashAlgorithm::SSL_MD5:
391-
return HashAlgorithmType::Md5;
392-
393-
case SSL_DataHashAlgorithm::SSL_SHA1:
394-
return HashAlgorithmType::Sha1;
395-
396-
#if HAVE_SSL_CIPHER_SPLIT_ALGORITHMS
397-
case SSL_DataHashAlgorithm::SSL_GOST94:
398-
return HashAlgorithmType::SSL_GOST94;
399-
400-
case SSL_DataHashAlgorithm::SSL_GOST89MAC:
401-
return HashAlgorithmType::SSL_GOST89;
402-
403-
case SSL_DataHashAlgorithm::SSL_SHA256:
404-
return HashAlgorithmType::SSL_SHA256;
405-
406-
case SSL_DataHashAlgorithm::SSL_SHA384:
407-
return HashAlgorithmType::SSL_SHA384;
408-
409-
case SSL_DataHashAlgorithm::SSL_AEAD:
410-
return HashAlgorithmType::SSL_AEAD;
411-
#endif
412-
}
413-
414-
return HashAlgorithmType::None;
415-
}
416-
417-
static int32_t GetHashKeySize(const SSL_CIPHER* cipher)
367+
static void GetHashAlgorithmTypeAndSize(const SSL_CIPHER* cipher, HashAlgorithmType* dataHashAlg, DataHashSize* hashKeySize)
418368
{
419369
unsigned long mac;
420370
#if HAVE_SSL_CIPHER_SPLIT_ALGORITHMS
@@ -428,35 +378,58 @@ static int32_t GetHashKeySize(const SSL_CIPHER* cipher)
428378
switch (sslMac)
429379
{
430380
case SSL_DataHashAlgorithm::SSL_MD5:
431-
return SSL_DataHashSize::MD5_HashKeySize;
381+
*dataHashAlg = HashAlgorithmType::Md5;
382+
*hashKeySize = DataHashSize::MD5_HashKeySize;
383+
return;
432384

433385
case SSL_DataHashAlgorithm::SSL_SHA1:
434-
return SSL_DataHashSize::SHA1_HashKeySize;
386+
*dataHashAlg = HashAlgorithmType::Sha1;
387+
*hashKeySize = DataHashSize::SHA1_HashKeySize;
388+
return;
435389

436390
#if HAVE_SSL_CIPHER_SPLIT_ALGORITHMS
437391
case SSL_DataHashAlgorithm::SSL_GOST94:
438-
return SSL_DataHashSize::GOST_HashKeySize;
392+
*dataHashAlg = HashAlgorithmType::SSL_GOST94;
393+
*hashKeySize = DataHashSize::GOST_HashKeySize;
394+
return;
439395

440396
case SSL_DataHashAlgorithm::SSL_GOST89MAC:
441-
return SSL_DataHashSize::GOST_HashKeySize;
397+
*dataHashAlg = HashAlgorithmType::SSL_GOST89;
398+
*hashKeySize = DataHashSize::GOST_HashKeySize;
399+
return;
442400

443401
case SSL_DataHashAlgorithm::SSL_SHA256:
444-
return SSL_DataHashSize::SHA256_HashKeySize;
402+
*dataHashAlg = HashAlgorithmType::SSL_SHA256;
403+
*hashKeySize = DataHashSize::SHA256_HashKeySize;
404+
return;
445405

446406
case SSL_DataHashAlgorithm::SSL_SHA384:
447-
return SSL_DataHashSize::SHA384_HashKeySize;
407+
*dataHashAlg = HashAlgorithmType::SSL_SHA384;
408+
*hashKeySize = DataHashSize::SHA384_HashKeySize;
409+
return;
448410

449411
case SSL_DataHashAlgorithm::SSL_AEAD:
450-
return 0;
412+
*dataHashAlg = HashAlgorithmType::SSL_AEAD;
413+
*hashKeySize = DataHashSize::Default;
414+
return;
451415
#endif
452416
}
417+
418+
*dataHashAlg = HashAlgorithmType::None;
419+
*hashKeySize = DataHashSize::Default;
420+
return;
453421
}
454422

455-
extern "C" int32_t GetSslConnectionInfo(SSL* ssl, CipherAlgorithmType* dataCipherAlg, ExchangeAlgorithmType* keyExchangeAlg, HashAlgorithmType* dataHashAlg, int32_t* dataKeySize, int32_t* hashKeySize)
423+
extern "C" int32_t GetSslConnectionInfo(SSL* ssl,
424+
CipherAlgorithmType* dataCipherAlg,
425+
ExchangeAlgorithmType* keyExchangeAlg,
426+
HashAlgorithmType* dataHashAlg,
427+
int32_t* dataKeySize,
428+
DataHashSize* hashKeySize)
456429
{
457430
const SSL_CIPHER* cipher;
458431

459-
if (!ssl || !dataCipherAlg || !keyExchangeAlg || !dataHashAlg || !dataKeySize)
432+
if (!ssl || !dataCipherAlg || !keyExchangeAlg || !dataHashAlg || !dataKeySize || !hashKeySize)
460433
{
461434
goto err;
462435
}
@@ -469,9 +442,8 @@ extern "C" int32_t GetSslConnectionInfo(SSL* ssl, CipherAlgorithmType* dataCiphe
469442

470443
*dataCipherAlg = MapCipherAlgorithmType(cipher);
471444
*keyExchangeAlg = MapExchangeAlgorithmType(cipher);
472-
*dataHashAlg = MapHashAlgorithmType(cipher);
473445
*dataKeySize = cipher->alg_bits;
474-
*hashKeySize = GetHashKeySize(cipher);
446+
GetHashAlgorithmTypeAndSize(cipher, dataHashAlg, hashKeySize);
475447

476448
return 1;
477449

@@ -486,6 +458,8 @@ extern "C" int32_t GetSslConnectionInfo(SSL* ssl, CipherAlgorithmType* dataCiphe
486458
*dataHashAlg = HashAlgorithmType::None;
487459
if (dataKeySize)
488460
*dataKeySize = 0;
461+
if (hashKeySize)
462+
*hashKeySize = DataHashSize::Default;
489463

490464
return 0;
491465
}

src/Native/System.Security.Cryptography.Native/pal_ssl.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "pal_crypto_types.h"
55

66
#include <openssl/ssl.h>
7+
#include <openssl/md5.h>
8+
#include <openssl/sha.h>
79

810
/*
911
These values should be kept in sync with System.Security.Authentication.SslProtocols.
@@ -88,6 +90,16 @@ enum class HashAlgorithmType : int32_t
8890
SSL_AEAD = 229412,
8991
};
9092

93+
enum class DataHashSize : int32_t
94+
{
95+
MD5_HashKeySize = 8 * MD5_DIGEST_LENGTH,
96+
SHA1_HashKeySize = 8 * SHA_DIGEST_LENGTH,
97+
SHA256_HashKeySize = 8 * SHA256_DIGEST_LENGTH,
98+
SHA384_HashKeySize = 8 * SHA384_DIGEST_LENGTH,
99+
GOST_HashKeySize = 256,
100+
Default = 0,
101+
};
102+
91103
enum SslErrorCode : int32_t
92104
{
93105
PAL_SSL_ERROR_NONE = 0,
@@ -215,7 +227,12 @@ Returns the connection information for the SSL instance.
215227
Returns 1 upon success, otherwise 0.
216228
*/
217229

218-
extern "C" int32_t GetSslConnectionInfo(SSL* ssl, CipherAlgorithmType* dataCipherAlg, ExchangeAlgorithmType* keyExchangeAlg, HashAlgorithmType* dataHashAlg, int32_t* dataKeySize, int32_t* hashKeySize);
230+
extern "C" int32_t GetSslConnectionInfo(SSL* ssl,
231+
CipherAlgorithmType* dataCipherAlg,
232+
ExchangeAlgorithmType* keyExchangeAlg,
233+
HashAlgorithmType* dataHashAlg,
234+
int32_t* dataKeySize,
235+
DataHashSize* hashKeySize);
219236

220237
/*
221238
Shims the SSL_write method.

0 commit comments

Comments
 (0)