Esempio n. 1
0
        /**
         * Parse a secret key from one of the GPG S expression keys.
         *
         * @return a secret key object.
         */
        public static PgpSecretKey ParseSecretKeyFromSExpr(Stream inputStream, char[] passPhrase)
        {
            SXprUtilities.SkipOpenParenthesis(inputStream);

            string type = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

            if (type.Equals("protected-private-key"))
            {
                SXprUtilities.SkipOpenParenthesis(inputStream);

                string curveName;

                string keyType = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                if (keyType.Equals("ecc"))
                {
                    SXprUtilities.SkipOpenParenthesis(inputStream);

                    string curveID = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    curveName = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

                    if (curveName.StartsWith("NIST "))
                    {
                        curveName = curveName.Substring("NIST ".Length);
                    }

                    SXprUtilities.SkipCloseParenthesis(inputStream);
                }
                else
                {
                    throw new PgpException("no curve details found");
                }

                byte[] qVal;

                SXprUtilities.SkipOpenParenthesis(inputStream);

                type = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                if (type.Equals("q"))
                {
                    qVal = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());
                }
                else
                {
                    throw new PgpException("no q value found");
                }

                PublicKeyPacket pubPacket = new PublicKeyPacket(PublicKeyAlgorithmTag.ECDsa, DateTime.UtcNow,
                                                                new ECDsaPublicBcpgKey(ECNamedCurveTable.GetOid(curveName), new BigInteger(1, qVal)));

                SXprUtilities.SkipCloseParenthesis(inputStream);

                byte[] dValue = GetDValue(inputStream, passPhrase, curveName);
                // TODO: check SHA-1 hash.

                return(new PgpSecretKey(new SecretKeyPacket(pubPacket, SymmetricKeyAlgorithmTag.Null, null, null,
                                                            new ECSecretBcpgKey(new BigInteger(1, dValue)).GetEncoded()), new PgpPublicKey(pubPacket)));
            }

            throw new PgpException("unknown key type found");
        }
Esempio n. 2
0
        private static byte[] GetDValue(Stream inputStream, byte[] rawPassPhrase, bool clearPassPhrase, string curveName)
        {
            //IL_008a: Unknown result type (might be due to invalid IL or missing references)
            //IL_0091: Expected O, but got Unknown
            SXprUtilities.SkipOpenParenthesis(inputStream);
            string text = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

            if (text.Equals("protected"))
            {
                SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                SXprUtilities.SkipOpenParenthesis(inputStream);
                S2k    s2k = SXprUtilities.ParseS2k(inputStream);
                byte[] iv  = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());
                SXprUtilities.SkipCloseParenthesis(inputStream);
                byte[]       array  = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());
                KeyParameter key    = PgpUtilities.DoMakeKeyFromPassPhrase(SymmetricKeyAlgorithmTag.Aes128, s2k, rawPassPhrase, clearPassPhrase);
                byte[]       array2 = RecoverKeyData(SymmetricKeyAlgorithmTag.Aes128, "/CBC/NoPadding", key, iv, array, 0, array.Length);
                Stream       val    = (Stream) new MemoryStream(array2, false);
                SXprUtilities.SkipOpenParenthesis(val);
                SXprUtilities.SkipOpenParenthesis(val);
                SXprUtilities.SkipOpenParenthesis(val);
                SXprUtilities.ReadString(val, val.ReadByte());
                return(SXprUtilities.ReadBytes(val, val.ReadByte()));
            }
            throw new PgpException("protected block not found");
        }
Esempio n. 3
0
        internal static PgpSecretKey DoParseSecretKeyFromSExpr(Stream inputStream, byte[] rawPassPhrase, bool clearPassPhrase)
        {
            SXprUtilities.SkipOpenParenthesis(inputStream);
            string text = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

            if (text.Equals("protected-private-key"))
            {
                SXprUtilities.SkipOpenParenthesis(inputStream);
                string text2 = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                if (text2.Equals("ecc"))
                {
                    SXprUtilities.SkipOpenParenthesis(inputStream);
                    SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    string text3 = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    if (Platform.StartsWith(text3, "NIST "))
                    {
                        text3 = text3.Substring("NIST ".get_Length());
                    }
                    SXprUtilities.SkipCloseParenthesis(inputStream);
                    SXprUtilities.SkipOpenParenthesis(inputStream);
                    text = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    if (text.Equals("q"))
                    {
                        byte[]          bytes           = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());
                        PublicKeyPacket publicKeyPacket = new PublicKeyPacket(PublicKeyAlgorithmTag.ECDsa, global::System.DateTime.get_UtcNow(), new ECDsaPublicBcpgKey(ECNamedCurveTable.GetOid(text3), new BigInteger(1, bytes)));
                        SXprUtilities.SkipCloseParenthesis(inputStream);
                        byte[] dValue = GetDValue(inputStream, rawPassPhrase, clearPassPhrase, text3);
                        return(new PgpSecretKey(new SecretKeyPacket(publicKeyPacket, SymmetricKeyAlgorithmTag.Null, null, null, new ECSecretBcpgKey(new BigInteger(1, dValue)).GetEncoded()), new PgpPublicKey(publicKeyPacket)));
                    }
                    throw new PgpException("no q value found");
                }
                throw new PgpException("no curve details found");
            }
            throw new PgpException("unknown key type found");
        }
Esempio n. 4
0
        internal static PgpSecretKey DoParseSecretKeyFromSExpr(Stream inputStream, byte[] rawPassPhrase, bool clearPassPhrase, PgpPublicKey pubKey)
        {
            SXprUtilities.SkipOpenParenthesis(inputStream);
            string text = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

            if (text.Equals("protected-private-key"))
            {
                SXprUtilities.SkipOpenParenthesis(inputStream);
                string text2 = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                if (text2.Equals("ecc"))
                {
                    SXprUtilities.SkipOpenParenthesis(inputStream);
                    SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    string curveName = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    SXprUtilities.SkipCloseParenthesis(inputStream);
                    SXprUtilities.SkipOpenParenthesis(inputStream);
                    text = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    if (text.Equals("q"))
                    {
                        SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());
                        SXprUtilities.SkipCloseParenthesis(inputStream);
                        byte[] dValue = GetDValue(inputStream, rawPassPhrase, clearPassPhrase, curveName);
                        return(new PgpSecretKey(new SecretKeyPacket(pubKey.PublicKeyPacket, SymmetricKeyAlgorithmTag.Null, null, null, new ECSecretBcpgKey(new BigInteger(1, dValue)).GetEncoded()), pubKey));
                    }
                    throw new PgpException("no q value found");
                }
                throw new PgpException("no curve details found");
            }
            throw new PgpException("unknown key type found");
        }
Esempio n. 5
0
        /**
         * Parse a secret key from one of the GPG S expression keys associating it with the passed in public key.
         *
         * @return a secret key object.
         */
        public static PgpSecretKey ParseSecretKeyFromSExpr(Stream inputStream, char[] passPhrase, PgpPublicKey pubKey)
        {
            SXprUtilities.SkipOpenParenthesis(inputStream);

            string type = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

            if (type.Equals("protected-private-key"))
            {
                SXprUtilities.SkipOpenParenthesis(inputStream);

                string curveName;

                string keyType = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                if (keyType.Equals("ecc"))
                {
                    SXprUtilities.SkipOpenParenthesis(inputStream);

                    string curveID = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                    curveName = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

                    SXprUtilities.SkipCloseParenthesis(inputStream);
                }
                else
                {
                    throw new PgpException("no curve details found");
                }

                byte[] qVal;

                SXprUtilities.SkipOpenParenthesis(inputStream);

                type = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
                if (type.Equals("q"))
                {
                    qVal = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());
                }
                else
                {
                    throw new PgpException("no q value found");
                }

                SXprUtilities.SkipCloseParenthesis(inputStream);

                byte[] dValue = GetDValue(inputStream, passPhrase, curveName);
                // TODO: check SHA-1 hash.

                return(new PgpSecretKey(new SecretKeyPacket(pubKey.PublicKeyPacket, SymmetricKeyAlgorithmTag.Null, null, null,
                                                            new ECSecretBcpgKey(new BigInteger(1, dValue)).GetEncoded()), pubKey));
            }

            throw new PgpException("unknown key type found");
        }
Esempio n. 6
0
        private static byte[] GetDValue(Stream inputStream, char[] passPhrase, string curveName)
        {
            string type;

            SXprUtilities.SkipOpenParenthesis(inputStream);

            string protection;
            S2k    s2k;

            byte[] iv;
            byte[] secKeyData;

            type = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());
            if (type.Equals("protected"))
            {
                protection = SXprUtilities.ReadString(inputStream, inputStream.ReadByte());

                SXprUtilities.SkipOpenParenthesis(inputStream);

                s2k = SXprUtilities.ParseS2k(inputStream);

                iv = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());

                SXprUtilities.SkipCloseParenthesis(inputStream);

                secKeyData = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte());
            }
            else
            {
                throw new PgpException("protected block not found");
            }

            // TODO: recognise other algorithms
            KeyParameter key = PgpUtilities.MakeKeyFromPassPhrase(SymmetricKeyAlgorithmTag.Aes128, s2k, passPhrase);

            byte[] data = RecoverKeyData(SymmetricKeyAlgorithmTag.Aes128, "/CBC/NoPadding", key, iv, secKeyData, 0, secKeyData.Length);

            //
            // parse the secret key S-expr
            //
            Stream keyIn = new MemoryStream(data, false);

            SXprUtilities.SkipOpenParenthesis(keyIn);
            SXprUtilities.SkipOpenParenthesis(keyIn);
            SXprUtilities.SkipOpenParenthesis(keyIn);
            String name = SXprUtilities.ReadString(keyIn, keyIn.ReadByte());

            return(SXprUtilities.ReadBytes(keyIn, keyIn.ReadByte()));
        }