Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions EpiSource.KeePass.Ekf/Crypto/Windows/WindowsKeyPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,30 @@ public bool CanEncryptCms {
}
}

// Key usage flags asserting the certificate may be used for CMS encryption/decryption.
// Besides the standard RSA keyEncipherment and (EC)DH keyAgreement bits, some smartcards
// (e.g. PIV cards) mark their encryption certificate with dataEncipherment instead of
// keyEncipherment. Gate on any of these bits and let the key algorithm (pubKeyInfo) decide
// between key transfer and key agreement - requiring an exact bit per primitive locks out
// otherwise valid encryption keys (see issue #12). Absent a key usage extension all bits are
// assumed set (see UpdateKeyUsageFlags), so unrestricted certificates keep working.
private const X509KeyUsageFlags encryptionKeyUsage =
X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DataEncipherment | X509KeyUsageFlags.KeyAgreement;

private bool KeyUsageAllowsEncryption {
get { return (this.keyUsageFlags & encryptionKeyUsage) != X509KeyUsageFlags.None; }
}

public bool CanKeyAgree {
get {
return (this.keyUsageFlags & X509KeyUsageFlags.KeyAgreement) == X509KeyUsageFlags.KeyAgreement
return this.KeyUsageAllowsEncryption
&& this.pubKeyInfo.CanKeyAgree && (this.privKeyInfo == null || this.privKeyInfo.CanKeyAgree);
}
}

public bool CanKeyTransfer {
get {
return (this.keyUsageFlags & X509KeyUsageFlags.KeyEncipherment) == X509KeyUsageFlags.KeyEncipherment
return this.KeyUsageAllowsEncryption
&& this.pubKeyInfo.CanKeyTransfer && (this.privKeyInfo == null || this.privKeyInfo.CanKeyTransfer);
}
}
Expand Down