Exemplo n.º 1
0
        /// <summary>
        /// Create and initialize structure from binary data with defined header
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PrivateKeyBlob FromBinary(BinaryReader reader, BlobHeader header)
        {
            var privateKeyBlob = new PrivateKeyBlob
            {
                Header    = header,
                RSAPubKey = RSAPubKey.FromBinary(reader),
            };

            int byteLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 3);
            int wordLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 4);

            privateKeyBlob.Modulus = new byte[byteLength];
            reader.Read(privateKeyBlob.Modulus, 0, privateKeyBlob.Modulus.Length);

            privateKeyBlob.Prime1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime1, 0, privateKeyBlob.Prime1.Length);

            privateKeyBlob.Prime2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime2, 0, privateKeyBlob.Prime2.Length);

            privateKeyBlob.Exponent1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent1, 0, privateKeyBlob.Exponent1.Length);

            privateKeyBlob.Exponent2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent2, 0, privateKeyBlob.Exponent2.Length);

            privateKeyBlob.Coefficient = new byte[wordLength];
            reader.Read(privateKeyBlob.Coefficient, 0, privateKeyBlob.Coefficient.Length);

            privateKeyBlob.PrivateExponent = new byte[byteLength];
            reader.Read(privateKeyBlob.PrivateExponent, 0, privateKeyBlob.PrivateExponent.Length);

            return(privateKeyBlob);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create and initialize structure from binary data
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static BlobHeader FromBinary(BinaryReader reader)
        {
            var blobHeader = new BlobHeader
            {
                BlobType     = (KeyBlobType)reader.ReadByte(),
                Version      = reader.ReadByte(),
                Reserved     = reader.ReadUInt16(),
                KeyAlgorithm = (AlgorithmIdentifier)reader.ReadUInt32()
            };

            // Validate
            if (blobHeader.BlobType != KeyBlobType.PublicKeyBlob && blobHeader.BlobType != KeyBlobType.PrivateKeyBlob)
            {
                throw new CryptographicException(string.Format("Unsupported Key BLOB type [{0}] in BlobHeader",
                                                               blobHeader.BlobType));
            }

            if (blobHeader.Version != 0x02)
            {
                throw new CryptographicException(string.Format("Unknown version [{0}] in BlobHeader",
                                                               blobHeader.Version));
            }

            if (blobHeader.KeyAlgorithm != AlgorithmIdentifier.CALG_RSA_KEYX)
            {
                throw new CryptographicException(
                          string.Format("Unsupported algorithm identifier [{0:X4}] in BlobHeader",
                                        (uint)blobHeader.KeyAlgorithm));
            }
            return(blobHeader);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create and initialize structure from RSAParameters
        /// </summary>
        /// <returns>Initialized structure</returns>
        public static BlobHeader FromRSAParameters(KeyBlobType blobType)
        {
            var blobHeader = new BlobHeader
            {
                BlobType     = blobType,
                Version      = 0x02,
                Reserved     = 0x0000,
                KeyAlgorithm = AlgorithmIdentifier.CALG_RSA_KEYX
            };

            return(blobHeader);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create and initialize structure from RSAParameters
        /// </summary>
        /// <returns>Initialized structure</returns>
        /// <note>http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx</note>
        public static PrivateKeyBlob FromRSAParameters(RSAParameters @params)
        {
            var privateKeyBlob = new PrivateKeyBlob
            {
                Header    = BlobHeader.FromRSAParameters(KeyBlobType.PrivateKeyBlob),
                RSAPubKey = RSAPubKey.FromRSAParameters(@params, true),
            };

            privateKeyBlob.Modulus = new byte[@params.N.Length];
            for (int i = 0; i < privateKeyBlob.Modulus.Length; i++)
            {
                privateKeyBlob.Modulus[i] = @params.N[@params.N.Length - i - 1];
            }

            privateKeyBlob.Prime1 = new byte[@params.P.Length];
            for (int i = 0; i < privateKeyBlob.Prime1.Length; i++)
            {
                privateKeyBlob.Prime1[i] = @params.P[@params.P.Length - i - 1];
            }

            privateKeyBlob.Prime2 = new byte[@params.Q.Length];
            for (int i = 0; i < privateKeyBlob.Prime2.Length; i++)
            {
                privateKeyBlob.Prime2[i] = @params.Q[@params.Q.Length - i - 1];
            }

            privateKeyBlob.Exponent1 = new byte[@params.DP.Length];
            for (int i = 0; i < privateKeyBlob.Exponent1.Length; i++)
            {
                privateKeyBlob.Exponent1[i] = @params.DP[@params.DP.Length - i - 1];
            }

            privateKeyBlob.Exponent2 = new byte[@params.DQ.Length];
            for (int i = 0; i < privateKeyBlob.Exponent2.Length; i++)
            {
                privateKeyBlob.Exponent2[i] = @params.DQ[@params.DQ.Length - i - 1];
            }

            privateKeyBlob.Coefficient = new byte[@params.IQ.Length];
            for (int i = 0; i < privateKeyBlob.Coefficient.Length; i++)
            {
                privateKeyBlob.Coefficient[i] = @params.IQ[@params.IQ.Length - i - 1];
            }

            privateKeyBlob.PrivateExponent = new byte[@params.D.Length];
            for (int i = 0; i < privateKeyBlob.PrivateExponent.Length; i++)
            {
                privateKeyBlob.PrivateExponent[i] = @params.D[@params.D.Length - i - 1];
            }

            return(privateKeyBlob);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create and initialize structure from binary data with defined header
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PublicKeyBlob FromBinary(BinaryReader reader, BlobHeader header)
        {
            var publicKeyBlob = new PublicKeyBlob
            {
                Header    = header,
                RSAPubKey = RSAPubKey.FromBinary(reader),
            };

            int modulusLength = (int)(publicKeyBlob.RSAPubKey.BitLength >> 3);

            publicKeyBlob.Modulus = new byte[modulusLength];
            reader.Read(publicKeyBlob.Modulus, 0, publicKeyBlob.Modulus.Length);

            return(publicKeyBlob);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create and initialize structure from RSAParameters
        /// </summary>
        /// <returns>Initialized structure</returns>
        /// <note>http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx</note>
        public static PublicKeyBlob FromRSAParameters(RSAParameters @params)
        {
            var publicKeyBlob = new PublicKeyBlob
            {
                Header    = BlobHeader.FromRSAParameters(KeyBlobType.PublicKeyBlob),
                RSAPubKey = RSAPubKey.FromRSAParameters(@params, false),

                Modulus = new byte[@params.N.Length],
            };

            for (int i = 0; i < publicKeyBlob.Modulus.Length; i++)
            {
                publicKeyBlob.Modulus[i] = @params.N[@params.N.Length - i - 1];
            }

            return(publicKeyBlob);
        }
        /// <summary>
        /// Imports a blob that represents asymmetric key information.
        /// </summary>
        /// <param name="rawData">A byte array that represents an asymmetric key blob.</param>
        /// <exception cref="CryptographicException">Invalid key blob data</exception>
        /// <returns>Initialized RSAParameters structure</returns>
        public void ImportCspBlob(byte[] rawData)
        {
            using (var stream = new MemoryStream(rawData))
            {
                using (var reader = new BinaryReader(stream))
                {
                    BlobHeader header = BlobHeader.FromBinary(reader);

                    if (header.BlobType == KeyBlobType.PublicKeyBlob)
                    {
                        this.ImportParameters(PublicKeyBlob.FromBinary(reader, header).ToRSAParameters());
                        return;
                    }

                    if (header.BlobType == KeyBlobType.PrivateKeyBlob)
                    {
                        this.ImportParameters(PrivateKeyBlob.FromBinary(reader, header).ToRSAParameters());
                        return;
                    }
                }
            }

            throw new CryptographicException("Invalid key blob data");
        }
Exemplo n.º 8
0
        /// <summary>
        /// Create and initialize structure from binary data with defined header
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PublicKeyBlob FromBinary(BinaryReader reader, BlobHeader header)
        {
            var publicKeyBlob = new PublicKeyBlob
            {
                Header = header,
                RSAPubKey = RSAPubKey.FromBinary(reader),
            };

            int modulusLength = (int)(publicKeyBlob.RSAPubKey.BitLength >> 3);

            publicKeyBlob.Modulus = new byte[modulusLength];
            reader.Read(publicKeyBlob.Modulus, 0, publicKeyBlob.Modulus.Length);

            return publicKeyBlob;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Create and initialize structure from binary data with defined header
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PrivateKeyBlob FromBinary(BinaryReader reader, BlobHeader header)
        {
            var privateKeyBlob = new PrivateKeyBlob
            {
                Header = header,
                RSAPubKey = RSAPubKey.FromBinary(reader),
            };

            int byteLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 3);
            int wordLength = (int)(privateKeyBlob.RSAPubKey.BitLength >> 4);

            privateKeyBlob.Modulus = new byte[byteLength];
            reader.Read(privateKeyBlob.Modulus, 0, privateKeyBlob.Modulus.Length);

            privateKeyBlob.Prime1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime1, 0, privateKeyBlob.Prime1.Length);

            privateKeyBlob.Prime2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Prime2, 0, privateKeyBlob.Prime2.Length);

            privateKeyBlob.Exponent1 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent1, 0, privateKeyBlob.Exponent1.Length);

            privateKeyBlob.Exponent2 = new byte[wordLength];
            reader.Read(privateKeyBlob.Exponent2, 0, privateKeyBlob.Exponent2.Length);

            privateKeyBlob.Coefficient = new byte[wordLength];
            reader.Read(privateKeyBlob.Coefficient, 0, privateKeyBlob.Coefficient.Length);

            privateKeyBlob.PrivateExponent = new byte[byteLength];
            reader.Read(privateKeyBlob.PrivateExponent, 0, privateKeyBlob.PrivateExponent.Length);

            return privateKeyBlob;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Create and initialize structure from RSAParameters
        /// </summary>
        /// <returns>Initialized structure</returns>
        public static BlobHeader FromRSAParameters(KeyBlobType blobType)
        {
            var blobHeader = new BlobHeader
            {
                BlobType = blobType,
                Version = 0x02,
                Reserved = 0x0000,
                KeyAlgorithm = AlgorithmIdentifier.CALG_RSA_KEYX
            };

            return blobHeader;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Create and initialize structure from binary data
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static BlobHeader FromBinary(BinaryReader reader)
        {
            var blobHeader = new BlobHeader
            {
                BlobType = (KeyBlobType)reader.ReadByte(),
                Version = reader.ReadByte(),
                Reserved = reader.ReadUInt16(),
                KeyAlgorithm = (AlgorithmIdentifier)reader.ReadUInt32()
            };

            // Validate
            if (blobHeader.BlobType != KeyBlobType.PublicKeyBlob && blobHeader.BlobType != KeyBlobType.PrivateKeyBlob)
                throw new CryptographicException(string.Format("Unsupported Key BLOB type [{0}] in BlobHeader",
                                                               blobHeader.BlobType));

            if (blobHeader.Version != 0x02)
                throw new CryptographicException(string.Format("Unknown version [{0}] in BlobHeader",
                                                               blobHeader.Version));

            if (blobHeader.KeyAlgorithm != AlgorithmIdentifier.CALG_RSA_KEYX)
                throw new CryptographicException(
                    string.Format("Unsupported algorithm identifier [{0:X4}] in BlobHeader",
                                  (uint)blobHeader.KeyAlgorithm));
            return blobHeader;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Create and initialize structure from binary data
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PublicKeyBlob FromBinary(BinaryReader reader)
        {
            BlobHeader header = BlobHeader.FromBinary(reader);

            return(FromBinary(reader, header));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Create and initialize structure from binary data
        /// </summary>
        /// <exception cref="CryptographicException">On validate errors</exception>
        /// <returns>Initialized structure</returns>
        public static PrivateKeyBlob FromBinary(BinaryReader reader)
        {
            var header = BlobHeader.FromBinary(reader);

            return(FromBinary(reader, header));
        }