Exemple #1
0
        private string FindPrimaryUserID()
        {
            string      strReturn      = "";
            int         nUserIDCounter = 0;
            IEnumerator ieCertificates = this.alCertifications.GetEnumerator();

            while (ieCertificates.MoveNext())
            {
                if (!(ieCertificates.Current is CertifiedUserID))
                {
                    continue;
                }

                nUserIDCounter++;
                CertifiedUserID cuiID        = (CertifiedUserID)ieCertificates.Current;
                IEnumerator     ieSignatures = cuiID.Certificates.GetEnumerator();
                while (ieSignatures.MoveNext())
                {
                    if (!(ieSignatures.Current is SignaturePacket))
                    {
                        continue;
                    }

                    SignaturePacket spCertificate = (SignaturePacket)ieSignatures.Current;

                    // look only at selfsignatures
                    if (spCertificate.KeyID != this.PrimaryKey.KeyID)
                    {
                        continue;
                    }

                    if (nUserIDCounter == 1)
                    {
                        strReturn = cuiID.UserID.UserID;
                    }
                    for (int i = 0; i < spCertificate.UnhashedSubPackets.Length; i++)
                    {
                        if ((spCertificate.UnhashedSubPackets[i].Type == SignatureSubPacketTypes.PrimaryUserID) && (spCertificate.UnhashedSubPackets[i].PrimaryUserID))
                        {
                            strReturn = cuiID.UserID.UserID;
                        }
                    }
                    for (int i = 0; i < spCertificate.HashedSubPackets.Length; i++)
                    {
                        if ((spCertificate.HashedSubPackets[i].Type == SignatureSubPacketTypes.PrimaryUserID) && (spCertificate.HashedSubPackets[i].PrimaryUserID))
                        {
                            strReturn = cuiID.UserID.UserID;
                        }
                    }
                }
            }

            return(strReturn);
        }
 public override bool Equals(object o)
 {
     if (o is CertifiedUserID)
     {
         CertifiedUserID cuiComp = (CertifiedUserID)o;
         if (cuiComp.UserID.UserID == this.UserID.UserID)
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #3
0
        private DateTime FindExpirationDate()
        {
            IEnumerator ieCertificates = this.alCertifications.GetEnumerator();

            while (ieCertificates.MoveNext())
            {
                if (!(ieCertificates.Current is CertifiedUserID))
                {
                    continue;
                }

                CertifiedUserID cuiID        = (CertifiedUserID)ieCertificates.Current;
                IEnumerator     ieSignatures = cuiID.Certificates.GetEnumerator();
                while (ieSignatures.MoveNext())
                {
                    if (!(ieSignatures.Current is SignaturePacket))
                    {
                        continue;
                    }

                    SignaturePacket spCertificate = (SignaturePacket)ieSignatures.Current;

                    // look only at selfsignatures
                    if (spCertificate.KeyID != this.PrimaryKey.KeyID)
                    {
                        continue;
                    }

                    for (int i = 0; i < spCertificate.HashedSubPackets.Length; i++)
                    {
                        if (spCertificate.HashedSubPackets[i].Type == SignatureSubPacketTypes.KeyExpirationTime)
                        {
                            return(spCertificate.HashedSubPackets[i].KeyExpirationTime);
                        }
                    }
                }
            }

            throw new Exception("never");
        }
Exemple #4
0
        public SymAlgorithms[] FindPreferedAlgorithms()
        {
            IEnumerator ieCertificates = this.alCertifications.GetEnumerator();

            while (ieCertificates.MoveNext())
            {
                if (!(ieCertificates.Current is CertifiedUserID))
                {
                    continue;
                }

                CertifiedUserID cuiID        = (CertifiedUserID)ieCertificates.Current;
                IEnumerator     ieSignatures = cuiID.Certificates.GetEnumerator();
                while (ieSignatures.MoveNext())
                {
                    if (!(ieSignatures.Current is SignaturePacket))
                    {
                        continue;
                    }

                    SignaturePacket spCertificate = (SignaturePacket)ieSignatures.Current;

                    // look only at selfsignatures
                    if (spCertificate.KeyID != this.PrimaryKey.KeyID)
                    {
                        continue;
                    }

                    try {
                        SymAlgorithms[] saReturn = spCertificate.FindPreferedSymAlgorithms();
                        return(saReturn);
                    } catch (Exception) {}
                }
            }

            throw new Exception("none found!");
        }
Exemple #5
0
        /// <summary>
        /// Returns a string representation of the transportable public key.
        /// </summary>
        /// <remarks>No remarks</remarks>
        /// <returns>Returns a string representation of the transportable
        /// public key.</returns>
        public override string ToString()
        {
            string strReturn = "";

            strReturn += "Abstract:\r\n";
            strReturn += "Number of Certificates: " + this.Certifications.Count + "\r\n";


            strReturn += "\r\n\r\n\r\nPublic Key Packet: \r\n";
            strReturn += this.PrimaryKey.ToString() + "\r\n\r\n";

            IEnumerator ieLoop = this.Certifications.GetEnumerator();

            while (ieLoop.MoveNext())
            {
                if (ieLoop.Current is CertifiedUserID)
                {
                    CertifiedUserID cuiUserID = (CertifiedUserID)ieLoop.Current;
                    strReturn += cuiUserID.ToString();
                }
            }

            return(strReturn);
        }
Exemple #6
0
        /// <summary>
        /// Parses the radix64 encoded representation of a transportable public
        /// key given as an argument to populate the parameters of this.
        /// </summary>
        /// <remarks>No remarks</remarks>
        /// <param name="strRadix64">Radix64 representation of an transportable
        /// public key</param>
        /// <exception cref="System.ArgumentException">Throws an
        /// ArgumentException if the radix64 string given as a parameter is
        /// not an transportable public key.</exception>
        public void Parse(string strRadix64)
        {
            Packet[] pPackets = Packet.ParsePackets(strRadix64);

            int nCurrentPacket = 0;
            int nUserIDCounter = 0;

            try {
                // First we expect a PublicKeyPacket
                if (!(pPackets[0] is PublicKeyPacket))
                {
                    throw(new ArgumentException("The given packet is not in the required transportable public key format!"));
                }
                this.PrimaryKey = (PublicKeyPacket)pPackets[nCurrentPacket++];

                // Next we expect zero or more revocation signatures
                while ((nCurrentPacket < pPackets.Length) && (pPackets[nCurrentPacket] is SignaturePacket))
                {
                    SignaturePacket spRevocation = (SignaturePacket)pPackets[nCurrentPacket++];
                    if (spRevocation.SignatureType != SignatureTypes.KeyRevocationSignature)
                    {
                        throw(new ArgumentException("The given packet is not in the required transportable public key format!"));
                    }
                    this.RevocationSignatures.Add(spRevocation);
                }

                // Next in queue must be one or more UserID Packets
                while ((nCurrentPacket < pPackets.Length) && (pPackets[nCurrentPacket] is UserIDPacket))
                {
                    nUserIDCounter++;
                    CertifiedUserID cuiUserID = new CertifiedUserID();
                    UserIDPacket    uipUserID = (UserIDPacket)pPackets[nCurrentPacket++];
                    cuiUserID.UserID = uipUserID;

                    // For every UserIDPacket we expect zero or more Signatures (Certificates)
                    while ((nCurrentPacket < pPackets.Length) && (pPackets[nCurrentPacket] is SignaturePacket))
                    {
                        SignaturePacket spCertificate = (SignaturePacket)pPackets[nCurrentPacket++];
                        if ((spCertificate.SignatureType != SignatureTypes.UserIDSignature) &&
                            (spCertificate.SignatureType != SignatureTypes.UserIDSignature_CasualVerification) &&
                            (spCertificate.SignatureType != SignatureTypes.UserIDSignature_NoVerification) &&
                            (spCertificate.SignatureType != SignatureTypes.UserIDSignature_PositivVerification))
                        {
                            throw(new ArgumentException("The given packet is not in the required transportable public key format!"));
                        }
                        cuiUserID.Certificates.Add(spCertificate);
                    }
                    this.Certifications.Add(cuiUserID);
                }

                // There was no UserIDPacket. This is fatal!!!
                if (nUserIDCounter == 0)
                {
                    throw(new ArgumentException("The given packet is not in the required transportable public key format!"));
                }

                // Finally we have zero or more subkeys
                while ((nCurrentPacket < pPackets.Length) && (pPackets[nCurrentPacket] is PublicKeyPacket))
                {
                    PublicKeyPacket       pkpSubKey = (PublicKeyPacket)pPackets[nCurrentPacket++];
                    CertifiedPublicSubkey cpsSubKey = new CertifiedPublicSubkey();
                    cpsSubKey.Subkey = pkpSubKey;

                    if ((nCurrentPacket < pPackets.Length) && (pPackets[nCurrentPacket] is SignaturePacket))
                    {
                        SignaturePacket spKeySignature = (SignaturePacket)pPackets[nCurrentPacket++];
                        if (spKeySignature.SignatureType == SignatureTypes.SubkeyBindingSignature)
                        {
                            cpsSubKey.KeyBindingSignature = spKeySignature;
                        }
                        else if (spKeySignature.SignatureType == SignatureTypes.SubkeyRevocationSignature)
                        {
                            cpsSubKey.RevocationSignature = spKeySignature;
                        }
                    }
                    else
                    {
                        throw(new ArgumentException("The given packet is not in the required transportable public key format!"));
                    }
                    if (nCurrentPacket < pPackets.Length)
                    {
                        if (pPackets[nCurrentPacket] is SignaturePacket)
                        {
                            SignaturePacket spSubkeyRev = (SignaturePacket)pPackets[nCurrentPacket++];
                            cpsSubKey.RevocationSignature = spSubkeyRev;
                        }
                    }
                    this.SubKeys.Add(cpsSubKey);
                }
            } catch (System.IndexOutOfRangeException) {
                if (nUserIDCounter == 0)
                {
                    throw(new ArgumentException("The given packet is not in the required transportable public key format!"));
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Generates the transportable public key out of the properties
        /// in this.
        /// </summary>
        /// <remarks>No remarks</remarks>
        /// <returns>Returns a byte array containing the openpgp encoded
        /// representation of the transportable public key.</returns>
        public byte[] Generate()
        {
            byte[] bPrimaryKey = this.PrimaryKey.Generate();

            //Revocation signatures
            int         iLength       = 0;
            IEnumerator ieRevocations = this.RevocationSignatures.GetEnumerator();

            while (ieRevocations.MoveNext())
            {
                if (ieRevocations.Current is SignaturePacket)
                {
                    iLength += ((SignaturePacket)ieRevocations.Current).Generate().Length;
                }
            }
            byte[] bRevocations = new byte[iLength];
            ieRevocations = this.RevocationSignatures.GetEnumerator();
            int iPosition = 0;

            while (ieRevocations.MoveNext())
            {
                if (ieRevocations.Current is SignaturePacket)
                {
                    byte[] bRev = ((SignaturePacket)ieRevocations.Current).Generate();
                    Array.Copy(bRev, 0, bRevocations, iPosition, bRev.Length);
                    iPosition += bRev.Length;
                }
            }

            //Certificates
            iLength = 0;
            IEnumerator ieCertificates = this.Certifications.GetEnumerator();

            while (ieCertificates.MoveNext())
            {
                if (ieCertificates.Current is CertifiedUserID)
                {
                    CertifiedUserID cuiID = (CertifiedUserID)ieCertificates.Current;
                    iLength += cuiID.Generate().Length;
                }
            }
            byte[] bCertificates = new byte[iLength];
            iPosition      = 0;
            ieCertificates = this.Certifications.GetEnumerator();
            while (ieCertificates.MoveNext())
            {
                if (ieCertificates.Current is CertifiedUserID)
                {
                    CertifiedUserID cuiID = (CertifiedUserID)ieCertificates.Current;
                    byte[]          bCert = cuiID.Generate();
                    Array.Copy(bCert, 0, bCertificates, iPosition, bCert.Length);
                    iPosition += bCert.Length;
                }
            }

            //SubKeys
            iLength = 0;
            IEnumerator ieSubkeys = this.SubKeys.GetEnumerator();

            while (ieSubkeys.MoveNext())
            {
                if (ieSubkeys.Current is CertifiedPublicSubkey)
                {
                    CertifiedPublicSubkey cpsKey = (CertifiedPublicSubkey)ieSubkeys.Current;
                    iLength += cpsKey.Generate().Length;
                }
            }
            byte[] bSubkeys = new byte[iLength];
            iPosition = 0;
            ieSubkeys = this.SubKeys.GetEnumerator();
            while (ieSubkeys.MoveNext())
            {
                if (ieSubkeys.Current is CertifiedPublicSubkey)
                {
                    CertifiedPublicSubkey cpsKey = (CertifiedPublicSubkey)ieSubkeys.Current;
                    byte[] bKey = cpsKey.Generate();
                    Array.Copy(bKey, 0, bSubkeys, iPosition, bKey.Length);
                    iPosition += bKey.Length;
                }
            }

            byte[] bData = new byte[bPrimaryKey.Length + bRevocations.Length +
                                    bCertificates.Length + bSubkeys.Length];
            iPosition = 0;
            Array.Copy(bPrimaryKey, bData, bPrimaryKey.Length);
            iPosition = bPrimaryKey.Length;
            Array.Copy(bRevocations, 0, bData, iPosition, bRevocations.Length);
            iPosition += bRevocations.Length;
            Array.Copy(bCertificates, 0, bData, iPosition, bCertificates.Length);
            iPosition += bCertificates.Length;
            Array.Copy(bSubkeys, 0, bData, iPosition, bSubkeys.Length);

            return(bData);
        }