Esempio n. 1
0
        private protected PgpKey(KeyPacket keyPacket)
        {
            this.keyPacket         = keyPacket;
            this.keyCertifications = new List <PgpCertification>();
            this.ids = new List <PgpUser>();

            this.fingerprint = CalculateFingerprint();

            if (keyPacket.Version <= 3)
            {
                var rsaParameters = RsaKey.ReadOpenPgpPublicKey(keyPacket.KeyBytes, out var _);
                var modulus       = rsaParameters.Modulus !;

                this.keyId = (long)(((ulong)modulus[modulus.Length - 8] << 56)
                                    | ((ulong)modulus[modulus.Length - 7] << 48)
                                    | ((ulong)modulus[modulus.Length - 6] << 40)
                                    | ((ulong)modulus[modulus.Length - 5] << 32)
                                    | ((ulong)modulus[modulus.Length - 4] << 24)
                                    | ((ulong)modulus[modulus.Length - 3] << 16)
                                    | ((ulong)modulus[modulus.Length - 2] << 8)
                                    | (ulong)modulus[modulus.Length - 1]);
            }
            else
            {
                this.keyId = (long)(((ulong)fingerprint[fingerprint.Length - 8] << 56)
                                    | ((ulong)fingerprint[fingerprint.Length - 7] << 48)
                                    | ((ulong)fingerprint[fingerprint.Length - 6] << 40)
                                    | ((ulong)fingerprint[fingerprint.Length - 5] << 32)
                                    | ((ulong)fingerprint[fingerprint.Length - 4] << 24)
                                    | ((ulong)fingerprint[fingerprint.Length - 3] << 16)
                                    | ((ulong)fingerprint[fingerprint.Length - 2] << 8)
                                    | (ulong)fingerprint[fingerprint.Length - 1]);
            }
        }
Esempio n. 2
0
        private protected PgpKey(IPacketReader packetReader, KeyPacket keyPacket, bool subKey)
            : this(keyPacket)
        {
            // Ignore GPG comment packets if found.
            while (packetReader.NextPacketTag() == PacketTag.Experimental2)
            {
                packetReader.ReadContainedPacket();
            }

            this.trustPacket = packetReader.NextPacketTag() == PacketTag.Trust ? (TrustPacket)packetReader.ReadContainedPacket() : null;

            while (packetReader.NextPacketTag() == PacketTag.Signature)
            {
                SignaturePacket signaturePacket      = (SignaturePacket)packetReader.ReadContainedPacket();
                TrustPacket?    signatureTrustPacket = packetReader.NextPacketTag() == PacketTag.Trust ? (TrustPacket)packetReader.ReadContainedPacket() : null;
                var             signature            = new PgpSignature(signaturePacket, signatureTrustPacket);
                this.keyCertifications.Add(new PgpCertification(signature, null, this));
            }

            if (!subKey)
            {
                while (packetReader.NextPacketTag() == PacketTag.UserId ||
                       packetReader.NextPacketTag() == PacketTag.UserAttribute)
                {
                    ids.Add(new PgpUser(packetReader, this));
                }
            }
        }
Esempio n. 3
0
 public KeyPacket4(bool valid, KeyPacket pk0, KeyPacket pk1, KeyPacket pk2, KeyPacket pk3)
 {
     this.valid = valid;
     this.pk0   = pk0;
     this.pk1   = pk1;
     this.pk2   = pk2;
     this.pk3   = pk3;
 }
Esempio n. 4
0
 public GameCrypt(KeyPacket packet)
 {
     Logger = Log.ForContext <GameCrypt>();
     packet.EncryptionKey.CopyTo(serverToClientKey, 0);
     StaticPart.CopyTo(serverToClientKey, 8);
     packet.EncryptionKey.CopyTo(clientToServerKey, 0);
     StaticPart.CopyTo(clientToServerKey, 8);
 }
Esempio n. 5
0
        private static void WriteSExprPublicKey(SXprWriter writer, KeyPacket pubPacket, string curveName, string?protectedAt)
        {
            writer.StartList();
            switch (pubPacket.Algorithm)
            {
            case PgpPublicKeyAlgorithm.ECDsa:
            case PgpPublicKeyAlgorithm.EdDsa:
                writer.WriteString("ecc");
                writer.StartList();
                writer.WriteString("curve");
                writer.WriteString(curveName);
                writer.EndList();
                if (pubPacket.Algorithm == PgpPublicKeyAlgorithm.EdDsa)
                {
                    writer.StartList();
                    writer.WriteString("flags");
                    writer.WriteString("eddsa");
                    writer.EndList();
                }
                writer.StartList();
                writer.WriteString("q");
                var keyBytes = pubPacket.KeyBytes.AsSpan(0, pubPacket.PublicKeyLength);
                keyBytes = keyBytes.Slice(keyBytes[0] + 1 + 2);     // Skip OID and encoded point length
                writer.WriteBytes(keyBytes.ToArray());
                writer.EndList();
                break;

            /*case PgpPublicKeyAlgorithm.RsaEncrypt:
             * case PgpPublicKeyAlgorithm.RsaSign:
             * case PgpPublicKeyAlgorithm.RsaGeneral:
             *  RsaPublicBcpgKey rsaK = (RsaPublicBcpgKey)pubPacket.Key;
             *  writer.WriteString("rsa");
             *  writer.StartList();
             *  writer.WriteString("n");
             *  writer.WriteBytes(rsaK.Modulus.Value);
             *  writer.EndList();
             *  writer.StartList();
             *  writer.WriteString("e");
             *  writer.WriteBytes(rsaK.PublicExponent.Value);
             *  writer.EndList();
             *  break;*/

            // TODO: DSA, etc.
            default:
                throw new PgpException("unsupported algorithm in S expression");
            }

            if (protectedAt != null)
            {
                writer.StartList();
                writer.WriteString("protected-at");
                writer.WriteString(protectedAt);
                writer.EndList();
            }
            writer.EndList();
        }
Esempio n. 6
0
 public KeyPacket6(bool valid, KeyPacket pk0, KeyPacket pk1, KeyPacket pk2, KeyPacket pk3, KeyPacket pk4, KeyPacket pk5)
 {
     this.valid = valid;
     this.pk0   = pk0;
     this.pk1   = pk1;
     this.pk2   = pk2;
     this.pk3   = pk3;
     this.pk4   = pk4;
     this.pk5   = pk5;
 }
Esempio n. 7
0
            protected override void OnKey(ref KeyPacket packet)
            {
                var key        = packet.Key;
                var inputState = packet.InputState;
                var str        = $"\r\n{DateTime.Now} :" +
                                 $" {key} => {inputState.IsKeyUpTransition}; " +
                                 $"{inputState.RepeatCount}; " +
                                 $"{inputState.ScanCode}; " +
                                 $"{inputState.IsContextual}; " +
                                 $"{inputState.IsExtendedKey}" + "\r\n" +
                                 $"No. of text display changes: {this.m_timesExecuted}" + "\0";

                this.m_textBox.SetText(str);
                base.OnKey(ref packet);
            }
Esempio n. 8
0
        public void ProcessKey(ref KeyPacket packet)
        {
            VirtualKey key = packet.Key;

            if (packet.IsKeyDown)
            {
                if (!packet.InputState.IsPreviousKeyStatePressed)
                {
                    keys.Add(key);
                    RaiseKeyDown(key);
                }
            }
            else
            {
                keys.Remove(key);
                RaiseKeyUp(key);
            }
        }
Esempio n. 9
0
        /// <summary>Copy constructor.</summary>
        /// <param name="pubKey">The public key to copy.</param>
        protected PgpKey(PgpKey pubKey)
        {
            this.keyPacket = pubKey.keyPacket;

            this.keyCertifications = new List <PgpCertification>(pubKey.keyCertifications.Count);
            foreach (var keySig in pubKey.keyCertifications)
            {
                this.keyCertifications.Add(new PgpCertification(keySig.Signature, null, this));
            }

            this.ids = new List <PgpUser>(pubKey.ids.Count);
            foreach (var id in pubKey.ids)
            {
                this.ids.Add(new PgpUser(id, this));
            }

            this.fingerprint = pubKey.fingerprint;
            this.keyId       = pubKey.keyId;
        }
Esempio n. 10
0
 protected override void OnKey(ref KeyPacket packet)
 {
     keyboard.ProcessKey(ref packet);
 }
Esempio n. 11
0
 public KeyPacket2(bool valid, KeyPacket pk0, KeyPacket pk1)
 {
     this.valid = valid;
     this.pk0   = pk0;
     this.pk1   = pk1;
 }
Esempio n. 12
0
        /// <summary>
        /// Handles incoming packet.
        /// </summary>
        /// <param name="packet">Incoming packet.</param>
        protected override void Handle(Packet packet)
        {
            Logger.WriteLine(Source.OuterNetwork, "Received: {0}", packet.ToString());

            //Logger.WriteLine(Source.Debug, "packet.FirstOpcode = {0}", packet.FirstOpcode);

            switch (State)
            {
            case UserConnectionState.Disconnected:
                throw new InvalidOperationException();

            case UserConnectionState.Connected:
            {
                switch (packet.FirstOpcode)
                {
                case 0x0e:             // protocol
                {
                    int revision = packet.ReadInt();

                    //Logger.WriteLine(Source.Debug, "Protocol Revision: {0}", revision);

                    if (revision == -2)
                    {
                        CloseConnection();
                        // remove from active connections list
                    }

                    if (revision < Settings.Default.WorldMinProtoRev || revision > Settings.Default.WorldMaxProtoRev)
                    {
                        CloseConnection();
                        // remove from active connections, log
                    }

                    byte[] keyIN = BlowFishKeygen.GetNext();
                    //byte[] keyOUT = BlowFishKeygen.GetNext();

                    m_Crypt = new GameCrypt();
                    m_Crypt.setKey(keyIN);

                    SendNoCrypt(KeyPacket.Create(keyIN));

                    BeginReceive();

                    break;
                }

                case 0x2b:             // login
                {
                    new AuthLogin(packet).RunImpl();
                    break;
                }

                default:
                {
                    Logger.WriteLine(Source.OuterNetwork, "Unknown packet on state {0}: Opcode = {1}", State, packet.FirstOpcode);
                    break;
                }
                }

                return;
            }

            case UserConnectionState.Authed:
            {
                switch (packet.FirstOpcode)
                {
                default:
                {
                    Logger.WriteLine(Source.OuterNetwork, "Unknown packet on state {0}: Opcode = {1}", State, packet.FirstOpcode);
                    break;
                }
                }
                return;
            }
            }
        }
Esempio n. 13
0
        private static byte[] GetDValue(SXprReader reader, KeyPacket publicKey, byte[] rawPassPhrase, string curveName)
        {
            string type;

            reader.SkipOpenParenthesis();

            string protection;
            string?protectedAt = null;
            S2k    s2k;

            byte[] iv;
            byte[] secKeyData;

            type = reader.ReadString();
            if (type.Equals("protected", StringComparison.Ordinal))
            {
                protection = reader.ReadString();

                reader.SkipOpenParenthesis();

                s2k = reader.ParseS2k();

                iv = reader.ReadBytes();

                reader.SkipCloseParenthesis();

                secKeyData = reader.ReadBytes();

                reader.SkipCloseParenthesis();

                reader.SkipOpenParenthesis();

                if (reader.ReadString().Equals("protected-at", StringComparison.Ordinal))
                {
                    protectedAt = reader.ReadString();
                }
            }
            else
            {
                throw new PgpException("protected block not found");
            }

            byte[] data;

            switch (protection)
            {
            case "openpgp-s2k3-sha1-aes256-cbc":
            case "openpgp-s2k3-sha1-aes-cbc":
                PgpSymmetricKeyAlgorithm symmAlg =
                    protection.Equals("openpgp-s2k3-sha1-aes256-cbc", StringComparison.Ordinal) ?
                    PgpSymmetricKeyAlgorithm.Aes256 :
                    PgpSymmetricKeyAlgorithm.Aes128;
                using (var c = PgpUtilities.GetSymmetricAlgorithm(symmAlg))
                {
                    var keyBytes = new byte[c.KeySize / 8];
                    S2kBasedEncryption.MakeKey(rawPassPhrase, PgpHashAlgorithm.Sha1, s2k.GetIV(), s2k.IterationCount, keyBytes);
                    c.Key  = keyBytes;
                    c.IV   = iv;
                    c.Mode = CipherMode.CBC;
                    using var decryptor = new ZeroPaddedCryptoTransform(c.CreateDecryptor());
                    data = decryptor.TransformFinalBlock(secKeyData, 0, secKeyData.Length);
                    // TODO: check SHA-1 hash.
                }
                break;

            case "openpgp-s2k3-ocb-aes":
            {
                MemoryStream aad = new MemoryStream();
                WriteSExprPublicKey(new SXprWriter(aad), publicKey, curveName, protectedAt);
                var keyBytes = new byte[16];
                S2kBasedEncryption.MakeKey(rawPassPhrase, PgpHashAlgorithm.Sha1, s2k.GetIV(), s2k.IterationCount, keyBytes);
                using var aesOcb = new AesOcb(keyBytes);
                data             = new byte[secKeyData.Length - 16];
                aesOcb.Decrypt(iv, secKeyData.AsSpan(0, secKeyData.Length - 16), secKeyData.AsSpan(secKeyData.Length - 16), data, aad.ToArray());
            }
            break;

            case "openpgp-native":
            default:
                throw new PgpException(protection + " key format is not supported yet");
            }

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

            reader = new SXprReader(keyIn);
            reader.SkipOpenParenthesis();
            reader.SkipOpenParenthesis();
            reader.SkipOpenParenthesis();
            String name = reader.ReadString();

            return(reader.ReadBytes());
        }