# @origyn/hsm-identity

> HSM identity

Latest version **1.1.3** (published 2021-10-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install @origyn/hsm-identity
pnpm add @origyn/hsm-identity
yarn add @origyn/hsm-identity
bun add @origyn/hsm-identity
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2021-10-25 |
| First published | 2021-09-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 26.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ~dmytry-origyn, dpori, yurii-origyn, oleksii_havrylenko |

## Links

- npm: https://www.npmjs.com/package/@origyn/hsm-identity
- Repository: https://github.com/ORIGYN-SA/hsm-identity
- Homepage: https://github.com/ORIGYN-SA/hsm-identity#readme
- Issues: https://github.com/ORIGYN-SA/hsm-identity/issues
- npm.io page: https://npm.io/package/@origyn/hsm-identity

## Dependencies (9)

- [buffer](https://npm.io/package/buffer.md) ^6.0.3
- [dotenv](https://npm.io/package/dotenv.md) ^10.0.0
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [@dfinity/agent](https://npm.io/package/@dfinity/agent.md) ^0.10.0
- [@dfinity/candid](https://npm.io/package/@dfinity/candid.md) ^0.10.0
- [@dfinity/identity](https://npm.io/package/@dfinity/identity.md) ^0.10.0
- [@dfinity/principal](https://npm.io/package/@dfinity/principal.md) ^0.10.0
- [node-webcrypto-p11](https://npm.io/package/node-webcrypto-p11.md) 2.3.6
- [@peculiar/webcrypto](https://npm.io/package/@peculiar/webcrypto.md) ^1.1.7

## Recent versions

- 1.1.3 (latest) — 2021-10-25
- 1.1.2 — 2021-10-25
- 1.1.1 — 2021-10-25
- 1.1.0 — 2021-09-28
- 1.0.0 — 2021-09-01

## README

**Pre-required NitroHsm**
========================
OpenSC is a set of software tools and libraries for working with smart cards with a focus on smart cards with cryptographic capabilities. OpenSC simplifies the use of smart cards in security applications such as authentication, encryption, and digital signatures

```sh
sudo apt-get update -y
sudo apt-get install -y opensc
```

> After installing the opensc package, the "pkcs11-tool" and "sc-hsm-tool" tools, as well as a file opensc-pkcs11.so which will be required for work.

**We also need to initialize the token using sc-hsm-tool, then key pair gen!**
1. pkcs11-tool --module opensc-pkcs11.so --init-token --init-pin --so-pin=3537363231383830 --new-pin=648219 --label="HSM" --pin=648219
2. pkcs11-tool --module opensc-pkcs11.so --login --pin 648219 --keypairgen --key-type EC:secp256k1 --label "HSM EC Key"

> To familiarization the sc-hsm-tool go [here](http://manpages.ubuntu.com/manpages/bionic/man1/sc-hsm-tool.1.html), and pkcs11-tool go [here](http://manpages.ubuntu.com/manpages/bionic/en/man1/pkcs11-tool.1.html) 

**Environments for NitroHSM!**
```sh
LIBRARY_PATH => path to opensc-pkcs11.so file
PIN => pin initialize from token 
```
> By default path opensc-pkcs11.so in "ubuntu" /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so

**Pre-required SoftHsm**
========================
SoftHSM is an implementation of a cryptographic store accessible through a PKCS #11 interface. You can use it to explore PKCS #11 without having a Hardware Security Module. It is being developed as a part of the OpenDNSSEC project. SoftHSM uses Botan for its cryptographic operations.

```sh
sudo apt-get update -y
sudo apt-get install -y softhsm2
```

**Whilst initializing token as a non-root user, we invariably try to access the default /etc/softhsm/softhsm2.conf which points tokens to be staged under /var/lib/softhsm/tokens whose ownership/permission is limited to be used by root and its associated groups.
Changing ownership/permission of /var/lib/softhsm/tokens doesn't solve the problem as we cannot access /etc/softhsm/softhsm2.conf in the first place given the access limitation, so we should be doing this instead:**


- cd $HOME
- mkdir -p $HOME/lib/softhsm/tokens
- cd $HOME/lib/softhsm/
- echo "directories.tokendir = $PWD/tokens" > softhsm2.conf
- export SOFTHSM2_CONF=$HOME/lib/softhsm/softhsm2.conf

**The token can be initialized using this command:**
1. softhsm2-util --init-token --slot 0 --label "token"
2. After that, are required to enter a **pin** and **so-pin** corresponding to the this from the **environment variable**.

> To familiarization the softhsm2-util go [here](http://manpages.ubuntu.com/manpages/bionic/en/man1/softhsm2-util.1.html)

**Environments for SoftHsm!**
```sh
LIBRARY_PATH => path to libsofthsm2.so file
PIN => pin initialize from token 
```

> By default path libsofthsm2.so in "ubuntu" /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so

**Documentation**
========================
SoftHSM2Identity is implemented from [SignIdentity](https://erxue-5aaaa-aaaab-qaagq-cai.raw.ic0.app/agent/classes/signidentity.html) which is located in [@dfinity/agent](https://erxue-5aaaa-aaaab-qaagq-cai.raw.ic0.app/agent/index.html)

To generate a key pair, **Crypto** [node-webcrypto-p11](https://www.npmjs.com/package/node-webcrypto-p11) is used

SoftHSM2Identity method static **create** contains the options [ISoftHSM2Config] and algorithm [ISoftHSM2Algorithm] required to create an instance identifier.

```
export interface IHSMConfig {
    name?: string; // this name of PKCS11 module
    slot?: number; // this is the logical section where the token is stored, softhsm2-util --slot <number>  The slot where the token is located
    readWrite?: boolean; // required to configure Crypto from node-webcrypto-p11
    pin?: string; // this is the PIN for the normal user
    extractable?: boolean; // parameter that affects the extraction of keys
}

export interface IHSMAlgorithm {
    name: string; // Name public key algorithm for creating a digital signature; [Supported algorithms](https://www.npmjs.com/package/node-webcrypto-p11)
    namedCurve: string; // The named elliptic curves are over a prime field; Mechanism supports extended list of named curves P-256, P-384, P-521, and K-256;
    hash: string; // The named hashing algorithm; example SHA-256
}
```

[Crypto](https://www.npmjs.com/package/node-webcrypto-p11) accepts config which is implemented from [CryptoParams](https://github.com/PeculiarVentures/node-webcrypto-p11/blob/562da55f5b250c0342b721498a1d32028ff17f95/src/typings/index.d.ts#L1)

```
Config Crypto {
  library: string; // Path to the library
  name?: string; // Name of PKCS11 module
  slot?: number; // Index of the slot
  readWrite?: boolean;
  pin: string; // PIN of the slot
}
```

**Methods HSMIdentity**
========================
| METHOD | DESCRIPTION | TYPE |
| ------ | ------ | ------ |
| create | Creates crypto-keys based on parameters | STATIC |
| import | Gets crypto-keys based on parameters | STATIC |
| getCryptoKeys | Gets crypto-key storage keys | STATIC |
| clearCryptoKeys | Clear crypto-key storage | STATIC |
| getPublicKey | Get public key from identity instance | INSTANCE |

**Installation**
========================
`$ npm i -S @origyn/hsm-identity`

**Examples**
========================
>In order to determine which tool to use NitroHSM or SoftHSM, pass the corresponding path to the file  [ opensc-pkcs11.so, libsofthsm2.so ] to the "library".

>You may set `LIBRARY_PATH` and `PIN` via env variables or pass them with a config object as shown below.
Env variables would be applied by default.
```
    import { NitroHSMIdentity } from '@origyn/hsm-identity';
    
    const config = {
        library: process.env.LIBRARY_PATH,
        pin: process.env.PIN,
    }

    const initAlgorithm = {
        name: 'ECDSA',
        namedCurve: 'K-256',
        hash: 'SHA-256',
    };
    
    const identity = await NitroHSMIdentity.create(config, initAlgorithm);
    console.log(identity.getPublicKey());

```
```
    import { NitroHSMIdentity } from '@origyn/hsm-identity';
    
    const config = {
        library: process.env.LIBRARY_PATH,
        pin: process.env.PIN,
    }

    const initAlgorithm = {
        name: 'ECDSA',
        namedCurve: 'K-256',
        hash: 'SHA-256',
    };
    
    // Example: ['534d438fd95d04d6a3313efd0e1b8b33']
    // To get the keys from the terminal, use `` pkcs11-tool --list-objects ``
    const keys = await NitroHSMIdentity.getCryptoKeys(config);
    if (!keys.length) {
        throw 'Keys empty, need create crypto key for use import';
    }
    
    const identity = await NitroHSMIdentity.import(keys[0], config, initAlgorithm);
    console.log(identity.getPublicKey());

```

---
_Source: https://npm.io/package/@origyn/hsm-identity · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
