Skip to content

Commit

Permalink
Fix new certificates (#151)
Browse files Browse the repository at this point in the history
* Update certUtils.js

* Pass testing

* Add trim

* Deno support

* Revert package-lock.json

* Bump version

* Add tests
  • Loading branch information
popeeyy authored Dec 22, 2023
1 parent 00c5a5f commit de9e4a3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/certUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import { ab2str, coerceToArrayBuffer, isPem, pemToBase64, tools } from "./utils.

class Certificate {
constructor(cert) {

let decoded;

// Clean up base64 string
if (typeof cert === "string" || cert instanceof String) {
cert = cert.replace(/\r/g, "");
cert = cert.replace(/\r/g, "").trim();
decoded = ab2str(coerceToArrayBuffer(cert, "certificate"));
}

if (isPem(cert)) {
cert = pemToBase64(cert);
} else if (decoded && isPem(decoded)) {
cert = pemToBase64(decoded);
}

// Clean up certificate
if (typeof cert === "string" || cert instanceof String) {
cert = cert.replace(/\n/g, "");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fido2-lib",
"version": "3.4.3",
"version": "3.4.4",
"description": "A library for performing FIDO 2.0 / WebAuthn functionality",
"type": "module",
"main": "dist/main.cjs",
Expand Down
6 changes: 6 additions & 0 deletions test/certUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ describe("cert utils", function() {
assert.isObject(cert._cert);
});

it("can decode base64 encoded cert", function() {
const cert = new Certificate(h.certs.truUCert);
assert.instanceOf(cert, Certificate);
assert.isObject(cert._cert);
});

it("throws if no arg to constructor", function() {
assert.throws(() => {
new Certificate();
Expand Down
3 changes: 3 additions & 0 deletions test/helpers/fido2-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3782,11 +3782,14 @@ const tpmAttestation = new Uint8Array([
0x3D,
]).buffer;

const truUCert = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t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MV09WWWtSQUxvQkMyCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=";

const certs = {
yubiKeyAttestation,
yubicoRoot,
feitianFido2,
tpmAttestation,
truUCert,
};

/** ******************************************************************************
Expand Down

0 comments on commit de9e4a3

Please sign in to comment.