public static bool Update(KeyStoreDB keyStoreDb, long keyId, string trust)
        {
            if (keyStoreDb == null)
            {
                throw new ArgumentNullException("keyStoreDb");
            }
            if (keyId == 0)
            {
                throw new ArgumentOutOfRangeException("keyId");
            }
            if (string.IsNullOrEmpty(trust))
            {
                throw new ArgumentNullException("trust");
            }


            KeyStores updRow = keyStoreDb.KeyStores.Find(keyId);

            switch (updRow.KeyType)
            {
            case "Public":
                if (pubKeyLevels.Contains(trust.ToLower()))
                {
                    updRow.OwnerTrust = trust;
                }
                else
                {
                    updRow.OwnerTrust = string.Empty;
                }
                break;

            case "Secret":
                if (secKeyLevel.Equals(trust.ToLower()))
                {
                    updRow.OwnerTrust = trust;
                }
                else
                {
                    updRow.OwnerTrust = trust;
                }
                break;

            default:
                break;
            }
            keyStoreDb.SaveChanges();

            return(true);
        }
Beispiel #2
0
        private bool RemovePublicKeyForSecretKey(long keyId, KeyStoreDB keyStoreDB)
        {
            KeyStores pubKeyDelete = keyStoreDB.KeyStores.Find(keyId);

            if (pubKeyDelete != null)
            {
                KeyUsers pubUserDelete = keyStoreDB.KeyUsers.Find(keyId);
                if (pubUserDelete != null)
                {
                    keyStoreDB.KeyUsers.Remove(pubUserDelete);
                }
                keyStoreDB.KeyStores.Remove(pubKeyDelete);
                keyStoreDB.SaveChanges();
            }
            return(true);
        }
Beispiel #3
0
        public void UpdateDbSecretKey(PgpSecretKey key, string keyExportName)
        {
            Stream outFile  = File.Create(keyExportName);
            Stream outArmor = new ArmoredOutputStream(outFile);
            string secKey   = string.Empty;

            key.Encode(outArmor);
            outArmor.Close();
            using (StreamReader rdr = new StreamReader(outFile)) {
                rdr.BaseStream.Position = 0;
                secKey = rdr.ReadToEnd();
            }
            KeyStores updKey = m_keyStoreDb.KeyStores.Find(key.KeyId);

            updKey.ArmouredKeyFile = secKey;
            m_keyStoreDb.SaveChanges();
        }
Beispiel #4
0
        public int ImportSecretKey(string fileName, string filePath, KeyStoreDB keyStoreDB)
        {
            int cntImport = 0;

            string       keyPath           = Path.Combine(filePath, fileName);
            string       stringFileContent = File.ReadAllText(keyPath);
            PgpSecretKey secKey            = ReadSecretKey(keyPath, true);

            //bool removed = RemovePublicKeyForSecretKey(secKey.KeyId, keyStoreDB);

            try {
                keyStoreDB.KeyStores.Add(new KeyStores()
                {
                    KeyStoreID      = secKey.KeyId,
                    ArmouredKeyFile = stringFileContent,
                    Fingerprint     = secKey.PublicKey.GetFingerprint(),
                    CreationTime    = secKey.PublicKey.CreationTime,
                    ValidDays       = secKey.PublicKey.ValidDays,
                    IsEncryptionKey = false,
                    IsMasterKey     = secKey.IsMasterKey,
                    IsSigningKey    = secKey.IsSigningKey,
                    IsRevoked       = secKey.PublicKey.IsRevoked(),
                    KeyType         = "Secret"
                });
                IEnumerable userIDs = secKey.UserIds;
                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(secKey.KeyId);
                        if (userExists != null)
                        {
                            keyStoreDB.KeyUsers.Remove(userExists);
                            keyStoreDB.SaveChanges();
                        }
                        keyStoreDB.KeyUsers.Add(new KeyUsers()
                        {
                            KeyStoreID     = secKey.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)secKey.PublicKey.Algorithm).ToString(),
                            KeySize        = secKey.PublicKey.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);
        }