From 5a507aa1ba80c10576af4583c22489377d046451 Mon Sep 17 00:00:00 2001 From: MikeCAT Date: Sun, 30 Oct 2022 08:25:31 +0900 Subject: [PATCH] have "Parse X.509 certificate" emit user-friendly message on certificate load error --- src/core/operations/ParseX509Certificate.mjs | 38 +++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/core/operations/ParseX509Certificate.mjs b/src/core/operations/ParseX509Certificate.mjs index aeb5f677..9a0eb272 100644 --- a/src/core/operations/ParseX509Certificate.mjs +++ b/src/core/operations/ParseX509Certificate.mjs @@ -57,23 +57,29 @@ class ParseX509Certificate extends Operation { const cert = new r.X509(), inputFormat = args[0]; - switch (inputFormat) { - case "DER Hex": - input = input.replace(/\s/g, ""); - cert.readCertHex(input); - break; - case "PEM": - cert.readCertPEM(input); - break; - case "Base64": - cert.readCertHex(toHex(fromBase64(input, null, "byteArray"), "")); - break; - case "Raw": - cert.readCertHex(toHex(Utils.strToByteArray(input), "")); - break; - default: - throw "Undefined input format"; + let undefinedInputFormat = false; + try { + switch (inputFormat) { + case "DER Hex": + input = input.replace(/\s/g, ""); + cert.readCertHex(input); + break; + case "PEM": + cert.readCertPEM(input); + break; + case "Base64": + cert.readCertHex(toHex(fromBase64(input, null, "byteArray"), "")); + break; + case "Raw": + cert.readCertHex(toHex(Utils.strToByteArray(input), "")); + break; + default: + undefinedInputFormat = true; + } + } catch (e) { + throw "Certificate load error (non-certificate input?)"; } + if (undefinedInputFormat) throw "Undefined input format"; const sn = cert.getSerialNumberHex(), issuer = cert.getIssuer(),