private static string KeyParametersToXml(GostKeyExchangeParameters parameters) { var builder = new StringBuilder().AppendFormat("<{0}>", KeyValueXmlTag); if ((parameters.DigestParamSet != null) || (parameters.EncryptionParamSet != null) || (parameters.PublicKeyParamSet != null)) { builder.AppendFormat("<{0}>", PublicKeyParametersXmlTag); builder.AppendFormat("<{0}>{1}{2}</{0}>", PublicKeyParamSetXmlTag, UrnOidXmlTerm, parameters.PublicKeyParamSet); builder.AppendFormat("<{0}>{1}{2}</{0}>", DigestParamSetXmlTag, UrnOidXmlTerm, parameters.DigestParamSet); if (parameters.EncryptionParamSet != null) { builder.AppendFormat("<{0}>{1}{2}</{0}>", EncryptionParamSetXmlTag, UrnOidXmlTerm, parameters.EncryptionParamSet); } builder.AppendFormat("</{0}>", PublicKeyParametersXmlTag); } builder.AppendFormat("<{0}>{1}</{0}>", PublicKeyXmlTag, Convert.ToBase64String(parameters.PublicKey)); if (parameters.PrivateKey != null) { builder.AppendFormat("<{0}>{1}</{0}>", PrivateKeyXmlTag, Convert.ToBase64String(parameters.PublicKey)); } builder.AppendFormat("</{0}>", KeyValueXmlTag); return(builder.ToString()); }
private static GostKeyExchangeParameters DecodePublicBlob(byte[] encodedPublicBlob, GostAlgorithmType algorithm) { if (encodedPublicBlob == null) { throw ExceptionUtility.ArgumentNull("encodedPublicBlob"); } int size; switch (algorithm) { case GostAlgorithmType.Gost2001: size = 512; break; case GostAlgorithmType.Gost2012_256: size = 512; break; case GostAlgorithmType.Gost2012_512: size = 1024; break; default: throw new CryptographicException(Resources.AlgorithmNotAvailable); } if (encodedPublicBlob.Length < 16 + size / 8) { throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA); } var gostKeyMask = BitConverter.ToUInt32(encodedPublicBlob, 8); if (gostKeyMask != Constants.GR3410_1_MAGIC) { throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA); } var gostKeySize = BitConverter.ToUInt32(encodedPublicBlob, 12); if (gostKeySize != size) { throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA); } var publicKeyParameters = new GostKeyExchangeParameters(); var encodeKeyParameters = new byte[(encodedPublicBlob.Length - 16) - size / 8]; Array.Copy(encodedPublicBlob, 16, encodeKeyParameters, 0, (encodedPublicBlob.Length - 16) - size / 8); publicKeyParameters.DecodeParameters(encodeKeyParameters); var publicKey = new byte[64]; Array.Copy(encodedPublicBlob, encodedPublicBlob.Length - size / 8, publicKey, 0, size / 8); publicKeyParameters.PublicKey = publicKey; return(publicKeyParameters); }
public GostKeyExchangeParameters(GostKeyExchangeParameters parameters) { DigestParamSet = parameters.DigestParamSet; PublicKeyParamSet = parameters.PublicKeyParamSet; EncryptionParamSet = parameters.EncryptionParamSet; PublicKey = parameters.PublicKey; PrivateKey = parameters.PrivateKey; }
public Gost3410EphemeralAsymmetricAlgorithm(GostKeyExchangeParameters keyParameters) { if (keyParameters == null) { throw ExceptionUtility.ArgumentNull("keyParameters"); } _provHandle = CryptoApiHelper.ProviderHandle.DangerousAddRef(); _keyHandle = CryptoApiHelper.GenerateDhEphemeralKey(_provHandle, Constants.CALG_DH_EL_EPHEM, keyParameters.DigestParamSet, keyParameters.PublicKeyParamSet); }
public override GostSharedSecret CreateKeyExchange(GostKeyExchangeParameters keyParameters) { GostSharedSecret res = null; UsingKey(h => { res = new GostSharedSecretAlgorithm(_providerHandle, h, new GostKeyExchangeParameters(keyParameters), GostAlgorithmType.Gost2012_512); }); return(res); }
public override void ImportParameters(GostKeyExchangeParameters keyParameters) { if (keyParameters.PrivateKey != null) { throw ExceptionUtility.NotSupported(Resources.UserImportBulkKeyNotSupported); } _keyHandleFunc = () => { return(CryptoApiHelper.ImportPublicKey(_providerHandle, new GostKeyExchangeParameters(keyParameters), GostAlgorithmType.Gost2012_512)); }; }
public override void ImportParameters(GostKeyExchangeParameters keyParameters) { if (keyParameters.PrivateKey != null) { throw ExceptionUtility.NotSupported(Resources.UserImportBulkKeyNotSupported); } _keyHandle.TryDispose(); _providerHandle = CryptoApiHelper.ProviderHandle; _keyHandle = CryptoApiHelper.ImportPublicKey(_providerHandle, new GostKeyExchangeParameters(keyParameters)); _isPublicKeyOnly = true; }
public override GostKeyExchangeParameters ExportParameters(bool includePrivateKey) { if (includePrivateKey) { throw new NotSupportedException(); } GostKeyExchangeParameters res = null; UsingKey(h => { res = CryptoApiHelper.ExportPublicKey(h, GostAlgorithmType.Gost2012_512); }); return(res); }
public byte[] EncodeParameters() { byte[] data; var publicKeyParameters = new Gost3410PublicKeyParameters(); publicKeyParameters.DigestParamSet = Asn1ObjectIdentifier.FromOidString(_digestParamSet); publicKeyParameters.PublicKeyParamSet = Asn1ObjectIdentifier.FromOidString(_publicKeyParamSet); publicKeyParameters.EncryptionParamSet = GostKeyExchangeParameters.CreateEncryptionParamSet(_encryptionParamSet); var asnEncoder = new Asn1BerEncodeBuffer(); publicKeyParameters.Encode(asnEncoder); data = asnEncoder.MsgCopy; return(data); }
public static byte[] EncodePublicBlob(GostKeyExchangeParameters publicKeyParameters, GostAlgorithmType algorithm) { if (publicKeyParameters == null) { throw ExceptionUtility.ArgumentNull("publicKeyParameters"); } int size; int value; switch (algorithm) { case GostAlgorithmType.Gost2001: size = 512; value = Constants.CALG_GR3410EL; break; case GostAlgorithmType.Gost2012_256: size = 512; value = Constants.CALG_GR3410_2012_256; break; case GostAlgorithmType.Gost2012_512: size = 1024; value = Constants.CALG_GR3410_2012_512; break; default: throw new CryptographicException(Resources.AlgorithmNotAvailable); } var encodeKeyParameters = publicKeyParameters.EncodeParameters(); var importedKeyBytes = new byte[(encodeKeyParameters.Length + 16) + publicKeyParameters.PublicKey.Length]; importedKeyBytes[0] = 6; importedKeyBytes[1] = 32; Array.Copy(BitConverter.GetBytes(value), 0, importedKeyBytes, 4, 4); Array.Copy(BitConverter.GetBytes(Constants.GR3410_1_MAGIC), 0, importedKeyBytes, 8, 4); Array.Copy(BitConverter.GetBytes(size), 0, importedKeyBytes, 12, 4); Array.Copy(encodeKeyParameters, 0, importedKeyBytes, 16, encodeKeyParameters.Length); Array.Copy(publicKeyParameters.PublicKey, 0, importedKeyBytes, encodeKeyParameters.Length + 16, publicKeyParameters.PublicKey.Length); return(importedKeyBytes); }
public static byte[] EncodePublicBlob(GostKeyExchangeParameters publicKeyParameters) { if (publicKeyParameters == null) { throw new Exception("ArgumentNull - publicKeyParameters"); } var encodeKeyParameters = publicKeyParameters.EncodeParameters(); var importedKeyBytes = new byte[(encodeKeyParameters.Length + 16) + publicKeyParameters.PublicKey.Length]; importedKeyBytes[0] = 6; importedKeyBytes[1] = 32; Array.Copy(BitConverter.GetBytes(GostConstants.CALG_GR3410EL), 0, importedKeyBytes, 4, 4); Array.Copy(BitConverter.GetBytes(GostConstants.GR3410_1_MAGIC), 0, importedKeyBytes, 8, 4); Array.Copy(BitConverter.GetBytes(GostConstants.GOST_3410EL_SIZE), 0, importedKeyBytes, 12, 4); Array.Copy(encodeKeyParameters, 0, importedKeyBytes, 16, encodeKeyParameters.Length); Array.Copy(publicKeyParameters.PublicKey, 0, importedKeyBytes, encodeKeyParameters.Length + 16, publicKeyParameters.PublicKey.Length); return(importedKeyBytes); }
public static AsymmetricAlgorithm GetPublicKeyAlgorithm(this X509Certificate2 certificate) { if (certificate == null) { throw ExceptionUtility.ArgumentNull("certificate"); } var cspObject = new GostKeyExchangeParameters(); cspObject.DecodeParameters(certificate.PublicKey.EncodedParameters.RawData); cspObject.DecodePublicKey(certificate.PublicKey.EncodedKeyValue.RawData); var cspBlobData = CryptoApiHelper.EncodePublicBlob(cspObject); var publicKey = new Gost3410AsymmetricAlgorithm(); publicKey.ImportCspBlob(cspBlobData); return(publicKey); }
public GostKeyExchangeAlgorithm(SafeProvHandleImpl provHandle, SafeKeyHandleImpl keyHandle, GostKeyExchangeParameters keyExchangeParameters) { if (provHandle == null) { throw ExceptionUtility.ArgumentNull("provHandle"); } if (keyHandle == null) { throw ExceptionUtility.ArgumentNull("keyHandle"); } if (keyExchangeParameters == null) { throw ExceptionUtility.ArgumentNull("keyExchangeParameters"); } _provHandle = provHandle.DangerousAddRef(); _keyHandle = keyHandle.DangerousAddRef(); _keyExchangeParameters = keyExchangeParameters; }
private static GostKeyExchangeParameters DecodePublicBlob(byte[] encodedPublicBlob) { if (encodedPublicBlob == null) { throw ExceptionUtility.ArgumentNull("encodedPublicBlob"); } if (encodedPublicBlob.Length < 80) { throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA); } var gostKeyMask = BitConverter.ToUInt32(encodedPublicBlob, 8); if (gostKeyMask != Constants.GR3410_1_MAGIC) { throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA); } var gostKeySize = BitConverter.ToUInt32(encodedPublicBlob, 12); if (gostKeySize != 512) { throw ExceptionUtility.CryptographicException(Constants.NTE_BAD_DATA); } var publicKeyParameters = new GostKeyExchangeParameters(); var encodeKeyParameters = new byte[(encodedPublicBlob.Length - 16) - 64]; Array.Copy(encodedPublicBlob, 16, encodeKeyParameters, 0, (encodedPublicBlob.Length - 16) - 64); publicKeyParameters.DecodeParameters(encodeKeyParameters); var publicKey = new byte[64]; Array.Copy(encodedPublicBlob, encodedPublicBlob.Length - 64, publicKey, 0, 64); publicKeyParameters.PublicKey = publicKey; return(publicKeyParameters); }
public static byte[] EncodePublicBlob(GostKeyExchangeParameters publicKeyParameters, int algId) { if (publicKeyParameters == null) { throw new ArgumentNullException(nameof(publicKeyParameters)); } int keySize; if (algId == GostConstants.CALG_GR3410EL) { keySize = GostConstants.GOST_3410EL_SIZE; } else if (algId == GostConstants.CALG_GR3410_12_256) { keySize = GostConstants.GOST3410_2012_256KEY_SIZE; } else if (algId == GostConstants.CALG_GR3410_12_512) { keySize = GostConstants.GOST3410_2012_512KEY_SIZE; } else { throw new CryptographicException( SR.Cryptography_CSP_WrongKeySpec); } var encodeKeyParameters = publicKeyParameters.EncodeParameters(); var importedKeyBytes = new byte[(encodeKeyParameters.Length + 16) + publicKeyParameters.PublicKey.Length]; importedKeyBytes[0] = 6; importedKeyBytes[1] = 32; Array.Copy(BitConverter.GetBytes(algId), 0, importedKeyBytes, 4, 4); Array.Copy(BitConverter.GetBytes(GostConstants.GR3410_1_MAGIC), 0, importedKeyBytes, 8, 4); Array.Copy(BitConverter.GetBytes(keySize), 0, importedKeyBytes, 12, 4); Array.Copy(encodeKeyParameters, 0, importedKeyBytes, 16, encodeKeyParameters.Length); Array.Copy(publicKeyParameters.PublicKey, 0, importedKeyBytes, encodeKeyParameters.Length + 16, publicKeyParameters.PublicKey.Length); return(importedKeyBytes); }
public static SafeKeyHandleImpl ImportPublicKey(SafeProvHandleImpl providerHandle, GostKeyExchangeParameters publicKeyParameters, GostAlgorithmType algorithm) { if (publicKeyParameters == null) { throw ExceptionUtility.ArgumentNull("publicKeyParameters"); } var importedKeyBytes = EncodePublicBlob(publicKeyParameters, algorithm); SafeKeyHandleImpl hKeyExchange; ImportCspBlob(importedKeyBytes, providerHandle, SafeKeyHandleImpl.InvalidHandle, out hKeyExchange); return(hKeyExchange); }
public override GostSharedSecret CreateKeyExchange(GostKeyExchangeParameters keyParameters) { return(new GostSharedSecretAlgorithm(_provHandle, _keyHandle, new GostKeyExchangeParameters(keyParameters), GostAlgorithmType.Gost2001)); }
/// <summary> /// Разбор BLOB открытого ключа ГОСТ 34.10. /// </summary> /// /// <param name="obj">Gost3410CspObject</param> /// <param name="data">BLOB</param> /// <param name="alg">Тип алгоритма</param> /// /// <argnull name="obj" /> /// <exception cref="CryptographicException">Если /// <paramref name="obj"/> не объект типа /// <see cref="Gost3410CspObject"/></exception> /// /// <intdoc><para>Аналог в MS отсутствует, часть реализации /// присутствует в ImportKey. </para></intdoc> /// /// <unmanagedperm action="LinkDemand" /> internal static void DecodePublicBlob(Object obj, byte[] data, CspAlgorithmType alg) { int keySize; switch (alg) { case CspAlgorithmType.PROV_GOST_2001_DH: keySize = GostConstants.GOST_3410EL_SIZE; break; case CspAlgorithmType.PROV_GOST_2012_256: keySize = GostConstants.GOST3410_2012_256KEY_SIZE; break; case CspAlgorithmType.PROV_GOST_2012_512: keySize = GostConstants.GOST3410_2012_512KEY_SIZE; break; default: throw new CryptographicException(SR.Cryptography_CSP_WrongKeySpec); } if (obj == null) { throw new ArgumentNullException("obj"); } Gost3410CspObject cspObject = obj as Gost3410CspObject; if (cspObject == null) { throw new CryptographicException(GostConstants.NTE_BAD_ALGID); } if (data.Length < 16 + keySize / 8) { throw new CryptographicException(GostConstants.NTE_BAD_DATA); } // CRYPT_PUBKEYPARAM -> 8 { Magic, BitLen ) uint magic = BitConverter.ToUInt32(data, 8); uint bitlen = BitConverter.ToUInt32(data, 12); if (magic != GostConstants.GR3410_1_MAGIC) { throw new CryptographicException(GostConstants.NTE_BAD_DATA); } if (bitlen != keySize) { throw new CryptographicException(GostConstants.NTE_BAD_DATA); } byte[] tmp = new byte[data.Length - 16 - keySize / 8]; Array.Copy(data, 16, tmp, 0, data.Length - 16 - keySize / 8); var publicKeyParameters = new GostKeyExchangeParameters(); var encodeKeyParameters = new byte[(data.Length - 16) - keySize / 8]; Array.Copy(data, 16, encodeKeyParameters, 0, (data.Length - 16) - keySize / 8); publicKeyParameters.DecodeParameters(encodeKeyParameters); var publicKey = new byte[keySize / 8]; Array.Copy(data, data.Length - keySize / 8, publicKey, 0, keySize / 8); publicKeyParameters.PublicKey = publicKey; cspObject._publicKey = publicKeyParameters.PublicKey; cspObject._publicKeyParamSet = publicKeyParameters.PublicKeyParamSet; cspObject._digestParamSet = publicKeyParameters.DigestParamSet; }
/// <summary> /// Импортирует (дешифрует) параметры ключа, используемого для создания общего секретного ключа. /// </summary> /// <param name="keyParameters">Параметры ключа, используемого для создания общего секретного ключа.</param> /// <exception cref="NotSupportedException"></exception> public override void ImportParameters(GostKeyExchangeParameters keyParameters) { throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported); }
public AsymmetricAlgorithm DecodePublicKey(Oid oid, byte[] encodedKeyValue, byte[] encodedParameters, ICertificatePal certificatePal) { if (oid.Value == Oids.EcPublicKey && certificatePal != null) { return(DecodeECDsaPublicKey((CertificatePal)certificatePal)); } int algId = Interop.Crypt32.FindOidInfo(CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY, oid.Value, OidGroup.PublicKeyAlgorithm, fallBackToAllGroups: true).AlgId; switch (algId) { case AlgId.CALG_RSA_KEYX: case AlgId.CALG_RSA_SIGN: { byte[] keyBlob = DecodeKeyBlob(CryptDecodeObjectStructType.CNG_RSA_PUBLIC_KEY_BLOB, encodedKeyValue); CngKey cngKey = CngKey.Import(keyBlob, CngKeyBlobFormat.GenericPublicBlob); return(new RSACng(cngKey)); } //begin: gost case AlgId.CALG_GOST3410: { var cspObject = new GostKeyExchangeParameters(); cspObject.DecodeParameters(encodedParameters); cspObject.DecodePublicKey(encodedKeyValue, algId); var cspBlobData = GostKeyExchangeParameters.EncodePublicBlob(cspObject, algId); Gost3410CryptoServiceProvider gost_sp = new Gost3410CryptoServiceProvider(); gost_sp.ImportCspBlob(cspBlobData); return(gost_sp); } case AlgId.CALG_GOST3410_2012_256: { var cspObject = new GostKeyExchangeParameters(); cspObject.DecodeParameters(encodedParameters); cspObject.DecodePublicKey(encodedKeyValue, algId); var cspBlobData = GostKeyExchangeParameters.EncodePublicBlob(cspObject, algId); Gost3410_2012_256CryptoServiceProvider gost_sp = new Gost3410_2012_256CryptoServiceProvider(); gost_sp.ImportCspBlob(cspBlobData); return(gost_sp); } case AlgId.CALG_GOST3410_2012_512: { var cspObject = new GostKeyExchangeParameters(); cspObject.DecodeParameters(encodedParameters); cspObject.DecodePublicKey(encodedKeyValue, algId); var cspBlobData = GostKeyExchangeParameters.EncodePublicBlob(cspObject, algId); Gost3410_2012_512CryptoServiceProvider gost_sp = new Gost3410_2012_512CryptoServiceProvider(); gost_sp.ImportCspBlob(cspBlobData); return(gost_sp); } //end: gost case AlgId.CALG_DSS_SIGN: { byte[] keyBlob = ConstructDSSPublicKeyCspBlob(encodedKeyValue, encodedParameters); DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(); dsa.ImportCspBlob(keyBlob); return(dsa); } default: throw new NotSupportedException(SR.NotSupported_KeyAlgorithm); } }
/// <summary> /// Импортирует (дешифрует) параметры ключа, используемого для создания общего секретного ключа. /// </summary> /// <param name="keyParameters">Параметры ключа, используемого для создания общего секретного ключа.</param> public abstract void ImportParameters(GostKeyExchangeParameters keyParameters);
private static GostKeyExchangeParameters KeyParametersFromXml(string keyParametersXml) { var parameters = new GostKeyExchangeParameters(); var keyValue = SecurityElement.FromString(keyParametersXml); if (keyValue == null) { throw ExceptionUtility.CryptographicException(Resources.InvalidFromXmlString, KeyValueXmlTag); } keyValue = SelectChildElement(keyValue, KeyValueXmlTag) ?? keyValue; var publicKeyParameters = SelectChildElement(keyValue, PublicKeyParametersXmlTag); if (publicKeyParameters != null) { var publicKeyParamSet = RemoveWhiteSpaces(SelectChildElementText(publicKeyParameters, PublicKeyParamSetXmlTag, false)); if (!publicKeyParamSet.StartsWith(UrnOidXmlTerm, StringComparison.OrdinalIgnoreCase)) { throw ExceptionUtility.CryptographicException(Resources.InvalidFromXmlString, PublicKeyParamSetXmlTag); } parameters.PublicKeyParamSet = publicKeyParamSet.Substring(UrnOidXmlTerm.Length); var digestParamSet = RemoveWhiteSpaces(SelectChildElementText(publicKeyParameters, DigestParamSetXmlTag, false)); if (!digestParamSet.StartsWith(UrnOidXmlTerm, StringComparison.OrdinalIgnoreCase)) { throw ExceptionUtility.CryptographicException(Resources.InvalidFromXmlString, DigestParamSetXmlTag); } parameters.DigestParamSet = digestParamSet.Substring(UrnOidXmlTerm.Length); var encryptionParamSet = SelectChildElementText(publicKeyParameters, EncryptionParamSetXmlTag, true); if (!string.IsNullOrEmpty(encryptionParamSet)) { encryptionParamSet = RemoveWhiteSpaces(encryptionParamSet); if (!encryptionParamSet.StartsWith(UrnOidXmlTerm, StringComparison.OrdinalIgnoreCase)) { throw ExceptionUtility.CryptographicException(Resources.InvalidFromXmlString, EncryptionParamSetXmlTag); } parameters.EncryptionParamSet = encryptionParamSet.Substring(UrnOidXmlTerm.Length); } } var publicKey = SelectChildElementText(keyValue, PublicKeyXmlTag, false); parameters.PublicKey = Convert.FromBase64String(RemoveWhiteSpaces(publicKey)); var privateKey = SelectChildElementText(keyValue, PrivateKeyXmlTag, true); if (privateKey != null) { parameters.PrivateKey = Convert.FromBase64String(RemoveWhiteSpaces(privateKey)); } return(parameters); }
public override GostKeyExchangeAlgorithmBase CreateKeyExchange(GostKeyExchangeParameters keyParameters) { return(new GostKeyExchangeAlgorithm(_provHandle, _keyHandle, new GostKeyExchangeParameters(keyParameters))); }
/// <summary> /// Создает общий секретный ключ. /// </summary> /// <param name="keyParameters">Параметры открытого ключа, используемого для создания общего секретного ключа.</param> public abstract GostSharedSecret CreateKeyExchange(GostKeyExchangeParameters keyParameters);
public static SafeKeyHandleImpl ImportAndMakeKeyExchange(SafeProvHandleImpl providerHandle, GostKeyExchangeParameters keyExchangeParameters, SafeKeyHandleImpl publicKeyHandle, GostAlgorithmType alg) { if (keyExchangeParameters == null) { throw ExceptionUtility.ArgumentNull("keyExchangeParameters"); } var importedKeyBytes = EncodePublicBlob(keyExchangeParameters, alg); SafeKeyHandleImpl keyExchangeHandle; ImportCspBlob(importedKeyBytes, providerHandle, publicKeyHandle, out keyExchangeHandle); return(keyExchangeHandle); }
/// <summary> /// Создает общий секретный ключ. /// </summary> /// <param name="keyParameters">Параметры открытого ключа, используемого для создания общего секретного ключа.</param> public abstract GostKeyExchangeAlgorithmBase CreateKeyExchange(GostKeyExchangeParameters keyParameters);