Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major Update: Migrate from NAN to NAPI #104

Merged
merged 23 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5fab15a
feat: Remove NAN and use NAPI
microshine Jan 12, 2024
747f10e
ci: update ci runners
microshine Jan 14, 2024
14ec884
ci: update nodejs image
microshine Jan 14, 2024
4ef607b
fix: memory management in AttributesWrapper and get_args_attributes
microshine Jan 15, 2024
150cbf3
ci: update node version in test workflow
microshine Jan 15, 2024
6635466
fix: use pack pragma for Windows
microshine Jan 15, 2024
e7fce86
chore: use AdditionalOptions like it was in prev version
microshine Jan 15, 2024
c51827b
chore(deps): npm audit fix
microshine Jan 15, 2024
8220de1
refactor: update code to use std::vector for dynamic arrays and
microshine Jan 15, 2024
2460c61
ci: update actions
microshine Jan 15, 2024
28fa93e
chore: update module to version 2.0.0 and require Node.js v18
microshine Jan 15, 2024
2a32912
refactor: simplify worker
microshine Jan 15, 2024
404c901
refactor: optimize js implementation
microshine Jan 15, 2024
8a63c81
test: improve tests
microshine Jan 15, 2024
3b07771
fix: improve definitions
microshine Jan 15, 2024
57a1ae6
feat: sync headers to v2.40
microshine Jan 15, 2024
8537da4
feat: implement C_WaitForSlotEvent
microshine Jan 15, 2024
5eb9345
feat: add support for dual-function cryptographic operations
microshine Jan 15, 2024
db74f64
feat: implement params
microshine Jan 16, 2024
8e42215
chore: update text about PKCS#11 version
microshine Jan 16, 2024
9bd6a3d
feat: implement mech params
microshine Jan 16, 2024
78fc63e
fix: minor error
microshine Jan 16, 2024
646f836
2.0.1
microshine Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ on:
push:
branches:
- master
permissions:
contents: write
jobs:
build-and-deploy:
runs-on: ubuntu-latest
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
steps:
- name: Checkout
uses: actions/checkout@v2.3.1
uses: actions/checkout@v4
with:
persist-credentials: false

Expand All @@ -20,10 +22,9 @@ jobs:
- name: Build docs
run: npm run docs

- name: Deploy
uses: JamesIves/github-pages-deploy-action@3.6.1
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@v4
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages
FOLDER: docs
CLEAN: true
branch: gh-pages
folder: docs
clean: true
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '12.x'
node-version: '20.x'
- name: Install dependencies
run: npm install
- name: Build
Expand Down
19 changes: 7 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ on: [push, pull_request]

jobs:
build-and-test:
runs-on: macos-10.15
runs-on: macos-latest

strategy:
matrix:
# node-version: [10.x, 12.x, 14.x]
node-version: [16.x]
node-version: [18.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

Expand Down Expand Up @@ -56,22 +56,17 @@ jobs:
strategy:
matrix:
os:
- windows-2019
- windows-latest
- ubuntu-18.04
- ubuntu-latest
- macos-10.15
- macos-latest
node-version:
# - 12.x
# - 14.x
- 16.x
- 20.x # lts

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

Expand Down
84 changes: 16 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

[![NPM](https://nodei.co/npm/pkcs11js.png)](https://nodei.co/npm/pkcs11js/)

PKCS11js is a package for direct interaction with the PKCS#11 API, the standard interface for interacting with hardware crypto devices such as Smart Cards and Hardware Security Modules (HSMs). It was developed to the PKCS#11 2.40 specification and has been tested with a variety of devices.

We make a package called [Graphene](https://github.com/PeculiarVentures/graphene), it provides a simplistic Object Oriented interface for interacting with PKCS#11 devices, for most people this is the right level to build on. In some cases you may want to interact directly with the PKCS#11 API, if so PKCS11js is the package for you.
**Versioning Note:**
- Version 1.x was implemented using the `nan` module, which allowed the package to be built for older versions of Node.js.
- Starting from version 2.x, the module has been rewritten to use `napi`. As a result, the minimum required Node.js version is now v18.

PKCS#11 (also known as CryptoKI or PKCS11) is the standard interface for interacting with hardware crypto devices such as Smart Cards and Hardware Security Modules (HSMs).
For most use cases, we recommend our package [Graphene](https://github.com/PeculiarVentures/graphene), which provides a simplistic Object Oriented interface for interacting with PKCS#11 devices.

This was developed to the PKCS#11 2.30 specification, the 2.40 headers were not available at the time we created this, it should be easy enough to extend it for the new version at a later date.
This was developed to the PKCS#11 2.40 specification. It should be easy enough to extend it for any new versions at a later date.

It has been tested with :
- [SoftHSM2](https://www.opendnssec.org/softhsm/)
Expand Down Expand Up @@ -385,7 +388,7 @@ mod.C_Finalize();

### Example #11

Detect if smartcard is removed with C_WaitForSlotEvent function
Detect a slot event

```javascript
var pkcs11js = require("pkcs11js");
Expand All @@ -395,73 +398,18 @@ pkcs11.load("/usr/local/lib/softhsm/libsofthsm2.so");

pkcs11.C_Initialize();

var session;
var intervalId;

try {
// Getting info about PKCS11 Module
var module_info = pkcs11.C_GetInfo();

// Getting list of slots
var slots = pkcs11.C_GetSlotList(true);
var slot = slots[0];
console.log(slot);

// Getting info about slot
var slot_info = pkcs11.C_GetSlotInfo(slot);
// Getting info about token
var token_info = pkcs11.C_GetTokenInfo(slot);
console.log(slot_info);

// Getting info about Mechanism
var mechs = pkcs11.C_GetMechanismList(slot);
var mech_info = pkcs11.C_GetMechanismInfo(slot, mechs[0]);

session = pkcs11.C_OpenSession(slot, pkcs11js.CKF_RW_SESSION | pkcs11js.CKF_SERIAL_SESSION);

// Getting info about Session
var info = pkcs11.C_GetSessionInfo(session);
// pkcs11.C_Login(session, 1234, "password");

intervalId = setInterval(() => {
const rv = pkcs11.C_WaitForSlotEvent(pkcs11js.CKF_DONT_BLOCK, slot);
console.log('C_WaitForSlotEvent value : ' + rv.readUInt8(0));

if (rv.readUInt8(0) !== pkcs11js.CKR_NO_EVENT) {
/**
* Your code here to handle token removal for example
*/
}
}, 1000);

/**
* Your app code here
*/

// pkcs11.C_Logout(session);
}
catch(e){
const slotId = pkcs11.C_WaitForSlotEvent(pkcs11js.CKF_DONT_BLOCK);
if (slotId) {
console.log(`Slot ${slotId} has been inserted`);
} else {
console.log(`No slot event`);
}
} catch (e) {
console.error(e);
process.exit(1);
}
finally {
} finally {
pkcs11.C_Finalize();
}

function myCleanup() {
console.log('App specific cleanup code...');
clearInterval(intervalId);
try {
if (session) {
pkcs11.C_CloseSession(session);
pkcs11.C_Finalize();
}
}
catch(e){
}
console.log('Bye !');
};

process.on('SIGINT', myCleanup);
```

## Suitability
Expand Down
81 changes: 26 additions & 55 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,56 +1,27 @@

{
"variables": {
},
"targets": [
{
"include_dirs": [
"<!(node -e \"require(\'nan\')\")",
"includes"
],
'cflags!': ['-fno-exceptions'],
'cflags_cc!': ['-fno-exceptions'],
"target_name": "pkcs11",
"sources": [
"src/main.cpp",
"src/dl.cpp",
"src/const.cpp",
"src/pkcs11/error.cpp",
"src/pkcs11/v8_convert.cpp",
"src/pkcs11/template.cpp",
"src/pkcs11/mech.cpp",
"src/pkcs11/param.cpp",
"src/pkcs11/param_aes.cpp",
"src/pkcs11/param_rsa.cpp",
"src/pkcs11/param_ecdh.cpp",
"src/pkcs11/pkcs11.cpp",
"src/async.cpp",
"src/node.cpp"
],
'conditions': [
[
'OS=="mac"', {
'xcode_settings': {
'OTHER_CPLUSPLUSFLAGS': ['-std=c++14', '-stdlib=libc++', '-v'],
'OTHER_CFLAGS': ['-ObjC++'],
'OTHER_LDFLAGS': ['-stdlib=libc++'],
'MACOSX_DEPLOYMENT_TARGET': '10.7',
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
},
'libraries': [
'-lobjc'
],
},
'OS == "win"', {
'msvs_settings': {
'VCCLCompilerTool': {
'ExceptionHandling': 1,
'AdditionalOptions': [ '-std:c++17', ],
}
}
}
]
]
}
]
}
"targets": [
{
"target_name": "pkcs11",
"sources": [
"src/dl.cpp",
"src/common.cpp",
"src/main.cpp" ,
],
"include_dirs": [
"includes",
],
"defines": [
"NAPI_DISABLE_CPP_EXCEPTIONS",
],
'conditions': [
['OS=="win"', {
'msvs_settings': {
'VCCLCompilerTool': {
'AdditionalOptions': [ '-std:c++17' ],
},
},
}],
],
}
]
}
Loading
Loading