@p0security/cli 0.18.13 → 0.18.14
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.
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/** Copyright © 2024-present P0 Security
|
|
3
|
+
|
|
4
|
+
This file is part of @p0security/cli
|
|
5
|
+
|
|
6
|
+
@p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
|
|
7
|
+
|
|
8
|
+
@p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
9
|
+
|
|
10
|
+
You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
|
|
11
|
+
**/
|
|
12
|
+
import * as crypto from "crypto";
|
|
13
|
+
/**
|
|
14
|
+
* Convert a crypto.KeyObject RSA public key to OpenSSH "ssh-rsa ..."
|
|
15
|
+
*
|
|
16
|
+
* The "blob" is a sequence of length-prefixed strings:
|
|
17
|
+
* string "ssh-rsa"
|
|
18
|
+
* mpint e
|
|
19
|
+
* mpint n
|
|
20
|
+
*
|
|
21
|
+
* After building that blob, you base64 it and prepend ssh-rsa (plus an optional comment).
|
|
22
|
+
* See https://datatracker.ietf.org/doc/html/rfc4253#section-6.6
|
|
23
|
+
*/
|
|
24
|
+
export declare function toOpenSshFormat(keyObject: crypto.KeyObject, comment?: string): string;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toOpenSshFormat = void 0;
|
|
4
|
+
function toUInt32(n) {
|
|
5
|
+
const b = Buffer.alloc(4);
|
|
6
|
+
b.writeUInt32BE(n, 0);
|
|
7
|
+
return b;
|
|
8
|
+
}
|
|
9
|
+
const sshString = (b) => {
|
|
10
|
+
return Buffer.concat([toUInt32(b.length), Buffer.from(b)]);
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Convert a Buffer to a multiple precision integer (mpint)
|
|
14
|
+
*
|
|
15
|
+
* mpints are represented in two's complement format,
|
|
16
|
+
* stored as a string, 8 bits per byte, MSB first.
|
|
17
|
+
*
|
|
18
|
+
* https://datatracker.ietf.org/doc/html/rfc4251#section-5
|
|
19
|
+
*/
|
|
20
|
+
function toMultiplePrecisionInteger(raw) {
|
|
21
|
+
// strip all leading zeros
|
|
22
|
+
let i = 0;
|
|
23
|
+
while (i < raw.length && raw[i] === 0)
|
|
24
|
+
i++;
|
|
25
|
+
let b = raw.slice(i);
|
|
26
|
+
// zero -> single 0x00
|
|
27
|
+
if (b.length === 0)
|
|
28
|
+
b = Buffer.from([0]);
|
|
29
|
+
// MSB set -> pad
|
|
30
|
+
if (b[0] & 0x80)
|
|
31
|
+
b = Buffer.concat([Buffer.from([0]), b]);
|
|
32
|
+
return Buffer.concat([toUInt32(b.length), b]);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Convert a crypto.KeyObject RSA public key to OpenSSH "ssh-rsa ..."
|
|
36
|
+
*
|
|
37
|
+
* The "blob" is a sequence of length-prefixed strings:
|
|
38
|
+
* string "ssh-rsa"
|
|
39
|
+
* mpint e
|
|
40
|
+
* mpint n
|
|
41
|
+
*
|
|
42
|
+
* After building that blob, you base64 it and prepend ssh-rsa (plus an optional comment).
|
|
43
|
+
* See https://datatracker.ietf.org/doc/html/rfc4253#section-6.6
|
|
44
|
+
*/
|
|
45
|
+
function toOpenSshFormat(keyObject, comment = "p0-generated-key") {
|
|
46
|
+
const jwk = keyObject.export({ format: "jwk" });
|
|
47
|
+
if (jwk.kty !== "RSA" || !jwk.n || !jwk.e) {
|
|
48
|
+
throw new Error("Expected an RSA public key (JWK with n and e).");
|
|
49
|
+
}
|
|
50
|
+
const nBuffer = Buffer.from(jwk.n, "base64url");
|
|
51
|
+
const eBuffer = Buffer.from(jwk.e, "base64url");
|
|
52
|
+
const keyType = "ssh-rsa";
|
|
53
|
+
const blob = Buffer.concat([
|
|
54
|
+
sshString(keyType),
|
|
55
|
+
toMultiplePrecisionInteger(eBuffer),
|
|
56
|
+
toMultiplePrecisionInteger(nBuffer),
|
|
57
|
+
]);
|
|
58
|
+
return `${keyType} ${blob.toString("base64")}${comment ? " " + comment : ""}`;
|
|
59
|
+
}
|
|
60
|
+
exports.toOpenSshFormat = toOpenSshFormat;
|
|
61
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../../src/common/crypto.ts"],"names":[],"mappings":";;;AAYA,SAAS,QAAQ,CAAC,CAAS;IACzB,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC1B,CAAC,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACtB,OAAO,CAAC,CAAC;AACX,CAAC;AAED,MAAM,SAAS,GAAG,CAAC,CAAS,EAAU,EAAE;IACtC,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7D,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,SAAS,0BAA0B,CAAC,GAAW;IAC7C,0BAA0B;IAC1B,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,OAAO,CAAC,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC;QAAE,CAAC,EAAE,CAAC;IAC3C,IAAI,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACrB,sBAAsB;IACtB,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC;QAAE,CAAC,GAAG,MAAM,CAAC,IAAI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AApBD,0CAoBC"}
|
|
@@ -45,6 +45,7 @@ You should have received a copy of the GNU General Public License along with @p0
|
|
|
45
45
|
**/
|
|
46
46
|
const stdio_1 = require("../drivers/stdio");
|
|
47
47
|
const util_1 = require("../util");
|
|
48
|
+
const crypto_1 = require("./crypto");
|
|
48
49
|
const crypto = __importStar(require("crypto"));
|
|
49
50
|
const fs = __importStar(require("fs/promises"));
|
|
50
51
|
const path = __importStar(require("path"));
|
|
@@ -69,7 +70,7 @@ const createKeyPair = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
69
70
|
type: "pkcs8",
|
|
70
71
|
format: "pem",
|
|
71
72
|
});
|
|
72
|
-
const publicKey = toOpenSshFormat(keyPair.publicKey);
|
|
73
|
+
const publicKey = (0, crypto_1.toOpenSshFormat)(keyPair.publicKey);
|
|
73
74
|
yield fs.mkdir(path.dirname(exports.PUBLIC_KEY_PATH), { recursive: true });
|
|
74
75
|
yield fs.writeFile(exports.PUBLIC_KEY_PATH, publicKey, { mode: 0o600 });
|
|
75
76
|
yield fs.writeFile(exports.PRIVATE_KEY_PATH, privateKey, { mode: 0o600 });
|
|
@@ -86,37 +87,6 @@ const fileExists = (path) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
86
87
|
return false;
|
|
87
88
|
}
|
|
88
89
|
});
|
|
89
|
-
/**
|
|
90
|
-
* Convert a crypto.KeyObject RSA public key to OpenSSH format
|
|
91
|
-
*/
|
|
92
|
-
const toOpenSshFormat = (keyObject) => {
|
|
93
|
-
// Export the key in JWK format to get n and e values
|
|
94
|
-
const jwk = keyObject.export({ format: "jwk" });
|
|
95
|
-
// Convert base64url to buffer
|
|
96
|
-
const nBuffer = Buffer.from(jwk.n, "base64url");
|
|
97
|
-
const eBuffer = Buffer.from(jwk.e, "base64url");
|
|
98
|
-
// Create SSH wire format
|
|
99
|
-
const keyType = "ssh-rsa";
|
|
100
|
-
const keyTypeBuffer = Buffer.from(keyType);
|
|
101
|
-
// SSH wire format: [key_type_len][key_type][e_len][e][n_len][n]
|
|
102
|
-
const keyTypeLen = Buffer.alloc(4);
|
|
103
|
-
keyTypeLen.writeUInt32BE(keyTypeBuffer.length, 0);
|
|
104
|
-
const eLen = Buffer.alloc(4);
|
|
105
|
-
eLen.writeUInt32BE(eBuffer.length, 0);
|
|
106
|
-
const nLen = Buffer.alloc(4);
|
|
107
|
-
nLen.writeUInt32BE(nBuffer.length, 0);
|
|
108
|
-
const sshWireFormat = Buffer.concat([
|
|
109
|
-
keyTypeLen,
|
|
110
|
-
keyTypeBuffer,
|
|
111
|
-
eLen,
|
|
112
|
-
eBuffer,
|
|
113
|
-
nLen,
|
|
114
|
-
nBuffer,
|
|
115
|
-
]);
|
|
116
|
-
// Base64 encode and format as OpenSSH key
|
|
117
|
-
const base64Key = sshWireFormat.toString("base64");
|
|
118
|
-
return `${keyType} ${base64Key} p0-generated-key`;
|
|
119
|
-
};
|
|
120
90
|
exports.KNOWN_HOSTS_DIR = path.join(exports.P0_KEY_FOLDER, "known_hosts");
|
|
121
91
|
exports.KNOWN_HOSTS_PATH = path.join(exports.P0_KEY_FOLDER, "known_hosts_config");
|
|
122
92
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../../src/common/keys.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;GASG;AACH,4CAA0C;AAC1C,kCAAkC;AAClC,+CAAiC;AACjC,gDAAkC;AAClC,2CAA6B;AAEhB,QAAA,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,cAAO,EAAE,KAAK,CAAC,CAAC;AAC1C,QAAA,eAAe,GAAG,IAAI,CAAC,IAAI,CAAC,qBAAa,EAAE,YAAY,CAAC,CAAC;AACzD,QAAA,gBAAgB,GAAG,IAAI,CAAC,IAAI,CAAC,qBAAa,EAAE,QAAQ,CAAC,CAAC;AAEnE;;GAEG;AACI,MAAM,aAAa,GAAG,GAG1B,EA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