Skip to content

Commit

Permalink
Move most of the remaining Strong Name code to internal to the C++ fi…
Browse files Browse the repository at this point in the history
…le and remove duplicate validation (#95536)
  • Loading branch information
jkoritzinsky authored Dec 6, 2023
1 parent 55a5c34 commit 1890ac9
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 434 deletions.
10 changes: 3 additions & 7 deletions src/coreclr/binder/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ namespace BINDER_SPACE

const BYTE *pByteKey = publicKeyBLOB;
DWORD dwKeyLen = publicKeyBLOB.GetSize();
BYTE *pByteToken = NULL;
DWORD dwTokenLen = 0;
StrongNameToken token;

IF_FAIL_GO(StrongNameTokenFromPublicKey(
const_cast<BYTE*>(pByteKey),
dwKeyLen,
&pByteToken,
&dwTokenLen));
&token));

_ASSERTE(pByteToken != NULL);
publicKeyTokenBLOB.Set(pByteToken, dwTokenLen);
StrongNameFreeBuffer(pByteToken);
publicKeyTokenBLOB.Set(token.m_token, StrongNameToken::SIZEOF_TOKEN);

Exit:
return hr;
Expand Down
8 changes: 0 additions & 8 deletions src/coreclr/inc/ecmakey.h

This file was deleted.

23 changes: 0 additions & 23 deletions src/coreclr/inc/strongnameholders.h

This file was deleted.

23 changes: 10 additions & 13 deletions src/coreclr/inc/strongnameinternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#ifndef _STRONGNAME_INTERNAL_H
#define _STRONGNAME_INTERNAL_H

extern BYTE const* const g_coreLibPublicKey;
extern const ULONG g_coreLibPublicKeyLen;

// Public key blob binary format.
typedef struct {
unsigned int SigAlgID; // (ALG_ID) signature algorithm used to create the signature
Expand All @@ -15,21 +18,15 @@ typedef struct {
BYTE PublicKey[1]; // variable length byte array containing the key value in format output by CryptoAPI
} PublicKeyBlob;

// Determine the number of bytes in a public key
DWORD StrongNameSizeOfPublicKey(const PublicKeyBlob &keyPublicKey);

bool StrongNameIsValidPublicKey(_In_reads_(cbPublicKeyBlob) const BYTE *pbPublicKeyBlob, DWORD cbPublicKeyBlob);
bool StrongNameIsValidPublicKey(const PublicKeyBlob &keyPublicKey);

// Determine if a public key is the ECMA key
bool StrongNameIsEcmaKey(_In_reads_(cbKey) const BYTE *pbKey, DWORD cbKey);
bool StrongNameIsEcmaKey(const PublicKeyBlob &keyPublicKey);
struct StrongNameToken
{
static constexpr ULONG SIZEOF_TOKEN = 8;
BYTE m_token[SIZEOF_TOKEN];
};

HRESULT StrongNameTokenFromPublicKey(BYTE* pbPublicKeyBlob, // [in] public key blob
ULONG cbPublicKeyBlob,
BYTE** ppbStrongNameToken, // [out] strong name token
ULONG* pcbStrongNameToken);

VOID StrongNameFreeBuffer(BYTE* pbMemory);
StrongNameToken* token // [out] strong name token
);

#endif // !_STRONGNAME_INTERNAL_H
53 changes: 0 additions & 53 deletions src/coreclr/inc/thekey.h

This file was deleted.

54 changes: 17 additions & 37 deletions src/coreclr/md/compiler/importhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,7 @@ HRESULT ImportHelper::FindAssemblyRef(
const void *pbToken = NULL; // Token version of public key.
ULONG cbToken = 0; // Count of bytes in token.
#if !defined(FEATURE_METADATA_EMIT_IN_DEBUGGER) || defined(DACCESS_COMPILE)
const void *pbTmpToken; // Token version of public key.
ULONG cbTmpToken; // Count of bytes in token.
StrongNameToken token; // Local buffer for token version of public key.
bool fMatch; // Did public key or tokens match?
#endif // !FEATURE_METADATA_EMIT_IN_DEBUGGER || DACCESS_COMPILE

Expand Down Expand Up @@ -1392,13 +1391,12 @@ HRESULT ImportHelper::FindAssemblyRef(
#if defined(FEATURE_METADATA_EMIT_IN_DEBUGGER) && !defined(DACCESS_COMPILE)
return E_FAIL;
#else //!FEATURE_METADATA_EMIT_IN_DEBUGGER || DACCESS_COMPILE
StrongNameToken tmpToken;
// Need to compress target public key to see if it matches.
IfFailRet(StrongNameTokenFromPublicKey((BYTE*)pbTmp,
cbTmp,
(BYTE**)&pbTmpToken,
&cbTmpToken));
fMatch = cbTmpToken == cbPublicKeyOrToken && !memcmp(pbTmpToken, pbPublicKeyOrToken, cbTmpToken);
StrongNameFreeBuffer((BYTE*)pbTmpToken);
&tmpToken));
fMatch = StrongNameToken::SIZEOF_TOKEN == cbPublicKeyOrToken && !memcmp(&tmpToken, pbPublicKeyOrToken, StrongNameToken::SIZEOF_TOKEN);
if (!fMatch)
continue;
#endif //!FEATURE_METADATA_EMIT_IN_DEBUGGER || DACCESS_COMPILE
Expand All @@ -1414,30 +1412,19 @@ HRESULT ImportHelper::FindAssemblyRef(
#else //!FEATURE_METADATA_EMIT_IN_DEBUGGER || DACCESS_COMPILE
IfFailRet(StrongNameTokenFromPublicKey((BYTE*)pbPublicKeyOrToken,
cbPublicKeyOrToken,
(BYTE**)&pbToken,
&cbToken));
&token));
pbToken = &token;
cbToken = StrongNameToken::SIZEOF_TOKEN;
#endif //!FEATURE_METADATA_EMIT_IN_DEBUGGER || DACCESS_COMPILE
}
if (cbTmp != cbToken || memcmp(pbTmp, pbToken, cbToken))
continue;
}
}

if (pbToken && IsAfPublicKey(dwFlags))
{
#if !defined(FEATURE_METADATA_EMIT_IN_DEBUGGER) || defined(DACCESS_COMPILE)
StrongNameFreeBuffer((BYTE*)pbToken);
#endif
}
*pmar = TokenFromRid(i, mdtAssemblyRef);
return S_OK;
}
if (pbToken && IsAfPublicKey(dwFlags))
{
#if !defined(FEATURE_METADATA_EMIT_IN_DEBUGGER) || defined(DACCESS_COMPILE)
StrongNameFreeBuffer((BYTE*)pbToken);
#endif
}
return CLDB_E_RECORD_NOTFOUND;
} // ImportHelper::FindAssemblyRef

Expand Down Expand Up @@ -3186,8 +3173,7 @@ ImportHelper::CreateAssemblyRefFromAssembly(
LPCUTF8 szLocale;
mdAssemblyRef tkAssemRef;
HRESULT hr = S_OK;
const void *pbToken = NULL;
ULONG cbToken = 0;
StrongNameToken token;
ULONG i;

// Set output to Nil.
Expand All @@ -3206,8 +3192,7 @@ ImportHelper::CreateAssemblyRefFromAssembly(
dwFlags &= ~afPublicKey;
IfFailGo(StrongNameTokenFromPublicKey((BYTE*)pbPublicKey,
cbPublicKey,
(BYTE**)&pbToken,
&cbToken));
&token));
}
else
_ASSERTE(!IsAfPublicKey(dwFlags));
Expand All @@ -3224,8 +3209,8 @@ ImportHelper::CreateAssemblyRefFromAssembly(
continue;

// See if the AssemblyRef already exists in the emit scope.
hr = FindAssemblyRef(pMiniMdEmit, szName, szLocale, pbToken,
cbToken, usMajorVersion, usMinorVersion,
hr = FindAssemblyRef(pMiniMdEmit, szName, szLocale, &token,
StrongNameToken::SIZEOF_TOKEN, usMajorVersion, usMinorVersion,
usBuildNumber, usRevisionNumber, dwFlags,
&tkAssemRef);
if (hr == CLDB_E_RECORD_NOTFOUND)
Expand All @@ -3243,7 +3228,7 @@ ImportHelper::CreateAssemblyRefFromAssembly(
pRecordEmit->SetFlags(dwFlags);

IfFailGo(pMiniMdEmit->PutBlob(TBL_AssemblyRef, AssemblyRefRec::COL_PublicKeyOrToken,
pRecordEmit, pbToken, cbToken));
pRecordEmit, &token, StrongNameToken::SIZEOF_TOKEN));
IfFailGo(pMiniMdEmit->PutString(TBL_AssemblyRef, AssemblyRefRec::COL_Name,
pRecordEmit, szName));
IfFailGo(pMiniMdEmit->PutString(TBL_AssemblyRef, AssemblyRefRec::COL_Locale,
Expand All @@ -3261,8 +3246,6 @@ ImportHelper::CreateAssemblyRefFromAssembly(
*ptkAssemblyRef = tkAssemRef;
}
ErrExit:
if (pbToken)
StrongNameFreeBuffer((BYTE*)pbToken);
return hr;
#endif //!FEATURE_METADATA_EMIT_IN_DEBUGGER
} // ImportHelper::CreateAssemblyRefFromAssembly
Expand Down Expand Up @@ -3299,8 +3282,6 @@ HRESULT ImportHelper::CompareAssemblyRefToAssembly( // S_OK, S_FALSE or error
ULONG cbPublicKey2;
LPCUTF8 szName2;
LPCUTF8 szLocale2;
const void *pbToken = NULL;
ULONG cbToken = 0;
bool fMatch;

// Get the AssemblyRef props.
Expand Down Expand Up @@ -3341,16 +3322,15 @@ HRESULT ImportHelper::CompareAssemblyRefToAssembly( // S_OK, S_FALSE or error
memcmp(pbPublicKeyOrToken1, pbPublicKey2, cbPublicKeyOrToken1)))
return S_FALSE;

StrongNameToken token2;

// Otherwise we need to compress the def public key into a token.
IfFailRet(StrongNameTokenFromPublicKey((BYTE*)pbPublicKey2,
cbPublicKey2,
(BYTE**)&pbToken,
&cbToken));

fMatch = cbPublicKeyOrToken1 == cbToken &&
!memcmp(pbPublicKeyOrToken1, pbToken, cbPublicKeyOrToken1);
&token2));

StrongNameFreeBuffer((BYTE*)pbToken);
fMatch = cbPublicKeyOrToken1 == StrongNameToken::SIZEOF_TOKEN &&
!memcmp(pbPublicKeyOrToken1, &token2, cbPublicKeyOrToken1);

if (!fMatch)
return S_FALSE;
Expand Down
Loading

0 comments on commit 1890ac9

Please sign in to comment.