CVE-2021-41117
Last modified
CVE-2021-41117 is a critical-severity vulnerability rated 9.1/10 on the CVSS scale. keypair is a a RSA PEM key generator written in javascript. keypair implements a lot of cryptographic primitives on its own or by borrowing from other libraries where possible, including node-forge. EPSS estimates a 2.99% chance of exploitation in the next 30 days.
Description
keypair is a a RSA PEM key generator written in javascript. keypair implements a lot of cryptographic primitives on its own or by borrowing from other libraries where possible, including node-forge. An issue was discovered where this library was generating identical RSA keys used in SSH. This would mean that the library is generating identical P, Q (and thus N) values which, in practical terms, is impossible with RSA-2048 keys. Generating identical values, repeatedly, usually indicates an issue with poor random number generation, or, poor handling of CSPRNG output. Issue 1: Poor random number generation (`GHSL-2021-1012`). The library does not rely entirely on a platform provided CSPRNG, rather, it uses it's own counter-based CMAC approach. Where things go wrong is seeding the CMAC implementation with "true" random data in the function `defaultSeedFile`. In order to seed the AES-CMAC generator, the library will take two different approaches depending on the JavaScript execution environment. In a browser, the library will use [`window.crypto.getRandomValues()`](https://github.com/juliangruber/keypair/blob/87c62f255baa12c1ec4f98a91600f82af80be6db/index.js#L971). However, in a nodeJS execution environment, the `window` object is not defined, so it goes down a much less secure solution, also of which has a bug in it. It does look like the library tries to use node's CSPRNG when possible unfortunately, it looks like the `crypto` object is null because a variable was declared with the same name, and set to `null`. So the node CSPRNG path is never taken. However, when `window.crypto.getRandomValues()` is not available, a Lehmer LCG random number generator is used to seed the CMAC counter, and the LCG is seeded with `Math.random`. While this is poor and would likely qualify in a security bug in itself, it does not explain the extreme frequency in which duplicate keys occur. The main flaw: The output from the Lehmer LCG is encoded incorrectly. The specific [line][https://github.com/juliangruber/keypair/blob/87c62f255baa12c1ec4f98a91600f82af80be6db/index.js#L1008] with the flaw is: `b.putByte(String.fromCharCode(next & 0xFF))` The [definition](https://github.com/juliangruber/keypair/blob/87c62f255baa12c1ec4f98a91600f82af80be6db/index.js#L350-L352) of `putByte` is `util.ByteBuffer.prototype.putByte = function(b) {this.data += String.fromCharCode(b);};`. Simplified, this is `String.fromCharCode(String.fromCharCode(next & 0xFF))`. The double `String.fromCharCode` is almost certainly unintentional and the source of weak seeding. Unfortunately, this does not result in an error. Rather, it results most of the buffer containing zeros. Since we are masking with 0xFF, we can determine that 97% of the output from the LCG are converted to zeros. The only outputs that result in meaningful values are outputs 48 through 57, inclusive. The impact is that each byte in the RNG seed has a 97% chance of being 0 due to incorrect conversion. When it is not, the bytes are 0 through 9. In summary, there are three immediate concerns: 1. The library has an insecure random number fallback path. Ideally the library would require a strong CSPRNG instead of attempting to use a LCG and `Math.random`. 2. The library does not correctly use a strong random number generator when run in NodeJS, even though a strong CSPRNG is available. 3. The fallback path has an issue in the implementation where a majority of the seed data is going to effectively be zero. Due to the poor random number generation, keypair generates RSA keys that are relatively easy to guess. This could enable an attacker to decrypt confidential messages or gain authorized access to an account belonging to the victim.
Metrics
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:N
Weakness Enumeration
Affected Software
| Vendor | Product | Versions |
|---|---|---|
| Keypair Project | Keypair | < 1.0.4 |
References
- https://github.com/juliangruber/keypair/security/advisories/GHSA-3f99-hvg4-qjwjExploit, Patch, Third Party Advisory
- https://securitylab.github.com/advisories/GHSL-2021-1012-keypair/Exploit, Third Party Advisory
- https://github.com/juliangruber/keypair/security/advisories/GHSA-3f99-hvg4-qjwjExploit, Patch, Third Party Advisory
- https://securitylab.github.com/advisories/GHSL-2021-1012-keypair/Exploit, Third Party Advisory
Timeline
- Published
- Last Modified
- Status
- Modified
Frequently Asked Questions
What is CVE-2021-41117?
How severe is CVE-2021-41117?
How do I fix CVE-2021-41117?
How Strix Helps
- How Strix found a critical auth bypass in etcdStrix autonomously discovered a critical authentication bypass in etcd, later designated CVE-2026-33413.
- Autonomous PentestingAI agents that find and validate exploitable vulnerabilities like this one across your applications.
- PR ReviewsPentest every pull request so vulnerable code is caught before it ships to production.
- AI Penetration TestingHow AI-driven penetration testing continuously covers your attack surface.
Related CVEs from 2021
- CVE-2021-41111Rundeck is an open source automation service with a web cons…5.4
- CVE-2021-41112Rundeck is an open source automation service with a web cons…8.1
- CVE-2021-41113TYPO3 is an open source PHP based web content management sys…8.8
- CVE-2021-41114TYPO3 is an open source PHP based web content management sys…5.3
- CVE-2021-41115Zulip is an open source team chat server. In affected versio…6.5
- CVE-2021-41116Composer is an open source dependency manager for the PHP la…9.8
- CVE-2021-41118The DynamicPageList3 extension is a reporting tool for Media…7.5
- CVE-2021-41119Wire-server is the system server for the wire back-end servi…7.5
- CVE-2021-4112A flaw was found in ansible-tower where the default installa…8.8
- CVE-2021-41120sylius/paypal-plugin is a paypal plugin for the Sylius devel…7.5
- CVE-2021-41121Vyper is a Pythonic Smart Contract Language for the EVM. In …8.8
- CVE-2021-41122Vyper is a Pythonic Smart Contract Language for the EVM. In …4.3
Are you affected by CVE-2021-41117?
Run a free Strix scan to check your systems for this vulnerability.
Scan your code nowSource: NVD / NIST
