Пример #1
0
        internal ECParameters ExportParameters(bool includePrivateParameters, int keySizeInBIts)
        {
            SecKeyPair keys = GetOrGenerateKeys(keySizeInBIts);

            SafeSecKeyRefHandle keyHandle = includePrivateParameters ? keys.PrivateKey : keys.PublicKey;

            if (keyHandle == null)
            {
                throw new CryptographicException(SR.Cryptography_OpenInvalidHandle);
            }

            DerSequenceReader keyReader  = Interop.AppleCrypto.SecKeyExport(keyHandle, includePrivateParameters);
            ECParameters      parameters = new ECParameters();

            if (includePrivateParameters)
            {
                keyReader.ReadPkcs8Blob(ref parameters);
            }
            else
            {
                keyReader.ReadSubjectPublicKeyInfo(ref parameters);
            }

            int size = AsymmetricAlgorithmHelpers.BitsToBytes(keySizeInBIts);

            KeyBlobHelpers.PadOrTrim(ref parameters.Q.X, size);
            KeyBlobHelpers.PadOrTrim(ref parameters.Q.Y, size);

            if (includePrivateParameters)
            {
                KeyBlobHelpers.PadOrTrim(ref parameters.D, size);
            }

            return(parameters);
        }
Пример #2
0
        internal static void ReadSubjectPublicKeyInfo(
            this DerSequenceReader algParameters,
            byte[] publicKeyBlob,
            ref DSAParameters parameters)
        {
            parameters.P = algParameters.ReadIntegerBytes();
            parameters.Q = algParameters.ReadIntegerBytes();
            parameters.G = algParameters.ReadIntegerBytes();

            DerSequenceReader privateKeyReader = DerSequenceReader.CreateForPayload(publicKeyBlob);
            parameters.Y = privateKeyReader.ReadIntegerBytes();

            KeyBlobHelpers.TrimPaddingByte(ref parameters.P);
            KeyBlobHelpers.TrimPaddingByte(ref parameters.Q);

            KeyBlobHelpers.PadOrTrim(ref parameters.G, parameters.P.Length);
            KeyBlobHelpers.PadOrTrim(ref parameters.Y, parameters.P.Length);
        }
Пример #3
0
        private static void ReadPkcs1PrivateBlob(byte[] privateKeyBytes, ref RSAParameters parameters)
        {
            // RSAPrivateKey::= SEQUENCE {
            //    version Version,
            //    modulus           INTEGER,  --n
            //    publicExponent INTEGER,  --e
            //    privateExponent INTEGER,  --d
            //    prime1 INTEGER,  --p
            //    prime2 INTEGER,  --q
            //    exponent1 INTEGER,  --d mod(p - 1)
            //    exponent2 INTEGER,  --d mod(q - 1)
            //    coefficient INTEGER,  --(inverse of q) mod p
            //    otherPrimeInfos OtherPrimeInfos OPTIONAL
            // }
            DerSequenceReader privateKey = new DerSequenceReader(privateKeyBytes);
            int version = privateKey.ReadInteger();

            if (version != 0)
            {
                throw new CryptographicException();
            }

            parameters.Modulus  = KeyBlobHelpers.TrimPaddingByte(privateKey.ReadIntegerBytes());
            parameters.Exponent = KeyBlobHelpers.TrimPaddingByte(privateKey.ReadIntegerBytes());

            int modulusLen = parameters.Modulus.Length;
            // Add one so that odd byte-length values (RSA 1032) get padded correctly.
            int halfModulus = (modulusLen + 1) / 2;

            parameters.D        = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), modulusLen);
            parameters.P        = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus);
            parameters.Q        = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus);
            parameters.DP       = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus);
            parameters.DQ       = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus);
            parameters.InverseQ = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus);

            if (privateKey.HasData)
            {
                throw new CryptographicException();
            }
        }
Пример #4
0
            public override DSAParameters ExportParameters(bool includePrivateParameters)
            {
                SecKeyPair keys = GetKeys();

                if (keys.PublicKey == null ||
                    (includePrivateParameters && keys.PrivateKey == null))
                {
                    throw new CryptographicException(SR.Cryptography_OpenInvalidHandle);
                }

                DSAParameters parameters = new DSAParameters();

                DerSequenceReader publicKeyReader =
                    Interop.AppleCrypto.SecKeyExport(keys.PublicKey, exportPrivate: false);

                publicKeyReader.ReadSubjectPublicKeyInfo(ref parameters);

                if (includePrivateParameters)
                {
                    DerSequenceReader privateKeyReader =
                        Interop.AppleCrypto.SecKeyExport(keys.PrivateKey, exportPrivate: true);

                    privateKeyReader.ReadPkcs8Blob(ref parameters);
                }

                KeyBlobHelpers.TrimPaddingByte(ref parameters.P);
                KeyBlobHelpers.TrimPaddingByte(ref parameters.Q);

                KeyBlobHelpers.PadOrTrim(ref parameters.G, parameters.P.Length);
                KeyBlobHelpers.PadOrTrim(ref parameters.Y, parameters.P.Length);

                if (includePrivateParameters)
                {
                    KeyBlobHelpers.PadOrTrim(ref parameters.X, parameters.Q.Length);
                }

                return(parameters);
            }