forked from node-saml/node-saml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.ts
More file actions
83 lines (75 loc) · 2.68 KB
/
Copy pathcrypto.ts
File metadata and controls
83 lines (75 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as crypto from "crypto";
import { assertRequired } from "./utility";
import { PemLabel } from "./types";
/**
* PEM format has wide range of usages, but this library
* is enforcing RFC7468 which focuses on PKIX, PKCS and CMS.
*
* https://www.rfc-editor.org/rfc/rfc7468
*
* PEM_FORMAT_REGEX is validating given PEM file against RFC7468 'stricttextualmsg' definition.
*
* With few exceptions;
* - 'posteb' MAY have 'eol', but it is not mandatory.
* - 'preeb' and 'posteb' lines are limited to 64 characters, but
* should not cause any issues in context of PKIX, PKCS and CMS.
*
* normalizePemFile() -function is returning PEM files conforming
* RFC7468 'stricttextualmsg' definition.
*
* With couple of notes:
* - 'eol' is normalized to '\n'
*/
const PEM_FORMAT_REGEX =
/^(-----BEGIN [A-Z\x20]{1,48}-----(\r\n|\r|\n){1}.*(\r\n|\r|\n){1}-----END [A-Z\x20]{1,48}-----(\r\n|\r|\n){0,1})$/s;
const BASE64_REGEX =
/^(?:[A-Za-z0-9\+\/]{4}\n{0,1})*(?:[A-Za-z0-9\+\/]{2}==|[A-Za-z0-9\+\/]{3}=)?$/s; // eslint-disable-line no-useless-escape
/**
* -----BEGIN [LABEL]-----
* base64([DATA])
* -----END [LABEL]-----
*
* Above is shown what PEM file looks like. As can be seen, base64 data
* can be in single line or multiple lines.
*
* This function normalizes PEM presentation to;
* - contain PEM header and footer as they are given
* - normalize line endings to '\n'
* - normalize line length to maximum of 64 characters
* - ensure that 'preeb' has line ending '\n'
*/
const normalizePemFile = (pem: string): string => {
return `${(
pem
.trim()
.replace(/(\r\n|\r)/g, "\n")
.match(/.{1,64}/g) ?? []
).join("\n")}\n`;
};
/**
* This function currently expects to get data in PEM format or in base64 format.
*/
export const keyInfoToPem = (
keyInfo: string | Buffer,
pemLabel: PemLabel,
optionName = "keyInfo",
): string => {
const keyData = Buffer.isBuffer(keyInfo) ? keyInfo.toString("latin1") : keyInfo;
assertRequired(keyData, `${optionName} is not provided`);
if (PEM_FORMAT_REGEX.test(keyData)) {
return normalizePemFile(keyData);
}
const isBase64 = BASE64_REGEX.test(keyData);
assertRequired(isBase64 || undefined, `${optionName} is not in PEM format or in base64 format`);
const pem = `-----BEGIN ${pemLabel}-----\n${keyInfo}\n-----END ${pemLabel}-----`;
return normalizePemFile(pem);
};
export const generateUniqueId = (): string => {
return "_" + crypto.randomBytes(20).toString("hex");
};
export const stripPemHeaderAndFooter = (certificate: string): string => {
return certificate
.replace(/(\r\n|\r)/g, "\n")
.replace(/-----BEGIN [A-Z\x20]{1,48}-----\n?/, "")
.replace(/-----END [A-Z\x20]{1,48}-----\n?/, "");
};