Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguished Name implementation #314

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
523 changes: 226 additions & 297 deletions examples/CMSSignedComplexExample/es6.js

Large diffs are not rendered by default.

307 changes: 134 additions & 173 deletions examples/CRLComplexExample/es6.js

Large diffs are not rendered by default.

564 changes: 245 additions & 319 deletions examples/CertificateComplexExample/es6.js

Large diffs are not rendered by default.

249 changes: 115 additions & 134 deletions examples/HowToEncryptCMSviaCertificate/es6.js

Large diffs are not rendered by default.

526 changes: 232 additions & 294 deletions examples/HowToUseES6DirectlyInBrowser/application/es6.js

Large diffs are not rendered by default.

181 changes: 83 additions & 98 deletions examples/OCSPRequestComplexExample/es6.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
/* eslint-disable no-undef,no-unreachable,no-unused-vars */
import * as asn1js from "asn1js";
import { bufferToHexCodes, toBase64,arrayBufferToString } from "pvutils";
import { bufferToHexCodes, toBase64, arrayBufferToString } from "pvutils";
import { setEngine } from "../../src/common";
import { formatPEM } from "../../examples/examples_common";
import OCSPRequest from "../../src/OCSPRequest";
import GeneralName from "../../src/GeneralName";
import RelativeDistinguishedNames from "../../src/RelativeDistinguishedNames";
import AttributeTypeAndValue from "../../src/AttributeTypeAndValue";
import AttributeTypeDictionnary from "../../src/AttributeTypeDictionnary";
rviau42 marked this conversation as resolved.
Show resolved Hide resolved
import Request from "../../src/Request";
import CertID from "../../src/CertID";
import AlgorithmIdentifier from "../../src/AlgorithmIdentifier";
import Extension from "../../src/Extension";
import DistinguishedName from "../../src/DistinguishedName";
//<nodewebcryptoossl>
//*********************************************************************************
let ocspReqBuffer = new ArrayBuffer(0); // ArrayBuffer with loaded or created OCSP request
//*********************************************************************************
//region Create OCSP request
//*********************************************************************************
function createOCSPReqInternal()
{
function createOCSPReqInternal() {
//region Initial variables
const ocspReqSimpl = new OCSPRequest();
//endregion

//region Put static variables
ocspReqSimpl.tbsRequest.requestorName = new GeneralName({
type: 4,
value: new RelativeDistinguishedNames({
typesAndValues: [
new AttributeTypeAndValue({
type: "2.5.4.6", // Country name
value: new asn1js.PrintableString({ value: "RU" })
value: new DistinguishedName({
relativeDistinguishedNames: [
new RelativeDistinguishedNames({
typesAndValues: [
new AttributeTypeAndValue({
type: "2.5.4.6", // Country name
value: new asn1js.PrintableString({ value: "RU" })
}),
new AttributeTypeAndValue({
type: "2.5.4.3", // Common name
value: new asn1js.BmpString({ value: "Test" })
})
]
}),
new AttributeTypeAndValue({
type: "2.5.4.3", // Common name
value: new asn1js.BmpString({ value: "Test" })
new RelativeDistinguishedNames({
typesAndValues: [
new AttributeTypeAndValue({
type: "2.5.4.7", // Location
value: new asn1js.PrintableString({ value: "Moscow" })
}),
],
})
]
})
});

const fictionBuffer = new ArrayBuffer(4);
const fictionView = new Uint8Array(fictionBuffer);
fictionView[0] = 0x7F;
fictionView[1] = 0x01;
fictionView[2] = 0x02;
fictionView[3] = 0x03;

ocspReqSimpl.tbsRequest.requestList = [new Request({
reqCert: new CertID({
hashAlgorithm: new AlgorithmIdentifier({
Expand All @@ -57,37 +70,35 @@ function createOCSPReqInternal()
serialNumber: new asn1js.Integer({ valueHex: fictionBuffer })
})
})];

ocspReqSimpl.tbsRequest.requestExtensions = [
new Extension({
extnID: "1.3.6.1.5.5.7.48.1.2", // ocspNonce
extnValue: (new asn1js.OctetString({ valueHex: fictionBuffer })).toBER(false)
})
];
//endregion

//region Encode OCSP request and put on the Web page
ocspReqBuffer = ocspReqSimpl.toSchema(true).toBER(false);
//endregion

return Promise.resolve(true);
}
//*********************************************************************************
function createOCSPReq()
{
return Promise.resolve().then(() => createOCSPReqInternal()).then(() =>
{
function createOCSPReq() {
return Promise.resolve().then(() => createOCSPReqInternal()).then(() => {
let resultString = "";

resultString = `${resultString}\r\n-----BEGIN OCSP REQUEST-----\r\n`;
resultString = `${resultString}${formatPEM(toBase64(arrayBufferToString(ocspReqBuffer)))}`;
resultString = `${resultString}\r\n-----END OCSP REQUEST-----\r\n\r\n`;

// noinspection InnerHTMLJS
document.getElementById("new_signed_data").innerHTML = resultString;

parseOCSPReq();

alert("OCSP request has created successfully!");
});
}
Expand All @@ -96,42 +107,38 @@ function createOCSPReq()
//*********************************************************************************
//region Parse existing OCSP request
//*********************************************************************************
function parseOCSPReq()
{
function parseOCSPReq() {
//region Initial check
if(ocspReqBuffer.byteLength === 0)
{
if (ocspReqBuffer.byteLength === 0) {
alert("Nothing to parse!");
return;
}
//endregion

//region Initial activities
document.getElementById("ocsp-req-extn-div").style.display = "none";

const requestsTable = document.getElementById("ocsp-req-requests");
while(requestsTable.rows.length > 1)
while (requestsTable.rows.length > 1)
requestsTable.deleteRow(requestsTable.rows.length - 1);

const extensionTable = document.getElementById("ocsp-req-extn-table");
while(extensionTable.rows.length > 1)
while (extensionTable.rows.length > 1)
extensionTable.deleteRow(extensionTable.rows.length - 1);

const requestorTable = document.getElementById("ocsp-req-name");
while(requestorTable.rows.length > 1)
while (requestorTable.rows.length > 1)
requestorTable.deleteRow(requestorTable.rows.length - 1);
//endregion

//region Decode existing OCSP request
const asn1 = asn1js.fromBER(ocspReqBuffer);
const ocspReqSimpl = new OCSPRequest({ schema: asn1.result });
//endregion

//region Put information about OCSP request requestor
if("requestorName" in ocspReqSimpl.tbsRequest)
{
switch(ocspReqSimpl.tbsRequest.requestorName.type)
{
if ("requestorName" in ocspReqSimpl.tbsRequest) {
switch (ocspReqSimpl.tbsRequest.requestorName.type) {
case 1: // rfc822Name
case 2: // dNSName
case 6: // uniformResourceIdentifier
Expand All @@ -142,7 +149,7 @@ function parseOCSPReq()
case 7: // iPAddress
{
const view = new Uint8Array(ocspReqSimpl.tbsRequest.requestorName.value.valueBlock.valueHex);

// noinspection InnerHTMLJS
document.getElementById("ocsp-req-name-simpl").innerHTML = `${view[0].toString()}.${view[1].toString()}.${view[2].toString()}.${view[3].toString()}`;
document.getElementById("ocsp-req-nm-simpl").style.display = "block";
Expand All @@ -156,107 +163,85 @@ function parseOCSPReq()
break;
case 4: // directoryName
{
const rdnmap = {
"2.5.4.6": "C",
"2.5.4.10": "O",
"2.5.4.11": "OU",
"2.5.4.3": "CN",
"2.5.4.7": "L",
"2.5.4.8": "ST",
"2.5.4.12": "T",
"2.5.4.42": "GN",
"2.5.4.43": "I",
"2.5.4.4": "SN",
"1.2.840.113549.1.9.1": "E-mail"
};

for(let i = 0; i < ocspReqSimpl.tbsRequest.requestorName.value.typesAndValues.length; i++)
{
let typeval = rdnmap[ocspReqSimpl.tbsRequest.requestorName.value.typesAndValues[i].type];
if(typeof typeval === "undefined")
typeval = ocspReqSimpl.tbsRequest.requestorName.value.typesAndValues[i].type;

const subjval = ocspReqSimpl.tbsRequest.requestorName.value.typesAndValues[i].value.valueBlock.value;

const row = requestorTable.insertRow(requestorTable.rows.length);
const cell0 = row.insertCell(0);
// noinspection InnerHTMLJS
cell0.innerHTML = typeval;
const cell1 = row.insertCell(1);
// noinspection InnerHTMLJS
cell1.innerHTML = subjval;
for (let rdn of ocspReqSimpl.tbsRequest.requestorName.value) {
for (let i = 0; i < rdn.typesAndValues.length; i++) {
let typeval = AttributeTypeDictionnary[rdn.typesAndValues[i].type];
if (typeof typeval === "undefined")
typeval = rdn.typesAndValues[i].type;

const subjval = rdn.typesAndValues[i].value.valueBlock.value;

const row = requestorTable.insertRow(requestorTable.rows.length);
const cell0 = row.insertCell(0);
// noinspection InnerHTMLJS
cell0.innerHTML = typeval;
const cell1 = row.insertCell(1);
// noinspection InnerHTMLJS
cell1.innerHTML = subjval;
}
}

document.getElementById("ocsp-req-name-div").style.display = "block";
}
break;
default:
}
}
//endregion

//region Put information about requests
for(let i = 0; i < ocspReqSimpl.tbsRequest.requestList.length; i++)
{
for (let i = 0; i < ocspReqSimpl.tbsRequest.requestList.length; i++) {
const row = requestsTable.insertRow(requestsTable.rows.length);
const cell0 = row.insertCell(0);
// noinspection InnerHTMLJS
cell0.innerHTML = bufferToHexCodes(ocspReqSimpl.tbsRequest.requestList[i].reqCert.serialNumber.valueBlock.valueHex);
}
//endregion

//region Put information about request extensions
if("requestExtensions" in ocspReqSimpl.tbsRequest)
{
for(let i = 0; i < ocspReqSimpl.tbsRequest.requestExtensions.length; i++)
{
if ("requestExtensions" in ocspReqSimpl.tbsRequest) {
for (let i = 0; i < ocspReqSimpl.tbsRequest.requestExtensions.length; i++) {
const row = extensionTable.insertRow(extensionTable.rows.length);
const cell0 = row.insertCell(0);
// noinspection InnerHTMLJS
cell0.innerHTML = ocspReqSimpl.tbsRequest.requestExtensions[i].extnID;
}

document.getElementById("ocsp-req-extn-div").style.display = "block";
}
//endregion
}
//*********************************************************************************
//endregion
//*********************************************************************************
function handleFileBrowse(evt)
{
function handleFileBrowse(evt) {
const tempReader = new FileReader();

const currentFiles = evt.target.files;

// noinspection AnonymousFunctionJS
tempReader.onload =
function(event)
{
function (event) {
// noinspection JSUnresolvedVariable
ocspReqBuffer = event.target.result;
parseOCSPReq();
};

tempReader.readAsArrayBuffer(currentFiles[0]);
}
//*********************************************************************************
context("Hack for Rollup.js", () =>
{
context("Hack for Rollup.js", () => {
return;

// noinspection UnreachableCodeJS
createOCSPReq();
handleFileBrowse();
setEngine();
});
//*********************************************************************************
context("OCSP Request Complex Example", () =>
{
it("Create And Parse OCSP Request", () =>
{
return createOCSPReqInternal().then(() =>
{
context("OCSP Request Complex Example", () => {
it("Create And Parse OCSP Request", () => {
return createOCSPReqInternal().then(() => {
const asn1 = asn1js.fromBER(ocspReqBuffer);
// noinspection JSUnusedLocalSymbols
const ocspRequest = new OCSPRequest({ schema: asn1.result });
Expand Down
Loading