@aws-crypto/node-jose_aws-kms-extension

中文文档
2.1.0发布于 2024/05/20Apache-2.0TypeScriptCommonJS近 30 天下载 6636

This library is an extension of node-jose library with AWS KMS support

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. Examples
  4. Importing variables from modules @aws-crypto/node-jose_aws-…
  5. Creating AWS KMS Client
  6. Signing
  7. Encryption
  8. Decryption
  9. Verification
  10. License

node-jose_aws-kms-extension

This library is an extension of the node-jose library, and uses monkey-patching to extend the capabilities of node-jose. It provides JWE-based encrypters/decrypters and JWS-based signers/verifiers for cryptographic operations with keys stored in AWS Key Management Service (KMS).

Installation

You can install the library from npm using the following command. This library requires Node.js 18 or above.

npm install @aws-crypto/node-jose_aws-kms-extension

Usage

Import the necessary classes from @aws-crypto/node-jose_aws-kms-extension module:

import {
  KMSAsymmetricSigningKey,
  KMSSymmetricCEK,
  KMSSymmetricKey,
} from '@aws-crypto/node-jose_aws-kms-extension';

Import the main jose object.

import { jose } from '@aws-crypto/node-jose_aws-kms-extension';

Now, you can use all existing node-jose features as you would in the absence of this library. This library adds AWS KMS support transparently. You can use your AWS KMS keys for various encryption and signing operations using the regular node-jose functions. AWS KMS-specific algorithm names are supported.

Supported node-jose functions include:

  1. jose.JWE.createEncrypt()
  2. jose.JWE.createDecrypt()
  3. jose.JWS.createSign()
  4. jose.JWS.createVerify()

This library uses @aws-sdk/client-kms for all its communication with AWS KMS.

Supported KMS Signing Algorithms

Supported KMS Encryption Algorithms

Supported Data Key Spec

Examples

Importing variables from modules @aws-crypto/node-jose_aws-kms-extension and @aws-sdk/client-kms

import {
  jose,
  KMSAsymmetricSigningKey,
  KMSSymmetricCEK,
  KMSSymmetricKey,
} from '@aws-crypto/node-jose_aws-kms-extension';
import { KMSClient } from "@aws-sdk/client-kms";

Creating AWS KMS Client

const kmsClient: KMSClient = new KMSClient({ region: 'REGION' });

Signing

  1. Prepare signing options with format as compact, and header fields.
  2. Prepare signatory with key as KMSAsymmetricSigningKey object.
  3. Use jose.JWS.createSign method to sign the payload.
const signingOpts = {
  format: 'compact',
  fields: {
    alg: 'SIGNING_ALGORITHM',
    kid: 'KMS_SIGNING_KEY_ID',
    JWS_CRIT_HEADER1: 'JWS_CRIT_HEADER1_VALUE',
    JWS_CRIT_HEADER2: 'JWS_CRIT_HEADER2_VALUE',
    crit: ['JWS_CRIT_HEADER1', 'JWS_CRIT_HEADER2'],
  },
};

const signatory = {
  key: new KMSAsymmetricSigningKey('KMS_SIGNING_KEY_ID', kmsClient),
};

const dataToSign = 'This is the data to sign';
const signer = jose.JWS.createSign(signingOpts, signatory);
const jws: Promise<string> = signer
  .update(dataToSign)
  .final()
  .then(function (result: string) {
    console.log('JWS: ', result);
    return result;
  });

Encryption

  1. Prepare encryption options with format as compact, header fields and cek as KMSSymmetricCEK object.
  2. Prepare encryption recipient with reference as false, and key as KMSSymmetricKey object. Use same KMS key to create KMSSymmetricCEK and KMSSymmetricKey.
  3. Use jose.JWE.createEncrypt method to encrypt jws payload.
const encryptionOpts = {
  format: 'compact',
  fields: {
    kid: 'ENCRYPTION_KMS_KEY_ID',
    alg: 'KEY_ENCRYPTION_ALGORITHM',
    enc: 'CONTENT_ENCRYPTION_ALGORITHM',
    JWE_CRIT_HEADER1: 'JWE_CRIT_HEADER1_VALUE',
    JWE_CRIT_HEADER2: 'JWE_CRIT_HEADER2_VALUE',
    crit: ['JWE_CRIT_HEADER1', 'JWE_CRIT_HEADER2'],
  },
  cek: new KMSSymmetricCEK('KMS_ENCRYPTION_KEY_ID', kmsClient, 'KEY_SPEC'),
};

const encryptionRecipient = {
  reference: false,
  key: new KMSSymmetricKey('KMS_ENCRYPTION_KEY_ID', kmsClient),
};

const encrypter = jose.JWE.createEncrypt(
  encryptionOpts,
  encryptionRecipient,
);
const jwe: Promise<string> = jws.then(function (jws: string) {
  return encrypter
    .update(jws)
    .final()
    .then(function (result: string) {
      console.log('JWE: ', result);
      return result;
    });
});

Decryption

  1. Prepare decryption options with handlers to process jwe critical headers.
  2. Preare decryption assumed key as KMSSymmetricKey object
  3. Use jose.JWE.createDecrypt method to decrypt jwe payload.
const decryption_opts = {
  handlers: {
    JWE_CRIT_HEADER1: function(jwe: any) {
      console.log(jwe.header.JWE_CRIT_HEADER1)
      // process JWE_CRIT_HEADER1
    },
    JWE_CRIT_HEADER2: function(jwe: any) {
      console.log(jwe.header.JWE_CRIT_HEADER2)
      // process JWE_CRIT_HEADER2
    }
  },
};

const decryption_assumedKey = new KMSSymmetricKey(
  'KMS_ENCRYPTION_KEY_ID',
  kmsClient,
);

const decrypter = jose.JWE.createDecrypt(
  decryption_assumedKey,
  decryption_opts,
);

const decyptedContent: Promise<any> = jwe.then(function (jwe: string) {
  return decrypter.decrypt(jwe).then(function (result: any) {
    console.log('JWE decryption result: ', result);
    return result;
  });
});

Verification

  1. Prepare verification options with handlers to process jws critical headers.
  2. Prepare verification assumed key as KMSAsymmetricSigningKey object
  3. Use jose.JWS.createVerify method to verify jws payload.
const verification_opts = {
  handlers: {
    JWS_CRIT_HEADER1: function(jws: any) {
      console.log(jws.header.JWS_CRIT_HEADER1)
      // process JWS_CRIT_HEADER1
    },
    JWS_CRIT_HEADER2: function(jws: any) {
      console.log(jws.header.JWS_CRIT_HEADER2)
      // process JWS_CRIT_HEADER2
    }
  },
};

const verificationAssumedKey = new KMSAsymmetricSigningKey(
  'KMS_SIGNING_KEY_ID',
  kmsClient,
);

const verifier = jose.JWS.createVerify(
  verificationAssumedKey,
  verification_opts,
);
const verificationResult: Promise<any> = decyptedContent.then(function (
  decryptedContent: any,
) {
  const payloadToVerify = jose.util.utf8.encode(decryptedContent.payload);
  return verifier.verify(payloadToVerify).then(function (result: any) {
    console.log('JWS verification result: ', result);
    console.log('Verified payload: ', jose.util.utf8.encode(result.payload));
    return result;
  });
});

Note: KMS_SIGNING_KEY_ID and KMS_ENCRYPTION_KEY_ID can be KMS key ID, key ARN, alias name, or alias ARN. When using an alias name, it will have prefix alias/. It should be key ARN or alias ARN to specify in different Amazon Web Services account.

Security

See CONTRIBUTING for more information.

License

This library is distributed under the Apache License, Version 2.0, see LICENSE and NOTICE for more information.

版本与发布记录

@aws-crypto/node-jose_aws-kms-extension 共发布 4 个版本,首次发布于 2023/09/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.0latest2024/05/202 年前
2.0.22023/11/073 年前
2.0.12023/10/123 年前
2.0.02023/09/303 年前

关于 @aws-crypto/node-jose_aws-kms-extension 的常见问题

@aws-crypto/node-jose_aws-kms-extension 是什么?

This library is an extension of node-jose library with AWS KMS support

如何安装 @aws-crypto/node-jose_aws-kms-extension?

在项目目录运行 npm install @aws-crypto/node-jose_aws-kms-extension,或使用 pnpm add / yarn add / bun add。

@aws-crypto/node-jose_aws-kms-extension 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-crypto/node-jose_aws-kms-extension 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aws-crypto/node-jose_aws-kms-extension 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws-crypto/node-jose_aws-kms-extension 可以商用吗?

@aws-crypto/node-jose_aws-kms-extension 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws-crypto/node-jose_aws-kms-extension 的下载量有多少?

根据 npm 官方下载统计,@aws-crypto/node-jose_aws-kms-extension 在 2026/10/08 – 2026/11/06 这 30 天内被下载 6,636 次,日均约 221 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-crypto/node-jose_aws-kms-extension 还在维护吗?

最新版本 2.1.0 发布于 2024/05/20(2 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。