@opentdf/sdk 0.1.0-beta.1701
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 +52 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/src/access.js +155 -0
- package/dist/cjs/src/auth/Eas.js +60 -0
- package/dist/cjs/src/auth/auth.js +79 -0
- package/dist/cjs/src/auth/oidc-clientcredentials-provider.js +26 -0
- package/dist/cjs/src/auth/oidc-externaljwt-provider.js +33 -0
- package/dist/cjs/src/auth/oidc-refreshtoken-provider.js +34 -0
- package/dist/cjs/src/auth/oidc.js +222 -0
- package/dist/cjs/src/auth/providers.js +143 -0
- package/dist/cjs/src/encodings/base64.js +154 -0
- package/dist/cjs/src/encodings/hex.js +70 -0
- package/dist/cjs/src/encodings/index.js +29 -0
- package/dist/cjs/src/errors.js +138 -0
- package/dist/cjs/src/index.js +344 -0
- package/dist/cjs/src/nanotdf/Client.js +296 -0
- package/dist/cjs/src/nanotdf/NanoTDF.js +94 -0
- package/dist/cjs/src/nanotdf/browser-entry.js +19 -0
- package/dist/cjs/src/nanotdf/constants.js +5 -0
- package/dist/cjs/src/nanotdf/decrypt.js +17 -0
- package/dist/cjs/src/nanotdf/encrypt-dataset.js +38 -0
- package/dist/cjs/src/nanotdf/encrypt.js +132 -0
- package/dist/cjs/src/nanotdf/enum/CipherEnum.js +13 -0
- package/dist/cjs/src/nanotdf/enum/CurveNameEnum.js +15 -0
- package/dist/cjs/src/nanotdf/enum/EncodingEnum.js +8 -0
- package/dist/cjs/src/nanotdf/enum/PolicyTypeEnum.js +11 -0
- package/dist/cjs/src/nanotdf/enum/ProtocolEnum.js +10 -0
- package/dist/cjs/src/nanotdf/enum/ResourceLocatorIdentifierEnum.js +11 -0
- package/dist/cjs/src/nanotdf/helpers/calculateByCurve.js +29 -0
- package/dist/cjs/src/nanotdf/helpers/getHkdfSalt.js +11 -0
- package/dist/cjs/src/nanotdf/index.js +25 -0
- package/dist/cjs/src/nanotdf/interfaces/PolicyInterface.js +3 -0
- package/dist/cjs/src/nanotdf/models/Ciphers.js +61 -0
- package/dist/cjs/src/nanotdf/models/DefaultParams.js +27 -0
- package/dist/cjs/src/nanotdf/models/EcCurves.js +39 -0
- package/dist/cjs/src/nanotdf/models/Header.js +255 -0
- package/dist/cjs/src/nanotdf/models/Payload.js +158 -0
- package/dist/cjs/src/nanotdf/models/Policy/AbstractPolicy.js +73 -0
- package/dist/cjs/src/nanotdf/models/Policy/EmbeddedPolicy.js +82 -0
- package/dist/cjs/src/nanotdf/models/Policy/PolicyFactory.js +38 -0
- package/dist/cjs/src/nanotdf/models/Policy/RemotePolicy.js +62 -0
- package/dist/cjs/src/nanotdf/models/ResourceLocator.js +211 -0
- package/dist/cjs/src/nanotdf/models/Signature.js +77 -0
- package/dist/cjs/src/nanotdf-crypto/ciphers.js +17 -0
- package/dist/cjs/src/nanotdf-crypto/decrypt.js +24 -0
- package/dist/cjs/src/nanotdf-crypto/digest.js +7 -0
- package/dist/cjs/src/nanotdf-crypto/ecdsaSignature.js +83 -0
- package/dist/cjs/src/nanotdf-crypto/encrypt.js +24 -0
- package/dist/cjs/src/nanotdf-crypto/enums.js +52 -0
- package/dist/cjs/src/nanotdf-crypto/exportCryptoKey.js +20 -0
- package/dist/cjs/src/nanotdf-crypto/generateKeyPair.js +13 -0
- package/dist/cjs/src/nanotdf-crypto/generateRandomNumber.js +12 -0
- package/dist/cjs/src/nanotdf-crypto/importRawKey.js +18 -0
- package/dist/cjs/src/nanotdf-crypto/index.js +52 -0
- package/dist/cjs/src/nanotdf-crypto/keyAgreement.js +91 -0
- package/dist/cjs/src/nanotdf-crypto/pemPublicToCrypto.js +225 -0
- package/dist/cjs/src/policy/api.js +58 -0
- package/dist/cjs/src/policy/attributes.js +3 -0
- package/dist/cjs/src/policy/granter.js +146 -0
- package/dist/cjs/src/tdf/AttributeObject.js +15 -0
- package/dist/cjs/src/tdf/AttributeObjectJwt.js +3 -0
- package/dist/cjs/src/tdf/Crypto.js +47 -0
- package/dist/cjs/src/tdf/EntityObject.js +3 -0
- package/dist/cjs/src/tdf/NanoTDF/NanoTDF.js +38 -0
- package/dist/cjs/src/tdf/Policy.js +50 -0
- package/dist/cjs/src/tdf/PolicyObject.js +3 -0
- package/dist/cjs/src/tdf/TypedArray.js +3 -0
- package/dist/cjs/src/tdf/index.js +35 -0
- package/dist/cjs/src/types/index.js +3 -0
- package/dist/cjs/src/utils.js +147 -0
- package/dist/cjs/src/version.js +12 -0
- package/dist/cjs/tdf3/index.js +57 -0
- package/dist/cjs/tdf3/src/assertions.js +118 -0
- package/dist/cjs/tdf3/src/binary.js +153 -0
- package/dist/cjs/tdf3/src/ciphers/aes-gcm-cipher.js +56 -0
- package/dist/cjs/tdf3/src/ciphers/algorithms.js +8 -0
- package/dist/cjs/tdf3/src/ciphers/index.js +8 -0
- package/dist/cjs/tdf3/src/ciphers/symmetric-cipher-base.js +22 -0
- package/dist/cjs/tdf3/src/client/DecoratedReadableStream.js +116 -0
- package/dist/cjs/tdf3/src/client/builders.js +561 -0
- package/dist/cjs/tdf3/src/client/index.js +460 -0
- package/dist/cjs/tdf3/src/client/validation.js +63 -0
- package/dist/cjs/tdf3/src/crypto/crypto-utils.js +116 -0
- package/dist/cjs/tdf3/src/crypto/declarations.js +8 -0
- package/dist/cjs/tdf3/src/crypto/index.js +315 -0
- package/dist/cjs/tdf3/src/index.js +34 -0
- package/dist/cjs/tdf3/src/models/attribute-set.js +122 -0
- package/dist/cjs/tdf3/src/models/encryption-information.js +90 -0
- package/dist/cjs/tdf3/src/models/index.js +25 -0
- package/dist/cjs/tdf3/src/models/key-access.js +103 -0
- package/dist/cjs/tdf3/src/models/manifest.js +3 -0
- package/dist/cjs/tdf3/src/models/payload.js +3 -0
- package/dist/cjs/tdf3/src/models/policy.js +24 -0
- package/dist/cjs/tdf3/src/models/upsert-response.js +3 -0
- package/dist/cjs/tdf3/src/tdf.js +907 -0
- package/dist/cjs/tdf3/src/templates/default.html.js +98 -0
- package/dist/cjs/tdf3/src/templates/escaper.js +15 -0
- package/dist/cjs/tdf3/src/templates/index.js +12 -0
- package/dist/cjs/tdf3/src/utils/buffer-crc32.js +48 -0
- package/dist/cjs/tdf3/src/utils/chunkers.js +106 -0
- package/dist/cjs/tdf3/src/utils/index.js +296 -0
- package/dist/cjs/tdf3/src/utils/keysplit.js +61 -0
- package/dist/cjs/tdf3/src/utils/zip-reader.js +253 -0
- package/dist/cjs/tdf3/src/utils/zip-writer.js +308 -0
- package/dist/cjs/tdf3/src/version.js +6 -0
- package/dist/types/src/access.d.ts +47 -0
- package/dist/types/src/access.d.ts.map +1 -0
- package/dist/types/src/auth/Eas.d.ts +34 -0
- package/dist/types/src/auth/Eas.d.ts.map +1 -0
- package/dist/types/src/auth/auth.d.ts +86 -0
- package/dist/types/src/auth/auth.d.ts.map +1 -0
- package/dist/types/src/auth/oidc-clientcredentials-provider.d.ts +9 -0
- package/dist/types/src/auth/oidc-clientcredentials-provider.d.ts.map +1 -0
- package/dist/types/src/auth/oidc-externaljwt-provider.d.ts +10 -0
- package/dist/types/src/auth/oidc-externaljwt-provider.d.ts.map +1 -0
- package/dist/types/src/auth/oidc-refreshtoken-provider.d.ts +10 -0
- package/dist/types/src/auth/oidc-refreshtoken-provider.d.ts.map +1 -0
- package/dist/types/src/auth/oidc.d.ts +104 -0
- package/dist/types/src/auth/oidc.d.ts.map +1 -0
- package/dist/types/src/auth/providers.d.ts +67 -0
- package/dist/types/src/auth/providers.d.ts.map +1 -0
- package/dist/types/src/encodings/base64.d.ts +18 -0
- package/dist/types/src/encodings/base64.d.ts.map +1 -0
- package/dist/types/src/encodings/hex.d.ts +5 -0
- package/dist/types/src/encodings/hex.d.ts.map +1 -0
- package/dist/types/src/encodings/index.d.ts +3 -0
- package/dist/types/src/encodings/index.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +72 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +138 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/nanotdf/Client.d.ts +95 -0
- package/dist/types/src/nanotdf/Client.d.ts.map +1 -0
- package/dist/types/src/nanotdf/NanoTDF.d.ts +25 -0
- package/dist/types/src/nanotdf/NanoTDF.d.ts.map +1 -0
- package/dist/types/src/nanotdf/browser-entry.d.ts +17 -0
- package/dist/types/src/nanotdf/browser-entry.d.ts.map +1 -0
- package/dist/types/src/nanotdf/constants.d.ts +2 -0
- package/dist/types/src/nanotdf/constants.d.ts.map +1 -0
- package/dist/types/src/nanotdf/decrypt.d.ts +9 -0
- package/dist/types/src/nanotdf/decrypt.d.ts.map +1 -0
- package/dist/types/src/nanotdf/encrypt-dataset.d.ts +12 -0
- package/dist/types/src/nanotdf/encrypt-dataset.d.ts.map +1 -0
- package/dist/types/src/nanotdf/encrypt.d.ts +14 -0
- package/dist/types/src/nanotdf/encrypt.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts +10 -0
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/CurveNameEnum.d.ts +12 -0
- package/dist/types/src/nanotdf/enum/CurveNameEnum.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/EncodingEnum.d.ts +5 -0
- package/dist/types/src/nanotdf/enum/EncodingEnum.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts +8 -0
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/ProtocolEnum.d.ts +7 -0
- package/dist/types/src/nanotdf/enum/ProtocolEnum.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/ResourceLocatorIdentifierEnum.d.ts +8 -0
- package/dist/types/src/nanotdf/enum/ResourceLocatorIdentifierEnum.d.ts.map +1 -0
- package/dist/types/src/nanotdf/helpers/calculateByCurve.d.ts +20 -0
- package/dist/types/src/nanotdf/helpers/calculateByCurve.d.ts.map +1 -0
- package/dist/types/src/nanotdf/helpers/getHkdfSalt.d.ts +9 -0
- package/dist/types/src/nanotdf/helpers/getHkdfSalt.d.ts.map +1 -0
- package/dist/types/src/nanotdf/index.d.ts +9 -0
- package/dist/types/src/nanotdf/index.d.ts.map +1 -0
- package/dist/types/src/nanotdf/interfaces/PolicyInterface.d.ts +17 -0
- package/dist/types/src/nanotdf/interfaces/PolicyInterface.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Ciphers.d.ts +14 -0
- package/dist/types/src/nanotdf/models/Ciphers.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/DefaultParams.d.ts +21 -0
- package/dist/types/src/nanotdf/models/DefaultParams.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/EcCurves.d.ts +15 -0
- package/dist/types/src/nanotdf/models/EcCurves.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Header.d.ts +73 -0
- package/dist/types/src/nanotdf/models/Header.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Payload.d.ts +47 -0
- package/dist/types/src/nanotdf/models/Payload.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Policy/AbstractPolicy.d.ts +52 -0
- package/dist/types/src/nanotdf/models/Policy/AbstractPolicy.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Policy/EmbeddedPolicy.d.ts +35 -0
- package/dist/types/src/nanotdf/models/Policy/EmbeddedPolicy.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Policy/PolicyFactory.d.ts +11 -0
- package/dist/types/src/nanotdf/models/Policy/PolicyFactory.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Policy/RemotePolicy.d.ts +31 -0
- package/dist/types/src/nanotdf/models/Policy/RemotePolicy.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/ResourceLocator.d.ts +65 -0
- package/dist/types/src/nanotdf/models/ResourceLocator.d.ts.map +1 -0
- package/dist/types/src/nanotdf/models/Signature.d.ts +33 -0
- package/dist/types/src/nanotdf/models/Signature.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/ciphers.d.ts +8 -0
- package/dist/types/src/nanotdf-crypto/ciphers.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/decrypt.d.ts +14 -0
- package/dist/types/src/nanotdf-crypto/decrypt.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/digest.d.ts +3 -0
- package/dist/types/src/nanotdf-crypto/digest.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/ecdsaSignature.d.ts +35 -0
- package/dist/types/src/nanotdf-crypto/ecdsaSignature.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/encrypt.d.ts +14 -0
- package/dist/types/src/nanotdf-crypto/encrypt.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/enums.d.ts +42 -0
- package/dist/types/src/nanotdf-crypto/enums.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/exportCryptoKey.d.ts +7 -0
- package/dist/types/src/nanotdf-crypto/exportCryptoKey.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/generateKeyPair.d.ts +10 -0
- package/dist/types/src/nanotdf-crypto/generateKeyPair.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/generateRandomNumber.d.ts +5 -0
- package/dist/types/src/nanotdf-crypto/generateRandomNumber.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/importRawKey.d.ts +13 -0
- package/dist/types/src/nanotdf-crypto/importRawKey.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/index.d.ts +12 -0
- package/dist/types/src/nanotdf-crypto/index.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/keyAgreement.d.ts +28 -0
- package/dist/types/src/nanotdf-crypto/keyAgreement.d.ts.map +1 -0
- package/dist/types/src/nanotdf-crypto/pemPublicToCrypto.d.ts +28 -0
- package/dist/types/src/nanotdf-crypto/pemPublicToCrypto.d.ts.map +1 -0
- package/dist/types/src/policy/api.d.ts +4 -0
- package/dist/types/src/policy/api.d.ts.map +1 -0
- package/dist/types/src/policy/attributes.d.ts +95 -0
- package/dist/types/src/policy/attributes.d.ts.map +1 -0
- package/dist/types/src/policy/granter.d.ts +23 -0
- package/dist/types/src/policy/granter.d.ts.map +1 -0
- package/dist/types/src/tdf/AttributeObject.d.ts +13 -0
- package/dist/types/src/tdf/AttributeObject.d.ts.map +1 -0
- package/dist/types/src/tdf/AttributeObjectJwt.d.ts +4 -0
- package/dist/types/src/tdf/AttributeObjectJwt.d.ts.map +1 -0
- package/dist/types/src/tdf/Crypto.d.ts +37 -0
- package/dist/types/src/tdf/Crypto.d.ts.map +1 -0
- package/dist/types/src/tdf/EntityObject.d.ts +18 -0
- package/dist/types/src/tdf/EntityObject.d.ts.map +1 -0
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts +99 -0
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts.map +1 -0
- package/dist/types/src/tdf/Policy.d.ts +28 -0
- package/dist/types/src/tdf/Policy.d.ts.map +1 -0
- package/dist/types/src/tdf/PolicyObject.d.ts +11 -0
- package/dist/types/src/tdf/PolicyObject.d.ts.map +1 -0
- package/dist/types/src/tdf/TypedArray.d.ts +3 -0
- package/dist/types/src/tdf/TypedArray.d.ts.map +1 -0
- package/dist/types/src/tdf/index.d.ts +7 -0
- package/dist/types/src/tdf/index.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +45 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/utils.d.ts +45 -0
- package/dist/types/src/utils.d.ts.map +1 -0
- package/dist/types/src/version.d.ts +9 -0
- package/dist/types/src/version.d.ts.map +1 -0
- package/dist/types/tdf3/index.d.ts +16 -0
- package/dist/types/tdf3/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/assertions.d.ts +63 -0
- package/dist/types/tdf3/src/assertions.d.ts.map +1 -0
- package/dist/types/tdf3/src/binary.d.ts +38 -0
- package/dist/types/tdf3/src/binary.d.ts.map +1 -0
- package/dist/types/tdf3/src/ciphers/aes-gcm-cipher.d.ts +18 -0
- package/dist/types/tdf3/src/ciphers/aes-gcm-cipher.d.ts.map +1 -0
- package/dist/types/tdf3/src/ciphers/algorithms.d.ts +4 -0
- package/dist/types/tdf3/src/ciphers/algorithms.d.ts.map +1 -0
- package/dist/types/tdf3/src/ciphers/index.d.ts +3 -0
- package/dist/types/tdf3/src/ciphers/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/ciphers/symmetric-cipher-base.d.ts +14 -0
- package/dist/types/tdf3/src/ciphers/symmetric-cipher-base.d.ts.map +1 -0
- package/dist/types/tdf3/src/client/DecoratedReadableStream.d.ts +53 -0
- package/dist/types/tdf3/src/client/DecoratedReadableStream.d.ts.map +1 -0
- package/dist/types/tdf3/src/client/builders.d.ts +436 -0
- package/dist/types/tdf3/src/client/builders.d.ts.map +1 -0
- package/dist/types/tdf3/src/client/index.d.ts +139 -0
- package/dist/types/tdf3/src/client/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/client/validation.d.ts +8 -0
- package/dist/types/tdf3/src/client/validation.d.ts.map +1 -0
- package/dist/types/tdf3/src/crypto/crypto-utils.d.ts +34 -0
- package/dist/types/tdf3/src/crypto/crypto-utils.d.ts.map +1 -0
- package/dist/types/tdf3/src/crypto/declarations.d.ts +60 -0
- package/dist/types/tdf3/src/crypto/declarations.d.ts.map +1 -0
- package/dist/types/tdf3/src/crypto/index.d.ts +103 -0
- package/dist/types/tdf3/src/crypto/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/index.d.ts +5 -0
- package/dist/types/tdf3/src/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/attribute-set.d.ts +65 -0
- package/dist/types/tdf3/src/models/attribute-set.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/encryption-information.d.ts +49 -0
- package/dist/types/tdf3/src/models/encryption-information.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/index.d.ts +9 -0
- package/dist/types/tdf3/src/models/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/key-access.d.ts +42 -0
- package/dist/types/tdf3/src/models/key-access.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/manifest.d.ts +9 -0
- package/dist/types/tdf3/src/models/manifest.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/payload.d.ts +7 -0
- package/dist/types/tdf3/src/models/payload.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/policy.d.ts +13 -0
- package/dist/types/tdf3/src/models/policy.d.ts.map +1 -0
- package/dist/types/tdf3/src/models/upsert-response.d.ts +16 -0
- package/dist/types/tdf3/src/models/upsert-response.d.ts.map +1 -0
- package/dist/types/tdf3/src/tdf.d.ts +152 -0
- package/dist/types/tdf3/src/tdf.d.ts.map +1 -0
- package/dist/types/tdf3/src/templates/default.html.d.ts +8 -0
- package/dist/types/tdf3/src/templates/default.html.d.ts.map +1 -0
- package/dist/types/tdf3/src/templates/escaper.d.ts +6 -0
- package/dist/types/tdf3/src/templates/escaper.d.ts.map +1 -0
- package/dist/types/tdf3/src/templates/index.d.ts +3 -0
- package/dist/types/tdf3/src/templates/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/buffer-crc32.d.ts +2 -0
- package/dist/types/tdf3/src/utils/buffer-crc32.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/chunkers.d.ts +29 -0
- package/dist/types/tdf3/src/utils/chunkers.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/index.d.ts +36 -0
- package/dist/types/tdf3/src/utils/index.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/keysplit.d.ts +19 -0
- package/dist/types/tdf3/src/utils/keysplit.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/zip-reader.d.ts +63 -0
- package/dist/types/tdf3/src/utils/zip-reader.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/zip-writer.d.ts +35 -0
- package/dist/types/tdf3/src/utils/zip-writer.d.ts.map +1 -0
- package/dist/types/tdf3/src/version.d.ts +3 -0
- package/dist/types/tdf3/src/version.d.ts.map +1 -0
- package/dist/web/package.json +3 -0
- package/dist/web/src/access.js +147 -0
- package/dist/web/src/auth/Eas.js +55 -0
- package/dist/web/src/auth/auth.js +71 -0
- package/dist/web/src/auth/oidc-clientcredentials-provider.js +22 -0
- package/dist/web/src/auth/oidc-externaljwt-provider.js +29 -0
- package/dist/web/src/auth/oidc-refreshtoken-provider.js +30 -0
- package/dist/web/src/auth/oidc.js +215 -0
- package/dist/web/src/auth/providers.js +119 -0
- package/dist/web/src/encodings/base64.js +147 -0
- package/dist/web/src/encodings/hex.js +63 -0
- package/dist/web/src/encodings/index.js +3 -0
- package/dist/web/src/errors.js +123 -0
- package/dist/web/src/index.js +313 -0
- package/dist/web/src/nanotdf/Client.js +268 -0
- package/dist/web/src/nanotdf/NanoTDF.js +89 -0
- package/dist/web/src/nanotdf/browser-entry.js +14 -0
- package/dist/web/src/nanotdf/constants.js +2 -0
- package/dist/web/src/nanotdf/decrypt.js +14 -0
- package/dist/web/src/nanotdf/encrypt-dataset.js +32 -0
- package/dist/web/src/nanotdf/encrypt.js +126 -0
- package/dist/web/src/nanotdf/enum/CipherEnum.js +11 -0
- package/dist/web/src/nanotdf/enum/CurveNameEnum.js +13 -0
- package/dist/web/src/nanotdf/enum/EncodingEnum.js +6 -0
- package/dist/web/src/nanotdf/enum/PolicyTypeEnum.js +9 -0
- package/dist/web/src/nanotdf/enum/ProtocolEnum.js +8 -0
- package/dist/web/src/nanotdf/enum/ResourceLocatorIdentifierEnum.js +9 -0
- package/dist/web/src/nanotdf/helpers/calculateByCurve.js +24 -0
- package/dist/web/src/nanotdf/helpers/getHkdfSalt.js +8 -0
- package/dist/web/src/nanotdf/index.js +11 -0
- package/dist/web/src/nanotdf/interfaces/PolicyInterface.js +2 -0
- package/dist/web/src/nanotdf/models/Ciphers.js +54 -0
- package/dist/web/src/nanotdf/models/DefaultParams.js +22 -0
- package/dist/web/src/nanotdf/models/EcCurves.js +32 -0
- package/dist/web/src/nanotdf/models/Header.js +250 -0
- package/dist/web/src/nanotdf/models/Payload.js +156 -0
- package/dist/web/src/nanotdf/models/Policy/AbstractPolicy.js +71 -0
- package/dist/web/src/nanotdf/models/Policy/EmbeddedPolicy.js +77 -0
- package/dist/web/src/nanotdf/models/Policy/PolicyFactory.js +33 -0
- package/dist/web/src/nanotdf/models/Policy/RemotePolicy.js +57 -0
- package/dist/web/src/nanotdf/models/ResourceLocator.js +206 -0
- package/dist/web/src/nanotdf/models/Signature.js +74 -0
- package/dist/web/src/nanotdf-crypto/ciphers.js +14 -0
- package/dist/web/src/nanotdf-crypto/decrypt.js +21 -0
- package/dist/web/src/nanotdf-crypto/digest.js +4 -0
- package/dist/web/src/nanotdf-crypto/ecdsaSignature.js +77 -0
- package/dist/web/src/nanotdf-crypto/encrypt.js +21 -0
- package/dist/web/src/nanotdf-crypto/enums.js +49 -0
- package/dist/web/src/nanotdf-crypto/exportCryptoKey.js +17 -0
- package/dist/web/src/nanotdf-crypto/generateKeyPair.js +10 -0
- package/dist/web/src/nanotdf-crypto/generateRandomNumber.js +9 -0
- package/dist/web/src/nanotdf-crypto/importRawKey.js +15 -0
- package/dist/web/src/nanotdf-crypto/index.js +12 -0
- package/dist/web/src/nanotdf-crypto/keyAgreement.js +87 -0
- package/dist/web/src/nanotdf-crypto/pemPublicToCrypto.js +197 -0
- package/dist/web/src/policy/api.js +54 -0
- package/dist/web/src/policy/attributes.js +2 -0
- package/dist/web/src/policy/granter.js +141 -0
- package/dist/web/src/tdf/AttributeObject.js +11 -0
- package/dist/web/src/tdf/AttributeObjectJwt.js +2 -0
- package/dist/web/src/tdf/Crypto.js +44 -0
- package/dist/web/src/tdf/EntityObject.js +2 -0
- package/dist/web/src/tdf/NanoTDF/NanoTDF.js +35 -0
- package/dist/web/src/tdf/Policy.js +48 -0
- package/dist/web/src/tdf/PolicyObject.js +2 -0
- package/dist/web/src/tdf/TypedArray.js +2 -0
- package/dist/web/src/tdf/index.js +4 -0
- package/dist/web/src/types/index.js +2 -0
- package/dist/web/src/utils.js +133 -0
- package/dist/web/src/version.js +9 -0
- package/dist/web/tdf3/index.js +13 -0
- package/dist/web/tdf3/src/assertions.js +111 -0
- package/dist/web/tdf3/src/binary.js +149 -0
- package/dist/web/tdf3/src/ciphers/aes-gcm-cipher.js +52 -0
- package/dist/web/tdf3/src/ciphers/algorithms.js +5 -0
- package/dist/web/tdf3/src/ciphers/index.js +3 -0
- package/dist/web/tdf3/src/ciphers/symmetric-cipher-base.js +18 -0
- package/dist/web/tdf3/src/client/DecoratedReadableStream.js +107 -0
- package/dist/web/tdf3/src/client/builders.js +557 -0
- package/dist/web/tdf3/src/client/index.js +423 -0
- package/dist/web/tdf3/src/client/validation.js +58 -0
- package/dist/web/tdf3/src/crypto/crypto-utils.js +107 -0
- package/dist/web/tdf3/src/crypto/declarations.js +5 -0
- package/dist/web/tdf3/src/crypto/index.js +296 -0
- package/dist/web/tdf3/src/index.js +5 -0
- package/dist/web/tdf3/src/models/attribute-set.js +118 -0
- package/dist/web/tdf3/src/models/encryption-information.js +86 -0
- package/dist/web/tdf3/src/models/index.js +9 -0
- package/dist/web/tdf3/src/models/key-access.js +74 -0
- package/dist/web/tdf3/src/models/manifest.js +2 -0
- package/dist/web/tdf3/src/models/payload.js +2 -0
- package/dist/web/tdf3/src/models/policy.js +20 -0
- package/dist/web/tdf3/src/models/upsert-response.js +2 -0
- package/dist/web/tdf3/src/tdf.js +866 -0
- package/dist/web/tdf3/src/templates/default.html.js +96 -0
- package/dist/web/tdf3/src/templates/escaper.js +10 -0
- package/dist/web/tdf3/src/templates/index.js +3 -0
- package/dist/web/tdf3/src/utils/buffer-crc32.js +44 -0
- package/dist/web/tdf3/src/utils/chunkers.js +96 -0
- package/dist/web/tdf3/src/utils/index.js +248 -0
- package/dist/web/tdf3/src/utils/keysplit.js +55 -0
- package/dist/web/tdf3/src/utils/zip-reader.js +247 -0
- package/dist/web/tdf3/src/utils/zip-writer.js +302 -0
- package/dist/web/tdf3/src/version.js +3 -0
- package/package.json +126 -0
- package/src/access.ts +198 -0
- package/src/auth/Eas.ts +79 -0
- package/src/auth/auth.ts +141 -0
- package/src/auth/oidc-clientcredentials-provider.ts +32 -0
- package/src/auth/oidc-externaljwt-provider.ts +41 -0
- package/src/auth/oidc-refreshtoken-provider.ts +41 -0
- package/src/auth/oidc.ts +307 -0
- package/src/auth/providers.ts +139 -0
- package/src/encodings/base64.ts +160 -0
- package/src/encodings/hex.ts +69 -0
- package/src/encodings/index.ts +2 -0
- package/src/errors.ts +113 -0
- package/src/index.ts +441 -0
- package/src/nanotdf/Client.ts +349 -0
- package/src/nanotdf/NanoTDF.ts +121 -0
- package/src/nanotdf/browser-entry.ts +20 -0
- package/src/nanotdf/constants.ts +1 -0
- package/src/nanotdf/decrypt.ts +19 -0
- package/src/nanotdf/encrypt-dataset.ts +52 -0
- package/src/nanotdf/encrypt.ts +197 -0
- package/src/nanotdf/enum/CipherEnum.ts +10 -0
- package/src/nanotdf/enum/CurveNameEnum.ts +12 -0
- package/src/nanotdf/enum/EncodingEnum.ts +5 -0
- package/src/nanotdf/enum/PolicyTypeEnum.ts +8 -0
- package/src/nanotdf/enum/ProtocolEnum.ts +7 -0
- package/src/nanotdf/enum/ResourceLocatorIdentifierEnum.ts +8 -0
- package/src/nanotdf/helpers/calculateByCurve.ts +26 -0
- package/src/nanotdf/helpers/getHkdfSalt.ts +15 -0
- package/src/nanotdf/index.ts +10 -0
- package/src/nanotdf/interfaces/PolicyInterface.ts +27 -0
- package/src/nanotdf/models/Ciphers.ts +67 -0
- package/src/nanotdf/models/DefaultParams.ts +24 -0
- package/src/nanotdf/models/EcCurves.ts +40 -0
- package/src/nanotdf/models/Header.ts +322 -0
- package/src/nanotdf/models/Payload.ts +196 -0
- package/src/nanotdf/models/Policy/AbstractPolicy.ts +90 -0
- package/src/nanotdf/models/Policy/EmbeddedPolicy.ts +101 -0
- package/src/nanotdf/models/Policy/PolicyFactory.ts +48 -0
- package/src/nanotdf/models/Policy/RemotePolicy.ts +74 -0
- package/src/nanotdf/models/ResourceLocator.ts +212 -0
- package/src/nanotdf/models/Signature.ts +85 -0
- package/src/nanotdf-crypto/ciphers.ts +13 -0
- package/src/nanotdf-crypto/decrypt.ts +30 -0
- package/src/nanotdf-crypto/digest.ts +8 -0
- package/src/nanotdf-crypto/ecdsaSignature.ts +109 -0
- package/src/nanotdf-crypto/encrypt.ts +30 -0
- package/src/nanotdf-crypto/enums.ts +47 -0
- package/src/nanotdf-crypto/exportCryptoKey.ts +17 -0
- package/src/nanotdf-crypto/generateKeyPair.ts +19 -0
- package/src/nanotdf-crypto/generateRandomNumber.ts +8 -0
- package/src/nanotdf-crypto/importRawKey.ts +19 -0
- package/src/nanotdf-crypto/index.ts +11 -0
- package/src/nanotdf-crypto/keyAgreement.ts +139 -0
- package/src/nanotdf-crypto/pemPublicToCrypto.ts +232 -0
- package/src/package-lock.json +6 -0
- package/src/package.json +3 -0
- package/src/platform/authorization/authorization_connect.d.ts +44 -0
- package/src/platform/authorization/authorization_connect.js +44 -0
- package/src/platform/authorization/authorization_pb.d.ts +707 -0
- package/src/platform/authorization/authorization_pb.js +372 -0
- package/src/platform/common/common_pb.d.ts +129 -0
- package/src/platform/common/common_pb.js +58 -0
- package/src/platform/entityresolution/entity_resolution_connect.d.ts +35 -0
- package/src/platform/entityresolution/entity_resolution_connect.js +35 -0
- package/src/platform/entityresolution/entity_resolution_pb.d.ts +242 -0
- package/src/platform/entityresolution/entity_resolution_pb.js +139 -0
- package/src/platform/kas/kas_connect.d.ts +59 -0
- package/src/platform/kas/kas_connect.js +59 -0
- package/src/platform/kas/kas_pb.d.ts +200 -0
- package/src/platform/kas/kas_pb.js +84 -0
- package/src/platform/policy/attributes/attributes_connect.d.ts +168 -0
- package/src/platform/policy/attributes/attributes_connect.js +168 -0
- package/src/platform/policy/attributes/attributes_pb.d.ts +929 -0
- package/src/platform/policy/attributes/attributes_pb.js +363 -0
- package/src/platform/policy/kasregistry/key_access_server_registry_connect.d.ts +62 -0
- package/src/platform/policy/kasregistry/key_access_server_registry_connect.js +62 -0
- package/src/platform/policy/kasregistry/key_access_server_registry_pb.d.ts +283 -0
- package/src/platform/policy/kasregistry/key_access_server_registry_pb.js +113 -0
- package/src/platform/policy/namespaces/namespaces_connect.d.ts +62 -0
- package/src/platform/policy/namespaces/namespaces_connect.js +62 -0
- package/src/platform/policy/namespaces/namespaces_pb.d.ts +270 -0
- package/src/platform/policy/namespaces/namespaces_pb.js +110 -0
- package/src/platform/policy/objects_pb.d.ts +725 -0
- package/src/platform/policy/objects_pb.js +288 -0
- package/src/platform/policy/resourcemapping/resource_mapping_connect.d.ts +259 -0
- package/src/platform/policy/resourcemapping/resource_mapping_connect.js +259 -0
- package/src/platform/policy/resourcemapping/resource_mapping_pb.d.ts +314 -0
- package/src/platform/policy/resourcemapping/resource_mapping_pb.js +142 -0
- package/src/platform/policy/selectors_pb.d.ts +269 -0
- package/src/platform/policy/selectors_pb.js +110 -0
- package/src/platform/policy/subjectmapping/subject_mapping_connect.d.ts +118 -0
- package/src/platform/policy/subjectmapping/subject_mapping_connect.js +118 -0
- package/src/platform/policy/subjectmapping/subject_mapping_pb.d.ts +672 -0
- package/src/platform/policy/subjectmapping/subject_mapping_pb.js +260 -0
- package/src/platform/wellknownconfiguration/wellknown_configuration_connect.d.ts +26 -0
- package/src/platform/wellknownconfiguration/wellknown_configuration_connect.js +26 -0
- package/src/platform/wellknownconfiguration/wellknown_configuration_pb.d.ts +75 -0
- package/src/platform/wellknownconfiguration/wellknown_configuration_pb.js +35 -0
- package/src/policy/api.ts +61 -0
- package/src/policy/attributes.ts +117 -0
- package/src/policy/granter.ts +181 -0
- package/src/tdf/AttributeObject.ts +27 -0
- package/src/tdf/AttributeObjectJwt.ts +3 -0
- package/src/tdf/Crypto.ts +42 -0
- package/src/tdf/EntityObject.ts +18 -0
- package/src/tdf/NanoTDF/NanoTDF.ts +120 -0
- package/src/tdf/Policy.ts +51 -0
- package/src/tdf/PolicyObject.ts +12 -0
- package/src/tdf/TypedArray.ts +12 -0
- package/src/tdf/index.ts +6 -0
- package/src/types/index.ts +55 -0
- package/src/utils.ts +149 -0
- package/src/version.ts +9 -0
- package/tdf3/index.ts +91 -0
- package/tdf3/package-lock.json +6 -0
- package/tdf3/package.json +3 -0
- package/tdf3/src/assertions.ts +191 -0
- package/tdf3/src/binary.ts +195 -0
- package/tdf3/src/ciphers/aes-gcm-cipher.ts +76 -0
- package/tdf3/src/ciphers/algorithms.ts +9 -0
- package/tdf3/src/ciphers/index.ts +2 -0
- package/tdf3/src/ciphers/symmetric-cipher-base.ts +38 -0
- package/tdf3/src/client/DecoratedReadableStream.ts +148 -0
- package/tdf3/src/client/builders.ts +701 -0
- package/tdf3/src/client/index.ts +637 -0
- package/tdf3/src/client/validation.ts +79 -0
- package/tdf3/src/crypto/crypto-utils.ts +119 -0
- package/tdf3/src/crypto/declarations.ts +89 -0
- package/tdf3/src/crypto/index.ts +394 -0
- package/tdf3/src/index.ts +4 -0
- package/tdf3/src/models/attribute-set.ts +142 -0
- package/tdf3/src/models/encryption-information.ts +172 -0
- package/tdf3/src/models/index.ts +8 -0
- package/tdf3/src/models/key-access.ts +128 -0
- package/tdf3/src/models/manifest.ts +9 -0
- package/tdf3/src/models/payload.ts +6 -0
- package/tdf3/src/models/policy.ts +35 -0
- package/tdf3/src/models/upsert-response.ts +17 -0
- package/tdf3/src/tdf.ts +1351 -0
- package/tdf3/src/templates/default.html.ts +105 -0
- package/tdf3/src/templates/escaper.ts +10 -0
- package/tdf3/src/templates/index.ts +2 -0
- package/tdf3/src/utils/buffer-crc32.ts +46 -0
- package/tdf3/src/utils/chunkers.ts +118 -0
- package/tdf3/src/utils/index.ts +309 -0
- package/tdf3/src/utils/keysplit.ts +63 -0
- package/tdf3/src/utils/zip-reader.ts +341 -0
- package/tdf3/src/utils/zip-writer.ts +375 -0
- package/tdf3/src/version.ts +2 -0
- package/tdf3/types.d.ts +14 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { AttributeValidationError } from '../../../src/errors.js';
|
|
2
|
+
|
|
3
|
+
const sageGetMatch = (match: RegExpMatchArray | null) => (match ? match[0] : null);
|
|
4
|
+
|
|
5
|
+
export const ATTR_NAME_PROP_NAME = 'attr';
|
|
6
|
+
export const ATTR_VALUE_PROP_NAME = 'value';
|
|
7
|
+
|
|
8
|
+
// Validate attribute url protocol starts with `http://` or `https://`
|
|
9
|
+
const SCHEME = '(https?://)';
|
|
10
|
+
|
|
11
|
+
// validate url host be like `localhost:4000`
|
|
12
|
+
const HOST_PORT = '([a-z0-9][a-z0-9]{1,}:[0-9]{1,4})';
|
|
13
|
+
|
|
14
|
+
// validate url host be like `www.example.com`
|
|
15
|
+
const WWW_HOST = '([a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z]{2,}';
|
|
16
|
+
|
|
17
|
+
// validate url host be like `127.0.0.1:4000`
|
|
18
|
+
const IP_HOST_PORT = '([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}:[0-9]{1,4})';
|
|
19
|
+
|
|
20
|
+
// validate host is one of those above
|
|
21
|
+
const HOST = `(${HOST_PORT}|${WWW_HOST}|${IP_HOST_PORT})`;
|
|
22
|
+
|
|
23
|
+
// validate attr name be like `/attr/<attr_name>`
|
|
24
|
+
export const ATTR_NAME = `(/${ATTR_NAME_PROP_NAME}/[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]?)`;
|
|
25
|
+
|
|
26
|
+
// validate value pattern
|
|
27
|
+
export const ATTR_VALUE = `(/${ATTR_VALUE_PROP_NAME}/[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]?)`;
|
|
28
|
+
|
|
29
|
+
// validate attribute authority e.g. https://example.com
|
|
30
|
+
const ATTR_AUTHORITY_PATTERN = `(${SCHEME}${HOST})`;
|
|
31
|
+
|
|
32
|
+
// validate attribute namespace e.g. https://example.com/attr/someattribute
|
|
33
|
+
const ATTR_NAMESPACE_PATTERN = `(${ATTR_AUTHORITY_PATTERN}${ATTR_NAME})`;
|
|
34
|
+
|
|
35
|
+
// validate whole attribute e.g. https://example.com/attr/someattribute/value/somevalue
|
|
36
|
+
export const ATTR_ATTRIBUTE_PATTERN = `^(${ATTR_NAMESPACE_PATTERN}${ATTR_VALUE})$`;
|
|
37
|
+
|
|
38
|
+
export const validateAttributeObject = (attr: unknown): true | never => {
|
|
39
|
+
const isObject = typeof attr === 'object';
|
|
40
|
+
if (!isObject) {
|
|
41
|
+
throw new AttributeValidationError(`attribute should be an object`, attr);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const { attribute } = attr as Record<string, unknown>;
|
|
45
|
+
const isString = typeof attribute === 'string';
|
|
46
|
+
if (!isString) {
|
|
47
|
+
throw new AttributeValidationError(`attribute prop should be a string`, attr);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return validateAttribute(attribute);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export function validateAttribute(attribute: string): true | never {
|
|
54
|
+
if (!attribute.match(ATTR_ATTRIBUTE_PATTERN)) {
|
|
55
|
+
throw new AttributeValidationError(`attribute is in invalid format [${attribute}]`, attribute);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const ATTR_NAME_PREFIX = `/${ATTR_NAME_PROP_NAME}/`;
|
|
59
|
+
const ATTR_VALUE_PREFIX = `/${ATTR_VALUE_PROP_NAME}/`;
|
|
60
|
+
const attrNameMatch = sageGetMatch(attribute.match(ATTR_NAME));
|
|
61
|
+
const attrValueMatch = sageGetMatch(attribute.match(ATTR_VALUE));
|
|
62
|
+
|
|
63
|
+
if (!attrNameMatch) {
|
|
64
|
+
throw new AttributeValidationError(`attribute name matching error`, attribute);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!attrValueMatch) {
|
|
68
|
+
throw new AttributeValidationError(`attribute value matching error`, attribute);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const attributeName = attrNameMatch.slice(ATTR_NAME_PREFIX.length);
|
|
72
|
+
const attributeValue = attrValueMatch.slice(ATTR_VALUE_PREFIX.length);
|
|
73
|
+
|
|
74
|
+
if (attributeName === attributeValue) {
|
|
75
|
+
throw new AttributeValidationError(`attribute name should be unique with its value`, attribute);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { base64 } from '../../../src/encodings/index.js';
|
|
2
|
+
import { type AnyKeyPair, type PemKeyPair } from './declarations.js';
|
|
3
|
+
import { rsaPkcs1Sha256 } from './index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validates a specified key size
|
|
7
|
+
* @param size in bits requested
|
|
8
|
+
* @param minSize in bits allowed
|
|
9
|
+
*/
|
|
10
|
+
export const isValidAsymmetricKeySize = (size: number | undefined, minSize?: number): boolean => {
|
|
11
|
+
// No size specified is fine because the minSize will be used
|
|
12
|
+
if (size === undefined) {
|
|
13
|
+
return !!minSize;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (typeof size !== 'number' || (minSize && size < minSize)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return true;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Format a base64 string representation of a key file
|
|
25
|
+
* in PEM PKCS#8 format by adding a header and footer
|
|
26
|
+
* and new lines.
|
|
27
|
+
*
|
|
28
|
+
* The PEM spec says to use <CR><LF> (\r\n) per
|
|
29
|
+
* https://tools.ietf.org/html/rfc1421#section-4.3.2.2, but
|
|
30
|
+
* many implementations use just \n, so this function
|
|
31
|
+
* follows the convention over the spec.
|
|
32
|
+
*
|
|
33
|
+
* @param base64KeyString input
|
|
34
|
+
* @param label header and footer label that identifies key type
|
|
35
|
+
* @return formatted output
|
|
36
|
+
*/
|
|
37
|
+
export const formatAsPem = (bytes: ArrayBuffer, label: string): string => {
|
|
38
|
+
let pemCert = `-----BEGIN ${label}-----\n`;
|
|
39
|
+
let nextIndex = 0;
|
|
40
|
+
const base64KeyString = base64.encodeArrayBuffer(bytes);
|
|
41
|
+
while (nextIndex < base64KeyString.length) {
|
|
42
|
+
if (nextIndex + 64 <= base64KeyString.length) {
|
|
43
|
+
pemCert += `${base64KeyString.substr(nextIndex, 64)}\n`;
|
|
44
|
+
} else {
|
|
45
|
+
pemCert += `${base64KeyString.substr(nextIndex)}\n`;
|
|
46
|
+
}
|
|
47
|
+
nextIndex += 64;
|
|
48
|
+
}
|
|
49
|
+
pemCert += `-----END ${label}-----\n`;
|
|
50
|
+
return pemCert;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Remove PEM formatting (new line characters and headers / footers)
|
|
55
|
+
* from a PEM string
|
|
56
|
+
*
|
|
57
|
+
* @param input - PEM formatted string
|
|
58
|
+
* @return String with formatting removed
|
|
59
|
+
*/
|
|
60
|
+
export const removePemFormatting = (input: string): string => {
|
|
61
|
+
if (typeof input !== 'string') {
|
|
62
|
+
console.error('Not a pem string', input);
|
|
63
|
+
return input;
|
|
64
|
+
}
|
|
65
|
+
const oneLiner = input.replace(/[\n\r]/g, '');
|
|
66
|
+
// https://www.rfc-editor.org/rfc/rfc7468#section-2
|
|
67
|
+
return oneLiner.replace(
|
|
68
|
+
/-----(?:BEGIN|END)\s(?:RSA\s)?(?:PUBLIC|PRIVATE|CERTIFICATE)\sKEY-----/g,
|
|
69
|
+
''
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const PEMRE =
|
|
74
|
+
/-----BEGIN\s((?:RSA\s)?(?:PUBLIC\sKEY|PRIVATE\sKEY|CERTIFICATE))-----[\s0-9A-Za-z+/=]+-----END\s\1-----/;
|
|
75
|
+
|
|
76
|
+
export const isPemKeyPair = (i: AnyKeyPair): i is PemKeyPair => {
|
|
77
|
+
const { privateKey, publicKey } = i;
|
|
78
|
+
if (typeof privateKey !== 'string' || typeof publicKey !== 'string') {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const privateMatch = PEMRE.exec(privateKey);
|
|
82
|
+
if (!privateMatch || !privateMatch[1] || privateMatch[1].indexOf('PRIVATE KEY') < 0) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
const publicMatch = PEMRE.exec(publicKey);
|
|
86
|
+
if (!publicMatch || !publicMatch[1] || publicMatch[1].indexOf('PRIVATE') >= 0) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const isCryptoKeyPair = (i: AnyKeyPair): i is CryptoKeyPair => {
|
|
93
|
+
const { privateKey, publicKey } = i;
|
|
94
|
+
if (typeof privateKey !== 'object' || typeof publicKey !== 'object') {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
if (!(privateKey instanceof CryptoKey) || !(publicKey instanceof CryptoKey)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return privateKey.type === 'private' && publicKey.type === 'public';
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const toCryptoKeyPair = async (input: AnyKeyPair): Promise<CryptoKeyPair> => {
|
|
104
|
+
if (isCryptoKeyPair(input)) {
|
|
105
|
+
return input;
|
|
106
|
+
}
|
|
107
|
+
if (!isPemKeyPair(input)) {
|
|
108
|
+
throw new Error('internal: generated invalid keypair');
|
|
109
|
+
}
|
|
110
|
+
const k = [input.publicKey, input.privateKey]
|
|
111
|
+
.map(removePemFormatting)
|
|
112
|
+
.map((e) => base64.decodeArrayBuffer(e));
|
|
113
|
+
const algorithm = rsaPkcs1Sha256();
|
|
114
|
+
const [publicKey, privateKey] = await Promise.all([
|
|
115
|
+
crypto.subtle.importKey('spki', k[0], algorithm, true, ['verify']),
|
|
116
|
+
crypto.subtle.importKey('pkcs8', k[1], algorithm, true, ['sign']),
|
|
117
|
+
]);
|
|
118
|
+
return { privateKey, publicKey };
|
|
119
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Binary } from '../binary.js';
|
|
2
|
+
import { type AlgorithmUrn } from '../ciphers/algorithms.js';
|
|
3
|
+
|
|
4
|
+
export type EncryptResult = {
|
|
5
|
+
/** Encrypted payload. */
|
|
6
|
+
payload: Binary;
|
|
7
|
+
/** Auth tag, if generated/ */
|
|
8
|
+
authTag?: Binary;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type DecryptResult = {
|
|
12
|
+
payload: Binary;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* PEM formatted keypair.
|
|
17
|
+
*/
|
|
18
|
+
export type PemKeyPair = {
|
|
19
|
+
publicKey: string;
|
|
20
|
+
privateKey: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The minimum acceptable asymetric key size, currently 2^11.
|
|
25
|
+
*/
|
|
26
|
+
export const MIN_ASYMMETRIC_KEY_SIZE_BITS = 2048;
|
|
27
|
+
|
|
28
|
+
export type AnyKeyPair = PemKeyPair | CryptoKeyPair;
|
|
29
|
+
|
|
30
|
+
export type CryptoService = {
|
|
31
|
+
/** Track which crypto implementation we are using */
|
|
32
|
+
name: string;
|
|
33
|
+
|
|
34
|
+
/** Default algorithm identifier. */
|
|
35
|
+
method: AlgorithmUrn;
|
|
36
|
+
|
|
37
|
+
/** Convert or narrow from AnyKeyPair to PemKeyPair */
|
|
38
|
+
cryptoToPemPair: (keys: AnyKeyPair) => Promise<PemKeyPair>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Try to decrypt content with the default or handed algorithm. Throws on
|
|
42
|
+
* most failure, if auth tagging is implemented for example.
|
|
43
|
+
*/
|
|
44
|
+
decrypt: (
|
|
45
|
+
payload: Binary,
|
|
46
|
+
key: Binary,
|
|
47
|
+
iv: Binary,
|
|
48
|
+
algorithm?: AlgorithmUrn,
|
|
49
|
+
authTag?: Binary
|
|
50
|
+
) => Promise<DecryptResult>;
|
|
51
|
+
|
|
52
|
+
decryptWithPrivateKey: (encryptedPayload: Binary, privateKey: string) => Promise<Binary>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Encrypt content with the default or handed algorithm.
|
|
56
|
+
*/
|
|
57
|
+
encrypt: (
|
|
58
|
+
payload: Binary,
|
|
59
|
+
key: Binary,
|
|
60
|
+
iv: Binary,
|
|
61
|
+
algorithm?: AlgorithmUrn
|
|
62
|
+
) => Promise<EncryptResult>;
|
|
63
|
+
|
|
64
|
+
encryptWithPublicKey: (payload: Binary, publicKey: string) => Promise<Binary>;
|
|
65
|
+
|
|
66
|
+
/** Get length random bytes as a hex-encoded string. */
|
|
67
|
+
generateInitializationVector: (length?: number) => Promise<string>;
|
|
68
|
+
|
|
69
|
+
/** Get length random bytes as a hex-encoded string. */
|
|
70
|
+
generateKey: (length?: number) => Promise<string>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Generate an RSA key pair
|
|
74
|
+
* @param size in bits, defaults to a reasonable size for the default method
|
|
75
|
+
*/
|
|
76
|
+
generateKeyPair: (size?: number) => Promise<AnyKeyPair>;
|
|
77
|
+
|
|
78
|
+
generateSigningKeyPair: () => Promise<AnyKeyPair>;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Create an HMAC SHA256 hash
|
|
82
|
+
*/
|
|
83
|
+
hmac: (key: string, content: string) => Promise<string>;
|
|
84
|
+
|
|
85
|
+
randomBytes: (byteLength: number) => Promise<Uint8Array>;
|
|
86
|
+
|
|
87
|
+
/** Compute the hex-encoded SHA hash of a UTF-16 encoded string. */
|
|
88
|
+
sha256: (content: string) => Promise<string>;
|
|
89
|
+
};
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is for using native crypto in the browser.
|
|
3
|
+
*
|
|
4
|
+
* @private
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Algorithms } from '../ciphers/index.js';
|
|
8
|
+
import { Binary } from '../binary.js';
|
|
9
|
+
import {
|
|
10
|
+
CryptoService,
|
|
11
|
+
DecryptResult,
|
|
12
|
+
EncryptResult,
|
|
13
|
+
MIN_ASYMMETRIC_KEY_SIZE_BITS,
|
|
14
|
+
PemKeyPair,
|
|
15
|
+
} from './declarations.js';
|
|
16
|
+
import { ConfigurationError, DecryptError } from '../../../src/errors.js';
|
|
17
|
+
import { formatAsPem, removePemFormatting } from './crypto-utils.js';
|
|
18
|
+
import { encodeArrayBuffer as hexEncode } from '../../../src/encodings/hex.js';
|
|
19
|
+
import { decodeArrayBuffer as base64Decode } from '../../../src/encodings/base64.js';
|
|
20
|
+
import { AlgorithmUrn } from '../ciphers/algorithms.js';
|
|
21
|
+
|
|
22
|
+
// Used to pass into native crypto functions
|
|
23
|
+
const METHODS: KeyUsage[] = ['encrypt', 'decrypt'];
|
|
24
|
+
export const isSupported = typeof globalThis?.crypto !== 'undefined';
|
|
25
|
+
|
|
26
|
+
export const method = 'http://www.w3.org/2001/04/xmlenc#aes256-cbc';
|
|
27
|
+
export const name = 'BrowserNativeCryptoService';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Get a DOMString representing the algorithm to use for an
|
|
31
|
+
* asymmetric key generation.
|
|
32
|
+
*/
|
|
33
|
+
export function rsaOaepSha1(
|
|
34
|
+
modulusLength: number = MIN_ASYMMETRIC_KEY_SIZE_BITS
|
|
35
|
+
): RsaHashedKeyGenParams {
|
|
36
|
+
if (!modulusLength || modulusLength < MIN_ASYMMETRIC_KEY_SIZE_BITS) {
|
|
37
|
+
throw new ConfigurationError('Invalid key size requested');
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
name: 'RSA-OAEP',
|
|
41
|
+
hash: {
|
|
42
|
+
name: 'SHA-1',
|
|
43
|
+
},
|
|
44
|
+
modulusLength,
|
|
45
|
+
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // 24 bit representation of 65537
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function rsaPkcs1Sha256(
|
|
50
|
+
modulusLength: number = MIN_ASYMMETRIC_KEY_SIZE_BITS
|
|
51
|
+
): RsaHashedKeyGenParams {
|
|
52
|
+
if (!modulusLength || modulusLength < MIN_ASYMMETRIC_KEY_SIZE_BITS) {
|
|
53
|
+
throw new ConfigurationError('Invalid key size requested');
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
name: 'RSASSA-PKCS1-v1_5',
|
|
57
|
+
hash: {
|
|
58
|
+
name: 'SHA-256',
|
|
59
|
+
},
|
|
60
|
+
modulusLength,
|
|
61
|
+
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // 24 bit representation of 65537
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Generate a random hex key
|
|
67
|
+
* @return New key as a hex string
|
|
68
|
+
*/
|
|
69
|
+
export async function generateKey(length?: number): Promise<string> {
|
|
70
|
+
return randomBytesAsHex(length || 32);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Generate an RSA key pair
|
|
75
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey}
|
|
76
|
+
* @param size in bits
|
|
77
|
+
*/
|
|
78
|
+
export async function generateKeyPair(size?: number): Promise<CryptoKeyPair> {
|
|
79
|
+
const algoDomString = rsaOaepSha1(size || MIN_ASYMMETRIC_KEY_SIZE_BITS);
|
|
80
|
+
return crypto.subtle.generateKey(algoDomString, true, METHODS);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Generate an RSA key pair suitable for signatures
|
|
85
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey}
|
|
86
|
+
*/
|
|
87
|
+
export async function generateSigningKeyPair(): Promise<CryptoKeyPair> {
|
|
88
|
+
return crypto.subtle.generateKey(
|
|
89
|
+
{
|
|
90
|
+
name: 'RSASSA-PKCS1-v1_5',
|
|
91
|
+
hash: 'SHA-256',
|
|
92
|
+
modulusLength: 2048,
|
|
93
|
+
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
|
94
|
+
},
|
|
95
|
+
true,
|
|
96
|
+
['sign', 'verify']
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function cryptoToPemPair(keysMaybe: unknown): Promise<PemKeyPair> {
|
|
101
|
+
const keys = keysMaybe as CryptoKeyPair;
|
|
102
|
+
if (!keys.privateKey || !keys.publicKey) {
|
|
103
|
+
// These are only ever generated here, so this should not happen
|
|
104
|
+
throw new Error('internal: invalid keys');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const [exPublic, exPrivate] = await Promise.all([
|
|
108
|
+
crypto.subtle.exportKey('spki', keys.publicKey),
|
|
109
|
+
crypto.subtle.exportKey('pkcs8', keys.privateKey),
|
|
110
|
+
]);
|
|
111
|
+
return {
|
|
112
|
+
publicKey: formatAsPem(exPublic, 'PUBLIC KEY'),
|
|
113
|
+
privateKey: formatAsPem(exPrivate, 'PRIVATE KEY'),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Encrypt using a public key
|
|
119
|
+
* @param payload Payload to encrypt
|
|
120
|
+
* @param publicKey PEM formatted public key
|
|
121
|
+
* @return Encrypted payload
|
|
122
|
+
*/
|
|
123
|
+
export async function encryptWithPublicKey(payload: Binary, publicKey: string): Promise<Binary> {
|
|
124
|
+
console.assert(typeof payload === 'object');
|
|
125
|
+
console.assert(typeof publicKey === 'string');
|
|
126
|
+
|
|
127
|
+
const algoDomString = rsaOaepSha1();
|
|
128
|
+
|
|
129
|
+
// Web Crypto APIs don't work with PEM formatted strings
|
|
130
|
+
publicKey = removePemFormatting(publicKey);
|
|
131
|
+
|
|
132
|
+
const keyBuffer = base64Decode(publicKey);
|
|
133
|
+
const cryptoKey = await crypto.subtle.importKey('spki', keyBuffer, algoDomString, false, [
|
|
134
|
+
'encrypt',
|
|
135
|
+
]);
|
|
136
|
+
const result = await crypto.subtle.encrypt(
|
|
137
|
+
{ name: 'RSA-OAEP' },
|
|
138
|
+
cryptoKey,
|
|
139
|
+
payload.asArrayBuffer()
|
|
140
|
+
);
|
|
141
|
+
return Binary.fromArrayBuffer(result);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Generate a 16-byte initialization vector
|
|
146
|
+
*/
|
|
147
|
+
export async function generateInitializationVector(length?: number): Promise<string> {
|
|
148
|
+
return randomBytesAsHex(length || 16);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export async function randomBytes(byteLength: number): Promise<Uint8Array> {
|
|
152
|
+
const r = new Uint8Array(byteLength);
|
|
153
|
+
crypto.getRandomValues(r);
|
|
154
|
+
return r;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Returns a promise to the encryption key as a binary string.
|
|
159
|
+
*
|
|
160
|
+
* Note: This function should almost never fail as it includes a fallback
|
|
161
|
+
* if for some reason the native generate key fails.
|
|
162
|
+
*
|
|
163
|
+
* @param length The key length, defaults to 256
|
|
164
|
+
*
|
|
165
|
+
* @returns The hex string.
|
|
166
|
+
*/
|
|
167
|
+
export async function randomBytesAsHex(length: number): Promise<string> {
|
|
168
|
+
// Create a typed array of the correct length to fill
|
|
169
|
+
const r = new Uint8Array(length);
|
|
170
|
+
crypto.getRandomValues(r);
|
|
171
|
+
return hexEncode(r.buffer);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Decrypt a public-key encrypted payload with a private key
|
|
176
|
+
* @param encryptedPayload Payload to decrypt
|
|
177
|
+
* @param privateKey PEM formatted private keynpmv
|
|
178
|
+
* @return Decrypted payload
|
|
179
|
+
*/
|
|
180
|
+
export async function decryptWithPrivateKey(
|
|
181
|
+
encryptedPayload: Binary,
|
|
182
|
+
privateKey: string
|
|
183
|
+
): Promise<Binary> {
|
|
184
|
+
console.assert(typeof encryptedPayload === 'object', 'encryptedPayload must be object');
|
|
185
|
+
console.assert(typeof privateKey === 'string', 'privateKey must be string');
|
|
186
|
+
|
|
187
|
+
const algoDomString = rsaOaepSha1();
|
|
188
|
+
|
|
189
|
+
// Web Crypto APIs don't work with PEM formatted strings
|
|
190
|
+
const keyDataString = removePemFormatting(privateKey);
|
|
191
|
+
const keyData = base64Decode(keyDataString);
|
|
192
|
+
|
|
193
|
+
const key = await crypto.subtle.importKey('pkcs8', keyData, algoDomString, false, ['decrypt']);
|
|
194
|
+
const payload = await crypto.subtle.decrypt(
|
|
195
|
+
{ name: 'RSA-OAEP' },
|
|
196
|
+
key,
|
|
197
|
+
encryptedPayload.asArrayBuffer()
|
|
198
|
+
);
|
|
199
|
+
const bufferView = new Uint8Array(payload);
|
|
200
|
+
return Binary.fromArrayBuffer(bufferView.buffer);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Decrypt content synchronously
|
|
205
|
+
* @param payload The payload to decrypt
|
|
206
|
+
* @param key The encryption key
|
|
207
|
+
* @param iv The initialization vector
|
|
208
|
+
* @param algorithm The algorithm to use for encryption
|
|
209
|
+
* @param authTag The authentication tag for authenticated crypto.
|
|
210
|
+
*/
|
|
211
|
+
export function decrypt(
|
|
212
|
+
payload: Binary,
|
|
213
|
+
key: Binary,
|
|
214
|
+
iv: Binary,
|
|
215
|
+
algorithm?: AlgorithmUrn,
|
|
216
|
+
authTag?: Binary
|
|
217
|
+
): Promise<DecryptResult> {
|
|
218
|
+
return _doDecrypt(payload, key, iv, algorithm, authTag);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Encrypt content synchronously
|
|
223
|
+
* @param payload The payload to encrypt
|
|
224
|
+
* @param key The encryption key
|
|
225
|
+
* @param iv The initialization vector
|
|
226
|
+
* @param algorithm The algorithm to use for encryption
|
|
227
|
+
*/
|
|
228
|
+
export function encrypt(
|
|
229
|
+
payload: Binary,
|
|
230
|
+
key: Binary,
|
|
231
|
+
iv: Binary,
|
|
232
|
+
algorithm?: AlgorithmUrn
|
|
233
|
+
): Promise<EncryptResult> {
|
|
234
|
+
return _doEncrypt(payload, key, iv, algorithm);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function _doEncrypt(
|
|
238
|
+
payload: Binary,
|
|
239
|
+
key: Binary,
|
|
240
|
+
iv: Binary,
|
|
241
|
+
algorithm?: AlgorithmUrn
|
|
242
|
+
): Promise<EncryptResult> {
|
|
243
|
+
console.assert(payload != null);
|
|
244
|
+
console.assert(key != null);
|
|
245
|
+
console.assert(iv != null);
|
|
246
|
+
|
|
247
|
+
const payloadBuffer = payload.asArrayBuffer();
|
|
248
|
+
const algoDomString = getSymmetricAlgoDomString(iv, algorithm);
|
|
249
|
+
|
|
250
|
+
const importedKey = await _importKey(key, algoDomString);
|
|
251
|
+
const encrypted = await crypto.subtle.encrypt(algoDomString, importedKey, payloadBuffer);
|
|
252
|
+
if (algoDomString.name === 'AES-GCM') {
|
|
253
|
+
return {
|
|
254
|
+
payload: Binary.fromArrayBuffer(encrypted.slice(0, -16)),
|
|
255
|
+
authTag: Binary.fromArrayBuffer(encrypted.slice(-16)),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
payload: Binary.fromArrayBuffer(encrypted),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function _doDecrypt(
|
|
264
|
+
payload: Binary,
|
|
265
|
+
key: Binary,
|
|
266
|
+
iv: Binary,
|
|
267
|
+
algorithm?: AlgorithmUrn,
|
|
268
|
+
authTag?: Binary
|
|
269
|
+
): Promise<DecryptResult> {
|
|
270
|
+
console.assert(payload != null);
|
|
271
|
+
console.assert(key != null);
|
|
272
|
+
console.assert(iv != null);
|
|
273
|
+
|
|
274
|
+
let payloadBuffer = payload.asArrayBuffer();
|
|
275
|
+
|
|
276
|
+
// Concat the the auth tag to the payload for decryption
|
|
277
|
+
if (authTag) {
|
|
278
|
+
const authTagBuffer = authTag.asArrayBuffer();
|
|
279
|
+
const gcmPayload = new Uint8Array(payloadBuffer.byteLength + authTagBuffer.byteLength);
|
|
280
|
+
gcmPayload.set(new Uint8Array(payloadBuffer), 0);
|
|
281
|
+
gcmPayload.set(new Uint8Array(authTagBuffer), payloadBuffer.byteLength);
|
|
282
|
+
payloadBuffer = gcmPayload.buffer;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const algoDomString = getSymmetricAlgoDomString(iv, algorithm);
|
|
286
|
+
|
|
287
|
+
const importedKey = await _importKey(key, algoDomString);
|
|
288
|
+
algoDomString.iv = iv.asArrayBuffer();
|
|
289
|
+
|
|
290
|
+
const decrypted = await crypto.subtle
|
|
291
|
+
.decrypt(algoDomString, importedKey, payloadBuffer)
|
|
292
|
+
// Catching this error so we can specifically check for OperationError
|
|
293
|
+
.catch((err) => {
|
|
294
|
+
if (err.name === 'OperationError') {
|
|
295
|
+
throw new DecryptError(err);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
throw err;
|
|
299
|
+
});
|
|
300
|
+
return { payload: Binary.fromArrayBuffer(decrypted) };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function _importKey(key: Binary, algorithm: AesCbcParams | AesGcmParams) {
|
|
304
|
+
return crypto.subtle.importKey('raw', key.asArrayBuffer(), algorithm, true, METHODS);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Get a DOMString representing the algorithm to use for a crypto
|
|
309
|
+
* operation. Defaults to AES-CBC.
|
|
310
|
+
* @param {String|undefined} algorithm
|
|
311
|
+
* @return {DOMString} Algorithm to use
|
|
312
|
+
*/
|
|
313
|
+
function getSymmetricAlgoDomString(
|
|
314
|
+
iv: Binary,
|
|
315
|
+
algorithm?: AlgorithmUrn
|
|
316
|
+
): AesCbcParams | AesGcmParams {
|
|
317
|
+
let nativeAlgorithm = 'AES-CBC';
|
|
318
|
+
if (algorithm === Algorithms.AES_256_GCM) {
|
|
319
|
+
nativeAlgorithm = 'AES-GCM';
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return {
|
|
323
|
+
name: nativeAlgorithm,
|
|
324
|
+
iv: iv.asArrayBuffer(),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Create a SHA256 hash. Code refrenced from MDN:
|
|
330
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
|
|
331
|
+
* @param content String content
|
|
332
|
+
* @return Hex hash
|
|
333
|
+
*/
|
|
334
|
+
export async function sha256(content: string): Promise<string> {
|
|
335
|
+
const buffer = new TextEncoder().encode(content);
|
|
336
|
+
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
|
|
337
|
+
return hexEncode(hashBuffer);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Create an HMAC SHA256 hash
|
|
342
|
+
* @param key Key string
|
|
343
|
+
* @param content Content string
|
|
344
|
+
* @return Hex hash
|
|
345
|
+
*/
|
|
346
|
+
export async function hmac(key: string, content: string): Promise<string> {
|
|
347
|
+
const contentBuffer = new TextEncoder().encode(content);
|
|
348
|
+
const keyBuffer = hex2Ab(key);
|
|
349
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
350
|
+
'raw',
|
|
351
|
+
keyBuffer,
|
|
352
|
+
{
|
|
353
|
+
name: 'HMAC',
|
|
354
|
+
hash: { name: 'SHA-256' },
|
|
355
|
+
},
|
|
356
|
+
true,
|
|
357
|
+
['sign', 'verify']
|
|
358
|
+
);
|
|
359
|
+
const hashBuffer = await crypto.subtle.sign('HMAC', cryptoKey, contentBuffer);
|
|
360
|
+
return hexEncode(hashBuffer);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Create an ArrayBuffer from a hex string.
|
|
365
|
+
* https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String?hl=en
|
|
366
|
+
* @param hex - Hex string
|
|
367
|
+
*/
|
|
368
|
+
export function hex2Ab(hex: string): ArrayBuffer {
|
|
369
|
+
const buffer = new ArrayBuffer(hex.length / 2);
|
|
370
|
+
const bufferView = new Uint8Array(buffer);
|
|
371
|
+
|
|
372
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
373
|
+
bufferView[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return buffer;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export const DefaultCryptoService: CryptoService = {
|
|
380
|
+
name,
|
|
381
|
+
method,
|
|
382
|
+
cryptoToPemPair,
|
|
383
|
+
decrypt,
|
|
384
|
+
decryptWithPrivateKey,
|
|
385
|
+
encrypt,
|
|
386
|
+
encryptWithPublicKey,
|
|
387
|
+
generateInitializationVector,
|
|
388
|
+
generateKey,
|
|
389
|
+
generateKeyPair,
|
|
390
|
+
generateSigningKeyPair,
|
|
391
|
+
hmac,
|
|
392
|
+
randomBytes,
|
|
393
|
+
sha256,
|
|
394
|
+
};
|