@hello.nrfcloud.com/certificate-helpers 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -0
- package/README.md +14 -0
- package/dist/ca.d.ts +4 -0
- package/dist/ca.js +39 -0
- package/dist/der.d.ts +1 -0
- package/dist/der.js +7 -0
- package/dist/device.d.ts +19 -0
- package/dist/device.js +78 -0
- package/dist/inspect.d.ts +2 -0
- package/dist/inspect.js +10 -0
- package/dist/locations.d.ts +19 -0
- package/dist/locations.js +29 -0
- package/dist/production.d.ts +4 -0
- package/dist/production.js +40 -0
- package/package.json +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Nordic Semiconductor ASA | nordicsemi.no
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Certificate Helpers [](https://www.npmjs.com/package/@hello.nrfcloud.com/certificate-helpers)
|
|
2
|
+
|
|
3
|
+
[](https://github.com/hello-nrfcloud/certificate-helpers/actions)
|
|
4
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
5
|
+
[](https://renovatebot.com)
|
|
6
|
+
[](https://github.com/conventional-changelog/commitlint/tree/master/@commitlint/config-conventional)
|
|
7
|
+
[](https://github.com/prettier/prettier/)
|
|
8
|
+
[](https://github.com/typescript-eslint/typescript-eslint)
|
|
9
|
+
|
|
10
|
+
Helper functions for creating device certificates.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
npm i --save --save-exact @hello.nrfcloud.com/certificate-helpers
|
package/dist/ca.d.ts
ADDED
package/dist/ca.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { stat } from 'node:fs/promises';
|
|
2
|
+
import run from '@bifravst/run';
|
|
3
|
+
import { simulatorCALocations } from './locations.js';
|
|
4
|
+
export const createCA = async (destinationFolder, CN, email) => {
|
|
5
|
+
// CA certificate
|
|
6
|
+
const certificates = simulatorCALocations(destinationFolder);
|
|
7
|
+
// Create a CA private key
|
|
8
|
+
try {
|
|
9
|
+
await stat(certificates.certificate);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
await run({
|
|
13
|
+
command: 'openssl',
|
|
14
|
+
args: ['genrsa', '-out', certificates.privateKey, '2048'],
|
|
15
|
+
});
|
|
16
|
+
let sub = `/C=NO/ST=Trondelag/L=Trondheim/O=Nordic Semiconductor ASA/OU=hello.nrfcloud.com/CN=${CN}`;
|
|
17
|
+
if (email !== undefined)
|
|
18
|
+
sub = `${sub}/emailAddress=${email}`;
|
|
19
|
+
await run({
|
|
20
|
+
command: 'openssl',
|
|
21
|
+
args: [
|
|
22
|
+
'req',
|
|
23
|
+
'-x509',
|
|
24
|
+
'-new',
|
|
25
|
+
'-nodes',
|
|
26
|
+
'-key',
|
|
27
|
+
certificates.privateKey,
|
|
28
|
+
'-sha256',
|
|
29
|
+
'-days',
|
|
30
|
+
'10957',
|
|
31
|
+
'-out',
|
|
32
|
+
certificates.certificate,
|
|
33
|
+
'-subj',
|
|
34
|
+
sub,
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return certificates;
|
|
39
|
+
};
|
package/dist/der.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const toPEM: (derLocation: string, pemLocation: string) => Promise<void>;
|
package/dist/der.js
ADDED
package/dist/device.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const signDeviceCertificate: ({ dir, deviceId, caCertificateLocation, caPrivateKeyLocation, }: {
|
|
2
|
+
dir: string;
|
|
3
|
+
deviceId: string;
|
|
4
|
+
caCertificateLocation: string;
|
|
5
|
+
caPrivateKeyLocation: string;
|
|
6
|
+
}) => Promise<void>;
|
|
7
|
+
export declare const createDeviceCertificate: ({ dest, caCertificates, deviceId, }: {
|
|
8
|
+
dest: string;
|
|
9
|
+
caCertificates: {
|
|
10
|
+
privateKey: string;
|
|
11
|
+
certificate: string;
|
|
12
|
+
};
|
|
13
|
+
deviceId: string;
|
|
14
|
+
}) => Promise<{
|
|
15
|
+
privateKey: string;
|
|
16
|
+
certificate: string;
|
|
17
|
+
CSR: string;
|
|
18
|
+
signedCert: string;
|
|
19
|
+
}>;
|
package/dist/device.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import run from '@bifravst/run';
|
|
2
|
+
import { deviceCertificateLocations } from './locations.js';
|
|
3
|
+
export const signDeviceCertificate = async ({ dir, deviceId, caCertificateLocation, caPrivateKeyLocation, }) => {
|
|
4
|
+
const { CSR: deviceCSRLocation, signedCert: deviceSignedCertLocation } = deviceCertificateLocations(dir, deviceId);
|
|
5
|
+
await run({
|
|
6
|
+
command: 'openssl',
|
|
7
|
+
args: [
|
|
8
|
+
'x509',
|
|
9
|
+
'-req',
|
|
10
|
+
'-CA',
|
|
11
|
+
caCertificateLocation,
|
|
12
|
+
'-CAkey',
|
|
13
|
+
caPrivateKeyLocation,
|
|
14
|
+
'-in',
|
|
15
|
+
deviceCSRLocation,
|
|
16
|
+
'-out',
|
|
17
|
+
deviceSignedCertLocation,
|
|
18
|
+
'-days',
|
|
19
|
+
'10957',
|
|
20
|
+
],
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
export const createDeviceCertificate = async ({ dest, caCertificates, deviceId, }) => {
|
|
24
|
+
const deviceCertificates = deviceCertificateLocations(dest, deviceId);
|
|
25
|
+
// Device private key
|
|
26
|
+
await run({
|
|
27
|
+
command: 'openssl',
|
|
28
|
+
args: [
|
|
29
|
+
'ecparam',
|
|
30
|
+
'-out',
|
|
31
|
+
deviceCertificates.privateKey,
|
|
32
|
+
'-name',
|
|
33
|
+
'prime256v1',
|
|
34
|
+
'-genkey',
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
// Device certificate
|
|
38
|
+
await run({
|
|
39
|
+
command: 'openssl',
|
|
40
|
+
args: [
|
|
41
|
+
'req',
|
|
42
|
+
'-x509',
|
|
43
|
+
'-new',
|
|
44
|
+
'-nodes',
|
|
45
|
+
'-key',
|
|
46
|
+
deviceCertificates.privateKey,
|
|
47
|
+
'-sha256',
|
|
48
|
+
'-days',
|
|
49
|
+
'10957',
|
|
50
|
+
'-out',
|
|
51
|
+
deviceCertificates.certificate,
|
|
52
|
+
'-subj',
|
|
53
|
+
`/CN=${deviceId}`,
|
|
54
|
+
],
|
|
55
|
+
});
|
|
56
|
+
// Create CSR
|
|
57
|
+
await run({
|
|
58
|
+
command: 'openssl',
|
|
59
|
+
args: [
|
|
60
|
+
'req',
|
|
61
|
+
'-key',
|
|
62
|
+
deviceCertificates.privateKey,
|
|
63
|
+
'-new',
|
|
64
|
+
'-out',
|
|
65
|
+
deviceCertificates.CSR,
|
|
66
|
+
'-subj',
|
|
67
|
+
`/CN=${deviceId}`,
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
// Sign device cert
|
|
71
|
+
await signDeviceCertificate({
|
|
72
|
+
dir: dest,
|
|
73
|
+
deviceId,
|
|
74
|
+
caCertificateLocation: caCertificates.certificate,
|
|
75
|
+
caPrivateKeyLocation: caCertificates.privateKey,
|
|
76
|
+
});
|
|
77
|
+
return deviceCertificates;
|
|
78
|
+
};
|
package/dist/inspect.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import run from '@bifravst/run';
|
|
2
|
+
export const inspectCert = async (location) => run({
|
|
3
|
+
command: 'openssl',
|
|
4
|
+
args: ['x509', '-text', '-noout', '-in', location],
|
|
5
|
+
});
|
|
6
|
+
export const inspectString = async (cert) => run({
|
|
7
|
+
command: 'openssl',
|
|
8
|
+
args: ['x509', '-text', '-noout'],
|
|
9
|
+
input: cert,
|
|
10
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Environment } from 'aws-cdk-lib';
|
|
2
|
+
/**
|
|
3
|
+
* Provide the directory to store certificates
|
|
4
|
+
*/
|
|
5
|
+
export declare const ensureCertificateDir: (env: Required<Environment>) => string;
|
|
6
|
+
export declare const simulatorCALocations: (certificatesDir: string) => {
|
|
7
|
+
privateKey: string;
|
|
8
|
+
certificate: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const productionRunCALocations: (certificatesDir: string, productionRun: number) => {
|
|
11
|
+
privateKey: string;
|
|
12
|
+
certificate: string;
|
|
13
|
+
};
|
|
14
|
+
export declare const deviceCertificateLocations: (certificatesDir: string, deviceId: string) => {
|
|
15
|
+
privateKey: string;
|
|
16
|
+
certificate: string;
|
|
17
|
+
CSR: string;
|
|
18
|
+
signedCert: string;
|
|
19
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { mkdirSync, statSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Provide the directory to store certificates
|
|
5
|
+
*/
|
|
6
|
+
export const ensureCertificateDir = (env) => {
|
|
7
|
+
const dirName = path.join(process.cwd(), 'certificates', `${env.account}@${env.region}`);
|
|
8
|
+
try {
|
|
9
|
+
statSync(dirName);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
mkdirSync(dirName, { recursive: true });
|
|
13
|
+
}
|
|
14
|
+
return dirName;
|
|
15
|
+
};
|
|
16
|
+
export const simulatorCALocations = (certificatesDir) => ({
|
|
17
|
+
privateKey: path.join(certificatesDir, 'simulator.CA.key'),
|
|
18
|
+
certificate: path.join(certificatesDir, 'simulator.CA.pem'),
|
|
19
|
+
});
|
|
20
|
+
export const productionRunCALocations = (certificatesDir, productionRun) => ({
|
|
21
|
+
privateKey: path.join(certificatesDir, `production-${productionRun.toString(16)}.CA.key`),
|
|
22
|
+
certificate: path.join(certificatesDir, `production-${productionRun.toString(16)}.CA.pem`),
|
|
23
|
+
});
|
|
24
|
+
export const deviceCertificateLocations = (certificatesDir, deviceId) => ({
|
|
25
|
+
privateKey: path.join(certificatesDir, `${deviceId}.key`),
|
|
26
|
+
certificate: path.join(certificatesDir, `${deviceId}.pem`),
|
|
27
|
+
CSR: path.join(certificatesDir, `${deviceId}.csr`),
|
|
28
|
+
signedCert: path.join(certificatesDir, `${deviceId}.signed.pem`),
|
|
29
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { stat } from 'node:fs/promises';
|
|
3
|
+
import run from '@bifravst/run';
|
|
4
|
+
import { productionRunCALocations } from './locations.js';
|
|
5
|
+
export const ensureProductionRunCACertificate = async (dir, productionRun) => {
|
|
6
|
+
const { privateKey, certificate } = productionRunCALocations(dir, productionRun);
|
|
7
|
+
try {
|
|
8
|
+
await stat(certificate);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
// Create a CA private key
|
|
12
|
+
await run({
|
|
13
|
+
command: 'openssl',
|
|
14
|
+
args: ['genrsa', '-out', privateKey, '2048'],
|
|
15
|
+
});
|
|
16
|
+
await run({
|
|
17
|
+
command: 'openssl',
|
|
18
|
+
args: [
|
|
19
|
+
'req',
|
|
20
|
+
'-x509',
|
|
21
|
+
'-new',
|
|
22
|
+
'-nodes',
|
|
23
|
+
'-key',
|
|
24
|
+
privateKey,
|
|
25
|
+
'-sha256',
|
|
26
|
+
'-days',
|
|
27
|
+
'10957',
|
|
28
|
+
'-out',
|
|
29
|
+
certificate,
|
|
30
|
+
'-subj',
|
|
31
|
+
`/OU=Cellular IoT Applications Team, CN=Production Run ${productionRun}`,
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
console.log(chalk.yellow(`CA certificate for production run ${productionRun.toString(16)}:`), chalk.blue(certificate));
|
|
36
|
+
return {
|
|
37
|
+
privateKey,
|
|
38
|
+
certificate,
|
|
39
|
+
};
|
|
40
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hello.nrfcloud.com/certificate-helpers",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Helper functions for creating device certificates.",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./*": {
|
|
7
|
+
"import": {
|
|
8
|
+
"types": "./dist/*.d.ts",
|
|
9
|
+
"default": "./dist/*.js"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "tsx --no-warnings --test ./src/*.spec.ts",
|
|
16
|
+
"prepare": "husky",
|
|
17
|
+
"prepublishOnly": "npx tsc --noEmit false --outDir ./dist -d"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/hello-nrfcloud/certificate-helpers.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/hello-nrfcloud/certificate-helpers/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/hello-nrfcloud/certificate-helpers",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"aws",
|
|
29
|
+
"lambda",
|
|
30
|
+
"typescript"
|
|
31
|
+
],
|
|
32
|
+
"author": "Nordic Semiconductor ASA | nordicsemi.no",
|
|
33
|
+
"license": "BSD-3-Clause",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@bifravst/eslint-config-typescript": "6.0.17",
|
|
36
|
+
"@bifravst/prettier-config": "1.0.0",
|
|
37
|
+
"@commitlint/config-conventional": "19.1.0",
|
|
38
|
+
"@types/node": "20.12.3",
|
|
39
|
+
"aws-cdk-lib": "2.135.0",
|
|
40
|
+
"husky": "9.0.11",
|
|
41
|
+
"tsx": "4.7.1"
|
|
42
|
+
},
|
|
43
|
+
"lint-staged": {
|
|
44
|
+
"*.ts": [
|
|
45
|
+
"prettier --write",
|
|
46
|
+
"eslint"
|
|
47
|
+
],
|
|
48
|
+
"*.{md,json,yaml,yml}": [
|
|
49
|
+
"prettier --write"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=20",
|
|
54
|
+
"npm": ">=9"
|
|
55
|
+
},
|
|
56
|
+
"release": {
|
|
57
|
+
"branches": [
|
|
58
|
+
"saga",
|
|
59
|
+
{
|
|
60
|
+
"name": "!(saga)",
|
|
61
|
+
"prerelease": true
|
|
62
|
+
}
|
|
63
|
+
],
|
|
64
|
+
"remoteTags": true,
|
|
65
|
+
"plugins": [
|
|
66
|
+
"@semantic-release/commit-analyzer",
|
|
67
|
+
"@semantic-release/release-notes-generator",
|
|
68
|
+
"@semantic-release/npm",
|
|
69
|
+
[
|
|
70
|
+
"@semantic-release/github",
|
|
71
|
+
{
|
|
72
|
+
"successComment": false,
|
|
73
|
+
"failTitle": false
|
|
74
|
+
}
|
|
75
|
+
]
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
"publishConfig": {
|
|
79
|
+
"access": "public"
|
|
80
|
+
},
|
|
81
|
+
"files": [
|
|
82
|
+
"package-lock.json",
|
|
83
|
+
"dist",
|
|
84
|
+
"LICENSE",
|
|
85
|
+
"README.md"
|
|
86
|
+
],
|
|
87
|
+
"prettier": "@bifravst/prettier-config",
|
|
88
|
+
"dependencies": {
|
|
89
|
+
"@bifravst/run": "1.2.0"
|
|
90
|
+
}
|
|
91
|
+
}
|