Skip to content

Commit

Permalink
Clean the code
Browse files Browse the repository at this point in the history
  • Loading branch information
saltiyazan committed Jun 24, 2024
1 parent 0d2fc17 commit 311fc67
Showing 1 changed file with 38 additions and 40 deletions.
78 changes: 38 additions & 40 deletions ui/src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const oidToName = (oid: string) => {
return map[oid]
}

function fromPEM(pem: string) {
const b64 = pem.replace(/(-----(BEGIN|END) CERTIFICATE REQUEST-----|\n)/g, "");
function pemToArrayBuffer(pem: string): ArrayBuffer {
const b64 = pem.replace(/(-----(BEGIN|END) [A-Z ]+-----|\n|\r)/g, "");
const binaryDerString = atob(b64);
const binaryDer = pvutils.stringToArrayBuffer(binaryDerString);
return binaryDer;
Expand All @@ -60,9 +60,39 @@ function hexToIp(hex: ArrayBuffer): string {
return Array.from(byteArray).map(byte => byte.toString(10)).join('.');
}

function parseExtensions(extensions: Extensions) {
const sansDns: string[] = [];
const sansIp: string[] = [];
let is_ca = false;

extensions.extensions.forEach(extension => {
let extensionName: string;
try {
extensionName = oidToName(extension.extnID);
} catch (error) {
console.error(`Unrecognized extension OID: ${extension.extnID}`);
return;
}

if (extensionName === "Subject Alternative Name") {
extension.parsedValue.altNames.forEach((altName: { type: number; value: any; }) => {
if (altName.type == 2) {
sansDns.push(altName.value);
} else if (altName.type == 7) {
sansIp.push(hexToIp(altName.value.valueBlock.valueHex));
}
});
} else if (extensionName === "Basic Constraint") {
console.log(extension);
is_ca = extension.parsedValue.cA;
}
});

return { sansDns, sansIp, is_ca };
}

export const extractCSR = (csrPemString: string) => {
const arrayBuffer = fromPEM(csrPemString);
const arrayBuffer = pemToArrayBuffer(csrPemString);
const asn1 = fromBER(arrayBuffer);
const csr = new CertificationRequest({ schema: asn1.result });

Expand All @@ -79,41 +109,15 @@ export const extractCSR = (csrPemString: string) => {
const country = getValue("Country");
const locality = getValue("Locality");

// Look for extensions attribute in CSR
const attributes = csr.attributes?.map(typeAndValue => ({
type: oidToName(typeAndValue.type),
value: typeAndValue.values
}))
let sansDns: string[] = [];
let sansIp: string[] = [];
let is_ca = false;

if (csr.attributes) {
const extensionAttributes = csr.attributes.filter(attribute => attribute.type === "1.2.840.113549.1.9.14");
const extensionAttributes = csr.attributes.filter(attribute => oidToName(attribute.type) === "Extension Request");
if (extensionAttributes.length > 0) {
const extensions = new Extensions({ schema: extensionAttributes[0].values[0] });
extensions.extensions.forEach(extension => {
let extensionName: string;
try {
extensionName = oidToName(extension.extnID);
} catch (error) {
console.error(`Unrecognized extension OID: ${extension.extnID}`);
return;
}
if (extensionName === "Subject Alternative Name") {
extension.parsedValue.altNames.forEach((altName: { type: number; value: any; }) => {
if (altName.type == 2) {
sansDns.push(altName.value);
} else if (altName.type == 7) {
sansIp.push(hexToIp(altName.value.valueBlock.valueHex))
}
});
}
else if (extensionName === "Basic Constraint") {
console.log(extension);
is_ca = extension.parsedValue.cA;
}
});
({ sansDns, sansIp, is_ca } = parseExtensions(extensions));
}
}
return {
Expand All @@ -130,18 +134,12 @@ export const extractCSR = (csrPemString: string) => {

export const extractCert = (certPemString: string) => {
if (certPemString == "" || certPemString == "rejected") { return }

// Decode PEM to DER
const pemHeader = "-----BEGIN CERTIFICATE-----";
const pemFooter = "-----END CERTIFICATE-----";
const pemContents = certPemString.substring(pemHeader.length, certPemString.length - pemFooter.length);
const binaryDerString = window.atob(pemContents);
const binaryDer = new Uint8Array(binaryDerString.length);
for (let i = 0; i < binaryDerString.length; i++) {
binaryDer[i] = binaryDerString.charCodeAt(i);
}
const binaryDer = pemToArrayBuffer(certPemString);

// Parse DER encoded certificate
const asn1 = fromBER(binaryDer.buffer);
const asn1 = fromBER(binaryDer);
if (asn1.offset === -1) {
throw new Error("Error parsing certificate");
}
Expand Down

0 comments on commit 311fc67

Please sign in to comment.