PeculiarVentures / PKI.js

PKI.js is a pure JavaScript library implementing the formats that are used in PKI applications (signing, encryption, certificate requests, OCSP and TSP requests/responses). It is built on WebCrypto (Web Cryptography API) and requires no plug-ins.
http://pkijs.org
Other
1.3k stars 204 forks source link

Example for adding custom extensions in x509 certificate #273

Closed jefjos closed 4 years ago

jefjos commented 4 years ago

Hello

Could you provide an example of adding custom extension to x509 certificate? So that the extensions section in certificate would look something like this

        X509v3 extensions:
            X509v3 Key Usage: critical
                Digital Signature
            X509v3 Basic Constraints: critical
                CA:FALSE
            1.2.3.4.5.6.7.8:
                {"a": {"b":"c"}}

Thanks!

rmhrisk commented 4 years ago

We don’t write code on demand for other peoples applications.

Do you have a concrete issue with the library trying to do something we can help with?

Have you seen: https://pkijs.org/examples/X509_cert_complex_example.html

Which shows adding extensions?

YuryStrozhevsky commented 4 years ago

this link better

jefjos commented 4 years ago

Thank you for the pointers. Here is what I have tried.


    const attr= "{\"a\":\"b\"}";
    cert.extensions.push(new Extension({
      extnID: "1.2.3.4.5.6.7.8",
      critical: false,
      extnValue: (new asn1js.OctetString({ valueHex: toArrayBuffer(Buffer.from(attr)) })).toBER(false)
    }));

function toArrayBuffer(buf) {
  var ab = new ArrayBuffer(buf.length);
  var view = new Uint8Array(ab);
  for (var i = 0; i < buf.length; ++i) {
      view[i] = buf[i];
  }
  return ab;
}

The resulting certificate shows the following

    X509v3 extensions:
        X509v3 Basic Constraints: critical
            CA:FALSE
        1.2.3.4.5.6.7.8:
            ..{"a":"b"}

The ".." before the {"a";"b"} is unexpected.

YuryStrozhevsky commented 4 years ago

@jefjos Please search closed issues for “extension” tag. I remember I had such question before. Also check issue #166 - probably it would help.

jefjos commented 4 years ago

Thanks for the pointer. I figured it out! The Extension constructor wraps the octetstring under another octetstring, which is the reason why the decoded certificate does not show the string as is. This worked for me


    cert.extensions.push(new Extension({
      extnID: "1.2.3.4.5.6.7.8.1",
      critical: false,
      extnValue: toArrayBuffer(Buffer.from(attr))
    }));

Closing this issue now.