Example #1
0
        public void CreateKeyRoundtripBlob()
        {
            const int KeySize = 1024;

            byte[] blob;

            using (var gost = GetGostProvider())
            {
                CspKeyContainerInfo containerInfo = gost.CspKeyContainerInfo;
                Assert.Equal(Gost2012_512ProvType, containerInfo.ProviderType);
                Assert.Equal(KeySize, gost.KeySize);

                blob = gost.ExportCspBlob(false);
            }

            using (var gost = new Gost3410_2012_512CryptoServiceProvider())
            {
                gost.ImportCspBlob(blob);

                CspKeyContainerInfo containerInfo = gost.CspKeyContainerInfo;

                // The provider information is not persisted in the blob
                Assert.Equal(Gost2012_512ProvType, containerInfo.ProviderType);
                Assert.Equal(KeySize, gost.KeySize);
            }
        }
Example #2
0
        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);
            }
        }