PKCS11 Objects
Sample code uses ES6 language features such as arrow functions and promises. For compatibility with IE11, code written with these features must be either transpiled using tools like Babel or refactored accordingly using callbacks.

Introduction

The PKCS #11 standard defines a platform-independent API to cryptographic tokens, such as hardware security modules (HSM), smart cards, and names the API itself "Cryptoki" (from "cryptographic token interface" and pronounced as "crypto-key" - but "PKCS #11" is often used to refer to the API as well as the standard that defines it). The API defines most commonly used cryptographic object types (RSAX.509 keys, DES/Triple DES Certificates/keys, etc.) and all the functions needed to use, create/generate, modify and delete those objects. This container relies on a PKCS#11 a library which handles the communication with the token/card. This can be a vendor specific library or an opensource one, please select the correct one depending on the type of token/card you are using.

Interface Summary

The Abstract PKCS #11 smartcard interface is summarised in the following snippet:
1
export interface AbstractPkcs11 {
2
certificates(slotId: string, parseCerts?: boolean, callback?: (error: T1CLibException, data: Pkcs11ObjectCertificatesResponse) => void): Promise<Pkcs11ObjectCertificatesResponse>;
3
signData(data: Pkcs11SignData, callback?: (error: T1CLibException, data: Pkcs11ObjectSignResponse) => void): Promise<Pkcs11ObjectSignResponse>;
4
slots(callback?: (error: T1CLibException, data: Pkcs11ObjectSlotsResponse) => void): Promise<Pkcs11ObjectSlotsResponse>;
5
slotsWithTokenPresent(callback?: (error: T1CLibException, data: Pkcs11ObjectSlotsResponse) => void): Promise<Pkcs11ObjectSlotsResponse>;
6
token(slotId: string, callback?: (error: T1CLibException, data: Pkcs11ObjectTokenResponse) => void): Promise<Pkcs11ObjectTokenResponse>;
7
}
Copied!

Pkcs11 object models

1
export class Pkcs11ObjectInfoResponse extends DataObjectResponse {
2
constructor(public data: Pkcs11Info, public success: boolean) {
3
super(data, success);
4
}
5
}
6
7
export class Pkcs11ObjectSign {
8
constructor(public data: string) {
9
}
10
}
11
12
export class Pkcs11ObjectSignResponse {
13
constructor(public data: Pkcs11ObjectSign, public success: boolean) {
14
}
15
}
16
17
export class Pkcs11ObjectSlots {
18
constructor(public slots: Pkcs11Slot[]) {
19
}
20
}
21
22
export class Pkcs11ObjectSlotsResponse {
23
constructor(public data: Pkcs11ObjectSlots, public success: boolean) {
24
}
25
}
26
27
28
export class Pkcs11ObjectCertificates {
29
constructor(public certificates: Pkcs11ObjectCertificate[]) {}
30
}
31
32
33
export class Pkcs11ObjectCertificate {
34
constructor(public id: string, public certificate: string, public parsed?: Certificate) {}
35
}
36
37
export class Pkcs11ObjectCertificatesResponse {
38
constructor(public data: Pkcs11ObjectCertificates, public success: boolean) {
39
}
40
}
41
42
export class Pkcs11SignData {
43
constructor(public slotId: string,
44
public certificateId: string,
45
public algorithm: string,
46
public data: string,
47
public pin?: string,
48
public osDialog?: boolean) {}
49
}
50
51
export class Pkcs11ObjectTokenResponse {
52
constructor(public data: Pkcs11TokenInfo, public success: boolean) {
53
}
54
}
55
56
export class Pkcs11SetConfigResponse {
57
constructor(public data: string, public success: boolean) {
58
}
59
}
60
61
export class Pkcs11Slots {
62
constructor(public slots: Pkcs11Slot[]) {
63
}
64
}
65
66
export class Pkcs11Slot {
67
constructor(public slot: number,
68
public description: string) {
69
}
70
}
71
72
export class Pkcs11TokenInfo {
73
constructor(public slot: string,
74
public label: string,
75
public manufacturerId: string,
76
public model: string,
77
public serialNumber: string,
78
public flags: string,
79
public ulMaxSessionCount: number,
80
public ulSessionCount: number,
81
public ulMaxRwSessionCount: number,
82
public ulMaxPinLen: number,
83
public ulMinPinLen: number,
84
public ulTotalPublicMemory: number,
85
public ulFreePublicMemory: number,
86
public ulTotalPrivateMemory: number,
87
public ulFreePrivateMemory: number,
88
public hardwareVersion: string,
89
public firmwareVersion: string) {
90
}
91
}
Copied!

Get the PKCS #11 container object

For more information on how to configure the JS client library see Configuration.
1
const winLocation = "C:\\Windows\\System32\\eTPKCS11.dll"
2
const macLocation = "/usr/local/lib/libeTPkcs11.dylib";
3
4
T1CSdk.T1CClient.initialize(config).then(res => {
5
client = res;
6
console.log("Client config: ", client.localConfig)
7
core = client.core();
8
9
// Depending on the OS select the appropriate location of the library
10
const pkcs11 = client.pkcs11(winLocation);
11
12
}, err => {});
13
Copied!
Depending on the OS you need to provide a valid location to the desired PKCS11 library to be used.
Call a function for the PKCS #11 container:
1
const slotId = 0;
2
3
pkcs11.certificates(slotId, true).then(res => {
4
console.log(JSON.stringify(data, null, ' '));
5
}, err => {
6
console.log("Error:",JSON.stringify(err, null, ' '));
7
});
Copied!

Reading data

Slots

This methods returns the available slots on the system.
1
pkcs11.slots();
Copied!
An example response:
1
{
2
"data":
3
[
4
{
5
"slot": 0,
6
"description": "eToken 5100",
7
},
8
{
9
"slot": 1,
10
"description": "",
11
}
12
]
13
"success": true
14
}
Copied!

Slots with tokens present

This method is similar the the slots endpoint but only returns a list of slots where a token is present.
1
pkcs11.slotsWithTokenPresent(callback);
Copied!
An example response:
1
{
2
"data":
3
[
4
{
5
"slot": 0,
6
"description": "eToken 5100"
7
}
8
]
9
"success": true
10
}
Copied!

Token

This methods returns the token information for a slot.
1
pkcs11.token(slot_id, callback);
Copied!
An example response:
1
{
2
"data":
3
{
4
"slot": 1,
5
"label": "Token Label",
6
"manufacturerId": "Gemalto",
7
"model": "Safenet Token e5100",
8
"serialNumber": "1234567890",
9
"flags": 7,
10
"maxSessionCount": 10,
11
"sessionCount": 1,
12
"maxRwSessionCount": 5,
13
"rwSessionCount": 1,
14
"maxPinLength": 20,
15
"minPinLength": 8,
16
"totalPublicMemory": 10000,
17
"freePublicMemory": 5000,
18
"totalPrivateMemory": 1000,
19
"freePrivateMemory": 50,
20
"hardwareVersion": "1",
21
"firmwareVersion": "1"
22
},
23
"success": true
24
}
Copied!

Certificates

This methods allows you to retrieve the certificates from the PKCS #11 token.
1
// retrieve certificates for token in slot_id 0
2
pkcs11.certificates(0, false);
Copied!
Response:
1
{
2
"data": [
3
{
4
"certificate": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
5
"id": "E8CE05618E79A79825722AE067EC0029851D860D"
6
},
7
{
8
"certificate": "MIIFjjCCA3agAwI...rTBDdrlEWVaLrY+M+xeIctrC0WnP7u4xg==",
9
"id": "9F8F1CD68867526B1DF919832219B217282D2B0B"
10
}
11
],
12
"success": true
13
}
Copied!

Signing data

To successfully sign data, we need the following parameters:
    Slot ID of the token to use
    Certificate ID of the signing certificate
    PIN code
    Hashed data to sign
    Hashing algorithm used
The slot id can be found using either a call to slots, slotsWithTokenPresent. Once the slot id is found, the certificates can be retrieved with a call to certificates. This then returns the certificate id. Now we can combine this with the PIN code and hashed data + hashing algorithm (SHA1, SHA256, SHA384, SHA512) to create the final signData call:

signData call

Returns signed data for provided input data.
1
var signDataPayload = {
2
slotId: 1,
3
certificateId: "9F8F1CD68867526B1DF919832219B217282D2B0B",
4
pin: "thepincodeforthetoken",
5
data: "hashed data"
6
algorithm: "sha256"
7
}
8
9
pkcs11.signData(signDataPayload, callback);
Copied!
An example response:
1
{
2
"data": "L3BWs7lvoajPg5tC9GEmlSMdXMcDkNDUVVvt+KFqbJbXkJdI3DGoM3IOeXLaeTCxKmJ33/QQApL1nEAMuHY38V41czyswfSdpnqeex3EisXKkiYHeNzt9AXeoxDtsWbGkejKqru6X4xzAy/1SnccjZ2BQB/uUwyfyyweFjbZAGCLfTzWraCxG0ud2c8itsjmsBgXSuGjLqxKIJXbtuX22gd0nc4LeZLA91sxaoNFBCEVlOgJ/oJncFylf6T/Tz9R4VOA9kSf6NB1tAMsFEsihgjZafu/rzlrOfzZw4YY/T32LKY4Y2zNnDhJAucqflZjopGadDvkkSmTvxxEJuE+Bw==",
3
"success": true
4
}
Copied!

Error Handling

Error Object

The functions specified are asynchronous and always need a callback function. The callback function will reply with a data object in case of success, or with an error object in case of an error. An example callback:
1
function callback(err,data) {
2
if(err){
3
console.log("Error:",JSON.stringify(err, null, ' '));
4
}
5
else {
6
console.log(JSON.stringify(data, null, ' '));
7
}
8
}
Copied!
The error object returned:
1
{
2
success: false,
3
description: "some error description",
4
code: "some error code"
5
}
Copied!
Last modified 1yr ago