Skip to content

Commit e4b63a7

Browse files
authored
fix: support legacy RSA-* hash name aliases (#929)
1 parent 039e1e0 commit e4b63a7

6 files changed

Lines changed: 56 additions & 37 deletions

File tree

example/src/tests/keys/generate_keypair.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ test(SUITE, 'generateKeyPair DH with primeLength', async () => {
791791
generateKeyPair(
792792
'dh',
793793
{
794-
primeLength: 2048,
794+
primeLength: 512,
795795
publicKeyEncoding: { type: 'spki', format: 'pem' },
796796
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
797797
},

packages/react-native-quick-crypto/cpp/cipher/HybridRsaCipher.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ using margelo::nitro::NativeArrayBuffer;
1414
constexpr int kRsaPkcs1Padding = 1;
1515
constexpr int kRsaOaepPadding = 4;
1616

17-
const EVP_MD* getDigestByName(const std::string& hashAlgorithm) {
18-
if (hashAlgorithm == "SHA-1" || hashAlgorithm == "SHA1" || hashAlgorithm == "sha1" || hashAlgorithm == "sha-1") {
19-
return EVP_sha1();
20-
} else if (hashAlgorithm == "SHA-256" || hashAlgorithm == "SHA256" || hashAlgorithm == "sha256" || hashAlgorithm == "sha-256") {
21-
return EVP_sha256();
22-
} else if (hashAlgorithm == "SHA-384" || hashAlgorithm == "SHA384" || hashAlgorithm == "sha384" || hashAlgorithm == "sha-384") {
23-
return EVP_sha384();
24-
} else if (hashAlgorithm == "SHA-512" || hashAlgorithm == "SHA512" || hashAlgorithm == "sha512" || hashAlgorithm == "sha-512") {
25-
return EVP_sha512();
26-
}
27-
throw std::runtime_error("Unsupported hash algorithm: " + hashAlgorithm);
28-
}
29-
3017
int toOpenSSLPadding(int padding) {
3118
switch (padding) {
3219
case kRsaPkcs1Padding:

packages/react-native-quick-crypto/cpp/sign/SignUtils.hpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,15 @@
88
#include <openssl/evp.h>
99
#include <string>
1010

11+
#include "../utils/QuickCryptoUtils.hpp"
12+
1113
namespace margelo::nitro::crypto {
1214

1315
enum DSASigEnc {
1416
kSigEncDER = 0,
1517
kSigEncP1363 = 1,
1618
};
1719

18-
inline const EVP_MD* getDigestByName(const std::string& algorithm) {
19-
if (algorithm == "SHA1" || algorithm == "sha1" || algorithm == "SHA-1" || algorithm == "sha-1") {
20-
return EVP_sha1();
21-
} else if (algorithm == "SHA224" || algorithm == "sha224" || algorithm == "SHA-224" || algorithm == "sha-224") {
22-
return EVP_sha224();
23-
} else if (algorithm == "SHA256" || algorithm == "sha256" || algorithm == "SHA-256" || algorithm == "sha-256") {
24-
return EVP_sha256();
25-
} else if (algorithm == "SHA384" || algorithm == "sha384" || algorithm == "SHA-384" || algorithm == "sha-384") {
26-
return EVP_sha384();
27-
} else if (algorithm == "SHA512" || algorithm == "sha512" || algorithm == "SHA-512" || algorithm == "sha-512") {
28-
return EVP_sha512();
29-
} else if (algorithm == "SHA3-224" || algorithm == "sha3-224") {
30-
return EVP_sha3_224();
31-
} else if (algorithm == "SHA3-256" || algorithm == "sha3-256") {
32-
return EVP_sha3_256();
33-
} else if (algorithm == "SHA3-384" || algorithm == "sha3-384") {
34-
return EVP_sha3_384();
35-
} else if (algorithm == "SHA3-512" || algorithm == "sha3-512") {
36-
return EVP_sha3_512();
37-
}
38-
throw std::runtime_error("Unsupported hash algorithm: " + algorithm);
39-
}
40-
4120
inline unsigned int getBytesOfRS(EVP_PKEY* pkey) {
4221
int bits;
4322
int base_id = EVP_PKEY_base_id(pkey);

packages/react-native-quick-crypto/cpp/utils/QuickCryptoUtils.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cctype>
55
#include <limits>
66
#include <openssl/err.h>
7+
#include <openssl/evp.h>
78
#include <string>
89
#include <vector>
910

@@ -72,4 +73,36 @@ inline std::string toLower(std::string s) {
7273
return s;
7374
}
7475

76+
inline const EVP_MD* getDigestByName(const std::string& algorithm) {
77+
std::string algo = toLower(algorithm);
78+
79+
// Strip legacy RSA- prefix (e.g. rsa-sha256 -> sha256) for Node.js compat
80+
if (algo.size() > 4 && algo.compare(0, 4, "rsa-") == 0) {
81+
algo = algo.substr(4);
82+
}
83+
84+
if (algo == "sha1" || algo == "sha-1") {
85+
return EVP_sha1();
86+
} else if (algo == "sha224" || algo == "sha-224") {
87+
return EVP_sha224();
88+
} else if (algo == "sha256" || algo == "sha-256") {
89+
return EVP_sha256();
90+
} else if (algo == "sha384" || algo == "sha-384") {
91+
return EVP_sha384();
92+
} else if (algo == "sha512" || algo == "sha-512") {
93+
return EVP_sha512();
94+
} else if (algo == "sha3-224") {
95+
return EVP_sha3_224();
96+
} else if (algo == "sha3-256") {
97+
return EVP_sha3_256();
98+
} else if (algo == "sha3-384") {
99+
return EVP_sha3_384();
100+
} else if (algo == "sha3-512") {
101+
return EVP_sha3_512();
102+
} else if (algo == "ripemd160" || algo == "ripemd-160") {
103+
return EVP_ripemd160();
104+
}
105+
throw std::runtime_error("Unsupported hash algorithm: " + algorithm);
106+
}
107+
75108
} // namespace margelo::nitro::crypto

packages/react-native-quick-crypto/src/utils/hashnames.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ const kHashNames: HashNames = {
7676
kHashNames[alias] = kHashNames[keys[n]!]!;
7777
}
7878
}
79+
80+
// Add OpenSSL legacy RSA-* aliases (e.g. RSA-SHA256 -> sha256)
81+
for (let n: number = 0; n < keys.length; n++) {
82+
const key = keys[n]!;
83+
if (key.startsWith('sha') || key === 'ripemd160') {
84+
const rsaAlias = 'rsa-' + key;
85+
if (kHashNames[rsaAlias] === undefined) {
86+
kHashNames[rsaAlias] = kHashNames[key]!;
87+
}
88+
}
89+
}
7990
}
8091

8192
export function normalizeHashName(

packages/react-native-quick-crypto/test/hashnames.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ test('normalizeHashName happy', () => {
99
expect(normalizeHashName('SHA-512')).toBe('sha512');
1010
});
1111

12+
test('normalizeHashName RSA-* legacy aliases', () => {
13+
expect(normalizeHashName('rsa-sha1')).toBe('sha1');
14+
expect(normalizeHashName('rsa-sha256')).toBe('sha256');
15+
expect(normalizeHashName('rsa-sha384')).toBe('sha384');
16+
expect(normalizeHashName('rsa-sha512')).toBe('sha512');
17+
expect(normalizeHashName('rsa-ripemd160')).toBe('ripemd160');
18+
expect(normalizeHashName('RSA-SHA256')).toBe('sha256');
19+
});
20+
1221
test('normalizeHashName sad', () => {
1322
expect(normalizeHashName('SHA-2')).toBe('sha-2');
1423
expect(normalizeHashName('NOT-a-hash', HashContext.JwkRsaPss)).toBe(

0 commit comments

Comments
 (0)