Skip to content

Commit

Permalink
Fix SPKI parsing (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
martti007 authored Mar 6, 2024
1 parent be6d092 commit cccb0c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
11 changes: 3 additions & 8 deletions src/crypto/pkcs7/BrowserPublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import crypto from "node:crypto";
const { subtle } = globalThis.crypto;
import { SubjectPublicKeyInfo } from "./SubjectPublicKeyInfo.js";
import { PublicKey } from "./PublicKey.js";
import { DigestAlgorithm } from "./DigestAlgorithm.js";
Expand All @@ -15,20 +15,15 @@ export class BrowserPublicKey implements PublicKey {
data: Uint8Array,
signature: Uint8Array,
): Promise<boolean> {
const publicKey = await crypto.subtle.importKey(
const publicKey = await subtle.importKey(
"spki",
this.publicKeyInfo.getBytes(),
this.getSpkiAlgorithmParams(this.publicKeyInfo.algorithm, algorithm),
false,
["verify"],
);

return crypto.subtle.verify(
publicKey.algorithm.name,
publicKey,
signature,
data,
);
return subtle.verify(publicKey.algorithm.name, publicKey, signature, data);
}

private getSpkiAlgorithmParams(
Expand Down
6 changes: 5 additions & 1 deletion src/crypto/pkcs7/SubjectPublicKeyInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export class SubjectPublicKeyInfo {

public constructor(obj: Asn1Object) {
const children = obj.parseValueAsChildren();
this.algorithm = children.at(0)?.parseValueAsObjectIdentifier() as string;
this.algorithm = children
.at(0)
?.parseValueAsChildren()
.at(0)
?.parseValueAsObjectIdentifier() as string;
this.publicKey = children.at(1)?.parseValueBitStringAsBytes() as Uint8Array;
this.bytes = obj.getBytes();
}
Expand Down

0 comments on commit cccb0c2

Please sign in to comment.