/// <summary>
 /// Creates a new OnePassSignaturePacket with 
 /// the parameters in pSource
 /// </summary>
 /// <param name="pSource">Packet from which the
 /// parameters are derived</param>
 public OnePassSignaturePacket(Packet pSource)
 {
     lLength = pSource.Length;
     bBody = pSource.Body;
     ctContent = pSource.Content;
     pfFormat = pSource.Format;
     bHeader = pSource.Header;
     this.bIsUpdated = false;
 }
 /// <summary>
 /// Creates a new Compressed Data Packet with 
 /// the parameters in pSource
 /// </summary>
 /// <param name="pSource">Packet from which the
 /// parameters are derived</param>
 public CompressedDataPacket(Packet pSource)
 {
     lLength = pSource.Length;
     bBody = pSource.Body;
     ctContent = pSource.Content;
     pfFormat = pSource.Format;
     bHeader = pSource.Header;
     this.bIsUpdated = false;
 }
 /* This file is intentionally empty.
  * The only proptery that a symmetrically
  * encrypted data packet contains is the
  * encrypted data. It is stored in the
  * Body property of this class
  */
 /// <summary>
 /// Creates a new SymmetricallyEncryptedDataPacket with 
 /// the parameters in pSource
 /// </summary>
 /// <param name="pSource">Packet from which the
 /// parameters are derived</param>
 public SymmetricallyEncryptedDataPacket(Packet pSource)
 {
     lLength = pSource.Length;
     bBody = pSource.Body;
     ctContent = pSource.Content;
     pfFormat = pSource.Format;
     bHeader = pSource.Header;
     this.bIsUpdated = false;
 }
 /// <summary>
 /// Creates a new AsymSessionKeyPacket with the parameters
 /// in pSource
 /// </summary>
 /// <param name="pSource">Packet from which the
 /// parameters are derived</param>
 /// <remarks>No remarks</remarks>
 public AsymSessionKeyPacket(Packet pSource)
 {
     lLength = pSource.Length;
     bBody = pSource.Body;
     ctContent = pSource.Content;
     pfFormat = pSource.Format;
     bHeader = pSource.Header;
     this.bIsUpdated = false;
 }
 /// <summary>
 /// Creates a new SecretKeyPacket with the parameters
 /// in pSource
 /// </summary>
 /// <param name="pSource">Packet from which the
 /// parameters are derived</param>
 public SecretKeyPacket(Packet pSource)
 {
     lLength = pSource.Length;
     bBody = pSource.Body;
     ctContent = pSource.Content;
     pfFormat = pSource.Format;
     bHeader = pSource.Header;
     this.S2KSpecifier = new String2KeySpecifier();
     this.bIsUpdated = false;
 }
 /// <summary>
 /// Creates a new SignaturePacket with the parameters
 /// in pSource
 /// </summary>
 /// <param name="pSource">Packet from which the
 /// parameters are derived</param>
 /// <remarks>No remarks</remarks>
 public SignaturePacket(Packet pSource)
 {
     lLength = pSource.Length;
     bBody = pSource.Body;
     ctContent = pSource.Content;
     pfFormat = pSource.Format;
     bHeader = pSource.Header;
     bSignatureData = new byte[0];
     ssSignatureStatus = SignatureStatusTypes.Not_Verified;
     this.bIsUpdated = false;
 }
 /// <summary>
 /// Creates a new SymSessionKeyPacket with 
 /// the parameters in pSource
 /// </summary>
 /// <param name="pSource">Packet from which the
 /// parameters are derived</param>
 public SymSessionKeyPacket(Packet pSource)
 {
     lLength = pSource.Length;
     bBody = pSource.Body;
     ctContent = pSource.Content;
     pfFormat = pSource.Format;
     bHeader = pSource.Header;
     EncryptedSessionKey = new byte[0];
     s2kSpecifier = new String2KeySpecifier();
     this.bIsUpdated = false;
 }
Exemple #8
0
        /// <summary>
        /// Parses a single packet out of the given binary
        /// data. Even if there are more than one packets in the byte
        /// array, only the first packet is returned.
        /// </summary>
        /// <param name="bBinaryData">A byte array containing a set
        /// of OpenPGP packets</param>
        /// <returns>Returns an single OpenPGP packets</returns>
        /// <remarks>No remarks</remarks>
        public virtual Packet ParsePacket(byte[] bBinaryData)
        {
            Packet pReturnPacket = new Packet();

            if ((bBinaryData[0] & 0xC0) == 0xC0) {
                pfFormat = PacketFormats.New;
            } else if ((bBinaryData[0] & 0xC0) == 0x80) {
                pfFormat = PacketFormats.Old;
            } else {
                throw(new ArgumentException("This is not a valid OpenPGP Packet"));
            }

            if (pfFormat == PacketFormats.New) {
                int iBinaryDataPos = 1;
                ctContent = (ContentTypes)(bBinaryData[0] & 0x3F);
                lLength = bBinaryData[1];
                bBody = new byte[0];
                int iHeaderLength = 1;
                //partial body lengths
                while ((lLength > 223) && (lLength < 255)) {
                    iBinaryDataPos += 1;
                    iHeaderLength++;
                    int lPartialBody = 1 << ((int)(lLength & 0x1F));
                    int lOldLength = 0;
                    if (bBody.Length > 0) {
                        byte[] bOldBody = new byte[bBody.Length];
                        bBody.CopyTo(bOldBody, 0);
                        bBody = new byte[bOldBody.Length + lPartialBody];
                        bOldBody.CopyTo(bBody, 0);
                        lOldLength = bBody.Length;
                    } else {
                        bBody = new byte[lPartialBody];
                    }
                    Array.Copy(bBinaryData, iBinaryDataPos, bBody, bBody.Length - lPartialBody, lPartialBody);
                    lLength = bBinaryData[iBinaryDataPos + lPartialBody];
                    iBinaryDataPos += lPartialBody;
                } //partial bodies must end with a normal header!
                if (lLength < 192) {
                    iHeaderLength++;
                    bHeader = new byte[iHeaderLength];
                    if (bBody.Length == 0) {
                        Array.Copy(bBinaryData, 0, bHeader, 0, 2);
                        iBinaryDataPos = 1;
                    }
                    byte[] bOldBody = new byte[bBody.Length];
                    bBody.CopyTo(bOldBody, 0);
                    bBody = new byte[bOldBody.Length + lLength];
                    bOldBody.CopyTo(bBody, 0);
                    Array.Copy(bBinaryData, iBinaryDataPos + 1, bBody, bBody.Length - (int)lLength, (int)lLength);
                } else if ((lLength > 191) && (lLength < 224)) {
                    iHeaderLength += 2;
                    bHeader = new byte[iHeaderLength];
                    if (bBody.Length == 0) {
                        Array.Copy(bBinaryData, 0, bHeader, 0, 3);
                        iBinaryDataPos = 1;
                    }
                    lLength = ((bBinaryData[iBinaryDataPos++] - 192) << 8) + bBinaryData[iBinaryDataPos++] + 192;
                    byte[] bOldBody = new byte[bBody.Length];
                    bBody.CopyTo(bOldBody, 0);
                    bBody = new byte[bOldBody.Length + lLength];
                    bOldBody.CopyTo(bBody, 0);
                    Array.Copy(bBinaryData, iBinaryDataPos, bBody, bBody.Length - (int)lLength, (int)lLength);
                } else if (lLength == 255) {
                    iHeaderLength += 5;
                    bHeader = new byte[iHeaderLength];
                    if (bBody.Length == 0) {
                        Array.Copy(bBinaryData, 0, bHeader, 0, 6);
                        iBinaryDataPos = 1;
                    }
                    lLength = (bBinaryData[iBinaryDataPos++] << 24) ^ (bBinaryData[iBinaryDataPos++] << 16) ^
                              (bBinaryData[iBinaryDataPos++] << 8) ^ bBinaryData[iBinaryDataPos++];
                    byte[] bOldBody = new byte[bBody.Length];
                    bBody.CopyTo(bOldBody, 0);
                    bBody = new byte[bOldBody.Length + lLength];
                    bOldBody.CopyTo(bBody, 0);
                    Array.Copy(bBinaryData, iBinaryDataPos, bBody, bBody.Length - (int)lLength, (int)lLength);
                }

            } else {
                ctContent = (ContentTypes)((bBinaryData[0] & 0x3C) >> 2);
                switch (bBinaryData[0] & 0x03) {
                    case 0:
                        lLength = bBinaryData[1];
                        bHeader = new byte[2];
                        break;
                    case 1:
                        lLength = (bBinaryData[1] << 8) ^ (bBinaryData[2]);
                        bHeader = new byte[3];
                        break;
                    case 2:
                        lLength = (bBinaryData[1] << 16) ^ (bBinaryData[2] << 8) ^
                                  (bBinaryData[3]);
                        bHeader = new byte[4];
                        break;
                    case 3:
                        throw new System.NotSupportedException("Packets of indetermined length are not supported due to security considerations!");
                    default:
                        throw new System.ApplicationException("This is not a valid Packet!");
                }
                bBody = new byte[lLength];
                Array.Copy(bBinaryData, 0, bHeader, 0, bHeader.Length);
                Array.Copy(bBinaryData, bHeader.Length, bBody, 0, (int)lLength);
            }

            this.bIsUpdated = false;
            switch (ctContent) {
                case ContentTypes.AsymSessionKey:
                    pReturnPacket = new AsymSessionKeyPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.Compressed:
                    pReturnPacket = new CompressedDataPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.LiteralData:
                    pReturnPacket = new LiteralDataPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.Marker:
                    pReturnPacket = new Packet(this);
                    //We can savly ignore Marker packets!
                    //MessageBox.Show("This is a marker packet. It is not yet supported.");
                    break;
                case ContentTypes.OnePassSignature:
                    pReturnPacket = new OnePassSignaturePacket(this);
                    //System.Windows.Forms.MessageBox.Show("This is a One Pass Signature Packet. It is not yet supported");
                    break;
                //Content is Public Key Packet
                case ContentTypes.PublicKey:
                    pReturnPacket = new PublicKeyPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                //Content is Public Subkey Packet. Same format as Public Key Packet
                case ContentTypes.PublicSubkey:
                    pReturnPacket = new PublicKeyPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.SecretKey:
                    pReturnPacket = new SecretKeyPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.SecretSubkey:
                    pReturnPacket = new SecretKeyPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.Signature:
                    pReturnPacket = new SignaturePacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.SymEncrypted:
                    pReturnPacket = new SymmetricallyEncryptedDataPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.SymSessionKey:
                    pReturnPacket = new SymSessionKeyPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                case ContentTypes.Trust:
                    pReturnPacket = new Packet(this);
                    //throw new Exception("This is a Trust Packet. It is not yet supported");
                    break;
                case ContentTypes.UserID:
                    pReturnPacket = new UserIDPacket(this);
                    pReturnPacket = pReturnPacket.ParsePacket(bBody);
                    break;
                default:
                    pReturnPacket = new Packet(this);
                    //throw new Exception("Sorry, but this is a packet I don't know about!");
                    break;
            }

            pReturnPacket.bIsUpdated = false;
            return pReturnPacket;
        }
Exemple #9
0
        /// <summary>
        /// Static function that makes parsing packets easy. Just give
        /// a byte representation of a set of OpenPGP packets as
        /// parameter, and the function returns an array of packets
        /// that are the parsed OpenPGP packets.
        /// </summary>
        /// <param name="bBinaryData">A byte array containing a set
        /// of OpenPGP packets</param>
        /// <returns>Returns an array of packets</returns>
        /// <remarks>No remarks</remarks>
        public static Packet[] ParsePackets(byte[] bBinaryData)
        {
            ArrayList alPackets	= new ArrayList(100);
            byte[] bTmpData = new byte[bBinaryData.Length];
            Array.Copy(bBinaryData, bTmpData, bBinaryData.Length);

            long iCurrentIndex = 0;

            while (iCurrentIndex < bBinaryData.Length) {
                Packet pTmpPacket = new Packet();
                Packet pCurrentPacket = pTmpPacket.ParsePacket(bTmpData);

                if (pCurrentPacket != null) {
                    iCurrentIndex += pCurrentPacket.Length;
                    if (pCurrentPacket.Content != ContentTypes.Marker) {
                        alPackets.Add(pCurrentPacket);
                    }

                    bTmpData = new byte[bTmpData.Length - pCurrentPacket.Length];
                    Array.Copy(bBinaryData, (int)iCurrentIndex, bTmpData, 0, bBinaryData.Length - (int)iCurrentIndex);
                }
                //MessageBox.Show("Parsed a " + pCurrentPacket.Content.ToString() + "Packet. Current Index: " + iCurrentIndex + "/" + bBinaryData.Length);
            }

            Packet[] pReturnPackets = new Packet[alPackets.Count];
            IEnumerator iePacketEnum = alPackets.GetEnumerator();
            int iCount = 0;
            while (iePacketEnum.MoveNext()) {
                if (iePacketEnum.Current is Packet) {
                    pReturnPackets[iCount++] = (Packet)iePacketEnum.Current;
                }
            }

            return pReturnPackets;
        }