PeculiarVentures / node-webcrypto-p11

A WebCrypto Polyfill for Node in typescript built on PKCS#11.
MIT License
44 stars 15 forks source link

Support token, sensitive, label attributes #52

Closed microshine closed 4 years ago

microshine commented 5 years ago

Add token, sensitive, and label attributes to generate/import functions

Generate key

const alg = {
  name: "RSASSA-PKCS1-v1_5",
  hash: "SHA-256",
  publicExponent: new Uint8Array([1, 0, 1]),
  modulusLength: 2048,
  label: "custom",
  token: true,
  sensitive: true,
};

const keys = await crypto.subtle.generateKey(alg, false, ["sign", "verify"]);

// Get custom attributes from CryptoKey
console.log(keys.privateKey.label); // RSA-2048
console.log(keys.privateKey.token); // true
console.log(keys.privateKey.sensitive); // true

Import key

const alg = {
  name: "RSASSA-PKCS1-v1_5",
  hash: "SHA-256",
  label: "custom",
  token: true,
};
const jwk = {
  alg: "RS256",
  e: "AQAB",
  ext: true,
  key_ops: ["verify"],
  kty: "RSA",
  n: "vqpvdxuyZ6rKYnWTj_ZzDBFZAAAlpe5hpoiYHqa2j5kK7v8U5EaPY2bLib9m4B40j-n3FV9xUCGiplWdqMJJKT-4PjGO5E3S4N9kjFhu57noYT7z7302J0sJXeoFbXxlgE-4G55Oxlm52ID2_RJesP5nzcGTriQwoRbrJP5OEt0",
};

const publicKey = await crypto.subtle.importKey("jwk", jwk, alg, true, ["verify"]);

Import certificate

const pem = "MIIDqDCCApCgAwIBAgIJAP7c4...vkHve52Xdf+XlcCWWC/qu0bXu+TZLg==";
const alg = {
  name: "RSASSA-PKCS1-v1_5", 
  hash: "SHA-256",
  token: true,
  label: "mycert"
};
const x509 = crypto.certStorage.importCert("raw", Buffer.from(pem, "base64"),  alg, ["verify"])

// Get custom attributes from CryptoCertificate
console.log(x509.label); // mycert
console.log(x509.token); // true
coveralls commented 4 years ago

Coverage Status

Coverage increased (+2.02%) to 85.947% when pulling 1f2c3c3ae55918fb47cfe9cdc6ff02282f3ec6e6 on create-attrs into 869c8e202e34f5776ca579b8e964a84a7b2e5b66 on master.