Ejemplo n.º 1
0
        public static ICipherParameters GenerateCipherParameters(
            string algorithm,
            char[]          password,
            bool wrongPkcs12Zero,
            Asn1Encodable pbeParameters)
        {
            string mechanism = (string)algorithms[Platform.ToUpperInvariant(algorithm)];

            byte[] keyBytes       = null;
            byte[] salt           = null;
            int    iterationCount = 0;

            if (IsPkcs12(mechanism))
            {
                Pkcs12PbeParams pbeParams = Pkcs12PbeParams.GetInstance(pbeParameters);
                salt           = pbeParams.GetIV();
                iterationCount = pbeParams.Iterations.IntValue;
                keyBytes       = PbeParametersGenerator.Pkcs12PasswordToBytes(password, wrongPkcs12Zero);
            }
            else if (IsPkcs5Scheme2(mechanism))
            {
                // See below
            }
            else
            {
                PbeParameter pbeParams = PbeParameter.GetInstance(pbeParameters);
                salt           = pbeParams.GetSalt();
                iterationCount = pbeParams.IterationCount.IntValue;
                keyBytes       = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
            }

            ICipherParameters parameters = null;

            if (IsPkcs5Scheme2(mechanism))
            {
                PbeS2Parameters     s2p       = PbeS2Parameters.GetInstance(pbeParameters.ToAsn1Object());
                AlgorithmIdentifier encScheme = s2p.EncryptionScheme;
                DerObjectIdentifier encOid    = encScheme.Algorithm;
                Asn1Object          encParams = encScheme.Parameters.ToAsn1Object();

                // TODO What about s2p.KeyDerivationFunc.Algorithm?
                Pbkdf2Params pbeParams = Pbkdf2Params.GetInstance(s2p.KeyDerivationFunc.Parameters.ToAsn1Object());

                byte[] iv;
                if (encOid.Equals(PkcsObjectIdentifiers.RC2Cbc)) // PKCS5.B.2.3
                {
                    RC2CbcParameter rc2Params = RC2CbcParameter.GetInstance(encParams);
                    iv = rc2Params.GetIV();
                }
                else
                {
                    iv = Asn1OctetString.GetInstance(encParams).GetOctets();
                }

                salt           = pbeParams.GetSalt();
                iterationCount = pbeParams.IterationCount.IntValue;
                keyBytes       = PbeParametersGenerator.Pkcs5PasswordToBytes(password);

                int keyLength = pbeParams.KeyLength != null
                    ?   pbeParams.KeyLength.IntValue * 8
                    :   GeneratorUtilities.GetDefaultKeySize(encOid);

                PbeParametersGenerator gen = MakePbeGenerator(
                    (string)algorithmType[mechanism], null, keyBytes, salt, iterationCount);

                parameters = gen.GenerateDerivedParameters(encOid.Id, keyLength);

                if (iv != null)
                {
                    // FIXME? OpenSSL weirdness with IV of zeros (for ECB keys?)
                    if (Arrays.AreEqual(iv, new byte[iv.Length]))
                    {
                        //Console.Error.Write("***** IV all 0 (length " + iv.Length + ") *****");
                    }
                    else
                    {
                        parameters = new ParametersWithIV(parameters, iv);
                    }
                }
            }
            else if (Platform.StartsWith(mechanism, "PBEwithSHA-1"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string)algorithmType[mechanism], new Sha1Digest(), keyBytes, salt, iterationCount);

                if (mechanism.Equals("PBEwithSHA-1and128bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-1and192bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-1and256bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 256, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-1and128bitRC4"))
                {
                    parameters = generator.GenerateDerivedParameters("RC4", 128);
                }
                else if (mechanism.Equals("PBEwithSHA-1and40bitRC4"))
                {
                    parameters = generator.GenerateDerivedParameters("RC4", 40);
                }
                else if (mechanism.Equals("PBEwithSHA-1and3-keyDESEDE-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DESEDE", 192, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1and2-keyDESEDE-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DESEDE", 128, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1and128bitRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 128, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1and40bitRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 40, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1andDES-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1andRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
                }
            }
            else if (Platform.StartsWith(mechanism, "PBEwithSHA-256"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string)algorithmType[mechanism], new Sha256Digest(), keyBytes, salt, iterationCount);

                if (mechanism.Equals("PBEwithSHA-256and128bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-256and192bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-256and256bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (Platform.StartsWith(mechanism, "PBEwithMD5"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string)algorithmType[mechanism], new MD5Digest(), keyBytes, salt, iterationCount);

                if (mechanism.Equals("PBEwithMD5andDES-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (mechanism.Equals("PBEwithMD5andRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
                }
                else if (mechanism.Equals("PBEwithMD5and128bitAES-CBC-OpenSSL"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (mechanism.Equals("PBEwithMD5and192bitAES-CBC-OpenSSL"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (mechanism.Equals("PBEwithMD5and256bitAES-CBC-OpenSSL"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (Platform.StartsWith(mechanism, "PBEwithMD2"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string)algorithmType[mechanism], new MD2Digest(), keyBytes, salt, iterationCount);
                if (mechanism.Equals("PBEwithMD2andDES-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (mechanism.Equals("PBEwithMD2andRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
                }
            }
            else if (Platform.StartsWith(mechanism, "PBEwithHmac"))
            {
                string  digestName = mechanism.Substring("PBEwithHmac".Length);
                IDigest digest     = DigestUtilities.GetDigest(digestName);

                PbeParametersGenerator generator = MakePbeGenerator(
                    (string)algorithmType[mechanism], digest, keyBytes, salt, iterationCount);

                int bitLen = digest.GetDigestSize() * 8;
                parameters = generator.GenerateDerivedMacParameters(bitLen);
            }

            Array.Clear(keyBytes, 0, keyBytes.Length);

            return(FixDesParity(mechanism, parameters));
        }
Ejemplo n.º 2
0
            public override void PerformTest()
            {
                char[] password = "******".ToCharArray();
                PbeParametersGenerator generator = new Pkcs5S2ParametersGenerator();

                EncryptedPrivateKeyInfo info = null;

                try
                {
                    info = EncryptedPrivateKeyInfo.GetInstance(Asn1Object.FromByteArray(sample));
                }
                catch (System.Exception e)
                {
                    Fail("failed construction - exception " + e.ToString(), e);
                }

                PbeS2Parameters  alg    = new PbeS2Parameters((Asn1Sequence)info.EncryptionAlgorithm.Parameters);
                Pbkdf2Params     func   = Pbkdf2Params.GetInstance(alg.KeyDerivationFunc.Parameters);
                EncryptionScheme scheme = alg.EncryptionScheme;

                if (func.KeyLength != null)
                {
                    keySize = func.KeyLength.IntValue * 8;
                }

                int iterationCount = func.IterationCount.IntValue;

                byte[] salt = func.GetSalt();

                generator.Init(PbeParametersGenerator.Pkcs5PasswordToBytes(password), salt, iterationCount);

                DerObjectIdentifier algOid = scheme.ObjectID;

                byte[] iv;
                if (algOid.Equals(PkcsObjectIdentifiers.RC2Cbc))
                {
                    RC2CbcParameter rc2Params = RC2CbcParameter.GetInstance(scheme.Asn1Object);
                    iv = rc2Params.GetIV();
                }
                else
                {
                    iv = ((Asn1OctetString)scheme.Asn1Object).GetOctets();
                }

                ICipherParameters param = new ParametersWithIV(
                    generator.GenerateDerivedParameters(algOid.Id, keySize), iv);

                cipher.Init(false, param);

                byte[] data     = info.GetEncryptedData();
                byte[] outBytes = new byte[cipher.GetOutputSize(data.Length)];
                int    len      = cipher.ProcessBytes(data, 0, data.Length, outBytes, 0);

                try
                {
                    len += cipher.DoFinal(outBytes, len);
                }
                catch (Exception e)
                {
                    Fail("failed DoFinal - exception " + e.ToString());
                }

                if (result.Length != len)
                {
                    Fail("failed length");
                }

                for (int i = 0; i != len; i++)
                {
                    if (outBytes[i] != result[i])
                    {
                        Fail("failed comparison");
                    }
                }
            }
Ejemplo n.º 3
0
        public static ICipherParameters GenerateCipherParameters(string algorithm, char[] password, bool wrongPkcs12Zero, Asn1Encodable pbeParameters)
        {
            string text = (string)PbeUtilities.algorithms[Platform.ToUpperInvariant(algorithm)];

            byte[] array          = null;
            byte[] salt           = null;
            int    iterationCount = 0;

            if (PbeUtilities.IsPkcs12(text))
            {
                Pkcs12PbeParams instance = Pkcs12PbeParams.GetInstance(pbeParameters);
                salt           = instance.GetIV();
                iterationCount = instance.Iterations.IntValue;
                array          = PbeParametersGenerator.Pkcs12PasswordToBytes(password, wrongPkcs12Zero);
            }
            else if (!PbeUtilities.IsPkcs5Scheme2(text))
            {
                PbeParameter instance2 = PbeParameter.GetInstance(pbeParameters);
                salt           = instance2.GetSalt();
                iterationCount = instance2.IterationCount.IntValue;
                array          = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
            }
            ICipherParameters parameters = null;

            if (PbeUtilities.IsPkcs5Scheme2(text))
            {
                PbeS2Parameters     instance3        = PbeS2Parameters.GetInstance(pbeParameters.ToAsn1Object());
                AlgorithmIdentifier encryptionScheme = instance3.EncryptionScheme;
                DerObjectIdentifier objectID         = encryptionScheme.ObjectID;
                Asn1Object          obj       = encryptionScheme.Parameters.ToAsn1Object();
                Pbkdf2Params        instance4 = Pbkdf2Params.GetInstance(instance3.KeyDerivationFunc.Parameters.ToAsn1Object());
                byte[] array2;
                if (objectID.Equals(PkcsObjectIdentifiers.RC2Cbc))
                {
                    RC2CbcParameter instance5 = RC2CbcParameter.GetInstance(obj);
                    array2 = instance5.GetIV();
                }
                else
                {
                    array2 = Asn1OctetString.GetInstance(obj).GetOctets();
                }
                salt           = instance4.GetSalt();
                iterationCount = instance4.IterationCount.IntValue;
                array          = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
                int keySize = (instance4.KeyLength != null) ? (instance4.KeyLength.IntValue * 8) : GeneratorUtilities.GetDefaultKeySize(objectID);
                PbeParametersGenerator pbeParametersGenerator = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], null, array, salt, iterationCount);
                parameters = pbeParametersGenerator.GenerateDerivedParameters(objectID.Id, keySize);
                if (array2 != null && !Arrays.AreEqual(array2, new byte[array2.Length]))
                {
                    parameters = new ParametersWithIV(parameters, array2);
                }
            }
            else if (text.StartsWith("PBEwithSHA-1"))
            {
                PbeParametersGenerator pbeParametersGenerator2 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new Sha1Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithSHA-1and128bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (text.Equals("PBEwithSHA-1and192bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (text.Equals("PBEwithSHA-1and256bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("AES", 256, 128);
                }
                else if (text.Equals("PBEwithSHA-1and128bitRC4"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC4", 128);
                }
                else if (text.Equals("PBEwithSHA-1and40bitRC4"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC4", 40);
                }
                else if (text.Equals("PBEwithSHA-1and3-keyDESEDE-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("DESEDE", 192, 64);
                }
                else if (text.Equals("PBEwithSHA-1and2-keyDESEDE-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("DESEDE", 128, 64);
                }
                else if (text.Equals("PBEwithSHA-1and128bitRC2-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 128, 64);
                }
                else if (text.Equals("PBEwithSHA-1and40bitRC2-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 40, 64);
                }
                else if (text.Equals("PBEwithSHA-1andDES-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (text.Equals("PBEwithSHA-1andRC2-CBC"))
                {
                    parameters = pbeParametersGenerator2.GenerateDerivedParameters("RC2", 64, 64);
                }
            }
            else if (text.StartsWith("PBEwithSHA-256"))
            {
                PbeParametersGenerator pbeParametersGenerator3 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new Sha256Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithSHA-256and128bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (text.Equals("PBEwithSHA-256and192bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (text.Equals("PBEwithSHA-256and256bitAES-CBC-BC"))
                {
                    parameters = pbeParametersGenerator3.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (text.StartsWith("PBEwithMD5"))
            {
                PbeParametersGenerator pbeParametersGenerator4 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new MD5Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithMD5andDES-CBC"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (text.Equals("PBEwithMD5andRC2-CBC"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("RC2", 64, 64);
                }
                else if (text.Equals("PBEwithMD5and128bitAES-CBC-OpenSSL"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (text.Equals("PBEwithMD5and192bitAES-CBC-OpenSSL"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (text.Equals("PBEwithMD5and256bitAES-CBC-OpenSSL"))
                {
                    parameters = pbeParametersGenerator4.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (text.StartsWith("PBEwithMD2"))
            {
                PbeParametersGenerator pbeParametersGenerator5 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], new MD2Digest(), array, salt, iterationCount);
                if (text.Equals("PBEwithMD2andDES-CBC"))
                {
                    parameters = pbeParametersGenerator5.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (text.Equals("PBEwithMD2andRC2-CBC"))
                {
                    parameters = pbeParametersGenerator5.GenerateDerivedParameters("RC2", 64, 64);
                }
            }
            else if (text.StartsWith("PBEwithHmac"))
            {
                string  algorithm2 = text.Substring("PBEwithHmac".Length);
                IDigest digest     = DigestUtilities.GetDigest(algorithm2);
                PbeParametersGenerator pbeParametersGenerator6 = PbeUtilities.MakePbeGenerator((string)PbeUtilities.algorithmType[text], digest, array, salt, iterationCount);
                int keySize2 = digest.GetDigestSize() * 8;
                parameters = pbeParametersGenerator6.GenerateDerivedMacParameters(keySize2);
            }
            Array.Clear(array, 0, array.Length);
            return(PbeUtilities.FixDesParity(text, parameters));
        }