-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathshim.js
More file actions
208 lines (185 loc) · 7.83 KB
/
shim.js
File metadata and controls
208 lines (185 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import 'cross-fetch/polyfill';
import {DEBUG} from 'react-native-dotenv';
import {install as installCrypto} from 'react-native-quick-crypto';
import {p256} from '@noble/curves/p256';
import {p384} from '@noble/curves/p384';
import {p521} from '@noble/curves/p521';
installCrypto();
// Polyfill subtle.verify for ECDSA on Android
// react-native-quick-crypto passes P1363 (raw r||s) signatures directly to OpenSSL,
// but OpenSSL expects DER-encoded ASN.1 signatures, causing verification failures.
// iOS uses CommonCrypto which handles P1363 natively, so this is Android-only.
// See: HybridEcKeyPair.cpp verify() → EVP_DigestVerifyFinal expects DER
if (global.crypto && global.crypto.subtle) {
const {Platform} = require('react-native');
if (Platform.OS === 'android') {
const originalVerify = global.crypto.subtle.verify.bind(global.crypto.subtle);
// Convert IEEE P1363 signature (r||s) to DER-encoded ASN.1 signature
function p1363ToDer(p1363Bytes, curveByteLength) {
const r = p1363Bytes.slice(0, curveByteLength);
const s = p1363Bytes.slice(curveByteLength, curveByteLength * 2);
function intToDer(intBytes) {
// Strip leading zeros but keep one if high bit is set
let start = 0;
while (start < intBytes.length - 1 && intBytes[start] === 0) {
start++;
}
const trimmed = intBytes.slice(start);
// Prepend 0x00 if high bit set (positive integer in ASN.1)
const needsPadding = trimmed[0] & 0x80;
const len = trimmed.length + (needsPadding ? 1 : 0);
const der = new Uint8Array(2 + len);
der[0] = 0x02; // INTEGER tag
der[1] = len;
if (needsPadding) {
der[2] = 0x00;
der.set(trimmed, 3);
} else {
der.set(trimmed, 2);
}
return der;
}
const rDer = intToDer(r);
const sDer = intToDer(s);
const seqLen = rDer.length + sDer.length;
let header;
if (seqLen < 128) {
header = new Uint8Array([0x30, seqLen]);
} else {
header = new Uint8Array([0x30, 0x81, seqLen]);
}
const result = new Uint8Array(header.length + seqLen);
result.set(header, 0);
result.set(rDer, header.length);
result.set(sDer, header.length + rDer.length);
return result;
}
const ecdsaCurveSizes = {
'P-256': 32,
'P-384': 48,
'P-521': 66,
};
global.crypto.subtle.verify = async function (algorithm, key, signature, data) {
const alg = typeof algorithm === 'string' ? {name: algorithm} : algorithm;
if (alg.name === 'ECDSA' && key.algorithm && key.algorithm.namedCurve) {
const curveByteLength = ecdsaCurveSizes[key.algorithm.namedCurve];
if (curveByteLength) {
const sigBytes = new Uint8Array(signature instanceof ArrayBuffer ? signature : signature.buffer.slice(signature.byteOffset, signature.byteOffset + signature.byteLength));
// Only convert if signature length matches P1363 format (2 * curveByteLength)
if (sigBytes.length === curveByteLength * 2) {
const derSig = p1363ToDer(sigBytes, curveByteLength);
return originalVerify(algorithm, key, derSig.buffer, data);
}
}
}
return originalVerify(algorithm, key, signature, data);
};
}
}
// Polyfill subtle.deriveBits for ECDH (P-256, P-384, P-521)
// react-native-quick-crypto does not implement ECDH deriveBits yet
// See: https://github.com/margelo/react-native-quick-crypto/issues/647
if (global.crypto && global.crypto.subtle) {
const originalDeriveBits = global.crypto.subtle.deriveBits.bind(global.crypto.subtle);
const originalDeriveKey = global.crypto.subtle.deriveKey.bind(global.crypto.subtle);
const curveMap = {
'P-256': {curve: p256, byteLength: 32},
'P-384': {curve: p384, byteLength: 48},
'P-521': {curve: p521, byteLength: 66},
};
function base64UrlToHex(b64u) {
const b64 = b64u.replace(/-/g, '+').replace(/_/g, '/');
const padding = '='.repeat((4 - (b64.length % 4)) % 4);
const binary = atob(b64 + padding);
let hex = '';
for (let i = 0; i < binary.length; i++) {
const byte = binary.charCodeAt(i).toString(16);
hex += byte.length === 1 ? '0' + byte : byte;
}
return hex;
}
global.crypto.subtle.deriveBits = async function (algorithm, baseKey, length) {
if (algorithm && algorithm.name === 'ECDH' && baseKey.algorithm && baseKey.algorithm.namedCurve) {
const namedCurve = baseKey.algorithm.namedCurve;
const curveInfo = curveMap[namedCurve];
if (!curveInfo) {
throw new Error(`ECDH deriveBits polyfill: unsupported curve ${namedCurve}`);
}
function forceExportJwk(key) {
// Bypass extractable check by accessing the internal keyObject handle directly
if (key.keyObject && key.keyObject.handle && key.keyObject.handle.exportJwk) {
const jwk = key.keyObject.handle.exportJwk({key_ops: key.usages || [], ext: true}, true);
if (key.algorithm && key.algorithm.namedCurve) {
jwk.crv = jwk.crv || key.algorithm.namedCurve;
}
return jwk;
}
return global.crypto.subtle.exportKey('jwk', key);
}
const publicKeyJwk = await forceExportJwk(algorithm.public);
const privateKeyJwk = await forceExportJwk(baseKey);
const privateKeyHex = base64UrlToHex(privateKeyJwk.d);
const publicKeyHex = '04' + base64UrlToHex(publicKeyJwk.x) + base64UrlToHex(publicKeyJwk.y);
const sharedSecret = curveInfo.curve.getSharedSecret(privateKeyHex, publicKeyHex, false);
// getSharedSecret returns uncompressed point (04 || x || y), we need just x coordinate
const xBytes = sharedSecret.slice(1, 1 + curveInfo.byteLength);
const resultLength = length ? length / 8 : curveInfo.byteLength;
const result = xBytes.slice(0, resultLength);
return result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength);
}
return originalDeriveBits(algorithm, baseKey, length);
};
global.crypto.subtle.deriveKey = async function (algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages) {
if (algorithm && algorithm.name === 'ECDH' && baseKey.algorithm && baseKey.algorithm.namedCurve) {
const lengthMap = {
'A128GCM': 128,
'A192GCM': 192,
'A256GCM': 256,
'A128CBC-HS256': 256,
'A192CBC-HS384': 384,
'A256CBC-HS512': 512,
};
const bitLength = derivedKeyAlgorithm.length || lengthMap[derivedKeyAlgorithm.name] || 256;
const bits = await global.crypto.subtle.deriveBits(algorithm, baseKey, bitLength);
return global.crypto.subtle.importKey('raw', bits, derivedKeyAlgorithm, extractable, keyUsages);
}
return originalDeriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages);
};
}
if (typeof __dirname === 'undefined') global.__dirname = '/';
if (typeof __filename === 'undefined') global.__filename = '';
if (typeof process === 'undefined') {
global.process = require('process');
} else {
const bProcess = require('process');
for (const p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p];
}
}
}
const debug = require('debug');
process.browser = false;
// if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer;
// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__;
// console.log(JSON.stringify(process.env));
if (typeof process.env['NODE_ENV'] !== 'string') {
// process.env['NODE_ENV'] = isDev !== false ? 'development' : 'production';
}
const level = isDev ? DEBUG ?? '*' : '';
if (typeof window !== 'undefined') {
// @ts-ignore
process.type = 'renderer';
// @ts-ignore
window.localStorage = {
debug: level,
getItem: () => {
return level;
},
};
}
if (isDev) {
debug.log = console.info.bind(console);
debug.enable(level);
}