This repository was archived by the owner on Aug 12, 2025. It is now read-only.
Support SigPolicyId#57
Merged
microshine merged 1 commit intoNov 24, 2017
Merged
Conversation
Closed
Contributor
Author
|
@microshine as requested |
Contributor
|
@alexey-pelykh I'll publish it today. I have to fix tslint issues and make some tests. I'll notice you when it's done |
Contributor
|
@alexey-pelykh I published xmldsigjs and xadesjs Here is my test script TypeScriptimport * as fs from "fs";
import * as xmlcore from "xml-core";
import * as xmldsig from "xmldsigjs";
import * as xades from "xadesjs";
const { XMLSerializer } = require("xmldom-alpha");
const pkijs = require("pkijs");
const asn1js = require("asn1js");
const WebCrypto = require("node-webcrypto-ossl");
const crypto = new WebCrypto() as Crypto;
// Set crypto engine
xmldsig.Application.setEngine("OpenSSL", crypto);
pkijs.setEngine("OpenSSL", crypto, new pkijs.CryptoEngine({ name: "OpenSSL", crypto, subtle: crypto.subtle }));
async function main() {
await sign();
}
main()
.catch((err) => console.error(err));
async function verify() {
const ping = fs.readFileSync("ping.xml", "utf8");
const signatures1 = fs.readFileSync("test.xml", "utf8");
const signedDocument = xades.Parse(signatures1);
const pingDocument = xades.Parse(ping);
const xmlSignature = signedDocument.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature");
const signedXml = new xmldsig.SignedXml(pingDocument);
signedXml.LoadXml(xmlSignature[0]);
const res = await signedXml.Verify();
console.log((res ? "Valid" : "Invalid") + " signature");
}
async function sign() {
const ping = fs.readFileSync("ping.xml", "utf8");
const alg = {
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
publicExponent: new Uint8Array([1, 0, 1]),
modulusLength: 2048,
};
// parse xml
const asicDocument = xades.Parse(`<asic:XAdESSignatures xmlns:asic="http://uri.etsi.org/02918/v1.2.1#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"></asic:XAdESSignatures>`);
const pingDocument = xades.Parse(ping);
// generate key
const keys = await crypto.subtle.generateKey(alg, false, ["sign", "verify"]);
const cert = await GenerateCertificate(keys);
const certDer = Buffer.from(cert.toSchema().toBER());
await writePublicKey(keys);
fs.writeFileSync("cert.der", certDer, { encoding: "binary" });
let signature = new xades.SignedXml();
// set parent for xml signature
signature.Parent = asicDocument.firstChild as Element;
// Signing document
await signature.Sign(
alg,
keys.privateKey,
pingDocument,
{
keyValue: keys.publicKey,
x509: [certDer.toString("base64")],
references: [
{ hash: "SHA-256", uri: "ping.xml" },
],
signingCertificate: certDer.toString("base64"),
});
asicDocument.firstChild!.appendChild(signature.GetXml()!);
// console.log(await signature.Verify());
// serialize xml to string
const signedXml = new XMLSerializer().serializeToString(asicDocument);
fs.writeFileSync("test.xml", signedXml);
console.log(signedXml);
}
async function writePublicKey(keys: CryptoKeyPair) {
const raw = await crypto.subtle.exportKey("spki", keys.publicKey);
fs.writeFileSync("pubKey.der", Buffer.from(raw), { encoding: "binary" });
}
async function GenerateCertificate(keyPair: CryptoKeyPair) {
const certificate = new pkijs.Certificate();
// region Put a static values
certificate.version = 2;
const serialNumber = crypto.getRandomValues(new Uint8Array(10));
certificate.serialNumber = new asn1js.Integer();
certificate.serialNumber.valueBlock.valueHex = serialNumber.buffer;
const commonName = new pkijs.AttributeTypeAndValue({
type: "2.5.4.3", // Common name
value: new asn1js.PrintableString({ value: "Test certificate" }),
});
certificate.issuer.typesAndValues.push(commonName);
certificate.subject.typesAndValues.push(commonName);
// Valid period is 1 year
certificate.notBefore.value = new Date(); // current date
const notAfter = new Date();
notAfter.setFullYear(notAfter.getFullYear() + 1);
certificate.notAfter.value = notAfter;
certificate.extensions = []; // Extensions are not a part of certificate by default, it's an optional array
// Basic constraints
const basicConstraints = new pkijs.BasicConstraints({
cA: true,
pathLenConstraint: 2,
});
certificate.extensions.push(new pkijs.Extension({
extnID: "2.5.29.19",
critical: false,
extnValue: basicConstraints.toSchema().toBER(false),
parsedValue: basicConstraints,
}));
await certificate.subjectPublicKeyInfo.importKey(keyPair.publicKey);
await certificate.sign(keyPair.privateKey, "SHA-256");
return certificate;
}Output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.