예제 #1
0
        private ESKSequence CreateESKSequence(TransportablePublicKey[] tpkKeys, AsymActions aaAction, SymAlgorithms saAlgo, byte[] bSymKey)
        {
            ESKSequence esksReturn = new ESKSequence();

            for (int i = 0; i < tpkKeys.Length; i++)
            {
                TransportablePublicKey tpkKey = tpkKeys[i];
                PublicKeyPacket        pkpKey = tpkKey.FindKey(aaAction);

                if (pkpKey == null)
                {
                    throw new Exception("Could not find subkey fitting to the selected action. Concerned Key: " + tpkKey.PrimaryUserID);
                }

                AsymSessionKeyPacket skpKey = new AsymSessionKeyPacket();
                skpKey.KeyID              = pkpKey.KeyID;
                skpKey.PublicAlgorithm    = pkpKey.Algorithm;
                skpKey.SymmetricAlgorithm = saAlgo;
                skpKey.SessionKey         = bSymKey;

                skpKey.EncryptSessionKey(pkpKey);

                esksReturn.AddAsymSessionKey(skpKey);
            }

            return(esksReturn);
        }
예제 #2
0
        private static ESKSequence CreateESKSequence(ArrayList alKeys, AsymActions aaAction, SymAlgorithms saAlgo, byte[] bSymKey)
        {
            IEnumerator ieKeys     = alKeys.GetEnumerator();
            ESKSequence esksReturn = new ESKSequence();

            while (ieKeys.MoveNext())
            {
                TransportablePublicKey tpkKey = (TransportablePublicKey)ieKeys.Current;
                PublicKeyPacket        pkpKey = tpkKey.FindKey(aaAction);

                if (pkpKey == null)
                {
                    throw new Exception("Could not find subkey fitting to the selected action. Concerned Key: " + tpkKey.PrimaryUserID);
                }

                AsymSessionKeyPacket skpKey = new AsymSessionKeyPacket();
                skpKey.KeyID              = pkpKey.KeyID;
                skpKey.PublicAlgorithm    = pkpKey.Algorithm;
                skpKey.SymmetricAlgorithm = saAlgo;
                skpKey.SessionKey         = bSymKey;

                skpKey.EncryptSessionKey(pkpKey);

                esksReturn.AddAsymSessionKey(skpKey);
            }

            return(esksReturn);
        }
        /// <summary>
        /// Finds a subkey (or the primary key) that fits to the given
        /// requirements (meaning it must be supposed to be used for
        /// the given action, which can be either signing or encryption).
        /// If more than just one keys fullfill the requirements, the one
        /// with the newer creationdate is used.
        /// </summary>
        /// <remarks>No remarks</remarks>
        /// <param name="aaAction">The action (signing or encrypting) for
        /// which the key should be used</param>
        /// <returns>Returns a public key packet fullfilling the given
        /// requirements (the action) or null, if it did not find such
        /// a key.</returns>
        public SecretKeyPacket FindKey(AsymActions aaAction)
        {
            DateTime        dtCandidateTime = DateTime.Now;
            SecretKeyPacket skpCandidate    = new SecretKeyPacket();
            bool            bFound          = false;

            // First check the primary Key
            if (aaAction == AsymActions.Encrypt)
            {
                if (skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                    skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.ElGamal_Encrypt_Only ||
                    skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Only ||
                    skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                {
                    dtCandidateTime = skpPrimaryKey.PublicKey.TimeCreated;
                    bFound          = true;
                    skpCandidate    = skpPrimaryKey;
                }
            }
            else if (aaAction == AsymActions.Sign)
            {
                if (skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                    skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.DSA ||
                    skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Sign_Only ||
                    skpPrimaryKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                {
                    dtCandidateTime = skpPrimaryKey.PublicKey.TimeCreated;
                    bFound          = true;
                    skpCandidate    = skpPrimaryKey;
                }
            }

            // Now check the subkeys
            IEnumerator ieSubkeys = alSubkeys.GetEnumerator();

            while (ieSubkeys.MoveNext())
            {
                SecretKeyPacket skpKey = (SecretKeyPacket)ieSubkeys.Current;
                if (aaAction == AsymActions.Encrypt)
                {
                    if (skpKey.PublicKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                        skpKey.PublicKey.Algorithm == AsymAlgorithms.ElGamal_Encrypt_Only ||
                        skpKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Only ||
                        skpKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                    {
                        if ((bFound && dtCandidateTime < skpKey.PublicKey.TimeCreated) || (!bFound))
                        {
                            dtCandidateTime = skpKey.PublicKey.TimeCreated;
                            bFound          = true;
                            skpCandidate    = skpKey;
                        }
                    }
                }
                else if (aaAction == AsymActions.Sign)
                {
                    if (skpKey.PublicKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                        skpKey.PublicKey.Algorithm == AsymAlgorithms.DSA ||
                        skpKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Sign_Only ||
                        skpKey.PublicKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                    {
                        if ((bFound && dtCandidateTime < skpKey.PublicKey.TimeCreated) || (!bFound))
                        {
                            dtCandidateTime = skpKey.PublicKey.TimeCreated;
                            bFound          = true;
                            skpCandidate    = skpKey;
                        }
                    }
                }
            }

            if (bFound)
            {
                return(skpCandidate);
            }

            return(null);
        }
예제 #4
0
        /// <summary>
        /// Creates a simmetric encrypted sequence
        /// </summary>
        /// <param name="alKeys">keys to produce the sequence for</param>
        /// <param name="aaAction">encrypt/sign</param>
        /// <param name="saAlgo">algorithm used</param>
        /// <param name="bSymKey">simmetric key</param>
        /// <returns>a simmetric encrypted sequence</returns>
        private static ESKSequence CreateESKSequence(ArrayList alKeys, AsymActions aaAction, SymAlgorithms saAlgo, byte[] bSymKey)
        {
            IEnumerator ieKeys = alKeys.GetEnumerator();
            ESKSequence esksReturn = new ESKSequence();

            while (ieKeys.MoveNext()) {
                TransportablePublicKey tpkKey = (TransportablePublicKey)ieKeys.Current;
                PublicKeyPacket pkpKey = tpkKey.FindKey(aaAction);

                if (pkpKey == null)
                    throw new Exception("Could not find subkey fitting to the selected action. Concerned Key: " + tpkKey.PrimaryUserID);

                AsymSessionKeyPacket skpKey = new AsymSessionKeyPacket();
                skpKey.KeyID = pkpKey.KeyID;
                skpKey.PublicAlgorithm = pkpKey.Algorithm;
                skpKey.SymmetricAlgorithm = saAlgo;
                skpKey.SessionKey = bSymKey;

                skpKey.EncryptSessionKey(pkpKey);

                esksReturn.AddAsymSessionKey(skpKey);
            }

            return esksReturn;
        }
예제 #5
0
        /// <summary>
        /// Finds a subkey (or the primary key) that fits to the given
        /// requirements (meaning it must be supposed to be used for
        /// the given action, which can be either signing or encryption).
        /// If more than just one keys fullfill the requirements, the one
        /// with the newer creationdate is used.
        /// </summary>
        /// <remarks>No remarks</remarks>
        /// <param name="aaAction">The action (signing or encrypting) for
        /// which the key should be used</param>
        /// <returns>Returns a public key packet fullfilling the given
        /// requirements (the action) or null, if it did not find such
        /// a key.</returns>
        public PublicKeyPacket FindKey(AsymActions aaAction)
        {
            DateTime        dtCandidateTime = DateTime.Now;
            PublicKeyPacket pkpCandidate    = new PublicKeyPacket();
            bool            bFound          = false;

            // First check the primary Key
            if (aaAction == AsymActions.Encrypt)
            {
                if (pkpPrimaryKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.ElGamal_Encrypt_Only ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Only ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                {
                    dtCandidateTime = pkpPrimaryKey.TimeCreated;
                    bFound          = true;
                    pkpCandidate    = pkpPrimaryKey;
                }
            }
            else if (aaAction == AsymActions.Sign)
            {
                if (pkpPrimaryKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.DSA ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Sign_Only ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                {
                    dtCandidateTime = pkpPrimaryKey.TimeCreated;
                    bFound          = true;
                    pkpCandidate    = pkpPrimaryKey;
                }
            }

            // Now check the subkeys
            IEnumerator ieSubkeys = alSubkeys.GetEnumerator();

            while (ieSubkeys.MoveNext())
            {
                CertifiedPublicSubkey cpsKey = (CertifiedPublicSubkey)ieSubkeys.Current;

                // The subkey has been revoced
                if (cpsKey.RevocationSignature != null)
                {
                    continue;
                }

                PublicKeyPacket pkpKey = cpsKey.Subkey;
                if (aaAction == AsymActions.Encrypt)
                {
                    if (pkpKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                        pkpKey.Algorithm == AsymAlgorithms.ElGamal_Encrypt_Only ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Only ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                    {
                        if ((bFound && dtCandidateTime < pkpKey.TimeCreated) || (!bFound))
                        {
                            dtCandidateTime = pkpKey.TimeCreated;
                            bFound          = true;
                            pkpCandidate    = pkpKey;
                        }
                    }
                }
                else if (aaAction == AsymActions.Sign)
                {
                    if (pkpKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                        pkpKey.Algorithm == AsymAlgorithms.DSA ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Sign_Only ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign)
                    {
                        if ((bFound && dtCandidateTime < pkpKey.TimeCreated) || (!bFound))
                        {
                            dtCandidateTime = pkpKey.TimeCreated;
                            bFound          = true;
                            pkpCandidate    = pkpKey;
                        }
                    }
                }
            }

            if (bFound)
            {
                return(pkpCandidate);
            }

            return(null);
        }
        /// <summary>
        /// Finds a subkey (or the primary key) that fits to the given 
        /// requirements (meaning it must be supposed to be used for 
        /// the given action, which can be either signing or encryption).
        /// If more than just one keys fullfill the requirements, the one
        /// with the newer creationdate is used.
        /// </summary>
        /// <remarks>No remarks</remarks>
        /// <param name="aaAction">The action (signing or encrypting) for
        /// which the key should be used</param>
        /// <returns>Returns a public key packet fullfilling the given
        /// requirements (the action) or null, if it did not find such
        /// a key.</returns>
        public PublicKeyPacket FindKey(AsymActions aaAction)
        {
            DateTime dtCandidateTime = DateTime.Now;
            PublicKeyPacket pkpCandidate = new PublicKeyPacket();
            bool bFound = false;

            // First check the primary Key
            if (aaAction == AsymActions.Encrypt) {
                if (pkpPrimaryKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.ElGamal_Encrypt_Only ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Only ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign) {

                    dtCandidateTime = pkpPrimaryKey.TimeCreated;
                    bFound = true;
                    pkpCandidate = pkpPrimaryKey;
                }
            } else if (aaAction == AsymActions.Sign) {
                if (pkpPrimaryKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.DSA ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Sign_Only ||
                    pkpPrimaryKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign) {

                    dtCandidateTime = pkpPrimaryKey.TimeCreated;
                    bFound = true;
                    pkpCandidate = pkpPrimaryKey;
                }
            }

            // Now check the subkeys
            IEnumerator ieSubkeys = alSubkeys.GetEnumerator();
            while (ieSubkeys.MoveNext()) {
                CertifiedPublicSubkey cpsKey = (CertifiedPublicSubkey)ieSubkeys.Current;

                // The subkey has been revoced
                if (cpsKey.RevocationSignature != null)
                    continue;

                PublicKeyPacket pkpKey = cpsKey.Subkey;
                if (aaAction == AsymActions.Encrypt) {

                    if (pkpKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                        pkpKey.Algorithm == AsymAlgorithms.ElGamal_Encrypt_Only ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Only ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign) {

                        if ((bFound && dtCandidateTime < pkpKey.TimeCreated) || (!bFound)) {
                            dtCandidateTime = pkpKey.TimeCreated;
                            bFound = true;
                            pkpCandidate = pkpKey;
                        }
                    }
                } else if (aaAction == AsymActions.Sign) {
                    if (pkpKey.Algorithm == AsymAlgorithms.ElGama_Encrypt_Sign ||
                        pkpKey.Algorithm == AsymAlgorithms.DSA ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Sign_Only ||
                        pkpKey.Algorithm == AsymAlgorithms.RSA_Encrypt_Sign) {

                        if ((bFound && dtCandidateTime < pkpKey.TimeCreated) || (!bFound)) {
                            dtCandidateTime = pkpKey.TimeCreated;
                            bFound = true;
                            pkpCandidate = pkpKey;
                        }
                    }
                }

            }

            if (bFound)
                return pkpCandidate;

            return null;
        }
예제 #7
0
        private ESKSequence CreateESKSequence(TransportablePublicKey[] tpkKeys, AsymActions aaAction, SymAlgorithms saAlgo, byte[] bSymKey)
        {
            ESKSequence esksReturn = new ESKSequence();

            for (int i=0; i<tpkKeys.Length; i++) {
                TransportablePublicKey tpkKey = tpkKeys[i];
                PublicKeyPacket pkpKey = tpkKey.FindKey(aaAction);

                if (pkpKey == null)
                    throw new Exception("Could not find subkey fitting to the selected action. Concerned Key: " + tpkKey.PrimaryUserID);

                AsymSessionKeyPacket skpKey = new AsymSessionKeyPacket();
                skpKey.KeyID = pkpKey.KeyID;
                skpKey.PublicAlgorithm = pkpKey.Algorithm;
                skpKey.SymmetricAlgorithm = saAlgo;
                skpKey.SessionKey = bSymKey;

                skpKey.EncryptSessionKey(pkpKey);

                esksReturn.AddAsymSessionKey(skpKey);
            }

            return esksReturn;
        }