@opentdf/sdk 0.8.0-beta.71 → 0.8.0-beta.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -30
- package/dist/cjs/src/{nanotdf-crypto → crypto}/ciphers.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/decrypt.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/digest.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/ecdsaSignature.js +2 -2
- package/dist/cjs/src/{nanotdf-crypto → crypto}/encrypt.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/enums.js +1 -1
- package/dist/cjs/src/crypto/exportCryptoKey.js +20 -0
- package/dist/cjs/src/{nanotdf-crypto → crypto}/generateKeyPair.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/generateRandomNumber.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/index.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/keyAgreement.js +1 -1
- package/dist/cjs/src/{nanotdf-crypto → crypto}/pemPublicToCrypto.js +1 -1
- package/dist/cjs/src/opentdf.js +9 -260
- package/dist/cjs/src/utils.js +2 -2
- package/dist/cjs/tdf3/index.js +7 -8
- package/dist/cjs/tdf3/src/models/key-access.js +4 -4
- package/dist/cjs/tdf3/src/tdf.js +4 -4
- package/dist/types/src/crypto/ciphers.d.ts.map +1 -0
- package/dist/types/src/crypto/decrypt.d.ts.map +1 -0
- package/dist/types/src/crypto/digest.d.ts.map +1 -0
- package/dist/types/src/crypto/ecdsaSignature.d.ts.map +1 -0
- package/dist/types/src/crypto/encrypt.d.ts.map +1 -0
- package/dist/types/src/crypto/enums.d.ts.map +1 -0
- package/dist/types/src/crypto/exportCryptoKey.d.ts.map +1 -0
- package/dist/types/src/crypto/generateKeyPair.d.ts.map +1 -0
- package/dist/types/src/crypto/generateRandomNumber.d.ts.map +1 -0
- package/dist/types/src/crypto/index.d.ts.map +1 -0
- package/dist/types/src/crypto/keyAgreement.d.ts.map +1 -0
- package/dist/types/src/crypto/pemPublicToCrypto.d.ts.map +1 -0
- package/dist/types/src/opentdf.d.ts +1 -65
- package/dist/types/src/opentdf.d.ts.map +1 -1
- package/dist/types/tdf3/index.d.ts +4 -3
- package/dist/types/tdf3/index.d.ts.map +1 -1
- package/dist/web/src/crypto/ciphers.js +14 -0
- package/dist/web/src/{nanotdf-crypto → crypto}/decrypt.js +1 -1
- package/dist/web/src/crypto/digest.js +4 -0
- package/dist/web/src/{nanotdf-crypto → crypto}/ecdsaSignature.js +2 -2
- package/dist/web/src/{nanotdf-crypto → crypto}/encrypt.js +1 -1
- package/dist/web/src/{nanotdf-crypto → crypto}/enums.js +1 -1
- package/dist/web/src/crypto/exportCryptoKey.js +17 -0
- package/dist/web/src/crypto/generateKeyPair.js +10 -0
- package/dist/web/src/crypto/generateRandomNumber.js +9 -0
- package/dist/web/src/crypto/index.js +11 -0
- package/dist/web/src/{nanotdf-crypto → crypto}/keyAgreement.js +1 -1
- package/dist/web/src/{nanotdf-crypto → crypto}/pemPublicToCrypto.js +1 -1
- package/dist/web/src/opentdf.js +8 -255
- package/dist/web/src/utils.js +2 -2
- package/dist/web/tdf3/index.js +4 -3
- package/dist/web/tdf3/src/models/key-access.js +4 -4
- package/dist/web/tdf3/src/tdf.js +4 -4
- package/package.json +1 -6
- package/src/{nanotdf-crypto → crypto}/ecdsaSignature.ts +1 -1
- package/src/opentdf.ts +6 -343
- package/src/utils.ts +1 -1
- package/tdf3/index.ts +2 -12
- package/tdf3/src/models/key-access.ts +3 -3
- package/tdf3/src/tdf.ts +3 -3
- package/dist/cjs/src/nanoclients.js +0 -292
- package/dist/cjs/src/nanoindex.js +0 -47
- package/dist/cjs/src/nanotdf/Client.js +0 -315
- package/dist/cjs/src/nanotdf/NanoTDF.js +0 -94
- package/dist/cjs/src/nanotdf/browser-entry.js +0 -19
- package/dist/cjs/src/nanotdf/constants.js +0 -5
- package/dist/cjs/src/nanotdf/decrypt.js +0 -17
- package/dist/cjs/src/nanotdf/encrypt-dataset.js +0 -38
- package/dist/cjs/src/nanotdf/encrypt.js +0 -132
- package/dist/cjs/src/nanotdf/enum/CipherEnum.js +0 -13
- package/dist/cjs/src/nanotdf/enum/CurveNameEnum.js +0 -15
- package/dist/cjs/src/nanotdf/enum/EncodingEnum.js +0 -8
- package/dist/cjs/src/nanotdf/enum/PolicyTypeEnum.js +0 -11
- package/dist/cjs/src/nanotdf/enum/ProtocolEnum.js +0 -10
- package/dist/cjs/src/nanotdf/enum/ResourceLocatorIdentifierEnum.js +0 -11
- package/dist/cjs/src/nanotdf/helpers/calculateByCurve.js +0 -28
- package/dist/cjs/src/nanotdf/helpers/getHkdfSalt.js +0 -11
- package/dist/cjs/src/nanotdf/index.js +0 -25
- package/dist/cjs/src/nanotdf/interfaces/PolicyInterface.js +0 -3
- package/dist/cjs/src/nanotdf/models/Ciphers.js +0 -61
- package/dist/cjs/src/nanotdf/models/DefaultParams.js +0 -27
- package/dist/cjs/src/nanotdf/models/EcCurves.js +0 -39
- package/dist/cjs/src/nanotdf/models/Header.js +0 -255
- package/dist/cjs/src/nanotdf/models/Payload.js +0 -158
- package/dist/cjs/src/nanotdf/models/Policy/AbstractPolicy.js +0 -73
- package/dist/cjs/src/nanotdf/models/Policy/EmbeddedPolicy.js +0 -82
- package/dist/cjs/src/nanotdf/models/Policy/PolicyFactory.js +0 -38
- package/dist/cjs/src/nanotdf/models/Policy/RemotePolicy.js +0 -62
- package/dist/cjs/src/nanotdf/models/ResourceLocator.js +0 -211
- package/dist/cjs/src/nanotdf/models/Signature.js +0 -77
- package/dist/cjs/src/nanotdf-crypto/exportCryptoKey.js +0 -20
- package/dist/cjs/src/tdf/NanoTDF/NanoTDF.js +0 -38
- package/dist/cjs/src/types/index.js +0 -3
- package/dist/types/src/nanoclients.d.ts +0 -106
- package/dist/types/src/nanoclients.d.ts.map +0 -1
- package/dist/types/src/nanoindex.d.ts +0 -5
- package/dist/types/src/nanoindex.d.ts.map +0 -1
- package/dist/types/src/nanotdf/Client.d.ts +0 -91
- package/dist/types/src/nanotdf/Client.d.ts.map +0 -1
- package/dist/types/src/nanotdf/NanoTDF.d.ts +0 -24
- package/dist/types/src/nanotdf/NanoTDF.d.ts.map +0 -1
- package/dist/types/src/nanotdf/browser-entry.d.ts +0 -17
- package/dist/types/src/nanotdf/browser-entry.d.ts.map +0 -1
- package/dist/types/src/nanotdf/constants.d.ts +0 -2
- package/dist/types/src/nanotdf/constants.d.ts.map +0 -1
- package/dist/types/src/nanotdf/decrypt.d.ts +0 -9
- package/dist/types/src/nanotdf/decrypt.d.ts.map +0 -1
- package/dist/types/src/nanotdf/encrypt-dataset.d.ts +0 -11
- package/dist/types/src/nanotdf/encrypt-dataset.d.ts.map +0 -1
- package/dist/types/src/nanotdf/encrypt.d.ts +0 -13
- package/dist/types/src/nanotdf/encrypt.d.ts.map +0 -1
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts +0 -10
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts.map +0 -1
- package/dist/types/src/nanotdf/enum/CurveNameEnum.d.ts +0 -12
- package/dist/types/src/nanotdf/enum/CurveNameEnum.d.ts.map +0 -1
- package/dist/types/src/nanotdf/enum/EncodingEnum.d.ts +0 -5
- package/dist/types/src/nanotdf/enum/EncodingEnum.d.ts.map +0 -1
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts +0 -8
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts.map +0 -1
- package/dist/types/src/nanotdf/enum/ProtocolEnum.d.ts +0 -7
- package/dist/types/src/nanotdf/enum/ProtocolEnum.d.ts.map +0 -1
- package/dist/types/src/nanotdf/enum/ResourceLocatorIdentifierEnum.d.ts +0 -8
- package/dist/types/src/nanotdf/enum/ResourceLocatorIdentifierEnum.d.ts.map +0 -1
- package/dist/types/src/nanotdf/helpers/calculateByCurve.d.ts +0 -20
- package/dist/types/src/nanotdf/helpers/calculateByCurve.d.ts.map +0 -1
- package/dist/types/src/nanotdf/helpers/getHkdfSalt.d.ts +0 -8
- package/dist/types/src/nanotdf/helpers/getHkdfSalt.d.ts.map +0 -1
- package/dist/types/src/nanotdf/index.d.ts +0 -9
- package/dist/types/src/nanotdf/index.d.ts.map +0 -1
- package/dist/types/src/nanotdf/interfaces/PolicyInterface.d.ts +0 -17
- package/dist/types/src/nanotdf/interfaces/PolicyInterface.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Ciphers.d.ts +0 -14
- package/dist/types/src/nanotdf/models/Ciphers.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/DefaultParams.d.ts +0 -21
- package/dist/types/src/nanotdf/models/DefaultParams.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/EcCurves.d.ts +0 -15
- package/dist/types/src/nanotdf/models/EcCurves.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Header.d.ts +0 -73
- package/dist/types/src/nanotdf/models/Header.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Payload.d.ts +0 -47
- package/dist/types/src/nanotdf/models/Payload.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Policy/AbstractPolicy.d.ts +0 -52
- package/dist/types/src/nanotdf/models/Policy/AbstractPolicy.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Policy/EmbeddedPolicy.d.ts +0 -35
- package/dist/types/src/nanotdf/models/Policy/EmbeddedPolicy.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Policy/PolicyFactory.d.ts +0 -11
- package/dist/types/src/nanotdf/models/Policy/PolicyFactory.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Policy/RemotePolicy.d.ts +0 -31
- package/dist/types/src/nanotdf/models/Policy/RemotePolicy.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/ResourceLocator.d.ts +0 -65
- package/dist/types/src/nanotdf/models/ResourceLocator.d.ts.map +0 -1
- package/dist/types/src/nanotdf/models/Signature.d.ts +0 -33
- package/dist/types/src/nanotdf/models/Signature.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/ciphers.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/decrypt.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/digest.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/ecdsaSignature.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/encrypt.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/enums.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/exportCryptoKey.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/generateKeyPair.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/generateRandomNumber.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/index.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/keyAgreement.d.ts.map +0 -1
- package/dist/types/src/nanotdf-crypto/pemPublicToCrypto.d.ts.map +0 -1
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts +0 -99
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts.map +0 -1
- package/dist/types/src/types/index.d.ts +0 -45
- package/dist/types/src/types/index.d.ts.map +0 -1
- package/dist/web/src/nanoclients.js +0 -287
- package/dist/web/src/nanoindex.js +0 -5
- package/dist/web/src/nanotdf/Client.js +0 -310
- package/dist/web/src/nanotdf/NanoTDF.js +0 -89
- package/dist/web/src/nanotdf/browser-entry.js +0 -14
- package/dist/web/src/nanotdf/constants.js +0 -2
- package/dist/web/src/nanotdf/decrypt.js +0 -14
- package/dist/web/src/nanotdf/encrypt-dataset.js +0 -32
- package/dist/web/src/nanotdf/encrypt.js +0 -126
- package/dist/web/src/nanotdf/enum/CipherEnum.js +0 -11
- package/dist/web/src/nanotdf/enum/CurveNameEnum.js +0 -13
- package/dist/web/src/nanotdf/enum/EncodingEnum.js +0 -6
- package/dist/web/src/nanotdf/enum/PolicyTypeEnum.js +0 -9
- package/dist/web/src/nanotdf/enum/ProtocolEnum.js +0 -8
- package/dist/web/src/nanotdf/enum/ResourceLocatorIdentifierEnum.js +0 -9
- package/dist/web/src/nanotdf/helpers/calculateByCurve.js +0 -24
- package/dist/web/src/nanotdf/helpers/getHkdfSalt.js +0 -8
- package/dist/web/src/nanotdf/index.js +0 -11
- package/dist/web/src/nanotdf/interfaces/PolicyInterface.js +0 -2
- package/dist/web/src/nanotdf/models/Ciphers.js +0 -54
- package/dist/web/src/nanotdf/models/DefaultParams.js +0 -22
- package/dist/web/src/nanotdf/models/EcCurves.js +0 -32
- package/dist/web/src/nanotdf/models/Header.js +0 -250
- package/dist/web/src/nanotdf/models/Payload.js +0 -156
- package/dist/web/src/nanotdf/models/Policy/AbstractPolicy.js +0 -71
- package/dist/web/src/nanotdf/models/Policy/EmbeddedPolicy.js +0 -77
- package/dist/web/src/nanotdf/models/Policy/PolicyFactory.js +0 -33
- package/dist/web/src/nanotdf/models/Policy/RemotePolicy.js +0 -57
- package/dist/web/src/nanotdf/models/ResourceLocator.js +0 -206
- package/dist/web/src/nanotdf/models/Signature.js +0 -74
- package/dist/web/src/nanotdf-crypto/ciphers.js +0 -14
- package/dist/web/src/nanotdf-crypto/digest.js +0 -4
- package/dist/web/src/nanotdf-crypto/exportCryptoKey.js +0 -17
- package/dist/web/src/nanotdf-crypto/generateKeyPair.js +0 -10
- package/dist/web/src/nanotdf-crypto/generateRandomNumber.js +0 -9
- package/dist/web/src/nanotdf-crypto/index.js +0 -11
- package/dist/web/src/tdf/NanoTDF/NanoTDF.js +0 -35
- package/dist/web/src/types/index.js +0 -2
- package/src/nanoclients.ts +0 -406
- package/src/nanoindex.ts +0 -4
- package/src/nanotdf/Client.ts +0 -425
- package/src/nanotdf/NanoTDF.ts +0 -120
- package/src/nanotdf/browser-entry.ts +0 -20
- package/src/nanotdf/constants.ts +0 -1
- package/src/nanotdf/decrypt.ts +0 -19
- package/src/nanotdf/encrypt-dataset.ts +0 -51
- package/src/nanotdf/encrypt.ts +0 -196
- package/src/nanotdf/enum/CipherEnum.ts +0 -10
- package/src/nanotdf/enum/CurveNameEnum.ts +0 -12
- package/src/nanotdf/enum/EncodingEnum.ts +0 -5
- package/src/nanotdf/enum/PolicyTypeEnum.ts +0 -8
- package/src/nanotdf/enum/ProtocolEnum.ts +0 -7
- package/src/nanotdf/enum/ResourceLocatorIdentifierEnum.ts +0 -8
- package/src/nanotdf/helpers/calculateByCurve.ts +0 -26
- package/src/nanotdf/helpers/getHkdfSalt.ts +0 -13
- package/src/nanotdf/index.ts +0 -10
- package/src/nanotdf/interfaces/PolicyInterface.ts +0 -27
- package/src/nanotdf/models/Ciphers.ts +0 -67
- package/src/nanotdf/models/DefaultParams.ts +0 -24
- package/src/nanotdf/models/EcCurves.ts +0 -40
- package/src/nanotdf/models/Header.ts +0 -322
- package/src/nanotdf/models/Payload.ts +0 -196
- package/src/nanotdf/models/Policy/AbstractPolicy.ts +0 -90
- package/src/nanotdf/models/Policy/EmbeddedPolicy.ts +0 -101
- package/src/nanotdf/models/Policy/PolicyFactory.ts +0 -48
- package/src/nanotdf/models/Policy/RemotePolicy.ts +0 -74
- package/src/nanotdf/models/ResourceLocator.ts +0 -212
- package/src/nanotdf/models/Signature.ts +0 -85
- package/src/tdf/NanoTDF/NanoTDF.ts +0 -120
- package/src/types/index.ts +0 -55
- /package/dist/types/src/{nanotdf-crypto → crypto}/ciphers.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/decrypt.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/digest.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/ecdsaSignature.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/encrypt.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/enums.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/exportCryptoKey.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/generateKeyPair.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/generateRandomNumber.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/index.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/keyAgreement.d.ts +0 -0
- /package/dist/types/src/{nanotdf-crypto → crypto}/pemPublicToCrypto.d.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/ciphers.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/decrypt.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/digest.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/encrypt.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/enums.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/exportCryptoKey.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/generateKeyPair.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/generateRandomNumber.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/index.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/keyAgreement.ts +0 -0
- /package/src/{nanotdf-crypto → crypto}/pemPublicToCrypto.ts +0 -0
package/src/nanotdf/Client.ts
DELETED
|
@@ -1,425 +0,0 @@
|
|
|
1
|
-
import { create, toJsonString } from '@bufbuild/protobuf';
|
|
2
|
-
import {
|
|
3
|
-
UnsignedRewrapRequest_WithPolicyRequestSchema,
|
|
4
|
-
UnsignedRewrapRequestSchema,
|
|
5
|
-
} from '../platform/kas/kas_pb.js';
|
|
6
|
-
import { generateKeyPair, keyAgreement } from '../nanotdf-crypto/index.js';
|
|
7
|
-
import getHkdfSalt from './helpers/getHkdfSalt.js';
|
|
8
|
-
import DefaultParams from './models/DefaultParams.js';
|
|
9
|
-
import {
|
|
10
|
-
fetchKeyAccessServers,
|
|
11
|
-
fetchWrappedKey,
|
|
12
|
-
KasPublicKeyInfo,
|
|
13
|
-
OriginAllowList,
|
|
14
|
-
} from '../access.js';
|
|
15
|
-
import { handleRpcRewrapErrorString } from '../../src/access/access-rpc.js';
|
|
16
|
-
import { AuthProvider, isAuthProvider, reqSignature } from '../auth/providers.js';
|
|
17
|
-
import { ConfigurationError, DecryptError, TdfError, UnsafeUrlError } from '../errors.js';
|
|
18
|
-
import {
|
|
19
|
-
cryptoPublicToPem,
|
|
20
|
-
getRequiredObligationFQNs,
|
|
21
|
-
pemToCryptoPublicKey,
|
|
22
|
-
upgradeRewrapResponseV1,
|
|
23
|
-
validateSecureUrl,
|
|
24
|
-
getPlatformUrlFromKasEndpoint,
|
|
25
|
-
} from '../utils.js';
|
|
26
|
-
|
|
27
|
-
export interface ClientConfig {
|
|
28
|
-
allowedKases?: string[];
|
|
29
|
-
fulfillableObligationFQNs?: string[];
|
|
30
|
-
ignoreAllowList?: boolean;
|
|
31
|
-
authProvider: AuthProvider;
|
|
32
|
-
dpopEnabled?: boolean;
|
|
33
|
-
dpopKeys?: Promise<CryptoKeyPair>;
|
|
34
|
-
ephemeralKeyPair?: Promise<CryptoKeyPair>;
|
|
35
|
-
kasEndpoint: string;
|
|
36
|
-
platformUrl: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
type RewrapKeyResult = {
|
|
40
|
-
unwrappedKey: CryptoKey;
|
|
41
|
-
requiredObligations: string[];
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
function toJWSAlg(c: CryptoKey): string {
|
|
45
|
-
const { algorithm } = c;
|
|
46
|
-
switch (algorithm.name) {
|
|
47
|
-
case 'RSASSA-PKCS1-v1_5':
|
|
48
|
-
case 'RSA-PSS':
|
|
49
|
-
case 'RSA-OAEP': {
|
|
50
|
-
const r = algorithm as RsaHashedKeyGenParams;
|
|
51
|
-
switch (r.modulusLength) {
|
|
52
|
-
case 2048:
|
|
53
|
-
return 'RS256';
|
|
54
|
-
case 3072:
|
|
55
|
-
return 'RS384';
|
|
56
|
-
case 4096:
|
|
57
|
-
return 'RS512';
|
|
58
|
-
}
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
case 'ECDSA':
|
|
62
|
-
case 'ECDH': {
|
|
63
|
-
return 'ES256';
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
throw new ConfigurationError(`unsupported key algorithm ${JSON.stringify(algorithm)}`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async function generateEphemeralKeyPair(): Promise<CryptoKeyPair> {
|
|
70
|
-
const { publicKey, privateKey } = await generateKeyPair();
|
|
71
|
-
if (!privateKey || !publicKey) {
|
|
72
|
-
throw Error('Key pair generation failed');
|
|
73
|
-
}
|
|
74
|
-
return { publicKey, privateKey };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async function generateSignerKeyPair(): Promise<CryptoKeyPair> {
|
|
78
|
-
return crypto.subtle.generateKey(
|
|
79
|
-
{
|
|
80
|
-
name: 'RSASSA-PKCS1-v1_5',
|
|
81
|
-
hash: 'SHA-256',
|
|
82
|
-
modulusLength: 2048,
|
|
83
|
-
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
|
84
|
-
},
|
|
85
|
-
true,
|
|
86
|
-
['sign', 'verify']
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* A Client encapsulates sessions interacting with TDF3 and nanoTDF backends, KAS and any
|
|
92
|
-
* plugin-based sessions like identity and further attribute control. Most importantly, it is responsible
|
|
93
|
-
* for local key and token management, including the ephemeral public/private keypairs
|
|
94
|
-
* used for encrypting and decrypting information.
|
|
95
|
-
*
|
|
96
|
-
* @link https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* import { Client, clientAuthProvider, decrypt, encrypt } from '@opentdf/sdk/nanotdf`
|
|
100
|
-
*
|
|
101
|
-
* const OIDC_ENDPOINT = 'http://localhost:65432/auth/';
|
|
102
|
-
* const KAS_URL = 'http://localhost:65432/kas';
|
|
103
|
-
*
|
|
104
|
-
* let client = new Client(
|
|
105
|
-
* await clientAuthProvider({
|
|
106
|
-
* clientId: 'tdf-client',
|
|
107
|
-
* clientSecret: '123-456',
|
|
108
|
-
* oidcOrigin: OIDC_ENDPOINT,
|
|
109
|
-
* }),
|
|
110
|
-
* KAS_URL
|
|
111
|
-
* );
|
|
112
|
-
*
|
|
113
|
-
* // t=1
|
|
114
|
-
* let nanoTDFEncrypted = await encrypt('some string', client.unwrappedKey);
|
|
115
|
-
* let nanoTDFDecrypted = await decrypt(nanoTDFEncrypted, client.unwrappedKey);
|
|
116
|
-
* nanoTDFDecrypted.toString() // 'some string'
|
|
117
|
-
*
|
|
118
|
-
*/
|
|
119
|
-
export default class Client {
|
|
120
|
-
static readonly KEY_ACCESS_REMOTE = 'remote';
|
|
121
|
-
static readonly KAS_PROTOCOL = 'kas';
|
|
122
|
-
static readonly SDK_INITIAL_RELEASE = '0.0.0';
|
|
123
|
-
static readonly INITIAL_RELEASE_IV_SIZE = 3;
|
|
124
|
-
static readonly IV_SIZE = 12;
|
|
125
|
-
|
|
126
|
-
allowedKases?: OriginAllowList;
|
|
127
|
-
readonly fulfillableObligationFQNs: string[];
|
|
128
|
-
/*
|
|
129
|
-
These variables are expected to be either assigned during initialization or within the methods.
|
|
130
|
-
This is needed as the flow is very specific. Errors should be thrown if the necessary step is not completed.
|
|
131
|
-
*/
|
|
132
|
-
protected kasUrl: string;
|
|
133
|
-
readonly platformUrl: string;
|
|
134
|
-
kasPubKey?: KasPublicKeyInfo;
|
|
135
|
-
readonly authProvider: AuthProvider;
|
|
136
|
-
readonly dpopEnabled: boolean;
|
|
137
|
-
dissems: string[] = [];
|
|
138
|
-
dataAttributes: string[] = [];
|
|
139
|
-
protected ephemeralKeyPair: Promise<CryptoKeyPair>;
|
|
140
|
-
protected requestSignerKeyPair: Promise<CryptoKeyPair>;
|
|
141
|
-
protected iv?: number;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Create new NanoTDF Client
|
|
145
|
-
*
|
|
146
|
-
* The Ephemeral Key Pair can either be provided or will be generate when fetching the entity object. Once set it
|
|
147
|
-
* cannot be changed. If a new ephemeral key is desired it a new client should be initialized.
|
|
148
|
-
* There is no performance impact for creating a new client IFF the ephemeral key pair is provided.
|
|
149
|
-
*/
|
|
150
|
-
constructor(
|
|
151
|
-
optsOrOldAuthProvider: AuthProvider | ClientConfig,
|
|
152
|
-
kasUrl?: string,
|
|
153
|
-
ephemeralKeyPair?: CryptoKeyPair,
|
|
154
|
-
dpopEnabled = false
|
|
155
|
-
) {
|
|
156
|
-
const enwrapAuthProvider = (a: AuthProvider): AuthProvider => {
|
|
157
|
-
return {
|
|
158
|
-
updateClientPublicKey: async (signingKey) => {
|
|
159
|
-
await a.updateClientPublicKey(signingKey);
|
|
160
|
-
},
|
|
161
|
-
withCreds: async (httpReq) => {
|
|
162
|
-
const signer = await this.requestSignerKeyPair;
|
|
163
|
-
if (!signer) {
|
|
164
|
-
throw new ConfigurationError('failed to find or generate signer session key');
|
|
165
|
-
}
|
|
166
|
-
await a.updateClientPublicKey(signer);
|
|
167
|
-
return a.withCreds(httpReq);
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
|
-
};
|
|
171
|
-
if (isAuthProvider(optsOrOldAuthProvider)) {
|
|
172
|
-
this.authProvider = enwrapAuthProvider(optsOrOldAuthProvider);
|
|
173
|
-
if (!kasUrl) {
|
|
174
|
-
throw new ConfigurationError('please specify kasEndpoint');
|
|
175
|
-
}
|
|
176
|
-
// TODO Disallow http KAS. For now just log as error
|
|
177
|
-
validateSecureUrl(kasUrl);
|
|
178
|
-
this.kasUrl = kasUrl;
|
|
179
|
-
this.dpopEnabled = dpopEnabled;
|
|
180
|
-
|
|
181
|
-
if (ephemeralKeyPair) {
|
|
182
|
-
this.ephemeralKeyPair = Promise.resolve(ephemeralKeyPair);
|
|
183
|
-
} else {
|
|
184
|
-
this.ephemeralKeyPair = generateEphemeralKeyPair();
|
|
185
|
-
}
|
|
186
|
-
this.iv = 1;
|
|
187
|
-
} else {
|
|
188
|
-
const {
|
|
189
|
-
allowedKases,
|
|
190
|
-
fulfillableObligationFQNs = [],
|
|
191
|
-
ignoreAllowList,
|
|
192
|
-
authProvider,
|
|
193
|
-
dpopEnabled,
|
|
194
|
-
dpopKeys,
|
|
195
|
-
ephemeralKeyPair,
|
|
196
|
-
kasEndpoint,
|
|
197
|
-
platformUrl,
|
|
198
|
-
} = optsOrOldAuthProvider;
|
|
199
|
-
this.authProvider = enwrapAuthProvider(authProvider);
|
|
200
|
-
// TODO Disallow http KAS. For now just log as error
|
|
201
|
-
validateSecureUrl(kasEndpoint);
|
|
202
|
-
this.kasUrl = kasEndpoint;
|
|
203
|
-
this.platformUrl = platformUrl;
|
|
204
|
-
if (allowedKases?.length || ignoreAllowList) {
|
|
205
|
-
this.allowedKases = new OriginAllowList(allowedKases || [], ignoreAllowList);
|
|
206
|
-
}
|
|
207
|
-
this.fulfillableObligationFQNs = fulfillableObligationFQNs;
|
|
208
|
-
this.dpopEnabled = !!dpopEnabled;
|
|
209
|
-
if (dpopKeys) {
|
|
210
|
-
this.requestSignerKeyPair = dpopKeys;
|
|
211
|
-
} else {
|
|
212
|
-
this.requestSignerKeyPair = generateSignerKeyPair();
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (ephemeralKeyPair) {
|
|
216
|
-
this.ephemeralKeyPair = ephemeralKeyPair;
|
|
217
|
-
} else {
|
|
218
|
-
this.ephemeralKeyPair = generateEphemeralKeyPair();
|
|
219
|
-
}
|
|
220
|
-
this.iv = 1;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Add attribute to the TDF file/data
|
|
226
|
-
*
|
|
227
|
-
* @param attribute The attribute that decides the access control of the TDF.
|
|
228
|
-
*/
|
|
229
|
-
addAttribute(attribute: string): void {
|
|
230
|
-
this.dataAttributes.push(attribute);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Rewrap key
|
|
235
|
-
*
|
|
236
|
-
* @important the `fetchEntityObject` method must be called prior to
|
|
237
|
-
* @param nanoTdfHeader the full header for the nanotdf
|
|
238
|
-
* @param kasRewrapUrl key access server's rewrap endpoint
|
|
239
|
-
* @param magicNumberVersion nanotdf container version
|
|
240
|
-
* @param clientVersion version of the client, as SemVer
|
|
241
|
-
*/
|
|
242
|
-
async rewrapKey(
|
|
243
|
-
nanoTdfHeader: ArrayBufferLike,
|
|
244
|
-
kasRewrapUrl: string,
|
|
245
|
-
magicNumberVersion: ArrayBufferLike,
|
|
246
|
-
clientVersion: string
|
|
247
|
-
): Promise<RewrapKeyResult> {
|
|
248
|
-
let allowedKases = this.allowedKases;
|
|
249
|
-
|
|
250
|
-
if (!allowedKases) {
|
|
251
|
-
allowedKases = await fetchKeyAccessServers(this.platformUrl, this.authProvider);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (!allowedKases.allows(kasRewrapUrl)) {
|
|
255
|
-
throw new UnsafeUrlError(`request URL ∉ ${allowedKases.origins};`, kasRewrapUrl);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const ephemeralKeyPair = await this.ephemeralKeyPair;
|
|
259
|
-
const requestSignerKeyPair = await this.requestSignerKeyPair;
|
|
260
|
-
|
|
261
|
-
// Ensure the ephemeral key pair has been set or generated (see fetchEntityObject)
|
|
262
|
-
if (!ephemeralKeyPair?.privateKey) {
|
|
263
|
-
throw new ConfigurationError('Ephemeral key has not been set or generated');
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (!requestSignerKeyPair?.privateKey) {
|
|
267
|
-
throw new ConfigurationError('Signer key has not been set or generated');
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
const unsignedRequest = create(UnsignedRewrapRequestSchema, {
|
|
271
|
-
clientPublicKey: await cryptoPublicToPem(ephemeralKeyPair.publicKey),
|
|
272
|
-
requests: [
|
|
273
|
-
create(UnsignedRewrapRequest_WithPolicyRequestSchema, {
|
|
274
|
-
keyAccessObjects: [
|
|
275
|
-
{
|
|
276
|
-
keyAccessObjectId: 'kao-0', // only one kao, no bulk
|
|
277
|
-
keyAccessObject: {
|
|
278
|
-
header: new Uint8Array(nanoTdfHeader),
|
|
279
|
-
kasUrl: '',
|
|
280
|
-
protocol: Client.KAS_PROTOCOL,
|
|
281
|
-
keyType: Client.KEY_ACCESS_REMOTE,
|
|
282
|
-
},
|
|
283
|
-
},
|
|
284
|
-
],
|
|
285
|
-
algorithm: DefaultParams.defaultECAlgorithm,
|
|
286
|
-
}),
|
|
287
|
-
],
|
|
288
|
-
keyAccess: {
|
|
289
|
-
header: new Uint8Array(nanoTdfHeader),
|
|
290
|
-
kasUrl: '',
|
|
291
|
-
protocol: Client.KAS_PROTOCOL,
|
|
292
|
-
keyType: Client.KEY_ACCESS_REMOTE,
|
|
293
|
-
},
|
|
294
|
-
algorithm: DefaultParams.defaultECAlgorithm,
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
const requestBodyStr = toJsonString(UnsignedRewrapRequestSchema, unsignedRequest);
|
|
298
|
-
|
|
299
|
-
const jwtPayload = { requestBody: requestBodyStr };
|
|
300
|
-
|
|
301
|
-
const signedRequestToken = await reqSignature(jwtPayload, requestSignerKeyPair.privateKey, {
|
|
302
|
-
alg: toJWSAlg(requestSignerKeyPair.publicKey),
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
// Wrapped
|
|
306
|
-
const rewrapResp = await fetchWrappedKey(
|
|
307
|
-
kasRewrapUrl,
|
|
308
|
-
signedRequestToken,
|
|
309
|
-
this.authProvider,
|
|
310
|
-
this.fulfillableObligationFQNs
|
|
311
|
-
);
|
|
312
|
-
// Upgrade any V1 responses to V2
|
|
313
|
-
upgradeRewrapResponseV1(rewrapResp);
|
|
314
|
-
|
|
315
|
-
const result = rewrapResp.responses?.[0]?.results?.[0];
|
|
316
|
-
if (!result) {
|
|
317
|
-
// This should not happen - KAS should always return at least one response and one result
|
|
318
|
-
// or the upgradeRewrapResponseV1 should have created them
|
|
319
|
-
throw new DecryptError('KAS rewrap response missing expected response or result');
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
const requiredObligations = getRequiredObligationFQNs(rewrapResp);
|
|
323
|
-
|
|
324
|
-
let entityWrappedKey: Uint8Array<ArrayBufferLike>;
|
|
325
|
-
switch (result.result.case) {
|
|
326
|
-
case 'kasWrappedKey': {
|
|
327
|
-
entityWrappedKey = result.result.value;
|
|
328
|
-
break;
|
|
329
|
-
}
|
|
330
|
-
case 'error': {
|
|
331
|
-
handleRpcRewrapErrorString(
|
|
332
|
-
result.result.value,
|
|
333
|
-
getPlatformUrlFromKasEndpoint(kasRewrapUrl),
|
|
334
|
-
requiredObligations
|
|
335
|
-
);
|
|
336
|
-
}
|
|
337
|
-
default: {
|
|
338
|
-
throw new DecryptError('KAS rewrap response missing wrapped key');
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// Extract the iv and ciphertext
|
|
343
|
-
const ivLength =
|
|
344
|
-
clientVersion == Client.SDK_INITIAL_RELEASE ? Client.INITIAL_RELEASE_IV_SIZE : Client.IV_SIZE;
|
|
345
|
-
const iv = entityWrappedKey.subarray(0, ivLength);
|
|
346
|
-
const encryptedSharedKey = entityWrappedKey.subarray(ivLength);
|
|
347
|
-
|
|
348
|
-
let kasPublicKey;
|
|
349
|
-
try {
|
|
350
|
-
// Let us import public key as a cert or public key
|
|
351
|
-
kasPublicKey = await pemToCryptoPublicKey(rewrapResp.sessionPublicKey);
|
|
352
|
-
} catch (cause) {
|
|
353
|
-
throw new ConfigurationError(
|
|
354
|
-
`internal: [${kasRewrapUrl}] PEM Public Key to crypto public key failed. Is PEM formatted correctly?`,
|
|
355
|
-
cause
|
|
356
|
-
);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
let hkdfSalt;
|
|
360
|
-
try {
|
|
361
|
-
// Get the hkdf salt params
|
|
362
|
-
hkdfSalt = await getHkdfSalt(magicNumberVersion);
|
|
363
|
-
} catch (e) {
|
|
364
|
-
throw new TdfError('salting hkdf failed', e);
|
|
365
|
-
}
|
|
366
|
-
const { privateKey } = await this.ephemeralKeyPair;
|
|
367
|
-
|
|
368
|
-
// Get the unwrapping key
|
|
369
|
-
let unwrappingKey;
|
|
370
|
-
try {
|
|
371
|
-
unwrappingKey = await keyAgreement(
|
|
372
|
-
// Ephemeral private key
|
|
373
|
-
privateKey,
|
|
374
|
-
kasPublicKey,
|
|
375
|
-
hkdfSalt
|
|
376
|
-
);
|
|
377
|
-
} catch (e) {
|
|
378
|
-
if (e.name == 'InvalidAccessError' || e.name == 'OperationError') {
|
|
379
|
-
throw new DecryptError('unable to solve key agreement', e);
|
|
380
|
-
} else if (e.name == 'NotSupported') {
|
|
381
|
-
throw new ConfigurationError('unable to unwrap key from kas', e);
|
|
382
|
-
}
|
|
383
|
-
throw new TdfError('unable to reach agreement', e);
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
const authTagLength = 8 * (encryptedSharedKey.byteLength - 32);
|
|
387
|
-
let decryptedKey;
|
|
388
|
-
try {
|
|
389
|
-
// Decrypt the wrapped key
|
|
390
|
-
decryptedKey = await crypto.subtle.decrypt(
|
|
391
|
-
{ name: 'AES-GCM', iv, tagLength: authTagLength },
|
|
392
|
-
unwrappingKey,
|
|
393
|
-
encryptedSharedKey
|
|
394
|
-
);
|
|
395
|
-
} catch (cause) {
|
|
396
|
-
throw new DecryptError(
|
|
397
|
-
`unable to decrypt key. Are you using the right KAS? Is the salt correct?`,
|
|
398
|
-
cause
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
// UnwrappedKey
|
|
403
|
-
let unwrappedKey;
|
|
404
|
-
try {
|
|
405
|
-
unwrappedKey = await crypto.subtle.importKey(
|
|
406
|
-
'raw',
|
|
407
|
-
decryptedKey,
|
|
408
|
-
'AES-GCM',
|
|
409
|
-
// @security This allows the key to be used in `exportKey` and `wrapKey`
|
|
410
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
|
|
411
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/wrapKey
|
|
412
|
-
true,
|
|
413
|
-
// Want to use the key to encrypt and decrypt. Signing key will be used later.
|
|
414
|
-
['encrypt', 'decrypt']
|
|
415
|
-
);
|
|
416
|
-
} catch (cause) {
|
|
417
|
-
throw new DecryptError('Unable to import raw key.', cause);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
return {
|
|
421
|
-
requiredObligations,
|
|
422
|
-
unwrappedKey: unwrappedKey,
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
}
|
package/src/nanotdf/NanoTDF.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { base64 } from '../encodings/index.js';
|
|
2
|
-
import Header from './models/Header.js';
|
|
3
|
-
import Payload from './models/Payload.js';
|
|
4
|
-
import Signature from './models/Signature.js';
|
|
5
|
-
import EncodingEnum from './enum/EncodingEnum.js';
|
|
6
|
-
import { ConfigurationError, InvalidFileError } from '../errors.js';
|
|
7
|
-
|
|
8
|
-
// Defaults when none set during encryption
|
|
9
|
-
|
|
10
|
-
export default class NanoTDF {
|
|
11
|
-
// Add encodings to the NanoTDF class for easy access
|
|
12
|
-
static Encodings: typeof EncodingEnum = EncodingEnum;
|
|
13
|
-
static Header = Header;
|
|
14
|
-
static Payload = Payload;
|
|
15
|
-
static Signature = Signature;
|
|
16
|
-
|
|
17
|
-
public header: Header;
|
|
18
|
-
public payload: Payload;
|
|
19
|
-
|
|
20
|
-
// TODO: This should be optional
|
|
21
|
-
public signature?: Signature;
|
|
22
|
-
|
|
23
|
-
static from(
|
|
24
|
-
content: ArrayBufferLike | string,
|
|
25
|
-
encoding?: EncodingEnum,
|
|
26
|
-
legacyTDF = false
|
|
27
|
-
): NanoTDF {
|
|
28
|
-
// If we don't assign an empty array buffer then TS reports buffer as unassigned
|
|
29
|
-
let buffer;
|
|
30
|
-
if (typeof content === 'string') {
|
|
31
|
-
if (!encoding || encoding === EncodingEnum.Base64) {
|
|
32
|
-
buffer = base64.decodeArrayBuffer(content);
|
|
33
|
-
} else {
|
|
34
|
-
throw new ConfigurationError(`Unsupported encoding: ${encoding}`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
// Handle Uint8Array types
|
|
38
|
-
else if (ArrayBuffer.isView(content) || content instanceof ArrayBuffer) {
|
|
39
|
-
buffer = content;
|
|
40
|
-
} else {
|
|
41
|
-
throw new ConfigurationError(`unsupported content type`);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const dataView = new Uint8Array(buffer);
|
|
45
|
-
let offset = 0;
|
|
46
|
-
|
|
47
|
-
// Header
|
|
48
|
-
const { header, offset: headerOffset } = Header.parse(dataView.subarray(offset));
|
|
49
|
-
offset += headerOffset;
|
|
50
|
-
|
|
51
|
-
// Payload
|
|
52
|
-
const { payload, offset: payloadOffset } = Payload.parse(
|
|
53
|
-
header,
|
|
54
|
-
dataView.subarray(offset),
|
|
55
|
-
legacyTDF
|
|
56
|
-
);
|
|
57
|
-
offset += payloadOffset;
|
|
58
|
-
|
|
59
|
-
// Signature
|
|
60
|
-
const { signature, offset: signatureOffset } = Signature.parse(
|
|
61
|
-
header,
|
|
62
|
-
dataView.subarray(offset)
|
|
63
|
-
);
|
|
64
|
-
offset += signatureOffset;
|
|
65
|
-
|
|
66
|
-
// Singature checking
|
|
67
|
-
if (!header.hasSignature && signature.length > 0) {
|
|
68
|
-
throw new InvalidFileError("Found signature when there shouldn't be one");
|
|
69
|
-
}
|
|
70
|
-
if (header.hasSignature && signature.length === 0) {
|
|
71
|
-
throw new InvalidFileError('Could not find signature');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return new NanoTDF(header, payload, signature);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
constructor(header: Header, payload: Payload, signature?: Signature) {
|
|
78
|
-
this.header = header;
|
|
79
|
-
this.payload = payload;
|
|
80
|
-
this.signature = signature;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Return the content of nano tdf as binary buffer
|
|
85
|
-
*/
|
|
86
|
-
toBuffer(): ArrayBuffer {
|
|
87
|
-
let offset = 0;
|
|
88
|
-
|
|
89
|
-
const lengthOfSignature = this.signature && this.signature.length ? this.signature.length : 0;
|
|
90
|
-
const lengthOfTDF = this.header.length + this.payload.length + lengthOfSignature;
|
|
91
|
-
|
|
92
|
-
const buffer = new ArrayBuffer(lengthOfTDF);
|
|
93
|
-
|
|
94
|
-
// Write the header
|
|
95
|
-
const headerBufferView = new Uint8Array(buffer, 0, this.header.length);
|
|
96
|
-
this.header.copyToBuffer(headerBufferView);
|
|
97
|
-
offset += headerBufferView.length;
|
|
98
|
-
|
|
99
|
-
// Write the payload
|
|
100
|
-
const payloadBufferView = new Uint8Array(buffer, offset, this.payload.length);
|
|
101
|
-
this.payload.copyToBuffer(payloadBufferView);
|
|
102
|
-
offset += payloadBufferView.length;
|
|
103
|
-
|
|
104
|
-
// Write the signature
|
|
105
|
-
if (this.header.hasSignature && this.signature) {
|
|
106
|
-
const signatureBufferView = new Uint8Array(buffer, offset, lengthOfSignature);
|
|
107
|
-
this.signature.copyToBuffer(signatureBufferView);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return buffer;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Return the content of nano tdf as base64 string
|
|
115
|
-
*/
|
|
116
|
-
toBase64(): string {
|
|
117
|
-
const arrayBuffer = this.toBuffer();
|
|
118
|
-
return base64.encodeArrayBuffer(arrayBuffer);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/* globals window */
|
|
2
|
-
/**
|
|
3
|
-
* This file is used for:
|
|
4
|
-
*
|
|
5
|
-
* - es5 browser version of nanoTDF and add it to the window as NanoTDF
|
|
6
|
-
*
|
|
7
|
-
* This is not used for:
|
|
8
|
-
*
|
|
9
|
-
* - es6 web development (use node modules)
|
|
10
|
-
* - node applications
|
|
11
|
-
*/
|
|
12
|
-
import NanoTDF from './NanoTDF.js';
|
|
13
|
-
|
|
14
|
-
declare global {
|
|
15
|
-
interface Window {
|
|
16
|
-
NanoTDF: typeof NanoTDF;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
window.NanoTDF = NanoTDF;
|
package/src/nanotdf/constants.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const GMAC_BINDING_LEN = 8;
|
package/src/nanotdf/decrypt.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { decrypt as cryptoDecrypt } from '../nanotdf-crypto/index.js';
|
|
2
|
-
import type NanoTDF from './NanoTDF.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Decrypt nanotdf with a crypto key
|
|
6
|
-
*
|
|
7
|
-
* @param key Crypto key used to decrypt nanotdf
|
|
8
|
-
* @param nanotdf NanoTDF to decrypt
|
|
9
|
-
*/
|
|
10
|
-
export default async function decrypt(key: CryptoKey, nanotdf: NanoTDF): Promise<ArrayBuffer> {
|
|
11
|
-
// console.log(`Decrypting for content: ${nanotdf}`, nanotdf.header.authTagLength);
|
|
12
|
-
return await cryptoDecrypt(
|
|
13
|
-
key,
|
|
14
|
-
nanotdf.payload.ciphertextWithAuthTag,
|
|
15
|
-
nanotdf.payload.iv,
|
|
16
|
-
// Auth tag length in bits
|
|
17
|
-
nanotdf.header.authTagLength
|
|
18
|
-
);
|
|
19
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import NanoTDF from './NanoTDF.js';
|
|
2
|
-
import Header from './models/Header.js';
|
|
3
|
-
import DefaultParams from './models/DefaultParams.js';
|
|
4
|
-
import Payload from './models/Payload.js';
|
|
5
|
-
import { getBitLength as authTagLengthForCipher } from './models/Ciphers.js';
|
|
6
|
-
import encrypt from '../nanotdf-crypto/encrypt.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Encrypt the plain data into nanotdf buffer
|
|
10
|
-
*
|
|
11
|
-
* @param symmetricKey Key to encrypt the payload
|
|
12
|
-
* @param header NanoTDF header
|
|
13
|
-
* @param iv IV to be used for encrypting the payload
|
|
14
|
-
* @param data The data to be encrypted
|
|
15
|
-
*/
|
|
16
|
-
export default async function encryptDataset(
|
|
17
|
-
symmetricKey: CryptoKey,
|
|
18
|
-
header: Header,
|
|
19
|
-
iv: Uint8Array,
|
|
20
|
-
data: string | ArrayBufferLike
|
|
21
|
-
): Promise<ArrayBuffer> {
|
|
22
|
-
// Auth tag length for policy and payload
|
|
23
|
-
const authTagLengthInBytes = authTagLengthForCipher(DefaultParams.symmetricCipher) / 8;
|
|
24
|
-
|
|
25
|
-
// Encrypt the payload
|
|
26
|
-
let payloadAsBuffer;
|
|
27
|
-
if (typeof data === 'string') {
|
|
28
|
-
payloadAsBuffer = new TextEncoder().encode(data);
|
|
29
|
-
} else {
|
|
30
|
-
payloadAsBuffer = data;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const encryptedPayload = await encrypt(
|
|
34
|
-
symmetricKey,
|
|
35
|
-
new Uint8Array(payloadAsBuffer),
|
|
36
|
-
iv,
|
|
37
|
-
authTagLengthInBytes * 8
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
// Create payload
|
|
41
|
-
const payload = new Payload(
|
|
42
|
-
iv.slice(-3),
|
|
43
|
-
new Uint8Array(encryptedPayload.slice(0, -authTagLengthInBytes)),
|
|
44
|
-
new Uint8Array(encryptedPayload.slice(-authTagLengthInBytes))
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
// Create a nanotdf.
|
|
48
|
-
const nanoTDF = new NanoTDF(header, payload);
|
|
49
|
-
|
|
50
|
-
return nanoTDF.toBuffer();
|
|
51
|
-
}
|