Example #1
0
        /// <summary>
        /// Remove any certifications associated with a user attribute subpacket on a key.
        /// </summary>
        /// <param name="key">The key the certifications are to be removed from.</param>
        /// <param name="userAttributes">The attributes to be removed.</param>
        /// <returns>
        /// The re-certified key, or null if the user attribute subpacket was not found on the key.
        /// </returns>
        public static TKey?RemoveCertification <TKey>(TKey key, PgpUserAttributes userAttributes)
            where TKey : PgpKey
        {
            for (int i = 0; i < key.ids.Count; i++)
            {
                if (key.ids[i].UserAttributes?.Equals(userAttributes) == true)
                {
                    var returnKey = (TKey)key.CreateMutableCopy();
                    returnKey.ids.RemoveAt(i);
                    return(returnKey);
                }
            }

            return(null);
        }
Example #2
0
        // FIXME: This method is too advanced
        public static PgpCertification GenerateUserCertification(
            PgpSignatureType signatureType,
            PgpKey signingKey,
            PgpPrivateKey signingPrivateKey,
            PgpUserAttributes userAttributes,
            PgpKey userPublicKey,
            PgpSignatureAttributes?hashedAttributes   = null,
            PgpSignatureAttributes?unhashedAttributes = null,
            PgpHashAlgorithm hashAlgorithm            = PgpHashAlgorithm.Sha1)
        {
            if (signingKey == null)
            {
                throw new ArgumentNullException(nameof(signingKey));
            }
            if (signingPrivateKey == null)
            {
                throw new ArgumentNullException(nameof(signingPrivateKey));
            }
            if (signingKey.KeyId != signingPrivateKey.KeyId)
            {
                throw new ArgumentException(SR.Cryptography_OpenPgp_SigningKeyIdMismatch);
            }
            if (userAttributes == null)
            {
                throw new ArgumentNullException(nameof(userAttributes));
            }
            if (userPublicKey == null)
            {
                throw new ArgumentNullException(nameof(userPublicKey));
            }

            var userPacket         = new UserAttributePacket(userAttributes.ToSubpacketArray());
            var signatureGenerator = new PgpSignatureGenerator(signatureType, signingPrivateKey, hashAlgorithm);

            if (hashedAttributes != null)
            {
                signatureGenerator.HashedAttributes = hashedAttributes;
            }
            if (unhashedAttributes != null)
            {
                signatureGenerator.UnhashedAttributes = unhashedAttributes;
            }
            var signature = signatureGenerator.Generate(GenerateCertificationData(signingKey, userPacket, userPublicKey));

            return(new PgpCertification(signature, userPacket, userPublicKey));
        }
Example #3
0
        /// <summary>Remove a certification associated with a given user attributes on a key.</summary>
        /// <param name="key">The key the certifications are to be removed from.</param>
        /// <param name="userAttributes">The user attributes that the certfication is to be removed from.</param>
        /// <param name="certification">The certification to be removed.</param>
        /// <returns>The re-certified key, or null if the certification was not found.</returns>
        public static TKey?RemoveCertification <TKey>(
            TKey key,
            PgpUserAttributes userAttributes,
            PgpCertification certification)
            where TKey : PgpKey
        {
            Debug.Assert(certification.PublicKey.KeyId == key.KeyId);

            for (int i = 0; i < key.ids.Count; i++)
            {
                if (key.ids[i].UserAttributes?.Equals(userAttributes) == true)
                {
                    var returnKey = (TKey)key.CreateMutableCopy();
                    returnKey.ids[i] = PgpUser.RemoveCertification(returnKey.ids[i], returnKey, certification);
                    return(returnKey);
                }
            }

            return(null);
        }
Example #4
0
        /// <summary>Add a certification for the given UserAttributeSubpackets to the given public key.</summary>
        /// <param name="key">The key the certification is to be added to.</param>
        /// <param name="userAttributes">The attributes the certification is associated with.</param>
        /// <param name="certification">The new certification.</param>
        /// <returns>The re-certified key.</returns>
        public static TKey AddCertification <TKey>(
            TKey key,
            PgpUserAttributes userAttributes,
            PgpCertification certification)
            where TKey : PgpKey
        {
            var returnKey = (TKey)key.CreateMutableCopy();

            Debug.Assert(certification.PublicKey.KeyId == key.KeyId);

            for (int i = 0; i < returnKey.ids.Count; i++)
            {
                if (returnKey.ids[i].UserAttributes?.Equals(userAttributes) == true)
                {
                    returnKey.ids[i] = PgpUser.AddCertification(returnKey.ids[i], returnKey, certification.Signature);
                    return(returnKey);
                }
            }

            returnKey.ids.Add(new PgpUser(new UserAttributePacket(userAttributes.ToSubpacketArray()), returnKey, certification.Signature));
            return(returnKey);
        }