Skip to content

Commit

Permalink
Merge pull request #104 from PeculiarVentures:napi
Browse files Browse the repository at this point in the history
Major Update: Migrate from NAN to NAPI
  • Loading branch information
microshine authored Jan 16, 2024
2 parents e30a91f + 646f836 commit ab0046f
Show file tree
Hide file tree
Showing 45 changed files with 9,709 additions and 7,383 deletions.
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

0 comments on commit ab0046f

Please sign in to comment.