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

Commit dc3b2a6

Browse files
committed
Fixed PR comments:Populate DataHash key size in SslConnectionInfo
1 parent a8716e4 commit dc3b2a6

File tree

2 files changed

+36
-55
lines changed

2 files changed

+36
-55
lines changed

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

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -367,54 +367,14 @@ enum class SSL_DataHashAlgorithm : int64_t
367367
class SSL_DataHashSize
368368
{
369369
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;
370+
static const int32_t MD5_HashKeySize = 8 * MD5_DIGEST_LENGTH;
371+
static const int32_t SHA1_HashKeySize = 8 * SHA_DIGEST_LENGTH;
372+
static const int32_t SHA256_HashKeySize = 8 * SHA256_DIGEST_LENGTH;
373+
static const int32_t SHA384_HashKeySize = 8 * SHA384_DIGEST_LENGTH;
374+
static const int32_t GOST_HashKeySize = 256;
375375
};
376376

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)
377+
static void GetHashAlgorithmTypeAndSize(const SSL_CIPHER* cipher, HashAlgorithmType* dataHashAlg, int32_t* hashKeySize)
418378
{
419379
unsigned long mac;
420380
#if HAVE_SSL_CIPHER_SPLIT_ALGORITHMS
@@ -428,28 +388,46 @@ static int32_t GetHashKeySize(const SSL_CIPHER* cipher)
428388
switch (sslMac)
429389
{
430390
case SSL_DataHashAlgorithm::SSL_MD5:
431-
return SSL_DataHashSize::MD5_HashKeySize;
391+
*dataHashAlg = HashAlgorithmType::Md5;
392+
*hashKeySize = SSL_DataHashSize::MD5_HashKeySize;
393+
return;
432394

433395
case SSL_DataHashAlgorithm::SSL_SHA1:
434-
return SSL_DataHashSize::SHA1_HashKeySize;
396+
*dataHashAlg = HashAlgorithmType::Sha1;
397+
*hashKeySize = SSL_DataHashSize::SHA1_HashKeySize;
398+
return;
435399

436400
#if HAVE_SSL_CIPHER_SPLIT_ALGORITHMS
437401
case SSL_DataHashAlgorithm::SSL_GOST94:
438-
return SSL_DataHashSize::GOST_HashKeySize;
402+
*dataHashAlg = HashAlgorithmType::SSL_GOST94;
403+
*hashKeySize = SSL_DataHashSize::GOST_HashKeySize;
404+
return;
439405

440406
case SSL_DataHashAlgorithm::SSL_GOST89MAC:
441-
return SSL_DataHashSize::GOST_HashKeySize;
407+
*dataHashAlg = HashAlgorithmType::SSL_GOST89;
408+
*hashKeySize = SSL_DataHashSize::GOST_HashKeySize;
409+
return;
442410

443411
case SSL_DataHashAlgorithm::SSL_SHA256:
444-
return SSL_DataHashSize::SHA256_HashKeySize;
412+
*dataHashAlg = HashAlgorithmType::SSL_SHA256;
413+
*hashKeySize = SSL_DataHashSize::SHA256_HashKeySize;
414+
return;
445415

446416
case SSL_DataHashAlgorithm::SSL_SHA384:
447-
return SSL_DataHashSize::SHA384_HashKeySize;
417+
*dataHashAlg = HashAlgorithmType::SSL_SHA384;
418+
*hashKeySize = SSL_DataHashSize::SHA384_HashKeySize;
419+
return;
448420

449421
case SSL_DataHashAlgorithm::SSL_AEAD:
450-
return 0;
422+
*dataHashAlg = HashAlgorithmType::SSL_AEAD;
423+
*hashKeySize = 0;
424+
return;
451425
#endif
452426
}
427+
428+
*dataHashAlg = HashAlgorithmType::None;
429+
*hashKeySize = 0;
430+
return;
453431
}
454432

455433
extern "C" int32_t GetSslConnectionInfo(SSL* ssl, CipherAlgorithmType* dataCipherAlg, ExchangeAlgorithmType* keyExchangeAlg, HashAlgorithmType* dataHashAlg, int32_t* dataKeySize, int32_t* hashKeySize)
@@ -469,9 +447,8 @@ extern "C" int32_t GetSslConnectionInfo(SSL* ssl, CipherAlgorithmType* dataCiphe
469447

470448
*dataCipherAlg = MapCipherAlgorithmType(cipher);
471449
*keyExchangeAlg = MapExchangeAlgorithmType(cipher);
472-
*dataHashAlg = MapHashAlgorithmType(cipher);
473450
*dataKeySize = cipher->alg_bits;
474-
*hashKeySize = GetHashKeySize(cipher);
451+
GetHashAlgorithmTypeAndSize(cipher, dataHashAlg, hashKeySize);
475452

476453
return 1;
477454

@@ -486,6 +463,8 @@ extern "C" int32_t GetSslConnectionInfo(SSL* ssl, CipherAlgorithmType* dataCiphe
486463
*dataHashAlg = HashAlgorithmType::None;
487464
if (dataKeySize)
488465
*dataKeySize = 0;
466+
if (hashKeySize)
467+
*hashKeySize = 0;
489468

490469
return 0;
491470
}

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

Lines changed: 2 additions & 0 deletions
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.

0 commit comments

Comments
 (0)