1
0
mirror of synced 2024-11-24 06:50:16 +01:00
This commit is contained in:
n1474335 2022-06-03 22:21:37 +01:00
commit 1464e5d5e4
3 changed files with 323 additions and 11 deletions

View File

@ -1,11 +1,14 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @author cplussharp
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import r from "jsrsasign";
import {fromBase64} from "../lib/Base64.mjs";
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import Utils from "../Utils.mjs";
/**
* PEM to Hex operation
@ -25,6 +28,12 @@ class PEMToHex extends Operation {
this.inputType = "string";
this.outputType = "string";
this.args = [];
this.checks = [
{
"pattern": "----BEGIN ([A-Z][A-Z ]+[A-Z])-----",
"args": []
}
];
}
/**
@ -33,17 +42,25 @@ class PEMToHex extends Operation {
* @returns {string}
*/
run(input, args) {
if (input.indexOf("-----BEGIN") < 0) {
// Add header so that the KEYUTIL function works
input = "-----BEGIN CERTIFICATE-----" + input;
let output = "";
let match;
const regex = /-----BEGIN ([A-Z][A-Z ]+[A-Z])-----/g;
while ((match = regex.exec(input)) !== null) {
// find corresponding end tag
const indexBase64 = match.index + match[0].length;
const footer = `-----END ${match[1]}-----`;
const indexFooter = input.indexOf(footer, indexBase64);
if (indexFooter === -1) {
throw new OperationError(`PEM footer '${footer}' not found`);
}
// decode base64 content
const base64 = input.substring(indexBase64, indexFooter);
const bytes = fromBase64(base64, "A-Za-z0-9+/=", "byteArray", true);
const hex = bytes.map(b => Utils.hex(b)).join("");
output += hex;
}
if (input.indexOf("-----END") < 0) {
// Add footer so that the KEYUTIL function works
input = input + "-----END CERTIFICATE-----";
}
const cert = new r.X509();
cert.readCertPEM(input);
return cert.hex;
return output;
}
}

View File

@ -67,6 +67,7 @@ import "./tests/PGP.mjs";
import "./tests/PHP.mjs";
import "./tests/ParseIPRange.mjs";
import "./tests/ParseQRCode.mjs";
import "./tests/PEMtoHex.mjs";
import "./tests/PowerSet.mjs";
import "./tests/Regex.mjs";
import "./tests/Register.mjs";

View File

@ -0,0 +1,294 @@
/**
* Test PEMtoHex with different inputs
*
* @author cplussharp
*
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
/** RSA 2048bit key pair as PKCS1 and PKCS8 and as certificate */
const PEMS_RSA_PRIVATE_KEY_PKCS1 = `-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA5WykLKHiBAhmZh5WhocgpQQqZjdrApuRxRT21SJZx6Ce+Oz2
V17/heozu5LEz63jCxW1NrBckzl/Ys8p9LeqYTu6x/LbKloTjfEWxlzXnzUSqn9J
HIxmmJQzjXp9X1D99Tj+NWpRGEIiCFE7JfDhe2KnMGVDDg6kfCLokDdLo256LeQ4
CEkViwY6d+at4xDlIHwvZZmG4Smk56eHhvQE3I8sSAzgoLMBamQ5m3MbiULAYtxs
kCpCfjFxrL6Ziaaj7HZoneF40R30KCI9ygF8vkzxLwe3t5Y4XgHL9TYQm1+BDnin
upIB/zTeO1ygBGA66m6zpmkmuG7d8HXIducz+wIDAQABAoIBACQu3jWr0lmQeZXh
cwQEi8F6xrUYSGhA4NyUUdmLcV1ql6fqt29QLDySk1Yh76hRZF17LvlRF0ig6NZM
lfFihhyPrwWZ57bmPe9E9rKSMe+KD0eUi5NVEVk/BmJpzxwZSfRC6NTDz8Zjp7po
FUwGkYlEJdocHlc5N/fcCZG1Jti/Z1AsZIjO6r6S0O7neC7icECBKHUyWa8/yE1N
++TVyMV+z53Ad1PC+SHMGDlbqJAM3o4wAzD/FAIzyVo6GSnnC+bFdgMtIwtwgYTH
rbr8M8j5fqAHTqNJeblqt/5KHEj2VVsIsHIuQ6lv4llESEqmH+N5KE4O33U7/Wmj
y/+VGAECgYEA9ysROHXOx1ofh3tI/r2C2FUan6/AAe4dc+8E4jMGI5T4xeqYHTRV
l1yS+ZKIxqclIoAmd6SJ7Nx2GdQ55MmokZdZRqsFN1flFOZw2kFH/I0zQZXdOjF+
sf5Lu0FfcTw3VJhJ/YU3CVdlgdP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UMQvdCCCv6RVm56
7dBCijJIHg23JbgPJD72JCluXtTYWllv3duSwuWeYHo5EftU456pDcztkgn9XwFk
PFGnHLmbjpSzjE7u29qCjwHl3CiUsjfUlYFl/mf27oDXPqaWqPYv3fWH3H3ymiZQ
cqptUF4hDtPkaNkKWFmlljChN92o8g/jrv4DVDgJzQ==
-----END CERTIFICATE-----`;
/** EC P-256 key pair and certificate */
const PEMS_EC_P256_PRIVATE_KEY = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIIhdQxQIcMnCHD3X4WqNv+VgycWmFoEZpRl9X0+dT9uHoAoGCCqGSM49
AwEHoUQDQgAEFLQcBbzDweo6af4k3k0gKWMNWOZVn8+9hH2rv4DKKYZ7E1z64LBt
PnB1gMz++HDKySr2ozD3/46dIbQMXUZKpw==
-----END EC PRIVATE KEY-----`;
const PEMS_EC_P256_PRIVATE_KEY_PKCS8 = `-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgiF1DFAhwycIcPdfh
ao2/5WDJxaYWgRmlGX1fT51P24ehRANCAAQUtBwFvMPB6jpp/iTeTSApYw1Y5lWf
z72Efau/gMophnsTXPrgsG0+cHWAzP74cMrJKvajMPf/jp0htAxdRkqn
-----END PRIVATE KEY-----`;
const PEMS_EC_P256_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEFLQcBbzDweo6af4k3k0gKWMNWOZV
n8+9hH2rv4DKKYZ7E1z64LBtPnB1gMz++HDKySr2ozD3/46dIbQMXUZKpw==
-----END PUBLIC KEY-----`;
const PEMS_FOO = `-----BEGIN FOO-----
Rk9P
-----END FOO-----`;
const PEMS_BAR = `-----BEGIN BAR-----
QkFS
-----END BAR-----`;
TestRegister.addTests([
{
name: "PEMtoHex: Nothing",
input: "",
expectedOutput: "",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
}
]
},
{
name: "PEMtoHex: No footer",
input: PEMS_RSA_PRIVATE_KEY_PKCS1.substr(0, 200),
expectedOutput: "PEM footer '-----END RSA PRIVATE KEY-----' not found",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
}
]
},
{
name: "PEMtoHex: Multiple PEMs",
input: PEMS_FOO + '\n' + PEMS_BAR,
expectedOutput: "FOOBAR",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
},
{
"op": "From Hex",
"args": ["None"]
}
]
},
{
name: "PEMtoHex: Single line PEM",
input: PEMS_FOO.replace(/(\n|\r)/gm,""),
expectedOutput: "FOO",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
},
{
"op": "From Hex",
"args": ["None"]
}
]
},
{
name: "PEMtoHex: EC P-256 Private Key",
input: PEMS_EC_P256_PRIVATE_KEY,
expectedOutput: "30770201010420885d43140870c9c21c3dd7e16a8dbfe560c9c5a6168119a5197d5f4f9d4fdb87a00a06082a8648ce3d030107a1440342000414b41c05bcc3c1ea3a69fe24de4d2029630d58e6559fcfbd847dabbf80ca29867b135cfae0b06d3e707580ccfef870cac92af6a330f7ff8e9d21b40c5d464aa7",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
}
]
},
{
name: "PEMtoHex: EC P-256 Private Key PKCS8",
input: PEMS_EC_P256_PRIVATE_KEY_PKCS8,
expectedOutput: "308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b0201010420885d43140870c9c21c3dd7e16a8dbfe560c9c5a6168119a5197d5f4f9d4fdb87a1440342000414b41c05bcc3c1ea3a69fe24de4d2029630d58e6559fcfbd847dabbf80ca29867b135cfae0b06d3e707580ccfef870cac92af6a330f7ff8e9d21b40c5d464aa7",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
}
]
},
{
name: "PEMtoHex: EC P-256 Public Key",
input: PEMS_EC_P256_PUBLIC_KEY,
expectedOutput: "3059301306072a8648ce3d020106082a8648ce3d0301070342000414b41c05bcc3c1ea3a69fe24de4d2029630d58e6559fcfbd847dabbf80ca29867b135cfae0b06d3e707580ccfef870cac92af6a330f7ff8e9d21b40c5d464aa7",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
}
]
},
{
name: "PEMtoHex: RSA Private Key PKCS1",
input: PEMS_RSA_PRIVATE_KEY_PKCS1,
expectedOutput: "fb49bd96ffc5d1351a35d773921fac03",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
},
{
"op": "MD5",
"args": []
}
]
},
{
name: "PEMtoHex: RSA Private Key PKCS8",
input: PEMS_RSA_PRIVATE_KEY_PKCS8,
expectedOutput: "23086d03633689fee64680c3c24409eb",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
},
{
"op": "MD5",
"args": []
}
]
},
{
name: "PEMtoHex: RSA Public Key PKCS1",
input: PEMS_RSA_PUBLIC_KEY_PKCS1,
expectedOutput: "5fc3f1f6c5d5806760b12eaad0c0292c",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
},
{
"op": "MD5",
"args": []
}
]
},
{
name: "PEMtoHex: RSA Public Key PKCS8",
input: PEMS_RSA_PUBLIC_KEY_PKCS8,
expectedOutput: "30fbe8e9495d591232affebdd6206ea6",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
},
{
"op": "MD5",
"args": []
}
]
},
{
name: "PEMtoHex: Certificate",
input: PEMS_RSA_CERT,
expectedOutput: "6694d8ca4a0ceb84c3951d25dc05ec6e",
recipeConfig: [
{
"op": "PEM to Hex",
"args": []
},
{
"op": "MD5",
"args": []
}
]
}
]);