feat(lib): Encapsulate all crypto in cryptoservice, make it pluggable#835
feat(lib): Encapsulate all crypto in cryptoservice, make it pluggable#835elizabethhealy wants to merge 6 commits intomainfrom
Conversation
* extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * fixes to ec implementation Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions * not implemented error --------- Signed-off-by: Elizabeth Healy <ehealy@virtru.com>
* extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * fixes to ec implementation Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * vendor jose lib functions to avoid maintenance * gemini suggestions, updates to better match jose behavior * remove issue with merge * gemini suggestions * strict jose version since were vendoring * forgot to update package lock --------- Signed-off-by: Elizabeth Healy <ehealy@virtru.com>
* extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * fixes to ec implementation Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * expose cryptoservice in opentdf obj * expose cryptoservice in opentdf obj * fix tests * fix issue with merge * use not implemented var --------- Signed-off-by: Elizabeth Healy <ehealy@virtru.com>
Summary of ChangesHello @elizabethhealy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant architectural improvement by refactoring the cryptography layer into a pluggable service. This change enhances modularity, testability, and extensibility, allowing for flexible integration of different cryptographic implementations. The core library now centralizes all crypto operations, providing a consistent and secure foundation for future development. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Ignored Files
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request significantly refactors cryptographic operations into a pluggable CryptoService, enhancing architectural flexibility and test coverage. However, a medium-severity vulnerability was identified in the RSA public key import logic, which incorrectly validates and accepts weak RSA keys (less than 2048 bits). Additionally, there is a high-severity finding concerning the extractPublicKeyPem implementation that could impact EC-based certificates.
lib/tdf3/src/crypto/index.ts
Outdated
| export async function extractPublicKeyPem(certOrPem: string): Promise<string> { | ||
| // If it's a certificate, extract the public key | ||
| if (certOrPem.includes('-----BEGIN CERTIFICATE-----')) { | ||
| const cert = await importX509(certOrPem, 'RS256', { extractable: true }); |
There was a problem hiding this comment.
The extractPublicKeyPem function hardcodes the algorithm to 'RS256' when calling jose.importX509. This will cause it to fail when processing X.509 certificates that contain EC public keys, as the underlying key material will not match the expected RSA algorithm. This is a significant limitation, as a KAS might provide an EC certificate.
To fix this, you should avoid hardcoding the algorithm. One approach could be to attempt to import the certificate with different supported algorithms (e.g., 'RS256', 'ES256', 'ES384', 'ES512') in a try-catch block, similar to how extractEcCurveFromPublicKey determines the curve.
For example:
if (certOrPem.includes('-----BEGIN CERTIFICATE-----')) {
// The `jose.importX509` function requires an algorithm hint. We'll try common ones.
const algorithmsToTry: AsymmetricSigningAlgorithm[] = ['RS256', 'ES256', 'ES384', 'ES512'];
for (const alg of algorithmsToTry) {
try {
const cert = await importX509(certOrPem, alg, { extractable: true });
return exportSPKI(cert);
} catch {
// Ignore errors and try the next algorithm.
}
}
throw new ConfigurationError(
'Could not extract public key from certificate. Unsupported algorithm?'
);
}| if (modulusBits <= 2048) { | ||
| algorithm = 'rsa:2048'; | ||
| } else if (modulusBits <= 4096) { | ||
| algorithm = 'rsa:4096'; | ||
| } else { | ||
| throw new ConfigurationError(`Unsupported RSA key size: ${modulusBits} bits`); | ||
| } |
There was a problem hiding this comment.
The importPublicKeyPem function uses permissive upper-bound checks (<= 2048 and <= 4096) to categorize RSA keys. This logic incorrectly labels keys smaller than 2048 bits (e.g., 1024 bits) as rsa:2048 and allows their use. 1024-bit RSA keys are considered cryptographically weak and should be rejected according to modern security standards (e.g., NIST SP 800-57) and the library's own MIN_ASYMMETRIC_KEY_SIZE_BITS constant. Additionally, keys between 2048 and 4096 bits (e.g., 3072 bits) are incorrectly labeled as rsa:4096.
Recommendation: Enforce a minimum key size of 2048 bits and use exact matches or stricter ranges for categorization.
* extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * fixes to ec implementation Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * expose cryptoservice in opentdf obj * expose cryptoservice in opentdf obj * fix tests * assertions use crypto service * address comments * fix cli * remove dupe test * add comment * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> --------- Signed-off-by: Elizabeth Healy <ehealy@virtru.com>
* extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * fixes to ec implementation Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * expose cryptoservice in opentdf obj * expose cryptoservice in opentdf obj * fix tests * assertions use crypto service * address comments * fix cli * tdf using crypto service * format * use cs for ec ops and remove crypto key from kas * format * suggestions * moving off of cryptokeypair * try to fix cli and web app builds * salt generation * resolve conflicts * re add comment --------- Signed-off-by: Elizabeth Healy <ehealy@virtru.com>
* extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * extend the crypto service interface * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * enable ci for this branch, gemini suggestions * add ec methods to crypto service * format * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * fixes to ec implementation Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions * jwt utils * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * suggestions, format * add support for rs256 signing and verification * add suggestion * expose cryptoservice in opentdf obj * expose cryptoservice in opentdf obj * fix tests * assertions use crypto service * address comments * fix cli * tdf using crypto service * format * use cs for ec ops and remove crypto key from kas * format * suggestions * moving off of cryptokeypair * try to fix cli and web app builds * salt generation * dpop changes * fix cli * fix linting, remove dpop dep from package and lock * update webapp package and lock * suggestions * dont change api of auth providers * 🤖 🎨 Autoformat Signed-off-by: Elizabeth Healy <ehealy@virtru.com> * resolve conflicts * fix tests * resolve some issues with the merge --------- Signed-off-by: Elizabeth Healy <ehealy@virtru.com>
No description provided.