private static string KeyParametersToXml(Asn1.Common.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());
        }
        public Gost3410EphemeralAsymmetricAlgorithm(Asn1.Common.GostKeyExchangeParameters keyParameters)
        {
            if (keyParameters == null)
            {
                throw ExceptionUtility.ArgumentNull("keyParameters");
            }

            _provHandle = CryptoApiHelper.ProviderHandle.DangerousAddRef();
            _keyHandle  = CryptoApiHelper.GenerateDhEphemeralKey(_provHandle, keyParameters.DigestParamSet, keyParameters.PublicKeyParamSet);
        }
Esempio n. 3
0
        public override void ImportParameters(Asn1.Common.GostKeyExchangeParameters keyParameters)
        {
            if (keyParameters.PrivateKey != null)
            {
                throw ExceptionUtility.NotSupported(Resources.UserImportBulkKeyNotSupported);
            }

            _keyHandle.TryDispose();

            _providerHandle = CryptoApiHelper.ProviderHandle;
            _keyHandle      = CryptoApiHelper.ImportPublicKey(_providerHandle, new Asn1.Common.GostKeyExchangeParameters(keyParameters));

            _isPublicKeyOnly = true;
        }
        public static AsymmetricAlgorithm GetPublicKeyAlgorithm(this X509Certificate2 certificate)
        {
            if (certificate == null)
            {
                throw ExceptionUtility.ArgumentNull("certificate");
            }

            var cspObject = new Asn1.Common.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);
        }
Esempio n. 5
0
        public static byte[] EncodePublicBlob(Asn1.Common.GostKeyExchangeParameters publicKeyParameters)
        {
            if (publicKeyParameters == null)
            {
                throw ExceptionUtility.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(Constants.CALG_GR3410EL), 0, importedKeyBytes, 4, 4);
            Array.Copy(BitConverter.GetBytes(Constants.GR3410_1_MAGIC), 0, importedKeyBytes, 8, 4);
            Array.Copy(BitConverter.GetBytes(Constants.EL_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);
        }
Esempio n. 6
0
        public GostKeyExchangeAlgorithm(SafeProvHandleImpl provHandle, SafeKeyHandleImpl keyHandle, Asn1.Common.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;
        }
Esempio n. 7
0
        private static Asn1.Common.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 Asn1.Common.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);
        }
 /// <summary>
 /// Импортирует (дешифрует) параметры ключа, используемого для создания общего секретного ключа.
 /// </summary>
 /// <param name="keyParameters">Параметры ключа, используемого для создания общего секретного ключа.</param>
 /// <exception cref="NotSupportedException"></exception>
 public override void ImportParameters(Asn1.Common.GostKeyExchangeParameters keyParameters)
 {
     throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported);
 }
 public override GostKeyExchangeAlgorithmBase CreateKeyExchange(Asn1.Common.GostKeyExchangeParameters keyParameters)
 {
     return(new GostKeyExchangeAlgorithm(_provHandle, _keyHandle, new Asn1.Common.GostKeyExchangeParameters(keyParameters)));
 }
Esempio n. 10
0
        public static SafeKeyHandleImpl ImportPublicKey(SafeProvHandleImpl providerHandle, Asn1.Common.GostKeyExchangeParameters publicKeyParameters)
        {
            if (publicKeyParameters == null)
            {
                throw ExceptionUtility.ArgumentNull("publicKeyParameters");
            }

            var importedKeyBytes = EncodePublicBlob(publicKeyParameters);

            SafeKeyHandleImpl hKeyExchange;

            ImportCspBlob(importedKeyBytes, providerHandle, SafeKeyHandleImpl.InvalidHandle, out hKeyExchange);

            return(hKeyExchange);
        }
Esempio n. 11
0
        public static SafeKeyHandleImpl ImportAndMakeKeyExchange(SafeProvHandleImpl providerHandle, Asn1.Common.GostKeyExchangeParameters keyExchangeParameters, SafeKeyHandleImpl publicKeyHandle)
        {
            if (keyExchangeParameters == null)
            {
                throw ExceptionUtility.ArgumentNull("keyExchangeParameters");
            }

            var importedKeyBytes = EncodePublicBlob(keyExchangeParameters);

            SafeKeyHandleImpl keyExchangeHandle;

            ImportCspBlob(importedKeyBytes, providerHandle, publicKeyHandle, out keyExchangeHandle);

            return(keyExchangeHandle);
        }
 /// <summary>
 /// Импортирует (дешифрует) параметры ключа, используемого для создания общего секретного ключа.
 /// </summary>
 /// <param name="keyParameters">Параметры ключа, используемого для создания общего секретного ключа.</param>
 public abstract void ImportParameters(Asn1.Common.GostKeyExchangeParameters keyParameters);
 /// <summary>
 /// Создает общий секретный ключ.
 /// </summary>
 /// <param name="keyParameters">Параметры открытого ключа, используемого для создания общего секретного ключа.</param>
 public abstract GostKeyExchangeAlgorithmBase CreateKeyExchange(Asn1.Common.GostKeyExchangeParameters keyParameters);
        private static Asn1.Common.GostKeyExchangeParameters KeyParametersFromXml(string keyParametersXml)
        {
            var parameters = new Asn1.Common.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);
        }