示例#1
0
 /// <summary></summary>
 private void Initialise(nSignatureType eSignatureType)
 {
     _abFingerprint       = _abHashFingerprint = _abSignature = null;
     _iHashedLength       = _iSignatureBits = 0;
     _eVerified           = nVerified.Pending;
     _eSignatureType      = eSignatureType;
     _eTranslatedKeyFlags = nTranslatedKeyFlags.None;
     _ePublicKeyAlgorithm = nPublicKeyAlgorithm.RsaEncryptOrSign;
     _eHashAlgorithm      = nHashAlgorithm.Sha512;
     _PrivateKeyPacket    = null;
     _PublicKeyPacket     = null;
     _UserIdPacket        = null;
     _Cryptography        = null;
     _ltSubpackets        = new List <PgpSignatureSubpacket>();
 }
示例#2
0
        /// <summary></summary>
        public void Parse(PgpPublicKey MasterKey, List <PgpToken> ltTokens = null)
        {
            bool isThisMasterKey = true;
            byte bVersion;

            byte[]                abMasterFingerprint;
            int                   i, iSubpacketPointer, iSignatureBytes, iUnhashedLength;
            PgpSignature          FoundSubkey;
            PgpSignatureSubpacket DecodedPgpSubpacket, RawPgpSubpacket;

            if ((_abRawBytes == null) || (_abRawBytes.Length < _iHeaderLength + ciSignatureHeaderLength) || (_ePacketTag != nPacketTag.Signature))
            {
                _eStatus = nStatus.ParseError;
            }
            else if (_eStatus == nStatus.OK)
            {
                bVersion = _abRawBytes[_iHeaderLength];

                if (bVersion == cbAllowedVersion)
                {
                    _eSignatureType      = (nSignatureType)_abRawBytes[_iHeaderLength + 1];
                    _ePublicKeyAlgorithm = (nPublicKeyAlgorithm)_abRawBytes[_iHeaderLength + 2];
                    _eHashAlgorithm      = (nHashAlgorithm)_abRawBytes[_iHeaderLength + 3];
                    _iHashedLength       = (_abRawBytes[_iHeaderLength + 4] << 8) | _abRawBytes[_iHeaderLength + 5];

                    if (((_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaEncryptOrSign) || (_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaSignOnly)) &&
                        (_eHashAlgorithm == nHashAlgorithm.Sha1) || (_eHashAlgorithm == nHashAlgorithm.Sha256) || (_eHashAlgorithm == nHashAlgorithm.Sha384) || (_eHashAlgorithm == nHashAlgorithm.Sha512))
                    {
                        iSubpacketPointer = _iHeaderLength + ciSignatureHeaderLength;
                        while ((_eStatus == nStatus.OK) && (iSubpacketPointer < _iHeaderLength + ciSignatureHeaderLength + _iHashedLength))
                        {
                            RawPgpSubpacket = new PgpSignatureSubpacket(_abRawBytes, iSubpacketPointer);
                            if (RawPgpSubpacket.eStatus == nStatus.OK)
                            {
                                switch (RawPgpSubpacket.eType)
                                {
                                case PgpSignatureSubpacket.nSubpacketType.CreationTime: DecodedPgpSubpacket = new PgpCreationTime(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.KeyExpireTime: DecodedPgpSubpacket = new PgpKeyExpireTime(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.PreferredSymmetricAlgorithms: DecodedPgpSubpacket = new PgpPreferredSymmetricAlgorithms(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.IssuerKeyId: DecodedPgpSubpacket = new PgpIssuerKeyId(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.PreferredHashAlgorithms: DecodedPgpSubpacket = new PgpPreferredHashAlgorithms(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.PreferredCompressionAlgorithms: DecodedPgpSubpacket = new PgpPreferredCompressionAlgorithms(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.KeyServerPreferences: DecodedPgpSubpacket = new PgpKeyServerPreferences(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.PrimaryUserId: DecodedPgpSubpacket = new PgpPrimaryUserId(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.KeyFlags: DecodedPgpSubpacket = new PgpKeyFlags(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.RevocationReason: DecodedPgpSubpacket = new PgpRevocationReason(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.Features: DecodedPgpSubpacket = new PgpFeatures(RawPgpSubpacket); break;

                                case PgpSignatureSubpacket.nSubpacketType.MasterKeyFingerprint: DecodedPgpSubpacket = new PgpMasterKeyFingerprint(RawPgpSubpacket); break;

                                default: DecodedPgpSubpacket = null; Console.WriteLine("not implemented: eType=" + RawPgpSubpacket.eType.ToString()); break;
                                }

                                if ((DecodedPgpSubpacket == null) || (DecodedPgpSubpacket.eStatus != nStatus.OK))
                                {
                                    _eStatus = nStatus.ParseError;
                                }
                                else
                                {
                                    // Console.WriteLine("found eType=" + RawPgpSubpacket.eType.ToString());
                                    _ltSubpackets.Add(DecodedPgpSubpacket);
                                    iSubpacketPointer += (RawPgpSubpacket.iHeaderLength + RawPgpSubpacket.iDataLength);
                                }
                            }
                            else
                            {
                                _eStatus = nStatus.ParseError;
                            }
                        }

                        iUnhashedLength    = (_abRawBytes[iSubpacketPointer] << 8) | _abRawBytes[iSubpacketPointer + 1];
                        iSubpacketPointer += 2;

                        while ((_eStatus == nStatus.OK) && (iSubpacketPointer < _iHashedLength + iUnhashedLength + 8))
                        {
                            RawPgpSubpacket = new PgpSignatureSubpacket(_abRawBytes, iSubpacketPointer);
                            if (RawPgpSubpacket.eStatus == nStatus.OK)
                            {
                                switch (RawPgpSubpacket.eType)
                                {
                                case PgpSignatureSubpacket.nSubpacketType.IssuerKeyId: DecodedPgpSubpacket = new PgpIssuerKeyId(RawPgpSubpacket); break;

                                default: DecodedPgpSubpacket = null; Console.WriteLine("not implemented: eType=" + RawPgpSubpacket.eType.ToString()); break;
                                }

                                if ((DecodedPgpSubpacket == null) || (DecodedPgpSubpacket.eStatus != nStatus.OK))
                                {
                                    _eStatus = nStatus.ParseError;
                                }
                                else
                                {
                                    _ltSubpackets.Add(DecodedPgpSubpacket);
                                    iSubpacketPointer += (RawPgpSubpacket.iHeaderLength + RawPgpSubpacket.iDataLength);
                                }
                            }
                            else
                            {
                                _eStatus = nStatus.ParseError;
                            }
                        }

                        _abFingerprint       = ComputeFingerprint();
                        _eTranslatedKeyFlags = TranslateKeyFlags();

                        if (_abRawBytes.Length > iSubpacketPointer + 2)
                        {
                            _abHashFingerprint = CopyFromRawBytes(iSubpacketPointer, 2);
                            _iSignatureBits    = _abRawBytes[iSubpacketPointer + 2] << 8 | _abRawBytes[iSubpacketPointer + 3];
                            iSignatureBytes    = (_iSignatureBits + 7) >> 3;
                            iSubpacketPointer += 4;

                            if (_abRawBytes.Length == iSubpacketPointer + iSignatureBytes)
                            {
                                _abSignature = CopyFromRawBytes(iSubpacketPointer, iSignatureBytes);

                                if (_PublicKeyPacket != null)
                                {
                                    _PublicKeyPacket.Signature = this;
                                }

                                abMasterFingerprint = abMasterKeyFingerprint;   // do the underlying checks only once

                                if ((abMasterFingerprint != null) && (abMasterFingerprint.Length == ciSha1FingerprintLength) && (MasterKey.Signature != null) &&
                                    (MasterKey.Signature.abFingerprint != null) && (MasterKey.Signature.abFingerprint.Length == ciSha1FingerprintLength))
                                {
                                    for (i = 0; i < ciSha1FingerprintLength; i++)
                                    {
                                        isThisMasterKey = isThisMasterKey && (abMasterFingerprint[i] == MasterKey.Signature.abFingerprint[i]);
                                    }

                                    if (!isThisMasterKey)
                                    {
                                        MasterKey = null;

                                        if (ltTokens != null)
                                        {
                                            foreach (PgpToken Token in ltTokens)
                                            {
                                                if (MasterKey == null)
                                                {
                                                    FoundSubkey = Token.GetSubkey(abMasterFingerprint);

                                                    if (FoundSubkey != null)
                                                    {
                                                        MasterKey = FoundSubkey.PublicKeyPacket;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                if (MasterKey == null)
                                {
                                    _eVerified = nVerified.KeyNotFound;
                                }
                                else if (Verify(MasterKey))
                                {
                                    _eVerified = nVerified.True;
                                }
                                else
                                {
                                    _eVerified = nVerified.False;
                                }
                            }
                            else
                            {
                                _eStatus = nStatus.ParseError;
                            }
                        }
                        else
                        {
                            _eStatus = nStatus.ParseError;
                        }
                    }
                    else
                    {
                        _eStatus = nStatus.AlgorithmNotSupported;
                    }
                }
                else
                {
                    _eStatus = nStatus.VersionNotSupported;
                }
            }
            // else keep the value of _eStatus from PgpPacketBase
        }