Esempio n. 1
0
        public static RSA CreateRSA(this WriterParameters writer_parameters)
        {
            string key_container;

            if (writer_parameters.StrongNameKeyBlob != null)
            {
                return(CryptoConvert.FromCapiKeyBlob(writer_parameters.StrongNameKeyBlob));
            }

            if (writer_parameters.StrongNameKeyContainer != null)
            {
                key_container = writer_parameters.StrongNameKeyContainer;
            }
            else if (!TryGetKeyContainer(writer_parameters.StrongNameKeyPair, out byte[] key, out key_container))
Esempio n. 2
0
        public static RSA CreateRSA(this StrongNameKeyPair key_pair)
        {
            byte[] buffer;
            string str;

            if (!TryGetKeyContainer(key_pair, out buffer, out str))
            {
                return(CryptoConvert.FromCapiKeyBlob(buffer));
            }
            return(new RSACryptoServiceProvider(new CspParameters {
                Flags = CspProviderFlags.UseMachineKeyStore,
                KeyContainerName = str,
                KeyNumber = 2
            }));
        }
Esempio n. 3
0
        public void FromCapiKeyBlob()
        {
            // keypair
            RSA rsa = CryptoConvert.FromCapiKeyBlob(strongName, 0);

            Assert.AreEqual(strongNameString, rsa.ToXmlString(true), "KeyPair");
            Assert.AreEqual(strongNamePublicKeyString, rsa.ToXmlString(false), "PublicKey-1");

            // public key (direct)
            rsa = CryptoConvert.FromCapiKeyBlob(strongNamePublicKey, 12);
            Assert.AreEqual(strongNamePublicKeyString, rsa.ToXmlString(false), "PublicKey-2");

            // public key (indirect - inside header)
            rsa = CryptoConvert.FromCapiKeyBlob(strongNamePublicKey, 0);
            Assert.AreEqual(strongNamePublicKeyString, rsa.ToXmlString(false), "PublicKey-3");
        }
Esempio n. 4
0
		public static RSA CreateRSA (this StrongNameKeyPair key_pair)
		{
			byte [] key;
			string key_container;

			if (!TryGetKeyContainer (key_pair, out key, out key_container))
				return CryptoConvert.FromCapiKeyBlob (key);

			var parameters = new CspParameters {
				Flags = CspProviderFlags.UseMachineKeyStore,
				KeyContainerName = key_container,
				KeyNumber = 2,
			};

			return new RSACryptoServiceProvider (parameters);
		}
Esempio n. 5
0
        public static RSA CreateRSA(this StrongNameKeyPair key_pair)
        {
            byte[] blob;
            string keyContainerName;

            if (!Mixin.TryGetKeyContainer(key_pair, out blob, out keyContainerName))
            {
                return(CryptoConvert.FromCapiKeyBlob(blob));
            }
            CspParameters parameters = new CspParameters
            {
                Flags            = CspProviderFlags.UseMachineKeyStore,
                KeyContainerName = keyContainerName,
                KeyNumber        = 2
            };

            return(new RSACryptoServiceProvider(parameters));
        }
 private RSA GetRSA()
 {
     if (this._rsa != null)
     {
         return(this._rsa);
     }
     if (this._keyPairArray != null)
     {
         try
         {
             this._rsa = CryptoConvert.FromCapiKeyBlob(this._keyPairArray);
         }
         catch
         {
             this._keyPairArray = null;
         }
     }
     return(this._rsa);
 }
        public void ImportCspBlob(byte[] keyBlob)
        {
            if (keyBlob == null)
            {
                throw new ArgumentNullException("keyBlob");
            }

            RSA rsa = CryptoConvert.FromCapiKeyBlob(keyBlob);

            if (rsa is RSACryptoServiceProvider)
            {
                // default (if no change are present in machine.config)
                RSAParameters rsap = rsa.ExportParameters(!(rsa as RSACryptoServiceProvider).PublicOnly);
                ImportParameters(rsap);
            }
            else
            {
                // we can't know from RSA if the private key is available
                try
                {
                    // so we try it...
                    RSAParameters rsap = rsa.ExportParameters(true);
                    ImportParameters(rsap);
                }
                catch
                {
                    // and fall back
                    RSAParameters rsap = rsa.ExportParameters(false);
                    ImportParameters(rsap);
                }
            }

            var p = new CspParameters(PROV_RSA_FULL);

            p.KeyNumber = keyBlob[5] == 0x24 ? AT_SIGNATURE : AT_KEYEXCHANGE;
            if (useMachineKeyStore)
            {
                p.Flags |= CspProviderFlags.UseMachineKeyStore;
            }
            store = new KeyPairPersistence(p);
        }
Esempio n. 8
0
 public void FromCapiKeyBlob_UnknownBlob()
 {
     byte[] blob = new byte [160];
     RSA    rsa  = CryptoConvert.FromCapiKeyBlob(blob, 12);
 }
Esempio n. 9
0
 public void FromCapiKeyBlob_InvalidOffset()
 {
     RSA rsa = CryptoConvert.FromCapiKeyBlob(new byte [0], 0);
 }
Esempio n. 10
0
 public void FromCapiKeyBlob_Null()
 {
     RSA rsa = CryptoConvert.FromCapiKeyBlob(null);
 }
Esempio n. 11
0
        //
        // Either keyFile or keyContainer has to be non-null
        //
        void LoadPublicKey(string keyFile, string keyContainer)
        {
            if (keyContainer != null)
            {
                try {
                    private_key = new StrongNameKeyPair(keyContainer);
                    public_key  = private_key.PublicKey;
                } catch {
                    Error_AssemblySigning("The specified key container `" + keyContainer + "' does not exist");
                }

                return;
            }

            bool key_file_exists = File.Exists(keyFile);

            //
            // For attribute based KeyFile do additional lookup
            // in output assembly path
            //
            if (!key_file_exists && Compiler.Settings.StrongNameKeyFile == null)
            {
                //
                // The key file can be relative to output assembly
                //
                string test_path = Path.Combine(Path.GetDirectoryName(file_name), keyFile);
                key_file_exists = File.Exists(test_path);
                if (key_file_exists)
                {
                    keyFile = test_path;
                }
            }

            if (!key_file_exists)
            {
                Error_AssemblySigning("The specified key file `" + keyFile + "' does not exist");
                return;
            }

            using (FileStream fs = new FileStream(keyFile, FileMode.Open, FileAccess.Read)) {
                byte[] snkeypair = new byte[fs.Length];
                fs.Read(snkeypair, 0, snkeypair.Length);

                // check for ECMA key
                if (snkeypair.Length == 16)
                {
                    public_key = snkeypair;
                    return;
                }

                try {
                    // take it, with or without, a private key
                    RSA rsa = CryptoConvert.FromCapiKeyBlob(snkeypair);
                    // and make sure we only feed the public part to Sys.Ref
                    byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob(rsa);

                    // AssemblyName.SetPublicKey requires an additional header
                    byte[] publicKeyHeader = new byte[8] {
                        0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00
                    };

                    // Encode public key
                    public_key = new byte[12 + publickey.Length];
                    Buffer.BlockCopy(publicKeyHeader, 0, public_key, 0, publicKeyHeader.Length);

                    // Length of Public Key (in bytes)
                    int lastPart = public_key.Length - 12;
                    public_key[8]  = (byte)(lastPart & 0xFF);
                    public_key[9]  = (byte)((lastPart >> 8) & 0xFF);
                    public_key[10] = (byte)((lastPart >> 16) & 0xFF);
                    public_key[11] = (byte)((lastPart >> 24) & 0xFF);

                    Buffer.BlockCopy(publickey, 0, public_key, 12, publickey.Length);
                } catch {
                    Error_AssemblySigning("The specified key file `" + keyFile + "' has incorrect format");
                    return;
                }

                if (delay_sign)
                {
                    return;
                }

                try {
                    // TODO: Is there better way to test for a private key presence ?
                    CryptoConvert.FromCapiPrivateKeyBlob(snkeypair);
                    private_key = new StrongNameKeyPair(snkeypair);
                } catch { }
            }
        }