예제 #1
0
 /*
  * Sanity checks public PGP key, to make sure it's valid for encrypting MIME entities.
  */
 static void SanityCheckCryptographyKey(PgpPublicKey key)
 {
     if (!key.IsEncryptionKey)
     {
         throw new ArgumentException($"Key with fingerprint of '{PgpHelpers.GetFingerprint(key)}' is not an encryption key");
     }
     if (key.IsRevoked())
     {
         throw new ArgumentException($"Key with fingerprint of '{PgpHelpers.GetFingerprint(key)}' is revoked");
     }
 }
        internal static void InvokeLambda(
            ISignaler signaler,
            Node lambda,
            PgpPublicKey key)
        {
            // Parametrizing [.lambda] callback with key and data.
            var keyNode = new Node(".key");

            keyNode.Add(new Node("private", false));
            keyNode.Add(new Node("fingerprint", PgpHelpers.GetFingerprint(key)));
            keyNode.Add(new Node("id", key.KeyId));
            keyNode.Add(new Node("content", PgpHelpers.GetAsciiArmoredPublicKey(key)));
            keyNode.Add(new Node("created", key.CreationTime));
            keyNode.Add(new Node("valid-seconds", key.GetValidSeconds()));
            keyNode.Add(new Node("algorithm", key.Algorithm.ToString()));
            keyNode.Add(new Node("bit-strength", key.BitStrength));
            keyNode.Add(new Node("is-encryption", key.IsEncryptionKey));
            keyNode.Add(new Node("is-master", key.IsMasterKey));
            keyNode.Add(new Node("is-revoked", key.IsRevoked()));

            // Adding ID for key.
            var ids = new Node("ids");

            foreach (var idxId in key.GetUserIds())
            {
                ids.Add(new Node(".", idxId.ToString()));
            }
            if (ids.Children.Any())
            {
                keyNode.Add(ids);
            }

            // Invoking [.lambda] making sure we reset it after evaluation.
            var exe = lambda.Clone();

            lambda.Insert(0, keyNode);
            signaler.Signal("eval", lambda);
            lambda.Clear();
            lambda.AddRange(exe.Children.ToList());
        }
예제 #3
0
        public int ImportPublicKey(string fileName, string filePath, KeyStoreDB keyStoreDB)
        {
            int          cntImport         = 0;
            string       keyPath           = Path.Combine(filePath, fileName);
            string       stringFileContent = File.ReadAllText(keyPath);
            PgpPublicKey pubKey            = ReadPublicKey(keyPath);

            try {
                keyStoreDB.KeyStores.Add(new KeyStores()
                {
                    KeyStoreID      = pubKey.KeyId,
                    ArmouredKeyFile = stringFileContent,
                    Fingerprint     = pubKey.GetFingerprint(),
                    CreationTime    = pubKey.CreationTime,
                    ValidDays       = pubKey.ValidDays,
                    IsEncryptionKey = pubKey.IsEncryptionKey,
                    IsMasterKey     = pubKey.IsMasterKey,
                    IsSigningKey    = false,
                    IsRevoked       = pubKey.IsRevoked(),
                    KeyType         = "Public"
                });
                IEnumerable userIDs = pubKey.GetUserIds();
                foreach (string userId in userIDs)
                {
                    Match match = Regex.Match(userId, strRegex, RegexOptions.Compiled);
                    if (match != null)
                    {
                        string comment1 = match.Groups["comment1"] != null ? match.Groups["comment1"].Value : string.Empty;
                        string comment2 = match.Groups["comment2"] != null ? match.Groups["comment2"].Value : string.Empty;
                        if (!string.IsNullOrEmpty(comment2))
                        {
                            comment1 += " " + comment2;
                        }
                        KeyUsers userExists = keyStoreDB.KeyUsers.Find(pubKey.KeyId);
                        if (userExists != null)
                        {
                            keyStoreDB.KeyUsers.Remove(userExists);
                            keyStoreDB.SaveChanges();
                        }
                        keyStoreDB.KeyUsers.Add(new KeyUsers()
                        {
                            KeyStoreID     = pubKey.KeyId,
                            UserName       = match.Groups["user"] != null ? match.Groups["user"].Value : string.Empty,
                            Email          = match.Groups["email"] != null ? match.Groups["email"].Value : string.Empty,
                            Comment        = comment1,
                            EncryptionType = ((PublicKeyAlgorithmTag)pubKey.Algorithm).ToString(),
                            KeySize        = pubKey.BitStrength
                        });
                    }
                }
                keyStoreDB.SaveChanges();
            }
            catch (DbUpdateConcurrencyException dbConEx) {
                throw new DbUpdateConcurrencyException(dbConEx.Message);
            }
            catch (DbUpdateException dbEx) {
                throw new DbUpdateException(dbEx.Message);
            }
            catch (Exception ex) {
                throw new Exception(ex.Message);
            }
            return(++cntImport);
        }