@enbox/agent 0.0.1
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/dist/browser.js +2215 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +2215 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/index.js +8530 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/agent-did-resolver-cache.js +87 -0
- package/dist/esm/agent-did-resolver-cache.js.map +1 -0
- package/dist/esm/bearer-identity.js +41 -0
- package/dist/esm/bearer-identity.js.map +1 -0
- package/dist/esm/connect.js +191 -0
- package/dist/esm/connect.js.map +1 -0
- package/dist/esm/crypto-api.js +346 -0
- package/dist/esm/crypto-api.js.map +1 -0
- package/dist/esm/did-api.js +278 -0
- package/dist/esm/did-api.js.map +1 -0
- package/dist/esm/dwn-api.js +336 -0
- package/dist/esm/dwn-api.js.map +1 -0
- package/dist/esm/dwn-registrar.js +120 -0
- package/dist/esm/dwn-registrar.js.map +1 -0
- package/dist/esm/hd-identity-vault.js +729 -0
- package/dist/esm/hd-identity-vault.js.map +1 -0
- package/dist/esm/identity-api.js +262 -0
- package/dist/esm/identity-api.js.map +1 -0
- package/dist/esm/index.js +23 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/local-key-manager.js +498 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/oidc.js +507 -0
- package/dist/esm/oidc.js.map +1 -0
- package/dist/esm/permissions-api.js +322 -0
- package/dist/esm/permissions-api.js.map +1 -0
- package/dist/esm/prototyping/clients/dwn-rpc-types.js +2 -0
- package/dist/esm/prototyping/clients/dwn-rpc-types.js.map +1 -0
- package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js +74 -0
- package/dist/esm/prototyping/clients/dwn-server-info-cache-memory.js.map +1 -0
- package/dist/esm/prototyping/clients/http-dwn-rpc-client.js +105 -0
- package/dist/esm/prototyping/clients/http-dwn-rpc-client.js.map +1 -0
- package/dist/esm/prototyping/clients/json-rpc-socket.js +150 -0
- package/dist/esm/prototyping/clients/json-rpc-socket.js.map +1 -0
- package/dist/esm/prototyping/clients/json-rpc.js +58 -0
- package/dist/esm/prototyping/clients/json-rpc.js.map +1 -0
- package/dist/esm/prototyping/clients/server-info-types.js +2 -0
- package/dist/esm/prototyping/clients/server-info-types.js.map +1 -0
- package/dist/esm/prototyping/clients/web-socket-clients.js +90 -0
- package/dist/esm/prototyping/clients/web-socket-clients.js.map +1 -0
- package/dist/esm/prototyping/common/object.js +14 -0
- package/dist/esm/prototyping/common/object.js.map +1 -0
- package/dist/esm/prototyping/common/type-utils.js +2 -0
- package/dist/esm/prototyping/common/type-utils.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js +147 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-kw.js +137 -0
- package/dist/esm/prototyping/crypto/algorithms/aes-kw.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/ecdsa.js +307 -0
- package/dist/esm/prototyping/crypto/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/eddsa.js +264 -0
- package/dist/esm/prototyping/crypto/algorithms/eddsa.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/hkdf.js +39 -0
- package/dist/esm/prototyping/crypto/algorithms/hkdf.js.map +1 -0
- package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js +41 -0
- package/dist/esm/prototyping/crypto/algorithms/pbkdf2.js.map +1 -0
- package/dist/esm/prototyping/crypto/crypto-error.js +41 -0
- package/dist/esm/prototyping/crypto/crypto-error.js.map +1 -0
- package/dist/esm/prototyping/crypto/dsa.js +236 -0
- package/dist/esm/prototyping/crypto/dsa.js.map +1 -0
- package/dist/esm/prototyping/crypto/jose/jwe-compact.js +130 -0
- package/dist/esm/prototyping/crypto/jose/jwe-compact.js.map +1 -0
- package/dist/esm/prototyping/crypto/jose/jwe-flattened.js +294 -0
- package/dist/esm/prototyping/crypto/jose/jwe-flattened.js.map +1 -0
- package/dist/esm/prototyping/crypto/jose/jwe.js +308 -0
- package/dist/esm/prototyping/crypto/jose/jwe.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/aes-gcm.js +352 -0
- package/dist/esm/prototyping/crypto/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/aes-kw.js +247 -0
- package/dist/esm/prototyping/crypto/primitives/aes-kw.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/hkdf.js +80 -0
- package/dist/esm/prototyping/crypto/primitives/hkdf.js.map +1 -0
- package/dist/esm/prototyping/crypto/primitives/pbkdf2.js +85 -0
- package/dist/esm/prototyping/crypto/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/cipher.js +2 -0
- package/dist/esm/prototyping/crypto/types/cipher.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/crypto-api.js +2 -0
- package/dist/esm/prototyping/crypto/types/crypto-api.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-converter.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-converter.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-deriver.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-deriver.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-io.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-io.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-manager.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-manager.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/key-wrapper.js +2 -0
- package/dist/esm/prototyping/crypto/types/key-wrapper.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/params-direct.js +2 -0
- package/dist/esm/prototyping/crypto/types/params-direct.js.map +1 -0
- package/dist/esm/prototyping/crypto/types/params-kms.js +2 -0
- package/dist/esm/prototyping/crypto/types/params-kms.js.map +1 -0
- package/dist/esm/prototyping/crypto/utils.js +19 -0
- package/dist/esm/prototyping/crypto/utils.js.map +1 -0
- package/dist/esm/prototyping/dids/resolver-cache-memory.js +77 -0
- package/dist/esm/prototyping/dids/resolver-cache-memory.js.map +1 -0
- package/dist/esm/prototyping/dids/utils.js +9 -0
- package/dist/esm/prototyping/dids/utils.js.map +1 -0
- package/dist/esm/rpc-client.js +123 -0
- package/dist/esm/rpc-client.js.map +1 -0
- package/dist/esm/store-data-protocols.js +38 -0
- package/dist/esm/store-data-protocols.js.map +1 -0
- package/dist/esm/store-data.js +320 -0
- package/dist/esm/store-data.js.map +1 -0
- package/dist/esm/store-did.js +136 -0
- package/dist/esm/store-did.js.map +1 -0
- package/dist/esm/store-identity.js +140 -0
- package/dist/esm/store-identity.js.map +1 -0
- package/dist/esm/store-key.js +136 -0
- package/dist/esm/store-key.js.map +1 -0
- package/dist/esm/sync-api.js +61 -0
- package/dist/esm/sync-api.js.map +1 -0
- package/dist/esm/sync-engine-level.js +618 -0
- package/dist/esm/sync-engine-level.js.map +1 -0
- package/dist/esm/test-harness.js +239 -0
- package/dist/esm/test-harness.js.map +1 -0
- package/dist/esm/types/agent.js +2 -0
- package/dist/esm/types/agent.js.map +1 -0
- package/dist/esm/types/dwn.js +31 -0
- package/dist/esm/types/dwn.js.map +1 -0
- package/dist/esm/types/identity-vault.js +2 -0
- package/dist/esm/types/identity-vault.js.map +1 -0
- package/dist/esm/types/identity.js +2 -0
- package/dist/esm/types/identity.js.map +1 -0
- package/dist/esm/types/key-manager.js +2 -0
- package/dist/esm/types/key-manager.js.map +1 -0
- package/dist/esm/types/permissions.js +2 -0
- package/dist/esm/types/permissions.js.map +1 -0
- package/dist/esm/types/sync.js +2 -0
- package/dist/esm/types/sync.js.map +1 -0
- package/dist/esm/types/vc.js +5 -0
- package/dist/esm/types/vc.js.map +1 -0
- package/dist/esm/utils-internal.js +147 -0
- package/dist/esm/utils-internal.js.map +1 -0
- package/dist/esm/utils.js +161 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/agent-did-resolver-cache.d.ts +30 -0
- package/dist/types/agent-did-resolver-cache.d.ts.map +1 -0
- package/dist/types/bearer-identity.d.ts +31 -0
- package/dist/types/bearer-identity.d.ts.map +1 -0
- package/dist/types/connect.d.ts +88 -0
- package/dist/types/connect.d.ts.map +1 -0
- package/dist/types/crypto-api.d.ts +286 -0
- package/dist/types/crypto-api.d.ts.map +1 -0
- package/dist/types/did-api.d.ts +119 -0
- package/dist/types/did-api.d.ts.map +1 -0
- package/dist/types/dwn-api.d.ts +66 -0
- package/dist/types/dwn-api.d.ts.map +1 -0
- package/dist/types/dwn-registrar.d.ts +29 -0
- package/dist/types/dwn-registrar.d.ts.map +1 -0
- package/dist/types/hd-identity-vault.d.ts +306 -0
- package/dist/types/hd-identity-vault.d.ts.map +1 -0
- package/dist/types/identity-api.d.ts +107 -0
- package/dist/types/identity-api.d.ts.map +1 -0
- package/dist/types/index.d.ts +30 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +311 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/oidc.d.ts +247 -0
- package/dist/types/oidc.d.ts.map +1 -0
- package/dist/types/permissions-api.d.ts +35 -0
- package/dist/types/permissions-api.d.ts.map +1 -0
- package/dist/types/prototyping/clients/dwn-rpc-types.d.ts +45 -0
- package/dist/types/prototyping/clients/dwn-rpc-types.d.ts.map +1 -0
- package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts +57 -0
- package/dist/types/prototyping/clients/dwn-server-info-cache-memory.d.ts.map +1 -0
- package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts +13 -0
- package/dist/types/prototyping/clients/http-dwn-rpc-client.d.ts.map +1 -0
- package/dist/types/prototyping/clients/json-rpc-socket.d.ts +43 -0
- package/dist/types/prototyping/clients/json-rpc-socket.d.ts.map +1 -0
- package/dist/types/prototyping/clients/json-rpc.d.ts +49 -0
- package/dist/types/prototyping/clients/json-rpc.d.ts.map +1 -0
- package/dist/types/prototyping/clients/server-info-types.d.ts +20 -0
- package/dist/types/prototyping/clients/server-info-types.d.ts.map +1 -0
- package/dist/types/prototyping/clients/web-socket-clients.d.ts +10 -0
- package/dist/types/prototyping/clients/web-socket-clients.d.ts.map +1 -0
- package/dist/types/prototyping/common/object.d.ts +2 -0
- package/dist/types/prototyping/common/object.d.ts.map +1 -0
- package/dist/types/prototyping/common/type-utils.d.ts +7 -0
- package/dist/types/prototyping/common/type-utils.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts +151 -0
- package/dist/types/prototyping/crypto/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts +109 -0
- package/dist/types/prototyping/crypto/algorithms/aes-kw.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts +160 -0
- package/dist/types/prototyping/crypto/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts +157 -0
- package/dist/types/prototyping/crypto/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts +21 -0
- package/dist/types/prototyping/crypto/algorithms/hkdf.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts +21 -0
- package/dist/types/prototyping/crypto/algorithms/pbkdf2.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/crypto-error.d.ts +29 -0
- package/dist/types/prototyping/crypto/crypto-error.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/dsa.d.ts +169 -0
- package/dist/types/prototyping/crypto/dsa.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts +135 -0
- package/dist/types/prototyping/crypto/jose/jwe-compact.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts +134 -0
- package/dist/types/prototyping/crypto/jose/jwe-flattened.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/jose/jwe.d.ts +378 -0
- package/dist/types/prototyping/crypto/jose/jwe.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/prototyping/crypto/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/aes-kw.d.ts +103 -0
- package/dist/types/prototyping/crypto/primitives/aes-kw.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/hkdf.d.ts +90 -0
- package/dist/types/prototyping/crypto/primitives/hkdf.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/primitives/pbkdf2.d.ts +84 -0
- package/dist/types/prototyping/crypto/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/cipher.d.ts +14 -0
- package/dist/types/prototyping/crypto/types/cipher.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/crypto-api.d.ts +35 -0
- package/dist/types/prototyping/crypto/types/crypto-api.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-converter.d.ts +49 -0
- package/dist/types/prototyping/crypto/types/key-converter.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-deriver.d.ts +50 -0
- package/dist/types/prototyping/crypto/types/key-deriver.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-io.d.ts +49 -0
- package/dist/types/prototyping/crypto/types/key-io.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-manager.d.ts +69 -0
- package/dist/types/prototyping/crypto/types/key-manager.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/key-wrapper.d.ts +14 -0
- package/dist/types/prototyping/crypto/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/params-direct.d.ts +75 -0
- package/dist/types/prototyping/crypto/types/params-direct.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/types/params-kms.d.ts +63 -0
- package/dist/types/prototyping/crypto/types/params-kms.d.ts.map +1 -0
- package/dist/types/prototyping/crypto/utils.d.ts +7 -0
- package/dist/types/prototyping/crypto/utils.d.ts.map +1 -0
- package/dist/types/prototyping/dids/resolver-cache-memory.d.ts +57 -0
- package/dist/types/prototyping/dids/resolver-cache-memory.d.ts.map +1 -0
- package/dist/types/prototyping/dids/utils.d.ts +3 -0
- package/dist/types/prototyping/dids/utils.d.ts.map +1 -0
- package/dist/types/rpc-client.d.ts +51 -0
- package/dist/types/rpc-client.d.ts.map +1 -0
- package/dist/types/store-data-protocols.d.ts +4 -0
- package/dist/types/store-data-protocols.d.ts.map +1 -0
- package/dist/types/store-data.d.ts +95 -0
- package/dist/types/store-data.d.ts.map +1 -0
- package/dist/types/store-did.d.ts +33 -0
- package/dist/types/store-did.d.ts.map +1 -0
- package/dist/types/store-identity.d.ts +34 -0
- package/dist/types/store-identity.d.ts.map +1 -0
- package/dist/types/store-key.d.ts +32 -0
- package/dist/types/store-key.d.ts.map +1 -0
- package/dist/types/sync-api.d.ts +41 -0
- package/dist/types/sync-api.d.ts.map +1 -0
- package/dist/types/sync-engine-level.d.ts +85 -0
- package/dist/types/sync-engine-level.d.ts.map +1 -0
- package/dist/types/test-harness.d.ts +69 -0
- package/dist/types/test-harness.d.ts.map +1 -0
- package/dist/types/types/agent.d.ts +172 -0
- package/dist/types/types/agent.d.ts.map +1 -0
- package/dist/types/types/dwn.d.ts +178 -0
- package/dist/types/types/dwn.d.ts.map +1 -0
- package/dist/types/types/identity-vault.d.ts +129 -0
- package/dist/types/types/identity-vault.d.ts.map +1 -0
- package/dist/types/types/identity.d.ts +16 -0
- package/dist/types/types/identity.d.ts.map +1 -0
- package/dist/types/types/key-manager.d.ts +9 -0
- package/dist/types/types/key-manager.d.ts.map +1 -0
- package/dist/types/types/permissions.d.ts +98 -0
- package/dist/types/types/permissions.d.ts.map +1 -0
- package/dist/types/types/sync.d.ts +66 -0
- package/dist/types/types/sync.d.ts.map +1 -0
- package/dist/types/types/vc.d.ts +7 -0
- package/dist/types/types/vc.d.ts.map +1 -0
- package/dist/types/utils-internal.d.ts +50 -0
- package/dist/types/utils-internal.d.ts.map +1 -0
- package/dist/types/utils.d.ts +37 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/package.json +112 -0
- package/src/agent-did-resolver-cache.ts +95 -0
- package/src/bearer-identity.ts +42 -0
- package/src/connect.ts +296 -0
- package/src/crypto-api.ts +593 -0
- package/src/did-api.ts +429 -0
- package/src/dwn-api.ts +462 -0
- package/src/dwn-registrar.ts +127 -0
- package/src/hd-identity-vault.ts +853 -0
- package/src/identity-api.ts +324 -0
- package/src/index.ts +30 -0
- package/src/local-key-manager.ts +672 -0
- package/src/oidc.ts +857 -0
- package/src/permissions-api.ts +408 -0
- package/src/prototyping/clients/dwn-rpc-types.ts +55 -0
- package/src/prototyping/clients/dwn-server-info-cache-memory.ts +79 -0
- package/src/prototyping/clients/http-dwn-rpc-client.ts +110 -0
- package/src/prototyping/clients/json-rpc-socket.ts +169 -0
- package/src/prototyping/clients/json-rpc.ts +113 -0
- package/src/prototyping/clients/server-info-types.ts +21 -0
- package/src/prototyping/clients/web-socket-clients.ts +100 -0
- package/src/prototyping/common/object.ts +15 -0
- package/src/prototyping/common/type-utils.ts +6 -0
- package/src/prototyping/crypto/algorithms/aes-gcm.ts +211 -0
- package/src/prototyping/crypto/algorithms/aes-kw.ts +164 -0
- package/src/prototyping/crypto/algorithms/ecdsa.ts +365 -0
- package/src/prototyping/crypto/algorithms/eddsa.ts +310 -0
- package/src/prototyping/crypto/algorithms/hkdf.ts +40 -0
- package/src/prototyping/crypto/algorithms/pbkdf2.ts +44 -0
- package/src/prototyping/crypto/crypto-error.ts +45 -0
- package/src/prototyping/crypto/dsa.ts +367 -0
- package/src/prototyping/crypto/jose/jwe-compact.ts +225 -0
- package/src/prototyping/crypto/jose/jwe-flattened.ts +459 -0
- package/src/prototyping/crypto/jose/jwe.ts +653 -0
- package/src/prototyping/crypto/primitives/aes-gcm.ts +374 -0
- package/src/prototyping/crypto/primitives/aes-kw.ts +271 -0
- package/src/prototyping/crypto/primitives/hkdf.ts +121 -0
- package/src/prototyping/crypto/primitives/pbkdf2.ts +116 -0
- package/src/prototyping/crypto/types/cipher.ts +17 -0
- package/src/prototyping/crypto/types/crypto-api.ts +78 -0
- package/src/prototyping/crypto/types/key-converter.ts +53 -0
- package/src/prototyping/crypto/types/key-deriver.ts +56 -0
- package/src/prototyping/crypto/types/key-io.ts +51 -0
- package/src/prototyping/crypto/types/key-manager.ts +83 -0
- package/src/prototyping/crypto/types/key-wrapper.ts +17 -0
- package/src/prototyping/crypto/types/params-direct.ts +95 -0
- package/src/prototyping/crypto/types/params-kms.ts +76 -0
- package/src/prototyping/crypto/utils.ts +41 -0
- package/src/prototyping/dids/resolver-cache-memory.ts +83 -0
- package/src/prototyping/dids/utils.ts +10 -0
- package/src/rpc-client.ts +162 -0
- package/src/store-data-protocols.ts +40 -0
- package/src/store-data.ts +400 -0
- package/src/store-did.ts +105 -0
- package/src/store-identity.ts +109 -0
- package/src/store-key.ts +104 -0
- package/src/sync-api.ts +71 -0
- package/src/sync-engine-level.ts +714 -0
- package/src/test-harness.ts +330 -0
- package/src/types/agent.ts +195 -0
- package/src/types/dwn.ts +278 -0
- package/src/types/identity-vault.ts +137 -0
- package/src/types/identity.ts +18 -0
- package/src/types/key-manager.ts +15 -0
- package/src/types/permissions.ts +115 -0
- package/src/types/sync.ts +58 -0
- package/src/types/vc.ts +7 -0
- package/src/utils-internal.ts +157 -0
- package/src/utils.ts +181 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Jwk,
|
|
3
|
+
Hasher,
|
|
4
|
+
Signer,
|
|
5
|
+
SignParams,
|
|
6
|
+
DigestParams,
|
|
7
|
+
VerifyParams,
|
|
8
|
+
GenerateKeyParams,
|
|
9
|
+
GetPublicKeyParams,
|
|
10
|
+
KmsGetKeyUriParams,
|
|
11
|
+
AsymmetricKeyGenerator,
|
|
12
|
+
} from '@enbox/crypto';
|
|
13
|
+
|
|
14
|
+
import { CryptoAlgorithm, Sha2Algorithm, computeJwkThumbprint } from '@enbox/crypto';
|
|
15
|
+
|
|
16
|
+
import type { DsaApi } from './types/crypto-api.js';
|
|
17
|
+
import type { AsymmetricKeyConverter, KeyConverter } from './types/key-converter.js';
|
|
18
|
+
import type { BytesToPrivateKeyParams, BytesToPublicKeyParams, PrivateKeyToBytesParams, PublicKeyToBytesParams } from './types/params-direct.js';
|
|
19
|
+
|
|
20
|
+
import { EcdsaAlgorithm } from './algorithms/ecdsa.js';
|
|
21
|
+
import { EdDsaAlgorithm } from './algorithms/eddsa.js';
|
|
22
|
+
import { CryptoError, CryptoErrorCode } from './crypto-error.js';
|
|
23
|
+
|
|
24
|
+
export interface DsaBytesToPrivateKeyParams extends BytesToPrivateKeyParams {
|
|
25
|
+
algorithm: KeyConversionAlgorithm;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface DsaBytesToPublicKeyParams extends BytesToPublicKeyParams {
|
|
29
|
+
algorithm: AsymmetricKeyConversionAlgorithm;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The `DsaDigestParams` interface defines the algorithm-specific parameters that should
|
|
34
|
+
* be passed into the {@link AgentDsa.digest | `AgentDsa.digest()`} method.
|
|
35
|
+
*/
|
|
36
|
+
export interface DsaDigestParams extends DigestParams {
|
|
37
|
+
/**
|
|
38
|
+
* A string defining the name of hash function to use. The value must be one of the following:
|
|
39
|
+
* - `"SHA-256"`: Generates a 256-bit digest.
|
|
40
|
+
*/
|
|
41
|
+
algorithm: DigestAlgorithm;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface DsaGenerateKeyParams extends GenerateKeyParams {
|
|
45
|
+
algorithm: KeyGenerationAlgorithm;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* `supportedAlgorithms` is an object mapping algorithm names to their respective implementations
|
|
50
|
+
* Each entry in this map specifies the algorithm name and its associated properties, including the
|
|
51
|
+
* implementation class and any relevant names or identifiers for the algorithm. This structure
|
|
52
|
+
* allows for easy retrieval and instantiation of algorithm implementations based on the algorithm
|
|
53
|
+
* name or key specification. It facilitates the support of multiple algorithms within the
|
|
54
|
+
* `LocalKeyManager` class.
|
|
55
|
+
*/
|
|
56
|
+
const supportedAlgorithms = {
|
|
57
|
+
'Ed25519': {
|
|
58
|
+
implementation : EdDsaAlgorithm,
|
|
59
|
+
names : ['Ed25519'],
|
|
60
|
+
operations : ['bytesToPrivateKey', 'bytesToPublicKey', 'generateKey', 'sign', 'verify'],
|
|
61
|
+
},
|
|
62
|
+
'secp256k1': {
|
|
63
|
+
implementation : EcdsaAlgorithm,
|
|
64
|
+
names : ['ES256K', 'secp256k1'],
|
|
65
|
+
operations : ['bytesToPrivateKey', 'bytesToPublicKey', 'generateKey', 'sign', 'verify'],
|
|
66
|
+
},
|
|
67
|
+
'secp256r1': {
|
|
68
|
+
implementation : EcdsaAlgorithm,
|
|
69
|
+
names : ['ES256', 'secp256r1'],
|
|
70
|
+
operations : ['bytesToPrivateKey', 'bytesToPublicKey', 'generateKey', 'sign', 'verify'],
|
|
71
|
+
},
|
|
72
|
+
'SHA-256': {
|
|
73
|
+
implementation : Sha2Algorithm,
|
|
74
|
+
names : ['SHA-256'],
|
|
75
|
+
operations : ['digest'],
|
|
76
|
+
}
|
|
77
|
+
} as const;
|
|
78
|
+
|
|
79
|
+
/* Helper types for `supportedAlgorithms`. */
|
|
80
|
+
type SupportedAlgorithm = keyof typeof supportedAlgorithms;
|
|
81
|
+
type SupportedAlgorithms = typeof supportedAlgorithms;
|
|
82
|
+
|
|
83
|
+
/* Helper type for `supportedAlgorithms` implementations. */
|
|
84
|
+
type AlgorithmConstructor = typeof supportedAlgorithms[SupportedAlgorithm]['implementation'];
|
|
85
|
+
|
|
86
|
+
type DigestAlgorithms = {
|
|
87
|
+
[K in keyof SupportedAlgorithms]: 'digest' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
88
|
+
}[keyof SupportedAlgorithms];
|
|
89
|
+
|
|
90
|
+
type DigestAlgorithm = typeof supportedAlgorithms[DigestAlgorithms]['names'][number];
|
|
91
|
+
|
|
92
|
+
type KeyConversionAlgorithms = {
|
|
93
|
+
[K in keyof SupportedAlgorithms]: 'bytesToPrivateKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
94
|
+
}[keyof SupportedAlgorithms];
|
|
95
|
+
|
|
96
|
+
type KeyConversionAlgorithm = typeof supportedAlgorithms[KeyConversionAlgorithms]['names'][number];
|
|
97
|
+
|
|
98
|
+
type AsymmetricKeyConversionAlgorithms = {
|
|
99
|
+
[K in keyof SupportedAlgorithms]: 'bytesToPublicKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
100
|
+
}[keyof SupportedAlgorithms];
|
|
101
|
+
|
|
102
|
+
type AsymmetricKeyConversionAlgorithm = typeof supportedAlgorithms[AsymmetricKeyConversionAlgorithms]['names'][number];
|
|
103
|
+
|
|
104
|
+
type KeyGenerationAlgorithms = {
|
|
105
|
+
[K in keyof SupportedAlgorithms]: 'generateKey' extends SupportedAlgorithms[K]['operations'][number] ? K : never
|
|
106
|
+
}[keyof SupportedAlgorithms];
|
|
107
|
+
|
|
108
|
+
type KeyGenerationAlgorithm = typeof supportedAlgorithms[KeyGenerationAlgorithms]['names'][number];
|
|
109
|
+
|
|
110
|
+
export class Dsa implements DsaApi<
|
|
111
|
+
DsaGenerateKeyParams, Jwk, GetPublicKeyParams, DsaDigestParams, SignParams, VerifyParams
|
|
112
|
+
> {
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A private map that stores instances of cryptographic algorithm implementations. Each key in
|
|
116
|
+
* this map is an `AlgorithmConstructor`, and its corresponding value is an instance of a class
|
|
117
|
+
* that implements a specific cryptographic algorithm. This map is used to cache and reuse
|
|
118
|
+
* instances for performance optimization, ensuring that each algorithm is instantiated only once.
|
|
119
|
+
*/
|
|
120
|
+
private _algorithmInstances: Map<AlgorithmConstructor, InstanceType<typeof CryptoAlgorithm>> = new Map();
|
|
121
|
+
|
|
122
|
+
public async bytesToPrivateKey({ algorithm: algorithmIdentifier, privateKeyBytes }:
|
|
123
|
+
DsaBytesToPrivateKeyParams
|
|
124
|
+
): Promise<Jwk> {
|
|
125
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
126
|
+
const algorithm = this.getAlgorithmName({ algorithm: algorithmIdentifier });
|
|
127
|
+
|
|
128
|
+
// Get the key converter based on the algorithm name.
|
|
129
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as KeyConverter<DsaBytesToPrivateKeyParams, PrivateKeyToBytesParams>;
|
|
130
|
+
|
|
131
|
+
// Convert the byte array to a JWK.
|
|
132
|
+
const privateKey = await keyConverter.bytesToPrivateKey({ algorithm: algorithmIdentifier, privateKeyBytes });
|
|
133
|
+
|
|
134
|
+
return privateKey;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public async bytesToPublicKey({ algorithm: algorithmIdentifier, publicKeyBytes }:
|
|
138
|
+
DsaBytesToPublicKeyParams
|
|
139
|
+
): Promise<Jwk> {
|
|
140
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
141
|
+
const algorithm = this.getAlgorithmName({ algorithm: algorithmIdentifier });
|
|
142
|
+
|
|
143
|
+
// Get the key converter based on the algorithm name.
|
|
144
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as AsymmetricKeyConverter<DsaBytesToPublicKeyParams, PublicKeyToBytesParams>;
|
|
145
|
+
|
|
146
|
+
// Convert the byte array to a JWK.
|
|
147
|
+
const publicKey = await keyConverter.bytesToPublicKey({ algorithm: algorithmIdentifier, publicKeyBytes });
|
|
148
|
+
|
|
149
|
+
return publicKey;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Generates a hash digest of the provided data.
|
|
154
|
+
*
|
|
155
|
+
* @remarks
|
|
156
|
+
* A digest is the output of the hash function. It's a fixed-size string of bytes that uniquely
|
|
157
|
+
* represents the data input into the hash function. The digest is often used for data integrity
|
|
158
|
+
* checks, as any alteration in the input data results in a significantly different digest.
|
|
159
|
+
*
|
|
160
|
+
* It takes the algorithm identifier of the hash function and data to digest as input and returns
|
|
161
|
+
* the digest of the data.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const Dsa = new AgentDsa();
|
|
166
|
+
* const data = new Uint8Array([...]);
|
|
167
|
+
* const digest = await Dsa.digest({ algorithm: 'SHA-256', data });
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @param params - The parameters for the digest operation.
|
|
171
|
+
* @param params.algorithm - The name of hash function to use.
|
|
172
|
+
* @param params.data - The data to digest.
|
|
173
|
+
*
|
|
174
|
+
* @returns A Promise which will be fulfilled with the hash digest.
|
|
175
|
+
*/
|
|
176
|
+
public async digest({ algorithm, data }:
|
|
177
|
+
DsaDigestParams
|
|
178
|
+
): Promise<Uint8Array> {
|
|
179
|
+
// Get the hash function implementation based on the specified `algorithm` parameter.
|
|
180
|
+
const hasher = this.getAlgorithm({ algorithm }) as Hasher<DsaDigestParams>;
|
|
181
|
+
|
|
182
|
+
// Compute the hash.
|
|
183
|
+
const hash = await hasher.digest({ algorithm, data });
|
|
184
|
+
|
|
185
|
+
return hash;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public async generateKey(params: DsaGenerateKeyParams): Promise<Jwk> {
|
|
189
|
+
// Determine the algorithm name based on the given algorithm identifier.
|
|
190
|
+
const algorithm = this.getAlgorithmName({ algorithm: params.algorithm });
|
|
191
|
+
|
|
192
|
+
// Get the key generator implementation based on the algorithm.
|
|
193
|
+
const keyGenerator = this.getAlgorithm({ algorithm }) as AsymmetricKeyGenerator<DsaGenerateKeyParams, Jwk, GetPublicKeyParams>;
|
|
194
|
+
|
|
195
|
+
// Generate the key.
|
|
196
|
+
const privateKey = await keyGenerator.generateKey({ algorithm: params.algorithm });
|
|
197
|
+
|
|
198
|
+
// If the key ID is undefined, set it to the JWK thumbprint.
|
|
199
|
+
privateKey.kid ??= await computeJwkThumbprint({ jwk: privateKey });
|
|
200
|
+
|
|
201
|
+
return privateKey;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ! TODO: Remove this once the `Dsa` interface is updated in @enbox/crypto to remove KMS-specific methods.
|
|
205
|
+
public async getKeyUri(_params: KmsGetKeyUriParams): Promise<string> {
|
|
206
|
+
throw new Error('Method not implemented.');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public async getPublicKey({ key }:
|
|
210
|
+
GetPublicKeyParams
|
|
211
|
+
): Promise<Jwk> {
|
|
212
|
+
// Determine the algorithm name based on the JWK's `alg` and `crv` properties.
|
|
213
|
+
const algorithm = this.getAlgorithmName({ key });
|
|
214
|
+
|
|
215
|
+
// Get the key generator based on the algorithm name.
|
|
216
|
+
const keyGenerator = this.getAlgorithm({ algorithm }) as AsymmetricKeyGenerator<DsaGenerateKeyParams, Jwk, GetPublicKeyParams>;
|
|
217
|
+
|
|
218
|
+
// Get the public key properties from the private JWK.
|
|
219
|
+
const publicKey = await keyGenerator.getPublicKey({ key });
|
|
220
|
+
|
|
221
|
+
return publicKey;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
public async privateKeyToBytes({ privateKey }: { privateKey: Jwk; }): Promise<Uint8Array> {
|
|
225
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
226
|
+
const algorithm = this.getAlgorithmName({ key: privateKey });
|
|
227
|
+
|
|
228
|
+
// Get the key converter based on the algorithm name.
|
|
229
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as KeyConverter<DsaBytesToPrivateKeyParams, PrivateKeyToBytesParams>;
|
|
230
|
+
|
|
231
|
+
// Convert the JWK to a byte array.
|
|
232
|
+
const privateKeyBytes = await keyConverter.privateKeyToBytes({ privateKey });
|
|
233
|
+
|
|
234
|
+
return privateKeyBytes;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
public async publicKeyToBytes({ publicKey }: { publicKey: Jwk; }): Promise<Uint8Array> {
|
|
238
|
+
// Determine the algorithm name based on the JWK's `alg` property.
|
|
239
|
+
const algorithm = this.getAlgorithmName({ key: publicKey });
|
|
240
|
+
|
|
241
|
+
// Get the key converter based on the algorithm name.
|
|
242
|
+
const keyConverter = this.getAlgorithm({ algorithm }) as AsymmetricKeyConverter<DsaBytesToPublicKeyParams, PublicKeyToBytesParams>;
|
|
243
|
+
|
|
244
|
+
// Convert the JWK to a byte array.
|
|
245
|
+
const publicKeyBytes = await keyConverter.publicKeyToBytes({ publicKey });
|
|
246
|
+
|
|
247
|
+
return publicKeyBytes;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public async sign({ key, data }:
|
|
251
|
+
SignParams
|
|
252
|
+
): Promise<Uint8Array> {
|
|
253
|
+
// Determine the algorithm name based on the JWK's `alg` and `crv` properties.
|
|
254
|
+
const algorithm = this.getAlgorithmName({ key });
|
|
255
|
+
|
|
256
|
+
// Get the signature algorithm based on the algorithm name.
|
|
257
|
+
const signer = this.getAlgorithm({ algorithm }) as Signer<SignParams, VerifyParams>;
|
|
258
|
+
|
|
259
|
+
// Sign the data.
|
|
260
|
+
const signature = signer.sign({ data, key });
|
|
261
|
+
|
|
262
|
+
return signature;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
public async verify({ key, signature, data }:
|
|
266
|
+
VerifyParams
|
|
267
|
+
): Promise<boolean> {
|
|
268
|
+
// Determine the algorithm name based on the JWK's `alg` and `crv` properties.
|
|
269
|
+
const algorithm = this.getAlgorithmName({ key });
|
|
270
|
+
|
|
271
|
+
// Get the signature algorithm based on the algorithm name.
|
|
272
|
+
const signer = this.getAlgorithm({ algorithm }) as Signer<SignParams, VerifyParams>;
|
|
273
|
+
|
|
274
|
+
// Verify the signature.
|
|
275
|
+
const isSignatureValid = signer.verify({ key, signature, data });
|
|
276
|
+
|
|
277
|
+
return isSignatureValid;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Retrieves an algorithm implementation instance based on the provided algorithm name.
|
|
282
|
+
*
|
|
283
|
+
* @remarks
|
|
284
|
+
* This method checks if the requested algorithm is supported and returns a cached instance
|
|
285
|
+
* if available. If an instance does not exist, it creates and caches a new one. This approach
|
|
286
|
+
* optimizes performance by reusing algorithm instances across cryptographic operations.
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* const signer = this.getAlgorithm({ algorithm: 'Ed25519' });
|
|
291
|
+
* ```
|
|
292
|
+
*
|
|
293
|
+
* @param params - The parameters for retrieving the algorithm implementation.
|
|
294
|
+
* @param params.algorithm - The name of the algorithm to retrieve.
|
|
295
|
+
*
|
|
296
|
+
* @returns An instance of the requested algorithm implementation.
|
|
297
|
+
*
|
|
298
|
+
* @throws Error if the requested algorithm is not supported.
|
|
299
|
+
*/
|
|
300
|
+
private getAlgorithm({ algorithm }: {
|
|
301
|
+
algorithm: SupportedAlgorithm;
|
|
302
|
+
}): InstanceType<typeof CryptoAlgorithm> {
|
|
303
|
+
// Check if algorithm is supported.
|
|
304
|
+
const AlgorithmImplementation = supportedAlgorithms[algorithm]?.['implementation'];
|
|
305
|
+
if (!AlgorithmImplementation) {
|
|
306
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Algorithm not supported: ${algorithm}`);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Check if instance already exists for the `AlgorithmImplementation`.
|
|
310
|
+
if (!this._algorithmInstances.has(AlgorithmImplementation)) {
|
|
311
|
+
// If not, create a new instance and store it in the cache
|
|
312
|
+
this._algorithmInstances.set(AlgorithmImplementation, new AlgorithmImplementation());
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Return the cached instance
|
|
316
|
+
return this._algorithmInstances.get(AlgorithmImplementation)!;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Determines the algorithm name based on the key's properties.
|
|
321
|
+
*
|
|
322
|
+
* @remarks
|
|
323
|
+
* This method facilitates the identification of the correct algorithm for cryptographic
|
|
324
|
+
* operations based on the `alg` or `crv` properties of a {@link Jwk | JWK}.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* const key = { ... }; // Public key in JWK format
|
|
329
|
+
* const algorithm = this.getAlgorithmName({ key });
|
|
330
|
+
* ```
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* ```ts
|
|
334
|
+
* const algorithm = this.getAlgorithmName({ algorithm: 'ES256' });
|
|
335
|
+
* ```
|
|
336
|
+
*
|
|
337
|
+
* @param params - The parameters for determining the algorithm name.
|
|
338
|
+
* @param params.key - A JWK containing the `alg` or `crv` properties.
|
|
339
|
+
*
|
|
340
|
+
* @returns The algorithm name associated with the key.
|
|
341
|
+
*
|
|
342
|
+
* @throws Error if the algorithm name cannot be determined from the provided input.
|
|
343
|
+
*/
|
|
344
|
+
private getAlgorithmName({ key }: { key: Jwk }): SupportedAlgorithm;
|
|
345
|
+
private getAlgorithmName({ algorithm }: { algorithm: string }): SupportedAlgorithm;
|
|
346
|
+
private getAlgorithmName({ algorithm, key }: {
|
|
347
|
+
algorithm?: string;
|
|
348
|
+
key?: { alg?: string, crv?: string };
|
|
349
|
+
}): SupportedAlgorithm {
|
|
350
|
+
const algProperty = key?.alg ?? algorithm;
|
|
351
|
+
const crvProperty = key?.crv;
|
|
352
|
+
|
|
353
|
+
for (const algorithmIdentifier of Object.keys(supportedAlgorithms) as SupportedAlgorithm[]) {
|
|
354
|
+
const algorithmNames = supportedAlgorithms[algorithmIdentifier].names as readonly string[];
|
|
355
|
+
if (algProperty && algorithmNames.includes(algProperty)) {
|
|
356
|
+
return algorithmIdentifier;
|
|
357
|
+
} else if (crvProperty && algorithmNames.includes(crvProperty)) {
|
|
358
|
+
return algorithmIdentifier;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported,
|
|
363
|
+
`Algorithm not supported based on provided input: alg=${algProperty}, crv=${crvProperty}. ` +
|
|
364
|
+
'Please check the documentation for the list of supported algorithms.'
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { Jwk, KeyIdentifier } from '@enbox/crypto';
|
|
2
|
+
|
|
3
|
+
import { LocalKeyManager } from '@enbox/crypto';
|
|
4
|
+
|
|
5
|
+
import type { CryptoApi } from '../types/crypto-api.js';
|
|
6
|
+
import type { KeyManager } from '../types/key-manager.js';
|
|
7
|
+
import type { JweDecryptOptions, JweEncryptOptions, JweHeaderParams } from './jwe.js';
|
|
8
|
+
|
|
9
|
+
import { isValidJweHeader } from './jwe.js';
|
|
10
|
+
import { FlattenedJwe } from './jwe-flattened.js';
|
|
11
|
+
import { AgentCryptoApi } from '../../../crypto-api.js';
|
|
12
|
+
import { CryptoError, CryptoErrorCode } from '../crypto-error.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Parameters required for decrypting a JWE in Compact Serialization format.
|
|
16
|
+
*
|
|
17
|
+
* @typeParam TKeyManager - The Key Manager used to manage cryptographic keys.
|
|
18
|
+
* @typeParam TCrypto - The Crypto API used to perform cryptographic operations.
|
|
19
|
+
*/
|
|
20
|
+
export interface CompactJweDecryptParams<TKeyManager, TCrypto> {
|
|
21
|
+
/** The JWE string in Compact Serialization format. */
|
|
22
|
+
jwe: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The decryption key which can be a Key Identifier such as a KMS key URI, a JSON Web Key (JWK),
|
|
26
|
+
* or raw key material represented as a byte array.
|
|
27
|
+
*/
|
|
28
|
+
key: KeyIdentifier | Jwk | Uint8Array;
|
|
29
|
+
|
|
30
|
+
/** Key Manager instanceß responsible for managing cryptographic keys. */
|
|
31
|
+
keyManager?: TKeyManager;
|
|
32
|
+
|
|
33
|
+
/** Crypto API instance that provides the necessary cryptographic operations. */
|
|
34
|
+
crypto?: TCrypto;
|
|
35
|
+
|
|
36
|
+
/** {@inheritDoc JweDecryptOptions} */
|
|
37
|
+
options?: JweDecryptOptions;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Result of decrypting a JWE in Compact Serialization format.
|
|
42
|
+
*/
|
|
43
|
+
export interface CompactJweDecryptResult {
|
|
44
|
+
/** Decrypted plaintext as a byte array. */
|
|
45
|
+
plaintext: Uint8Array;
|
|
46
|
+
|
|
47
|
+
/** The protected header of the JWE. */
|
|
48
|
+
protectedHeader: JweHeaderParams;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Parameters required for encrypting data into a JWE in Compact Serialization format.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam TKeyManager - The Key Manager used to manage cryptographic keys.
|
|
55
|
+
* @typeParam TCrypto - The Crypto API used to perform cryptographic operations.
|
|
56
|
+
*/
|
|
57
|
+
export interface CompactJweEncryptParams<TKeyManager, TCrypto> {
|
|
58
|
+
/** The plaintext data to be encrypted as a byte array. */
|
|
59
|
+
plaintext: Uint8Array;
|
|
60
|
+
|
|
61
|
+
/** JWE Protected Header containing encryption algorithm details. */
|
|
62
|
+
protectedHeader: JweHeaderParams;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The encryption key which can be a Key Identifier such as a KMS key URI, a JSON Web Key (JWK),
|
|
66
|
+
* or raw key material represented as a byte array.
|
|
67
|
+
*/
|
|
68
|
+
key: KeyIdentifier | Jwk | Uint8Array;
|
|
69
|
+
|
|
70
|
+
/** Key Manager instanceß responsible for managing cryptographic keys. */
|
|
71
|
+
keyManager?: TKeyManager;
|
|
72
|
+
|
|
73
|
+
/** Crypto API instance that provides the necessary cryptographic operations. */
|
|
74
|
+
crypto?: TCrypto;
|
|
75
|
+
|
|
76
|
+
/** {@inheritDoc JweEncryptOptions} */
|
|
77
|
+
options?: JweEncryptOptions;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The `CompactJwe` class facilitates encryption and decryption processes using the JSON Web
|
|
82
|
+
* Encryption (JWE) Compact Serialization format. This class adheres to the specifications
|
|
83
|
+
* outlined in {@link https://datatracker.ietf.org/doc/html/rfc7516 | RFC 7516}, enabling secure
|
|
84
|
+
* data encapsulation through various cryptographic algorithms.
|
|
85
|
+
*
|
|
86
|
+
* Compact Serialization is a space-efficient representation of JWE, suitable for contexts
|
|
87
|
+
* where verbose data structures are impractical, such as HTTP headers. It provides mechanisms to
|
|
88
|
+
* encrypt content and protect its integrity with authenticated encryption, ensuring
|
|
89
|
+
* confidentiality, authenticity, and non-repudiation.
|
|
90
|
+
*
|
|
91
|
+
* This class supports the following operations:
|
|
92
|
+
* - Decrypting data from a compact serialized JWE string.
|
|
93
|
+
* - Encrypting data and producing a compact serialized JWE string.
|
|
94
|
+
*
|
|
95
|
+
* Usage involves specifying the cryptographic details, such as keys and algorithms, and the class
|
|
96
|
+
* handles the complexities of the JWE processing, including parsing, validating, and applying the
|
|
97
|
+
* cryptographic operations defined in the JWE specification.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* // Example usage of encrypt method
|
|
102
|
+
* const plaintext = new TextEncoder().encode("Secret Message");
|
|
103
|
+
* const key = { kty: "oct", k: "your-secret-key" }; // Example symmetric key
|
|
104
|
+
* const protectedHeader = { alg: "dir", enc: "A256GCM" };
|
|
105
|
+
* const encryptedJweString = await CompactJwe.encrypt({
|
|
106
|
+
* plaintext,
|
|
107
|
+
* protectedHeader,
|
|
108
|
+
* key,
|
|
109
|
+
* });
|
|
110
|
+
* console.log(encryptedJweString); // Outputs the JWE string in Compact Serialization format
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* // Example usage of decrypt method
|
|
116
|
+
* const jweString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."; // A JWE in Compact Serialization
|
|
117
|
+
* const decryptionKey = { kty: "oct", k: "your-secret-key" }; // The key must match the one used for encryption
|
|
118
|
+
* const { plaintext, protectedHeader } = await CompactJwe.decrypt({
|
|
119
|
+
* jwe: jweString,
|
|
120
|
+
* key: decryptionKey,
|
|
121
|
+
* });
|
|
122
|
+
* console.log(new TextDecoder().decode(plaintext)); // Outputs the decrypted message
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export class CompactJwe {
|
|
126
|
+
/**
|
|
127
|
+
* Decrypts a JWE string in Compact Serialization format, extracting the plaintext and
|
|
128
|
+
* reconstructing the JWE Protected Header.
|
|
129
|
+
*
|
|
130
|
+
* This method parses the compact JWE, validates its structure, and applies the appropriate
|
|
131
|
+
* decryption algorithm as specified in the JWE Protected Header. It returns the decrypted
|
|
132
|
+
* plaintext along with the reconstructed protected header, ensuring the data's authenticity
|
|
133
|
+
* and integrity.
|
|
134
|
+
*
|
|
135
|
+
* @param params - The decryption parameters including the JWE string, cryptographic key, and
|
|
136
|
+
* optional instances of Key Manager and Crypto API.
|
|
137
|
+
* @returns A promise resolving to the decrypted content and the JWE Protected Header.
|
|
138
|
+
* @throws {@link CryptoError} if the JWE format is invalid or decryption fails.
|
|
139
|
+
*/
|
|
140
|
+
public static async decrypt<
|
|
141
|
+
TKeyManager extends KeyManager | undefined = KeyManager,
|
|
142
|
+
TCrypto extends CryptoApi | undefined = CryptoApi
|
|
143
|
+
>({
|
|
144
|
+
jwe,
|
|
145
|
+
key,
|
|
146
|
+
keyManager = new LocalKeyManager(),
|
|
147
|
+
crypto = new AgentCryptoApi(),
|
|
148
|
+
options = {}
|
|
149
|
+
}: CompactJweDecryptParams<TKeyManager, TCrypto>
|
|
150
|
+
): Promise<CompactJweDecryptResult> {
|
|
151
|
+
if (typeof jwe !== 'string') {
|
|
152
|
+
throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Invalid JWE format. JWE must be a string.');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Split the JWE into its constituent parts.
|
|
156
|
+
const {
|
|
157
|
+
0: protectedHeader,
|
|
158
|
+
1: encryptedKey,
|
|
159
|
+
2: initializationVector,
|
|
160
|
+
3: ciphertext,
|
|
161
|
+
4: authenticationTag,
|
|
162
|
+
length,
|
|
163
|
+
} = jwe.split('.');
|
|
164
|
+
|
|
165
|
+
// Ensure that the JWE has the required number of parts.
|
|
166
|
+
if (length !== 5) {
|
|
167
|
+
throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Invalid JWE format. JWE must have 5 parts.');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Decrypt the JWE using the provided Key URI.
|
|
171
|
+
const flattenedJwe = await FlattenedJwe.decrypt({
|
|
172
|
+
jwe: {
|
|
173
|
+
ciphertext,
|
|
174
|
+
encrypted_key : encryptedKey || undefined,
|
|
175
|
+
iv : initializationVector || undefined,
|
|
176
|
+
protected : protectedHeader,
|
|
177
|
+
tag : authenticationTag || undefined,
|
|
178
|
+
},
|
|
179
|
+
key,
|
|
180
|
+
keyManager,
|
|
181
|
+
crypto,
|
|
182
|
+
options
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
if (!isValidJweHeader(flattenedJwe.protectedHeader)) {
|
|
186
|
+
throw new CryptoError(CryptoErrorCode.InvalidJwe, 'Decrypt operation failed due to missing or malformed JWE Protected Header');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return { plaintext: flattenedJwe.plaintext, protectedHeader: flattenedJwe.protectedHeader };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Encrypts plaintext to a JWE string in Compact Serialization format, encapsulating the content
|
|
194
|
+
* with the specified cryptographic protections.
|
|
195
|
+
*
|
|
196
|
+
* It constructs the JWE by encrypting the plaintext, then serializing the output to the
|
|
197
|
+
* compact format, which includes concatenating various components like the protected header,
|
|
198
|
+
* encrypted key, initialization vector, ciphertext, and authentication tag.
|
|
199
|
+
*
|
|
200
|
+
* @param params - The encryption parameters, including plaintext, JWE Protected Header,
|
|
201
|
+
* cryptographic key, and optional Key Manager and Crypto API instances.
|
|
202
|
+
* @returns A promise that resolves to a string representing the JWE in Compact Serialization
|
|
203
|
+
* format.
|
|
204
|
+
* @throws {@link CryptoError} if encryption fails or the input parameters are invalid.
|
|
205
|
+
*/
|
|
206
|
+
public static async encrypt<
|
|
207
|
+
TKeyManager extends KeyManager | undefined = KeyManager,
|
|
208
|
+
TCrypto extends CryptoApi | undefined = CryptoApi
|
|
209
|
+
>({
|
|
210
|
+
plaintext,
|
|
211
|
+
protectedHeader,
|
|
212
|
+
key,
|
|
213
|
+
keyManager = new LocalKeyManager(),
|
|
214
|
+
crypto = new AgentCryptoApi(),
|
|
215
|
+
options = {}
|
|
216
|
+
}: CompactJweEncryptParams<TKeyManager, TCrypto>
|
|
217
|
+
): Promise<string> {
|
|
218
|
+
const jwe = await FlattenedJwe.encrypt({ plaintext, protectedHeader, key, keyManager, crypto, options });
|
|
219
|
+
|
|
220
|
+
// Create the Compact Serialization, which is the string BASE64URL(UTF8(JWE Protected Header))
|
|
221
|
+
// || '.' || BASE64URL(JWE Encrypted Key) || '.' || BASE64URL(JWE Initialization Vector)
|
|
222
|
+
// || '.' || BASE64URL(JWE Ciphertext) || '.' || BASE64URL(JWE Authentication Tag).
|
|
223
|
+
return [jwe.protected, jwe.encrypted_key, jwe.iv, jwe.ciphertext, jwe.tag].join('.');
|
|
224
|
+
}
|
|
225
|
+
}
|