C# Class Internal.NativeCrypto.CapiHelper

Show file Open project: dotnet/corefx Class Usage Examples

Public Methods

Method Description
CryptAcquireContext ( System.Security.Cryptography.SafeProvHandle &psafeProvHandle, string pszContainer, string pszProvider, int dwProvType, uint dwFlags ) : bool

Acquire a handle to a crypto service provider and optionally a key container

CryptDestroyHash ( IntPtr hHash ) : bool

Destroy a crypto hash.

CryptDestroyKey ( IntPtr hKey ) : bool

Destroy a crypto key.

CryptReleaseContext ( IntPtr safeProvHandle, int dwFlags ) : bool

Destroy a crypto provider.

GetProviderParameter ( System.Security.Cryptography.SafeProvHandle safeProvHandle, int keyNumber, int keyParam ) : object

This method queries the key container and get some of it's properties. Those properties should never cause UI to display.

NameOrOidToHashAlgId ( String nameOrOid ) : int

Helper for signing and verifications that accept a string to specify a hashing algorithm.

ObjToHashAlgId ( Object hashAlg ) : int

Helper for signing and verifications that accept a string/Type/HashAlgorithm to specify a hashing algorithm.

OpenCSP ( CspParameters cspParameters, uint flags, System.Security.Cryptography.SafeProvHandle &safeProvHandle ) : int

OpenCSP performs the core work of opening and creating CSPs and containers in CSPs

SignValue ( System.Security.Cryptography.SafeProvHandle hProv, SafeKeyHandle hKey, int keyNumber, int calgKey, int calgHash, byte hash ) : byte[]

Helper for RSACryptoServiceProvider.SignData/SignHash apis.

UpgradeRSA ( int dwProvType, string wszProvider ) : string

Check to see if a better CSP than the one requested is available RSA providers are supersets of each other in the following order: 1. MS_ENH_RSA_AES_PROV 2. MS_ENHANCED_PROV 3. MS_DEF_PROV This will return the best provider which is a superset of wszProvider, or NULL if there is no upgrade available on the machine.

VerifySign ( System.Security.Cryptography.SafeProvHandle hProv, SafeKeyHandle hKey, int calgKey, int calgHash, byte hash, byte signature ) : bool

Helper for RSACryptoServiceProvider.VerifyData/VerifyHash apis.

Private Methods

Method Description
AcquireCryptContext ( System.Security.Cryptography.SafeProvHandle &safeProvHandle, string keyContainer, string providerName, int providerType, uint flags ) : int

Acquire a handle to a crypto service provider and optionally a key container This function implements the WszCryptAcquireContext_SO_TOLERANT

AcquireCsp ( CspParameters cspParameters, System.Security.Cryptography.SafeProvHandle &safeProvHandle ) : void

This method opens the CSP using CRYPT_VERIFYCONTEXT KeyContainer must be null for the flag CRYPT_VERIFYCONTEXT This method asserts if keyContainer is not null

CreateCSP ( CspParameters parameters, bool randomKeyContainer, System.Security.Cryptography.SafeProvHandle &safeProvHandle ) : void

Creates a new key container

CreateHashHandle ( this hProv, byte hash, int calgHash ) : SafeHashHandle

Create a CAPI-1 hash handle that contains the specified bits as its hash value.

CreateProvHandle ( CspParameters parameters, bool randomKeyContainer ) : System.Security.Cryptography.SafeProvHandle

This method acquires CSP and returns the handle of CSP

DecryptData ( SafeKeyHandle hKey, byte input, int inputOffset, int inputCount, byte output, int outputOffset, int outputCount ) : int
DecryptKey ( SafeKeyHandle safeKeyHandle, byte encryptedData, int encryptedDataLength, bool fOAEP, byte &decryptedData ) : void
EncryptData ( SafeKeyHandle hKey, byte input, int inputOffset, int inputCount, byte output, int outputOffset, int outputCount, bool isFinal ) : int
EncryptKey ( SafeKeyHandle safeKeyHandle, byte pbKey, int cbKey, bool foep, byte &pbEncryptedKey ) : void
ExponentAsBytes ( uint exponent ) : byte[]

Helper for converting a UInt32 exponent to bytes.

ExportKeyBlob ( bool includePrivateParameters, SafeKeyHandle safeKeyHandle ) : byte[]

Helper for Export CSP

GenerateKey ( System.Security.Cryptography.SafeProvHandle safeProvHandle, int algID, int flags, uint keySize, SafeKeyHandle &safeKeyHandle ) : int

Generates the key if provided CSP handle is valid

GetAlgIdFromOid ( string oid, OidGroup oidGroup ) : int

Convert an OID into a CAPI-1 CALG ID.

GetDefaultProvider ( int dwType ) : string

Find the default provider name to be used in the case that we were not actually passed in a provider name. The main purpose of this code is really to deal with the enhanced/default provider problems given to us by CAPI.

GetErrorCode ( ) : int

Wrapper for get last error function

GetKeyPairHelper ( CspAlgorithmType keyType, CspParameters parameters, int keySize, System.Security.Cryptography.SafeProvHandle safeProvHandle ) : SafeKeyHandle

Helper function to get the key pair

GetKeyParameter ( SafeKeyHandle safeKeyHandle, int keyParam ) : byte[]

Method helps get the different key properties

GetPersistKeyInCsp ( System.Security.Cryptography.SafeProvHandle safeProvHandle ) : bool

Returns PersistKeyInCsp value

GetProviderParameterWorker ( System.Security.Cryptography.SafeProvHandle safeProvHandle, byte impType, int &cb, CryptGetProvParam flags ) : int

This method helps reduce the duplicate code in the GetProviderParameter method

GetUserKey ( System.Security.Cryptography.SafeProvHandle safeProvHandle, int keySpec, SafeKeyHandle &safeKeyHandle ) : int

Retrieves the handle for user public / private key pair.

ImportKeyBlob ( System.Security.Cryptography.SafeProvHandle saveProvHandle, CspProviderFlags flags, bool addNoSaltFlag, byte keyBlob, SafeKeyHandle &safeKeyHandle ) : void

Helper for Import CSP

IsFlagBitSet ( uint dwImp, uint flag ) : bool

This method validates the flag bits set or not. Only works for flags with just one bit set

MapCspKeyFlags ( int flags ) : int

Maps CspProviderFlags enumeration into CAPI flags.

MapCspProviderFlags ( int flags ) : uint

Maps CspProviderFlags enumeration into CAPI flags

ObjToHashAlgorithm ( Object hashAlg ) : HashAlgorithm
ReadReversed ( this br, int count ) : byte[]

Read in a byte array in reverse order.

SaveCspParameters ( CspAlgorithmType keyType, CspParameters userParameters, CspProviderFlags defaultFlags, bool &randomKeyContainer ) : CspParameters

Helper method to save the CSP parameters.

SetKeyParameter ( SafeKeyHandle safeKeyHandle, CryptGetKeyParamQueryType keyParam, byte value ) : void

Set a key property which is based on byte[]

SetKeyParameter ( SafeKeyHandle safeKeyHandle, CryptGetKeyParamQueryType keyParam, int value ) : void

Set a key property which is based on int

SetPersistKeyInCsp ( System.Security.Cryptography.SafeProvHandle safeProvHandle, bool fPersistKeyInCsp ) : void

Sets the PersistKeyInCsp

ToKeyBlob ( this rsaParameters, int algId ) : byte[]

Helper for RsaCryptoServiceProvider.ImportParameters()

ToPlainTextKeyBlob ( int algId, byte rawKey ) : byte[]

Helper for RC2CryptoServiceProvider and DESCryptoServiceProvider

ToRSAParameters ( this cspBlob, bool includePrivateParameters ) : RSAParameters

Helper for RsaCryptoServiceProvider.ExportParameters()

ValidateCspFlags ( CspProviderFlags flags ) : void

Validates the CSP flags are expected

VerifyValidHandle ( System.Security.Cryptography.SafeHandleZeroOrMinusOneIsInvalid handle ) : void

This method checks if the handle is invalid then it throws error

WriteKeyBlobHeader ( int algId, BinaryWriter bw ) : void
WriteReversed ( this bw, byte bytes ) : void

Write out a byte array in reverse order.

Method Details

CryptAcquireContext() public static method

Acquire a handle to a crypto service provider and optionally a key container
public static CryptAcquireContext ( System.Security.Cryptography.SafeProvHandle &psafeProvHandle, string pszContainer, string pszProvider, int dwProvType, uint dwFlags ) : bool
psafeProvHandle System.Security.Cryptography.SafeProvHandle
pszContainer string
pszProvider string
dwProvType int
dwFlags uint
return bool

CryptDestroyHash() public static method

Destroy a crypto hash.
public static CryptDestroyHash ( IntPtr hHash ) : bool
hHash System.IntPtr
return bool

CryptDestroyKey() public static method

Destroy a crypto key.
public static CryptDestroyKey ( IntPtr hKey ) : bool
hKey System.IntPtr
return bool

CryptReleaseContext() public static method

Destroy a crypto provider.
public static CryptReleaseContext ( IntPtr safeProvHandle, int dwFlags ) : bool
safeProvHandle System.IntPtr
dwFlags int
return bool

GetProviderParameter() public static method

This method queries the key container and get some of it's properties. Those properties should never cause UI to display.
public static GetProviderParameter ( System.Security.Cryptography.SafeProvHandle safeProvHandle, int keyNumber, int keyParam ) : object
safeProvHandle System.Security.Cryptography.SafeProvHandle
keyNumber int
keyParam int
return object

NameOrOidToHashAlgId() public static method

Helper for signing and verifications that accept a string to specify a hashing algorithm.
public static NameOrOidToHashAlgId ( String nameOrOid ) : int
nameOrOid String
return int

ObjToHashAlgId() public static method

Helper for signing and verifications that accept a string/Type/HashAlgorithm to specify a hashing algorithm.
public static ObjToHashAlgId ( Object hashAlg ) : int
hashAlg Object
return int

OpenCSP() public static method

OpenCSP performs the core work of opening and creating CSPs and containers in CSPs
public static OpenCSP ( CspParameters cspParameters, uint flags, System.Security.Cryptography.SafeProvHandle &safeProvHandle ) : int
cspParameters System.Security.Cryptography.CspParameters
flags uint
safeProvHandle System.Security.Cryptography.SafeProvHandle
return int

SignValue() public static method

Helper for RSACryptoServiceProvider.SignData/SignHash apis.
public static SignValue ( System.Security.Cryptography.SafeProvHandle hProv, SafeKeyHandle hKey, int keyNumber, int calgKey, int calgHash, byte hash ) : byte[]
hProv System.Security.Cryptography.SafeProvHandle
hKey SafeKeyHandle
keyNumber int
calgKey int
calgHash int
hash byte
return byte[]

UpgradeRSA() public static method

Check to see if a better CSP than the one requested is available RSA providers are supersets of each other in the following order: 1. MS_ENH_RSA_AES_PROV 2. MS_ENHANCED_PROV 3. MS_DEF_PROV This will return the best provider which is a superset of wszProvider, or NULL if there is no upgrade available on the machine.
public static UpgradeRSA ( int dwProvType, string wszProvider ) : string
dwProvType int Provider type
wszProvider string Provider name
return string

VerifySign() public static method

Helper for RSACryptoServiceProvider.VerifyData/VerifyHash apis.
public static VerifySign ( System.Security.Cryptography.SafeProvHandle hProv, SafeKeyHandle hKey, int calgKey, int calgHash, byte hash, byte signature ) : bool
hProv System.Security.Cryptography.SafeProvHandle
hKey SafeKeyHandle
calgKey int
calgHash int
hash byte
signature byte
return bool