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

Remove x509 dependency #2

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 1 addition & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"homepage": "https://github.com/mojoweb/islandis-login#readme",
"dependencies": {
"@fidm/x509": "^1.2.1",
"x509": "^0.3.4",
"xml-crypto": "^1.4.0",
"xml2js": "^0.4.23",
"xmldom": "^0.3.0"
Expand Down
47 changes: 18 additions & 29 deletions src/validateSignature.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const x509 = require("x509");
const path = require("path");
const { xpath } = require("xml-crypto");
const { DOMParser } = require("xmldom");
Expand Down Expand Up @@ -26,53 +25,42 @@ function FileKeyInfo(key) {
};
}

function isCertValid(certString) {
let cert;
function isCertificateDataValid(cert) {
const { serialName } = cert.subject;
const { organizationName } = cert.issuer;
const { validFrom, validTo } = cert;

try {
cert = x509.parseCert(certString);
} catch (e) {
if (serialName !== "6503760649" || organizationName !== "Audkenni ehf.") {
return false;
}

if (
cert.subject.serialNumber !== "6503760649" ||
cert.issuer.organizationName !== "Audkenni ehf."
) {
return false;
}

const dates = {
notBefore: new Date(cert.notBefore).getTime(),
notAfter: new Date(cert.notAfter).getTime(),
};

const timestamp = Date.now();

if (!(timestamp > dates.notBefore && timestamp < dates.notAfter)) {
if (
timestamp < new Date(validFrom).getTime() ||
timestamp > new Date(validTo).getTime()
) {
return false;
}

return true;
}

function checkSignature(doc, cert, xml) {
function checkSignature(doc, pem, xml) {
const signature = xpath(
doc,
"/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']"
)[0];

const sig = new SignedXml();
sig.keyInfoProvider = new FileKeyInfo(cert);
sig.keyInfoProvider = new FileKeyInfo(pem);
sig.loadSignature(signature);
const isValid = sig.checkSignature(xml);

return isValid;
}

function isCertificateValid(certificate) {
const c = Certificate.fromPEM(new Buffer.from(certificate));

// Reference: https://www.audkenni.is/adstod/skilriki-kortum/skilrikjakedjur/
const TrausturBunadur = Certificate.fromPEM(
readFileSync(path.resolve(__dirname, "../cert/TrausturBunadur.pem"))
Expand All @@ -82,9 +70,9 @@ function isCertificateValid(certificate) {
// to sign the message from Island.is
if (
TrausturBunadur.verifySubjectKeyIdentifier() &&
c.verifySubjectKeyIdentifier() &&
TrausturBunadur.checkSignature(c) === null &&
c.isIssuer(TrausturBunadur)
certificate.verifySubjectKeyIdentifier() &&
TrausturBunadur.checkSignature(certificate) === null &&
certificate.isIssuer(TrausturBunadur)
) {
return true;
}
Expand All @@ -107,17 +95,18 @@ function validate(xml, signature) {
const doc = new DOMParser().parseFromString(xml);

// construct x509 certificate
const cert = certToPEM(signature);
const pem = certToPEM(signature);
const cert = Certificate.fromPEM(new Buffer.from(pem));

// Verify certificate data, i.e.
// serialNumber & organization name is Auðkenni etc.
if (!isCertValid(cert)) {
if (!isCertificateDataValid(cert)) {
return reject("XML message is not signed by Auðkenni.");
}

// Verify that the XML document provided by the request was signed by the
// certificate provided with the request.
if (!checkSignature(doc, cert, xml)) {
if (!checkSignature(doc, pem, xml)) {
return reject("XML signature is invalid.");
}

Expand Down