@opentdf/sdk 0.2.0-beta.1758 → 0.2.0
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 +45 -38
- package/dist/cjs/src/access.js +47 -11
- package/dist/cjs/src/auth/auth.js +5 -5
- package/dist/cjs/src/auth/oidc-clientcredentials-provider.js +1 -1
- package/dist/cjs/src/auth/oidc-externaljwt-provider.js +1 -1
- package/dist/cjs/src/auth/oidc-refreshtoken-provider.js +1 -1
- package/dist/cjs/src/auth/oidc.js +1 -1
- package/dist/cjs/src/auth/providers.js +1 -1
- package/dist/cjs/src/concurrency.js +3 -4
- package/dist/cjs/src/encodings/base64.js +4 -4
- package/dist/cjs/src/encodings/hex.js +5 -6
- package/dist/cjs/src/encodings/index.js +18 -8
- package/dist/cjs/src/errors.js +1 -1
- package/dist/cjs/src/index.js +28 -318
- package/dist/cjs/src/nanoclients.js +285 -0
- package/dist/cjs/src/nanoindex.js +47 -0
- package/dist/cjs/src/nanotdf/Client.js +18 -8
- package/dist/cjs/src/nanotdf/NanoTDF.js +1 -1
- package/dist/cjs/src/nanotdf/decrypt.js +2 -2
- package/dist/cjs/src/nanotdf/encrypt-dataset.js +2 -2
- package/dist/cjs/src/nanotdf/encrypt.js +2 -2
- package/dist/cjs/src/nanotdf/helpers/calculateByCurve.js +3 -4
- package/dist/cjs/src/nanotdf/helpers/getHkdfSalt.js +2 -2
- package/dist/cjs/src/nanotdf/models/Ciphers.js +3 -3
- package/dist/cjs/src/nanotdf/models/EcCurves.js +3 -3
- package/dist/cjs/src/nanotdf/models/Header.js +1 -1
- package/dist/cjs/src/nanotdf/models/Payload.js +1 -1
- package/dist/cjs/src/nanotdf/models/Policy/AbstractPolicy.js +1 -1
- package/dist/cjs/src/nanotdf/models/Policy/EmbeddedPolicy.js +1 -1
- package/dist/cjs/src/nanotdf/models/Policy/PolicyFactory.js +1 -1
- package/dist/cjs/src/nanotdf/models/ResourceLocator.js +1 -1
- package/dist/cjs/src/nanotdf/models/Signature.js +1 -1
- package/dist/cjs/src/nanotdf-crypto/ciphers.js +1 -1
- package/dist/cjs/src/nanotdf-crypto/decrypt.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/digest.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/ecdsaSignature.js +4 -5
- package/dist/cjs/src/nanotdf-crypto/encrypt.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/exportCryptoKey.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/generateKeyPair.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/generateRandomNumber.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/index.js +21 -13
- package/dist/cjs/src/nanotdf-crypto/keyAgreement.js +10 -8
- package/dist/cjs/src/nanotdf-crypto/pemPublicToCrypto.js +20 -11
- package/dist/cjs/src/opentdf.js +251 -0
- package/dist/cjs/src/policy/api.js +2 -3
- package/dist/cjs/src/policy/granter.js +3 -4
- package/dist/cjs/src/seekable.js +157 -0
- package/dist/cjs/src/tdf/AttributeObject.js +2 -4
- package/dist/cjs/src/tdf/Policy.js +1 -2
- package/dist/cjs/src/utils.js +12 -14
- package/dist/cjs/src/version.js +6 -2
- package/dist/cjs/tdf3/index.js +27 -15
- package/dist/cjs/tdf3/src/assertions.js +25 -11
- package/dist/cjs/tdf3/src/binary.js +1 -1
- package/dist/cjs/tdf3/src/ciphers/aes-gcm-cipher.js +1 -1
- package/dist/cjs/tdf3/src/ciphers/symmetric-cipher-base.js +1 -1
- package/dist/cjs/tdf3/src/client/DecoratedReadableStream.js +7 -74
- package/dist/cjs/tdf3/src/client/builders.js +26 -22
- package/dist/cjs/tdf3/src/client/index.js +88 -61
- package/dist/cjs/tdf3/src/client/validation.js +3 -3
- package/dist/cjs/tdf3/src/crypto/crypto-utils.js +1 -1
- package/dist/cjs/tdf3/src/crypto/index.js +18 -18
- package/dist/cjs/tdf3/src/index.js +22 -11
- package/dist/cjs/tdf3/src/models/attribute-set.js +1 -1
- package/dist/cjs/tdf3/src/models/encryption-information.js +3 -3
- package/dist/cjs/tdf3/src/models/key-access.js +67 -35
- package/dist/cjs/tdf3/src/models/policy.js +3 -3
- package/dist/cjs/tdf3/src/tdf.js +177 -151
- package/dist/cjs/tdf3/src/utils/buffer-crc32.js +2 -3
- package/dist/cjs/tdf3/src/utils/index.js +30 -28
- package/dist/cjs/tdf3/src/utils/keysplit.js +4 -5
- package/dist/cjs/tdf3/src/utils/unwrap.js +21 -0
- package/dist/cjs/tdf3/src/utils/zip-reader.js +4 -4
- package/dist/cjs/tdf3/src/utils/zip-writer.js +4 -4
- package/dist/types/src/access.d.ts +3 -0
- package/dist/types/src/access.d.ts.map +1 -1
- package/dist/types/src/auth/providers.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +5 -136
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/nanoclients.d.ts +107 -0
- package/dist/types/src/nanoclients.d.ts.map +1 -0
- package/dist/types/src/nanoindex.d.ts +5 -0
- package/dist/types/src/nanoindex.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts +1 -1
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts.map +1 -1
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts +1 -1
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts.map +1 -1
- package/dist/types/src/nanotdf/models/DefaultParams.d.ts +1 -1
- package/dist/types/src/nanotdf/models/ResourceLocator.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/generateKeyPair.d.ts +1 -1
- package/dist/types/src/nanotdf-crypto/generateKeyPair.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/generateRandomNumber.d.ts +1 -1
- package/dist/types/src/nanotdf-crypto/generateRandomNumber.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/index.d.ts +2 -3
- package/dist/types/src/nanotdf-crypto/index.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/keyAgreement.d.ts.map +1 -1
- package/dist/types/src/opentdf.d.ts +110 -0
- package/dist/types/src/opentdf.d.ts.map +1 -0
- package/dist/types/src/seekable.d.ts +39 -0
- package/dist/types/src/seekable.d.ts.map +1 -0
- package/dist/types/src/tdf/AttributeObject.d.ts +0 -2
- package/dist/types/src/tdf/AttributeObject.d.ts.map +1 -1
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts +2 -2
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts.map +1 -1
- package/dist/types/src/tdf/Policy.d.ts.map +1 -1
- package/dist/types/src/tdf/PolicyObject.d.ts +0 -1
- package/dist/types/src/tdf/PolicyObject.d.ts.map +1 -1
- package/dist/types/src/utils.d.ts +0 -1
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/src/version.d.ts +4 -0
- package/dist/types/src/version.d.ts.map +1 -1
- package/dist/types/tdf3/index.d.ts +3 -2
- package/dist/types/tdf3/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/assertions.d.ts +3 -3
- package/dist/types/tdf3/src/assertions.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/DecoratedReadableStream.d.ts +1 -13
- package/dist/types/tdf3/src/client/DecoratedReadableStream.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/builders.d.ts +43 -37
- package/dist/types/tdf3/src/client/builders.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/index.d.ts +8 -9
- package/dist/types/tdf3/src/client/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/validation.d.ts +3 -3
- package/dist/types/tdf3/src/client/validation.d.ts.map +1 -1
- package/dist/types/tdf3/src/crypto/crypto-utils.d.ts.map +1 -1
- package/dist/types/tdf3/src/index.d.ts +1 -1
- package/dist/types/tdf3/src/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/models/key-access.d.ts +63 -15
- package/dist/types/tdf3/src/models/key-access.d.ts.map +1 -1
- package/dist/types/tdf3/src/models/manifest.d.ts +2 -0
- package/dist/types/tdf3/src/models/manifest.d.ts.map +1 -1
- package/dist/types/tdf3/src/models/policy.d.ts +0 -1
- package/dist/types/tdf3/src/models/policy.d.ts.map +1 -1
- package/dist/types/tdf3/src/tdf.d.ts +20 -24
- package/dist/types/tdf3/src/tdf.d.ts.map +1 -1
- package/dist/types/tdf3/src/utils/index.d.ts +0 -2
- package/dist/types/tdf3/src/utils/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/utils/unwrap.d.ts +2 -0
- package/dist/types/tdf3/src/utils/unwrap.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/zip-reader.d.ts +1 -1
- package/dist/types/tdf3/src/utils/zip-reader.d.ts.map +1 -1
- package/dist/types/tdf3/src/utils/zip-writer.d.ts +2 -2
- package/dist/web/src/access.js +40 -7
- package/dist/web/src/auth/auth.js +1 -1
- package/dist/web/src/auth/oidc-clientcredentials-provider.js +1 -1
- package/dist/web/src/auth/oidc-externaljwt-provider.js +1 -1
- package/dist/web/src/auth/oidc-refreshtoken-provider.js +1 -1
- package/dist/web/src/auth/oidc.js +1 -1
- package/dist/web/src/auth/providers.js +1 -1
- package/dist/web/src/concurrency.js +1 -1
- package/dist/web/src/encodings/base64.js +1 -1
- package/dist/web/src/encodings/hex.js +1 -1
- package/dist/web/src/errors.js +1 -1
- package/dist/web/src/index.js +6 -310
- package/dist/web/src/nanoclients.js +280 -0
- package/dist/web/src/nanoindex.js +5 -0
- package/dist/web/src/nanotdf/Client.js +1 -1
- package/dist/web/src/nanotdf/NanoTDF.js +1 -1
- package/dist/web/src/nanotdf/encrypt-dataset.js +1 -1
- package/dist/web/src/nanotdf/encrypt.js +1 -1
- package/dist/web/src/nanotdf/models/Ciphers.js +1 -1
- package/dist/web/src/nanotdf/models/EcCurves.js +1 -1
- package/dist/web/src/nanotdf/models/Header.js +1 -1
- package/dist/web/src/nanotdf/models/Payload.js +1 -1
- package/dist/web/src/nanotdf/models/Policy/AbstractPolicy.js +1 -1
- package/dist/web/src/nanotdf/models/Policy/EmbeddedPolicy.js +1 -1
- package/dist/web/src/nanotdf/models/Policy/PolicyFactory.js +1 -1
- package/dist/web/src/nanotdf/models/ResourceLocator.js +1 -1
- package/dist/web/src/nanotdf/models/Signature.js +1 -1
- package/dist/web/src/nanotdf-crypto/ciphers.js +1 -1
- package/dist/web/src/nanotdf-crypto/ecdsaSignature.js +1 -1
- package/dist/web/src/nanotdf-crypto/generateKeyPair.js +2 -2
- package/dist/web/src/nanotdf-crypto/generateRandomNumber.js +2 -2
- package/dist/web/src/nanotdf-crypto/index.js +3 -4
- package/dist/web/src/nanotdf-crypto/keyAgreement.js +9 -6
- package/dist/web/src/nanotdf-crypto/pemPublicToCrypto.js +1 -1
- package/dist/web/src/opentdf.js +242 -0
- package/dist/web/src/policy/api.js +1 -1
- package/dist/web/src/policy/granter.js +1 -1
- package/dist/web/src/seekable.js +148 -0
- package/dist/web/src/tdf/AttributeObject.js +1 -2
- package/dist/web/src/tdf/Policy.js +1 -2
- package/dist/web/src/utils.js +2 -3
- package/dist/web/src/version.js +5 -1
- package/dist/web/tdf3/index.js +3 -2
- package/dist/web/tdf3/src/assertions.js +21 -6
- package/dist/web/tdf3/src/binary.js +1 -1
- package/dist/web/tdf3/src/ciphers/aes-gcm-cipher.js +1 -1
- package/dist/web/tdf3/src/ciphers/symmetric-cipher-base.js +1 -1
- package/dist/web/tdf3/src/client/DecoratedReadableStream.js +4 -68
- package/dist/web/tdf3/src/client/builders.js +26 -22
- package/dist/web/tdf3/src/client/index.js +69 -52
- package/dist/web/tdf3/src/client/validation.js +1 -1
- package/dist/web/tdf3/src/crypto/crypto-utils.js +1 -1
- package/dist/web/tdf3/src/crypto/index.js +1 -1
- package/dist/web/tdf3/src/index.js +2 -2
- package/dist/web/tdf3/src/models/attribute-set.js +1 -1
- package/dist/web/tdf3/src/models/encryption-information.js +3 -3
- package/dist/web/tdf3/src/models/key-access.js +47 -24
- package/dist/web/tdf3/src/models/policy.js +1 -1
- package/dist/web/tdf3/src/tdf.js +149 -130
- package/dist/web/tdf3/src/utils/buffer-crc32.js +1 -1
- package/dist/web/tdf3/src/utils/index.js +1 -5
- package/dist/web/tdf3/src/utils/keysplit.js +1 -1
- package/dist/web/tdf3/src/utils/unwrap.js +18 -0
- package/dist/web/tdf3/src/utils/zip-reader.js +1 -1
- package/dist/web/tdf3/src/utils/zip-writer.js +1 -1
- package/package.json +45 -42
- package/src/access.ts +37 -1
- package/src/index.ts +5 -435
- package/src/nanoclients.ts +405 -0
- package/src/nanoindex.ts +4 -0
- package/src/nanotdf-crypto/generateKeyPair.ts +1 -1
- package/src/nanotdf-crypto/generateRandomNumber.ts +1 -1
- package/src/nanotdf-crypto/index.ts +2 -3
- package/src/nanotdf-crypto/keyAgreement.ts +14 -7
- package/src/opentdf.ts +473 -0
- package/{tdf3/src/utils/chunkers.ts → src/seekable.ts} +69 -20
- package/src/tdf/AttributeObject.ts +0 -3
- package/src/tdf/Policy.ts +0 -1
- package/src/tdf/PolicyObject.ts +0 -1
- package/src/utils.ts +1 -3
- package/src/version.ts +5 -0
- package/tdf3/index.ts +14 -2
- package/tdf3/src/assertions.ts +33 -8
- package/tdf3/src/client/DecoratedReadableStream.ts +2 -78
- package/tdf3/src/client/builders.ts +44 -26
- package/tdf3/src/client/index.ts +101 -86
- package/tdf3/src/index.ts +1 -1
- package/tdf3/src/models/encryption-information.ts +2 -2
- package/tdf3/src/models/key-access.ts +120 -38
- package/tdf3/src/models/manifest.ts +3 -0
- package/tdf3/src/models/policy.ts +0 -1
- package/tdf3/src/tdf.ts +251 -207
- package/tdf3/src/utils/index.ts +0 -5
- package/tdf3/src/utils/unwrap.ts +17 -0
- package/tdf3/src/utils/zip-reader.ts +1 -1
- package/dist/cjs/src/nanotdf-crypto/importRawKey.js +0 -18
- package/dist/cjs/tdf3/src/templates/default.html.js +0 -98
- package/dist/cjs/tdf3/src/templates/escaper.js +0 -15
- package/dist/cjs/tdf3/src/templates/index.js +0 -12
- package/dist/cjs/tdf3/src/utils/chunkers.js +0 -114
- package/dist/cjs/tdf3/src/version.js +0 -6
- package/dist/types/src/nanotdf-crypto/importRawKey.d.ts +0 -13
- package/dist/types/src/nanotdf-crypto/importRawKey.d.ts.map +0 -1
- package/dist/types/tdf3/src/templates/default.html.d.ts +0 -8
- package/dist/types/tdf3/src/templates/default.html.d.ts.map +0 -1
- package/dist/types/tdf3/src/templates/escaper.d.ts +0 -6
- package/dist/types/tdf3/src/templates/escaper.d.ts.map +0 -1
- package/dist/types/tdf3/src/templates/index.d.ts +0 -3
- package/dist/types/tdf3/src/templates/index.d.ts.map +0 -1
- package/dist/types/tdf3/src/utils/chunkers.d.ts +0 -29
- package/dist/types/tdf3/src/utils/chunkers.d.ts.map +0 -1
- package/dist/types/tdf3/src/version.d.ts +0 -3
- package/dist/types/tdf3/src/version.d.ts.map +0 -1
- package/dist/web/src/nanotdf-crypto/importRawKey.js +0 -15
- package/dist/web/tdf3/src/templates/default.html.js +0 -96
- package/dist/web/tdf3/src/templates/escaper.js +0 -10
- package/dist/web/tdf3/src/templates/index.js +0 -3
- package/dist/web/tdf3/src/utils/chunkers.js +0 -107
- package/dist/web/tdf3/src/version.js +0 -3
- package/src/nanotdf-crypto/importRawKey.ts +0 -19
- package/tdf3/src/templates/default.html.ts +0 -105
- package/tdf3/src/templates/escaper.ts +0 -10
- package/tdf3/src/templates/index.ts +0 -2
- package/tdf3/src/version.ts +0 -2
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Client,
|
|
3
|
+
NanoTDF,
|
|
4
|
+
Header,
|
|
5
|
+
encrypt,
|
|
6
|
+
decrypt,
|
|
7
|
+
encryptDataset,
|
|
8
|
+
getHkdfSalt,
|
|
9
|
+
DefaultParams,
|
|
10
|
+
} from './nanotdf/index.js';
|
|
11
|
+
import { keyAgreement } from './nanotdf-crypto/index.js';
|
|
12
|
+
import { Policy } from './tdf/Policy.js';
|
|
13
|
+
import { type TypedArray } from './tdf/TypedArray.js';
|
|
14
|
+
import { createAttribute } from './tdf/AttributeObject.js';
|
|
15
|
+
import { fetchECKasPubKey } from './access.js';
|
|
16
|
+
import { ClientConfig } from './nanotdf/Client.js';
|
|
17
|
+
import { ConfigurationError } from './errors.js';
|
|
18
|
+
|
|
19
|
+
// Define the EncryptOptions type
|
|
20
|
+
export type EncryptOptions = {
|
|
21
|
+
ecdsaBinding: boolean;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Define default options
|
|
25
|
+
const defaultOptions: EncryptOptions = {
|
|
26
|
+
ecdsaBinding: false,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* NanoTDF SDK Client. Deprecated in favor of OpenTDF.
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
export class NanoTDFClient extends Client {
|
|
34
|
+
/**
|
|
35
|
+
* Decrypt ciphertext
|
|
36
|
+
*
|
|
37
|
+
* Pass a base64 string, TypedArray, or ArrayBuffer ciphertext and get a promise which resolves plaintext
|
|
38
|
+
*
|
|
39
|
+
* @param ciphertext Ciphertext to decrypt
|
|
40
|
+
*/
|
|
41
|
+
async decrypt(ciphertext: string | TypedArray | ArrayBuffer): Promise<ArrayBuffer> {
|
|
42
|
+
// Parse ciphertext
|
|
43
|
+
const nanotdf = NanoTDF.from(ciphertext);
|
|
44
|
+
|
|
45
|
+
// TODO: The version number should be fetched from the API
|
|
46
|
+
const version = '0.0.1';
|
|
47
|
+
const kasUrl = nanotdf.header.getKasRewrapUrl();
|
|
48
|
+
|
|
49
|
+
// Rewrap key on every request
|
|
50
|
+
const ukey = await this.rewrapKey(
|
|
51
|
+
nanotdf.header.toBuffer(),
|
|
52
|
+
kasUrl,
|
|
53
|
+
nanotdf.header.magicNumberVersion,
|
|
54
|
+
version
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
if (!ukey) {
|
|
58
|
+
throw new Error('internal: key rewrap failure');
|
|
59
|
+
}
|
|
60
|
+
// Return decrypt promise
|
|
61
|
+
return decrypt(ukey, nanotdf);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Decrypt ciphertext of the legacy TDF, with the older, smaller i.v. calculation.
|
|
66
|
+
*
|
|
67
|
+
* Pass a base64 string, TypedArray, or ArrayBuffer ciphertext and get a promise which resolves plaintext
|
|
68
|
+
*
|
|
69
|
+
* @param ciphertext Ciphertext to decrypt
|
|
70
|
+
*/
|
|
71
|
+
async decryptLegacyTDF(ciphertext: string | TypedArray | ArrayBuffer): Promise<ArrayBuffer> {
|
|
72
|
+
// Parse ciphertext
|
|
73
|
+
const nanotdf = NanoTDF.from(ciphertext, undefined, true);
|
|
74
|
+
|
|
75
|
+
const legacyVersion = '0.0.0';
|
|
76
|
+
// Rewrap key on every request
|
|
77
|
+
const key = await this.rewrapKey(
|
|
78
|
+
nanotdf.header.toBuffer(),
|
|
79
|
+
nanotdf.header.getKasRewrapUrl(),
|
|
80
|
+
nanotdf.header.magicNumberVersion,
|
|
81
|
+
legacyVersion
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (!key) {
|
|
85
|
+
throw new Error('internal: failed unwrap');
|
|
86
|
+
}
|
|
87
|
+
// Return decrypt promise
|
|
88
|
+
return decrypt(key, nanotdf);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Encrypts the given data using the NanoTDF encryption scheme.
|
|
93
|
+
*
|
|
94
|
+
* @param {string | TypedArray | ArrayBuffer} data - The data to be encrypted.
|
|
95
|
+
* @param {EncryptOptions} [options=defaultOptions] - The encryption options (currently unused).
|
|
96
|
+
* @returns {Promise<ArrayBuffer>} A promise that resolves to the encrypted data as an ArrayBuffer.
|
|
97
|
+
* @throws {Error} If the initialization vector is not a number.
|
|
98
|
+
*/
|
|
99
|
+
async encrypt(
|
|
100
|
+
data: string | TypedArray | ArrayBuffer,
|
|
101
|
+
options?: EncryptOptions
|
|
102
|
+
): Promise<ArrayBuffer> {
|
|
103
|
+
// For encrypt always generate the client ephemeralKeyPair
|
|
104
|
+
const ephemeralKeyPair = await this.ephemeralKeyPair;
|
|
105
|
+
const initializationVector = this.iv;
|
|
106
|
+
|
|
107
|
+
if (typeof initializationVector !== 'number') {
|
|
108
|
+
throw new ConfigurationError(
|
|
109
|
+
'NanoTDF clients are single use. Please generate a new client and keypair.'
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
delete this.iv;
|
|
113
|
+
|
|
114
|
+
if (!this.kasPubKey) {
|
|
115
|
+
this.kasPubKey = await fetchECKasPubKey(this.kasUrl);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Create a policy for the tdf
|
|
119
|
+
const policy = new Policy();
|
|
120
|
+
|
|
121
|
+
// Add data attributes.
|
|
122
|
+
for (const dataAttribute of this.dataAttributes) {
|
|
123
|
+
const attribute = await createAttribute(dataAttribute, this.kasPubKey, this.kasUrl);
|
|
124
|
+
policy.addAttribute(attribute);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (this.dissems.length == 0 && this.dataAttributes.length == 0) {
|
|
128
|
+
console.warn(
|
|
129
|
+
'This policy has an empty attributes list and an empty dissemination list. This will allow any entity with a valid Entity Object to access this TDF.'
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Encrypt the policy.
|
|
134
|
+
const policyObjectAsStr = policy.toJSON();
|
|
135
|
+
|
|
136
|
+
// IV is always '1', since the new keypair is generated on encrypt
|
|
137
|
+
// using the same key is fine.
|
|
138
|
+
const lengthAsUint32 = new Uint32Array(1);
|
|
139
|
+
lengthAsUint32[0] = initializationVector;
|
|
140
|
+
|
|
141
|
+
const lengthAsUint24 = new Uint8Array(lengthAsUint32.buffer);
|
|
142
|
+
|
|
143
|
+
// NOTE: We are only interested in only first 3 bytes.
|
|
144
|
+
const payloadIV = new Uint8Array(12).fill(0);
|
|
145
|
+
payloadIV[9] = lengthAsUint24[2];
|
|
146
|
+
payloadIV[10] = lengthAsUint24[1];
|
|
147
|
+
payloadIV[11] = lengthAsUint24[0];
|
|
148
|
+
|
|
149
|
+
const mergedOptions: EncryptOptions = { ...defaultOptions, ...options };
|
|
150
|
+
return encrypt(
|
|
151
|
+
policyObjectAsStr,
|
|
152
|
+
this.kasPubKey,
|
|
153
|
+
ephemeralKeyPair,
|
|
154
|
+
payloadIV,
|
|
155
|
+
data,
|
|
156
|
+
mergedOptions.ecdsaBinding
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export type DatasetConfig = ClientConfig & {
|
|
162
|
+
maxKeyIterations?: number;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* NanoTDF Dataset SDK Client
|
|
167
|
+
*
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```
|
|
171
|
+
* import { clientSecretAuthProvider, NanoTDFDatasetClient } from '@opentdf/sdk';
|
|
172
|
+
*
|
|
173
|
+
* const OIDC_ENDPOINT = 'http://localhost:65432/auth/realms/opentdf';
|
|
174
|
+
* const KAS_URL = 'http://localhost:65432/api/kas/';
|
|
175
|
+
*
|
|
176
|
+
* const ciphertext = '...';
|
|
177
|
+
* const client = new NanoTDFDatasetClient({
|
|
178
|
+
* authProvider: await clientSecretAuthProvider({
|
|
179
|
+
* clientId: 'tdf-client',
|
|
180
|
+
* clientSecret: '123-456',
|
|
181
|
+
* exchange: 'client',
|
|
182
|
+
* oidcOrigin: OIDC_ENDPOINT,
|
|
183
|
+
* }),
|
|
184
|
+
* kasEndpoint: KAS_URL,
|
|
185
|
+
* });
|
|
186
|
+
* const plaintext = client.decrypt(ciphertext);
|
|
187
|
+
* console.log('Plaintext', plaintext);
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export class NanoTDFDatasetClient extends Client {
|
|
191
|
+
// Total unique IVs(2^24 -1) used for encrypting the nano tdf payloads
|
|
192
|
+
// IV starts from 1 since the 0 IV is reserved for policy encryption
|
|
193
|
+
static readonly NTDF_MAX_KEY_ITERATIONS = 8388606;
|
|
194
|
+
|
|
195
|
+
private maxKeyIteration: number;
|
|
196
|
+
private keyIterationCount: number;
|
|
197
|
+
private cachedEphemeralKey?: Uint8Array;
|
|
198
|
+
private unwrappedKey?: CryptoKey;
|
|
199
|
+
private symmetricKey?: CryptoKey;
|
|
200
|
+
private cachedHeader?: Header;
|
|
201
|
+
private ecdsaBinding: boolean;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Create new NanoTDF Dataset Client
|
|
205
|
+
*
|
|
206
|
+
* The Ephemeral Key Pair can either be provided or will be generate when fetching the entity object. Once set it
|
|
207
|
+
* cannot be changed. If a new ephemeral key is desired it a new client should be initialized.
|
|
208
|
+
* There is no performance impact for creating a new client IFF the ephemeral key pair is provided.
|
|
209
|
+
*
|
|
210
|
+
* @param clientConfig OIDC client credentials
|
|
211
|
+
* @param kasUrl Key access service URL
|
|
212
|
+
* @param ephemeralKeyPair (optional) ephemeral key pair to use
|
|
213
|
+
* @param maxKeyIterations Max iteration to performe without a key rotation
|
|
214
|
+
*/
|
|
215
|
+
constructor(opts: DatasetConfig) {
|
|
216
|
+
if (
|
|
217
|
+
opts.maxKeyIterations &&
|
|
218
|
+
opts.maxKeyIterations > NanoTDFDatasetClient.NTDF_MAX_KEY_ITERATIONS
|
|
219
|
+
) {
|
|
220
|
+
throw new ConfigurationError(
|
|
221
|
+
`key iteration exceeds max iterations(${NanoTDFDatasetClient.NTDF_MAX_KEY_ITERATIONS})`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
super(opts);
|
|
225
|
+
|
|
226
|
+
this.maxKeyIteration = opts.maxKeyIterations || NanoTDFDatasetClient.NTDF_MAX_KEY_ITERATIONS;
|
|
227
|
+
this.keyIterationCount = 0;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Encrypt data
|
|
232
|
+
*
|
|
233
|
+
* Pass a string, TypedArray, or ArrayBuffer data and get a promise which resolves ciphertext
|
|
234
|
+
*
|
|
235
|
+
* @param data to decrypt
|
|
236
|
+
*/
|
|
237
|
+
async encrypt(
|
|
238
|
+
data: string | TypedArray | ArrayBuffer,
|
|
239
|
+
options?: EncryptOptions
|
|
240
|
+
): Promise<ArrayBuffer> {
|
|
241
|
+
// Intial encrypt
|
|
242
|
+
if (this.keyIterationCount == 0) {
|
|
243
|
+
const mergedOptions: EncryptOptions = { ...defaultOptions, ...options };
|
|
244
|
+
this.ecdsaBinding = mergedOptions.ecdsaBinding;
|
|
245
|
+
// For encrypt always generate the client ephemeralKeyPair
|
|
246
|
+
const ephemeralKeyPair = await this.ephemeralKeyPair;
|
|
247
|
+
|
|
248
|
+
if (!this.kasPubKey) {
|
|
249
|
+
this.kasPubKey = await fetchECKasPubKey(this.kasUrl);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Create a policy for the tdf
|
|
253
|
+
const policy = new Policy();
|
|
254
|
+
|
|
255
|
+
// Add data attributes.
|
|
256
|
+
for (const dataAttribute of this.dataAttributes) {
|
|
257
|
+
const attribute = await createAttribute(dataAttribute, this.kasPubKey, this.kasUrl);
|
|
258
|
+
policy.addAttribute(attribute);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (this.dissems.length == 0 || this.dataAttributes.length == 0) {
|
|
262
|
+
console.warn(
|
|
263
|
+
'This policy has an empty attributes list and an empty dissemination list. This will allow any entity with a valid Entity Object to access this TDF.'
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Encrypt the policy.
|
|
268
|
+
const policyObjectAsStr = policy.toJSON();
|
|
269
|
+
|
|
270
|
+
const ivVector = this.generateIV();
|
|
271
|
+
|
|
272
|
+
// Generate a symmetric key.
|
|
273
|
+
this.symmetricKey = await keyAgreement(
|
|
274
|
+
ephemeralKeyPair.privateKey,
|
|
275
|
+
await this.kasPubKey.key,
|
|
276
|
+
await getHkdfSalt(DefaultParams.magicNumberVersion)
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
const nanoTDFBuffer = await encrypt(
|
|
280
|
+
policyObjectAsStr,
|
|
281
|
+
this.kasPubKey,
|
|
282
|
+
ephemeralKeyPair,
|
|
283
|
+
ivVector,
|
|
284
|
+
data,
|
|
285
|
+
this.ecdsaBinding
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
// Cache the header and increment the key iteration
|
|
289
|
+
if (!this.cachedHeader) {
|
|
290
|
+
const nanoTDF = NanoTDF.from(nanoTDFBuffer);
|
|
291
|
+
this.cachedHeader = nanoTDF.header;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
this.keyIterationCount += 1;
|
|
295
|
+
|
|
296
|
+
return nanoTDFBuffer;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
this.keyIterationCount += 1;
|
|
300
|
+
|
|
301
|
+
if (!this.cachedHeader) {
|
|
302
|
+
throw new ConfigurationError('invalid dataset client: empty nanoTDF header');
|
|
303
|
+
}
|
|
304
|
+
if (!this.symmetricKey) {
|
|
305
|
+
throw new ConfigurationError('invalid dataset client: empty dek');
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
this.keyIterationCount += 1;
|
|
309
|
+
if (this.keyIterationCount == this.maxKeyIteration) {
|
|
310
|
+
// reset the key iteration
|
|
311
|
+
this.keyIterationCount = 0;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const ivVector = this.generateIV();
|
|
315
|
+
|
|
316
|
+
return encryptDataset(this.symmetricKey, this.cachedHeader, ivVector, data);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Decrypt ciphertext
|
|
321
|
+
*
|
|
322
|
+
* Pass a base64 string, TypedArray, or ArrayBuffer ciphertext and get a promise which resolves plaintext
|
|
323
|
+
*
|
|
324
|
+
* @param ciphertext Ciphertext to decrypt
|
|
325
|
+
*/
|
|
326
|
+
async decrypt(ciphertext: string | TypedArray | ArrayBuffer): Promise<ArrayBuffer> {
|
|
327
|
+
// Parse ciphertext
|
|
328
|
+
const nanotdf = NanoTDF.from(ciphertext);
|
|
329
|
+
|
|
330
|
+
if (!this.cachedEphemeralKey) {
|
|
331
|
+
// First decrypt
|
|
332
|
+
return this.rewrapAndDecrypt(nanotdf);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Other encrypts
|
|
336
|
+
if (this.cachedEphemeralKey.toString() == nanotdf.header.ephemeralPublicKey.toString()) {
|
|
337
|
+
const ukey = this.unwrappedKey;
|
|
338
|
+
if (!ukey) {
|
|
339
|
+
// These should have thrown already.
|
|
340
|
+
throw new Error('internal: key rewrap failure');
|
|
341
|
+
}
|
|
342
|
+
// Return decrypt promise
|
|
343
|
+
return decrypt(ukey, nanotdf);
|
|
344
|
+
} else {
|
|
345
|
+
return this.rewrapAndDecrypt(nanotdf);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async rewrapAndDecrypt(nanotdf: NanoTDF) {
|
|
350
|
+
// TODO: The version number should be fetched from the API
|
|
351
|
+
const version = '0.0.1';
|
|
352
|
+
// Rewrap key on every request
|
|
353
|
+
const ukey = await this.rewrapKey(
|
|
354
|
+
nanotdf.header.toBuffer(),
|
|
355
|
+
nanotdf.header.getKasRewrapUrl(),
|
|
356
|
+
nanotdf.header.magicNumberVersion,
|
|
357
|
+
version
|
|
358
|
+
);
|
|
359
|
+
if (!ukey) {
|
|
360
|
+
// These should have thrown already.
|
|
361
|
+
throw new Error('internal: key rewrap failure');
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
this.cachedEphemeralKey = nanotdf.header.ephemeralPublicKey;
|
|
365
|
+
this.unwrappedKey = ukey;
|
|
366
|
+
|
|
367
|
+
// Return decrypt promise
|
|
368
|
+
return decrypt(ukey, nanotdf);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
generateIV(): Uint8Array {
|
|
372
|
+
const iv = this.iv;
|
|
373
|
+
if (iv === undefined) {
|
|
374
|
+
// iv has passed the maximum iteration count for this dek
|
|
375
|
+
throw new ConfigurationError('dataset full');
|
|
376
|
+
}
|
|
377
|
+
// assert iv ∈ ℤ ∩ (0, 2^24)
|
|
378
|
+
if (!Number.isInteger(iv) || iv <= 0 || 0xff_ffff < iv) {
|
|
379
|
+
// Something has fiddled with the iv outside of the expected behavior
|
|
380
|
+
// could indicate a race condition, e.g. if two workers or handlers are
|
|
381
|
+
// processing the file at once, for example.
|
|
382
|
+
throw new Error('internal: invalid state');
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const lengthAsUint32 = new Uint32Array(1);
|
|
386
|
+
lengthAsUint32[0] = iv;
|
|
387
|
+
|
|
388
|
+
const lengthAsUint24 = new Uint8Array(lengthAsUint32.buffer);
|
|
389
|
+
|
|
390
|
+
// NOTE: We are only interested in only first 3 bytes.
|
|
391
|
+
const ivVector = new Uint8Array(Client.IV_SIZE).fill(0);
|
|
392
|
+
ivVector[9] = lengthAsUint24[2];
|
|
393
|
+
ivVector[10] = lengthAsUint24[1];
|
|
394
|
+
ivVector[11] = lengthAsUint24[0];
|
|
395
|
+
|
|
396
|
+
// Increment the IV
|
|
397
|
+
if (iv == 0xff_ffff) {
|
|
398
|
+
delete this.iv;
|
|
399
|
+
} else {
|
|
400
|
+
this.iv = iv + 1;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return ivVector;
|
|
404
|
+
}
|
|
405
|
+
}
|
package/src/nanoindex.ts
ADDED
|
@@ -7,7 +7,7 @@ interface GenerateKeyPairOptions {
|
|
|
7
7
|
isExtractable: boolean;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export
|
|
10
|
+
export async function generateKeyPair(
|
|
11
11
|
{ type: name, curve: namedCurve, keyUsages, isExtractable }: GenerateKeyPairOptions = {
|
|
12
12
|
type: AlgorithmName.ECDH,
|
|
13
13
|
curve: NamedCurve.P256,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generate a random number of given length
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
4
|
+
export function generateRandomNumber(length: number): Uint8Array {
|
|
5
5
|
const byteArray = new Uint8Array(length);
|
|
6
6
|
crypto.getRandomValues(byteArray);
|
|
7
7
|
return byteArray;
|
|
@@ -2,10 +2,9 @@ export { Ciphers } from './ciphers.js';
|
|
|
2
2
|
export { default as decrypt } from './decrypt.js';
|
|
3
3
|
export { default as digest } from './digest.js';
|
|
4
4
|
export { default as encrypt } from './encrypt.js';
|
|
5
|
-
export {
|
|
6
|
-
export { default as importRawKey } from './importRawKey.js';
|
|
5
|
+
export { generateKeyPair } from './generateKeyPair.js';
|
|
7
6
|
export { keyAgreement } from './keyAgreement.js';
|
|
8
7
|
export { default as exportCryptoKey } from './exportCryptoKey.js';
|
|
9
|
-
export {
|
|
8
|
+
export { generateRandomNumber } from './generateRandomNumber.js';
|
|
10
9
|
export { pemPublicToCrypto, pemCertToCrypto } from './pemPublicToCrypto.js';
|
|
11
10
|
export * as enums from './enums.js';
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
|
+
import { keyAlgorithmToPublicKeyAlgorithm } from '../access.js';
|
|
30
31
|
import { ConfigurationError } from '../errors.js';
|
|
31
32
|
import { AlgorithmName, CipherType, HashType, KeyFormat, KeyType, KeyUsageType } from './enums.js';
|
|
32
33
|
|
|
@@ -69,19 +70,25 @@ export async function keyAgreement(
|
|
|
69
70
|
isExtractable: true,
|
|
70
71
|
}
|
|
71
72
|
): Promise<CryptoKey> {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
for (const k of [privateKey, publicKey]) {
|
|
74
|
+
const mechanism = keyAlgorithmToPublicKeyAlgorithm(k.algorithm);
|
|
75
|
+
if (mechanism !== 'ec:secp256r1') {
|
|
76
|
+
throw new ConfigurationError(
|
|
77
|
+
`${k.type} CryptoKey is expected to be of type ECDSA or ECDH, not [${k.algorithm?.name}]`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
77
80
|
}
|
|
78
81
|
|
|
79
82
|
if (privateKey.type !== KeyType.Private) {
|
|
80
|
-
throw new ConfigurationError(
|
|
83
|
+
throw new ConfigurationError(
|
|
84
|
+
`Expected input of privateKey to be a CryptoKey of type private, not [${privateKey.type}]`
|
|
85
|
+
);
|
|
81
86
|
}
|
|
82
87
|
|
|
83
88
|
if (publicKey.type !== KeyType.Public) {
|
|
84
|
-
throw new ConfigurationError(
|
|
89
|
+
throw new ConfigurationError(
|
|
90
|
+
`Expected input of publicKey to be a CryptoKey of type public, not [${publicKey.type}]`
|
|
91
|
+
);
|
|
85
92
|
}
|
|
86
93
|
|
|
87
94
|
const {
|