{"id":18601550,"url":"https://github.com/peculiarventures/node-webcrypto-p11","last_synced_at":"2025-04-05T21:05:25.261Z","repository":{"id":5856417,"uuid":"47779576","full_name":"PeculiarVentures/node-webcrypto-p11","owner":"PeculiarVentures","description":"A WebCrypto Polyfill for Node in typescript built on PKCS#11.","archived":false,"fork":false,"pushed_at":"2024-10-11T12:08:15.000Z","size":1233,"stargazers_count":44,"open_issues_count":28,"forks_count":15,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-10-29T18:10:04.632Z","etag":null,"topics":["ecc","hash","pkcs11","rsa","secp256k1","webcrypto"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PeculiarVentures.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"PeculiarVentures","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2015-12-10T18:21:56.000Z","updated_at":"2024-10-11T12:08:18.000Z","dependencies_parsed_at":"2024-01-23T00:38:42.373Z","dependency_job_id":"4f1d9183-8f17-4c65-9e43-b9b6d762182d","html_url":"https://github.com/PeculiarVentures/node-webcrypto-p11","commit_stats":{"total_commits":467,"total_committers":6,"mean_commits":77.83333333333333,"dds":"0.23340471092077086","last_synced_commit":"bdb50bd4f7b809aa2bb3b3a7a0b861912429e96d"},"previous_names":[],"tags_count":62,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fnode-webcrypto-p11","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fnode-webcrypto-p11/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fnode-webcrypto-p11/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeculiarVentures%2Fnode-webcrypto-p11/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PeculiarVentures","download_url":"https://codeload.github.com/PeculiarVentures/node-webcrypto-p11/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247399871,"owners_count":20932876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecc","hash","pkcs11","rsa","secp256k1","webcrypto"],"created_at":"2024-11-07T02:08:43.388Z","updated_at":"2025-04-05T21:05:25.240Z","avatar_url":"https://github.com/PeculiarVentures.png","language":"TypeScript","funding_links":["https://github.com/sponsors/PeculiarVentures"],"categories":[],"sub_categories":[],"readme":"# node-webcrypto-p11\n\n[![license](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://raw.githubusercontent.com/PeculiarVentures/node-webcrypto-p11/master/LICENSE)\n![test](https://github.com/PeculiarVentures/node-webcrypto-p11/workflows/test/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/PeculiarVentures/node-webcrypto-p11/badge.svg?branch=master)](https://coveralls.io/github/PeculiarVentures/node-webcrypto-p11?branch=master)\n[![npm version](https://badge.fury.io/js/node-webcrypto-p11.svg)](https://badge.fury.io/js/node-webcrypto-p11)\n\n[![NPM](https://nodei.co/npm/node-webcrypto-p11.png)](https://nodei.co/npm/node-webcrypto-p11/)\n\nWe wanted to be able to write Javascript that used crypto on both the client and the server but we did not want to rely on Javascript implementations of crypto. The only native cryptography availible in browser is [Web Crypto](caniuse.com/#search=cryptography), this resulted in us creating a [native polyfil for WebCrypto based on Openssl](https://github.com/PeculiarVentures/node-webcrypto-ossl).\n\nOur project also required us to utilize Hardware Security Modules and smart cards on the server side so we made a [library called Graphene that made it possible to use PKCS#11 devices from within Nodejs](https://github.com/PeculiarVentures/graphene).\n\nWe then thought that in most cases others did not care about interacting with the token directly and would prefer a higher level API they were already familiar with. We hope that library is `node-webcrypto-p11`, if you have code based on WebCrypto (for example the excelent [js-jose](https://github.com/square/js-jose)) with only a change in a constructor you can work with PKCS#11 devices.\n\nFor example to generate a key you this is all it takes:\n\n```js\nconst { Crypto } = require(\"node-webcrypto-p11\");\nconst config = {\n  library: \"/usr/local/lib/softhsm/libsofthsm2.so\",\n  name: \"SoftHSM v2.0\",\n  slot: 0,\n  readWrite: true,\n  pin: \"12345\",\n};\n\nconst crypto = new Crypto(config);\n\nconst keys = await crypto.subtle.generateKey(\n  {\n    name: \"RSASSA-PKCS1-v1_5\",\n    modulusLength: 1024,\n    publicExponent: new Uint8Array([1, 0, 1]),\n    hash: {\n      name: \"SHA-1\",\n    },\n  },\n  true,\n  [\"sign\", \"verify\"]\n);\n```\n\n## WARNING\n\n**At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.**\n\n## Algorithms\n\n## Supported algorithms\n\n| Algorithm name                | generateKey | digest | export/import | sign/verify | encrypt/decrypt | wrapKey/unwrapKey | derive |\n| ----------------------------- | ----------- | ------ | ------------- | ----------- | --------------- | ----------------- | ------ |\n| SHA-1                         |             | X      |               |             |                 |                   |        |\n| SHA-256                       |             | X      |               |             |                 |                   |        |\n| SHA-384                       |             | X      |               |             |                 |                   |        |\n| SHA-512                       |             | X      |               |             |                 |                   |        |\n| RSASSA-PKCS1-v1_5             | X           |        | X             | X           |                 |                   |        |\n| RSAES-PKCS1-v1_5 \u003csub\u003e2\u003c/sub\u003e | X           |        | X             |             | X               | X                 |        |\n| RSA-PSS                       | X           |        | X             | X           |                 |                   |        |\n| RSA-OAEP                      | X           |        | X             |             | X               | X                 |        |\n| AES-CBC                       | X           |        | X             |             | X               | X                 |        |\n| AES-ECB \u003csub\u003e2\u003c/sub\u003e          | X           |        | X             |             | X               | X                 |        |\n| AES-GCM                       | X           |        | X             |             | X               | X                 |        |\n| ECDSA \u003csub\u003e1\u003c/sub\u003e            | X           |        | X             | X           |                 |                   |        |\n| ECDH \u003csub\u003e2\u003c/sub\u003e             | X           |        | X             |             |                 |                   | X      |\n| HMAC                          | X           |        | X             | X           |                 |                   |        |\n\n\u003csup\u003e1\u003c/sup\u003e Mechanism supports extended list of named curves `P-256`, `P-384`, `P-521`, and `K-256`\n\n\u003csup\u003e2\u003c/sup\u003e Mechanism is not defined by the WebCrypto specifications. Use of mechanism in a safe way is hard, it was added for the purpose of enabling interoperability with an existing system. We recommend against its use unless needed for interoperability.\n\n## Installation\n\n### NPM\n\n```\nnpm install node-webcrypto-p11\n```\n\n### Clone Repository\n\n```\ngit clone https://github.com/PeculiarVentures/node-webcrypto-p11\ncd node-webcrypto-p11\n```\n\n### Install SoftHSM2\n\n- For OSX see the [instructions here](https://github.com/opendnssec/SoftHSMv2/blob/develop/OSX-NOTES.md)\n- For linux [instructions here](https://github.com/opendnssec/SoftHSMv2/blob/develop/README.md)\n\n### Install\n\n```\nnpm install\n```\n\n### Test\n\n```\nmocha\n```\n\n### Configuration\n\nTests and samples use a file called [config.js](https://github.com/PeculiarVentures/node-webcrypto-p11/blob/master/test/config.js) file for PKCS11 module configuration. The format of which is:\n\n```js\nmodule.exports = {\n    library: \"path/to/pkcs11/module.so\",\n    name: \"Name of PKCS11 module\",\n    slot: 0,        // number of slot\n    pin: \"password\"\n    readWrite: true,\n    vendors: []     // list of vendor files, optional\n}\n```\n\n## Threats\n\nThe threat model is defined in terms of what each possible attacker can achieve. The list is intended to be exhaustive.\n\n### Assumptions\n\nTODO: ADD ASSUMPTIONS\n\n### Threats From A node-webcrypto-p11 Defect\n\n`node-webcrypto-p11` handles ciphertext, cleartext, and sessions. A defect in this library could result in these values being exposed to an attacker. Examples of such defects include:\n\n- Buffer, Integer or other overflow related defects,\n- Parsing errors,\n- Logic errors,\n- Weak user seperation or permissions.\n\n### Threats From A PKCS#11 defect\n\nPKCS#11 implementations are often old, poorly maintained and incomplete. This can obviously lead to defects. Defects in the PKCS#11 implementation can result in:\n\n- Weakly implemented or applied cryptographic primitives,\n- Leaked sessions or secrets that expose use of the key,\n- Leaked cryptographic key material.\n\n### Threats From Weak Cryptography\n\nSecure use of cryptography requires the implementor to understand the security properties of a given algorithm as well as how to use it in a secure construction.\n\nAdditionally this library exposes some algorithms that may have known weakneses or are simply too old to be used safely.\n\n### Threats From Improper Use Of Cryptography\n\nIt is easy to apply cryptography but hard to apply it correctly. Algorithms each have their own security properties and appropriate constructions. The consumer of this library is responsible for understanding how to use the exposed algorithms securely.\n\n### Generates `ECDSA` key pair with named curve `P-256` and signs/verifies text message.\n\n```js\nconst { Crypto } = require(\"node-webcrypto-p11\");\n\nconst config = {\n  library: \"/usr/local/lib/softhsm/libsofthsm2.so\",\n  name: \"SoftHSM v2.0\",\n  slot: 0,\n  readWrite: true,\n  pin: \"12345\",\n};\n\nconst crypto = new Crypto(config);\n\nconst keys = await crypto.subtle.generateKey(\n  { name: \"ECDSA\", namedCurve: \"P-256\" },\n  false,\n  [\"sign\", \"verify\"]\n);\nconst signature = await crypto.subtle.sign(\n  { name: \"ECDSA\", hash: \"SHA-256\" },\n  keys.privateKey,\n  Buffer.from(\"Hello world!\")\n);\nconsole.log(`Signature: ${signature}`);\nconst ok = await crypto.subtle.verify(\n  { name: \"ECDSA\", hash: \"SHA-256\" },\n  keys.publicKey,\n  signature,\n  Buffer.from(\"Hello world!\")\n);\nconsole.log(`Verification: ${ok}`);\n```\n\n## Key Storage\n\nThe [CryptoKeyStorage](https://github.com/PeculiarVentures/webcrypto-docs/blob/master/KEY_STORAGE.md#cryptokeystorage) interface enables you to persist and retrieve keys across sessions.\n\n### Generate a cryptographic key and store it\n\n```js\nconst keys = await crypto.subtle.generateKey(\n  { name: \"ECDSA\", namedCurve: \"P-256\" },\n  false,\n  [\"sign\", \"verify\"]\n);\n// set private key to storage\nconst privateKeyID = await crypto.keyStorage.setItem(keys.privateKey);\n// set public key to storage\nconst publicKeyID = await crypto.keyStorage.setItem(keys.publicKey);\n// get list of keys\nconst indexes = await crypto.keyStorage.keys();\nconsole.log(indexes); // ['private-3239...', 'public-3239...']\n// get key by id\nconst privateKey = await crypto.keyStorage.getItem(\"private-3239...\");\n// signing data\nconst signature = await crypto.subtle.sign(\n  { name: \"ECDSA\", hash: \"SHA-256\" },\n  key,\n  Buffer.from(\"Message here\")\n);\nconsole.log(\"Signature:\", Buffer.from(signature).toString(\"hex\"));\n```\n\n## Certificate Storage\n\nThe [CryptoCertificateStorage](https://github.com/PeculiarVentures/webcrypto-docs/blob/master/CERT_STORAGE.md#cryptocertstorage) interface enables you to persist and retrieve certificates across sessions.\n\n### Add certificate to storage and use it for verification of signed data\n\n```javascript\nconst X509_RAW = Buffer.from(\"308203A830820290A003020...\", \"hex\");\n\nconst x509 = await crypto.certStorage.importCert(\n  \"raw\",\n  X509_RAW,\n  { name: \"RSASSA-PKCS1-v1_5\", hash: \"SHA-256\" },\n  [\"verify\"]\n);\nconsole.log(x509.subjectName); // C=name, O=...\nconst index = await crypto.certStorage.setItem(x509);\nconsole.log(index); // x509-2943...\nconst ok = await crypto.subtle.verify(\n  { name: \"RSASSA-PKCS1-v1_5\" },\n  x509.publicKey,\n  SIGNATURE,\n  MESSAGE\n);\nconsole.log(\"Signature:\", ok);\n```\n\n## Bug Reporting\n\nPlease report bugs either as pull requests or as issues in the issue tracker. Backwater has a full disclosure vulnerability policy. Please do NOT attempt to report any security vulnerability in this code privately to anybody.\n\n## Related\n\n- [webcrypto](https://github.com/PeculiarVentures/webcrypto)\n- [node-webcrypto-ossl](https://github.com/PeculiarVentures/node-webcrypto-ossl)\n- [webcrypto-liner](https://github.com/PeculiarVentures/webcrypto-liner)\n- [WebCrypto Examples](https://github.com/PeculiarVentures/webcrypto-docs#webcrypto)\n- [Graphene](https://github.com/PeculiarVentures/graphene)\n- [pkcs11js](https://github.com/PeculiarVentures/pkcs11js)\n- [OpenCryptoKi](https://sourceforge.net/projects/opencryptoki/)\n- [SoftHSM](https://github.com/opendnssec/SoftHSMv2)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeculiarventures%2Fnode-webcrypto-p11","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeculiarventures%2Fnode-webcrypto-p11","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeculiarventures%2Fnode-webcrypto-p11/lists"}