public SecretCollection FindSecrets(IEnumerable <EncryptionKey> keys) { if (keys == null) { throw new ArgumentNullException(nameof(keys)); } // Get a copy of the collection so we can remove keys as we use them. EncryptionKeyCollection unusedKeys = new EncryptionKeyCollection(""); unusedKeys.AddRange(keys); // Accumulate decrypted secrets here. SecretCollection secrets = new SecretCollection(); XmlNodeList nodeList = GetXecretsSession(XecretsDocument).SelectNodes("XecretsSession"); foreach (XmlNode node in nodeList) { SecretCollection decryptedSessionSecrets = AttemptDecryptXecretsSessionElement(unusedKeys, (XmlElement)node); secrets.AddRange(decryptedSessionSecrets); } secrets.OriginalCount = secrets.Count; return(secrets); }
private SecretCollection AttemptDecryptXecretsSessionElement(EncryptionKeyCollection keyCollection, XmlElement xecretsSessionElement) { foreach (EncryptionKey key in keyCollection) { DateTime lastUpdateUtc = DateTime.Parse(xecretsSessionElement.Attributes["LastUpdateUtc"].Value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); if (lastUpdateUtc > LastUpdateUtc) { LastUpdateUtc = lastUpdateUtc; } XmlDocument decryptedXml = DecryptEncryptedData(new InternalEncryptionKey(key), xecretsSessionElement); if (decryptedXml == null) { continue; } SecretCollection decryptedSecrets = GetSessionSecrets(decryptedXml.SelectNodes("Secrets/Secret"), key); // Set the last updated for each and every secret - we don't actually store this per secret, only per // session, but that's not a fact we expose externally. foreach (Secret secret in decryptedSecrets) { InternalSecret internalSecret = secret as InternalSecret; internalSecret.LastUpdateUtc = lastUpdateUtc; } keyCollection.Remove(key); return(decryptedSecrets); } return(new SecretCollection()); }
public FormatConfidence DetermineFormatConfidence(EncryptionKeyCollection keyCollection) { if (_data == null) { throw new InvalidOperationException("No data read to check format for"); } // Here try to extract from the data using the keys. If this fails (exception) it's not our format. // Even if we get zero secrets, it might not be our format, it could be a later format. try { // Since it's a lazy load, the property reference here must be inside the try if (XecretsDocument == null) { return(FormatConfidence.DefinitelyNot); } // Get what we can using the keyCollection IList <Secret> secrets = FindSecrets(keyCollection); if (secrets.Count > 0) { // If we got any secrest, we're definitely on the right track return(FormatConfidence.Definitely); } // If we just got zero, we're probably the right one. return(FormatConfidence.Probably); } catch (FormatException) { // Something went wrong... return(FormatConfidence.DefinitelyNot); } }