node-opcua-crypto-web 5.7.2 → 5.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -624,6 +624,8 @@ interface WebCryptoFromSigner {
624
624
  * that key ever existing as an in-process `CryptoKey`.
625
625
  */
626
626
  declare function webCryptoFromSigner(signer: CaSigner, baseCrypto?: Pick<Crypto, "subtle" | "getRandomValues">): Promise<WebCryptoFromSigner>;
627
+ /** True when `value` implements {@link CaSigner} rather than being a raw `CryptoKey`. */
628
+ declare function isCaSigner(value: CryptoKey | CaSigner): value is CaSigner;
627
629
  /** The `crypto`/`signingKey` pair a `@peculiar/x509` generator needs, resolved from either a raw key or a {@link CaSigner}. */
628
630
  interface ResolvedCaSigningKey {
629
631
  crypto: Crypto;
@@ -638,6 +640,28 @@ interface ResolvedCaSigningKey {
638
640
  * generation) need to branch on which kind of signing key they were given.
639
641
  */
640
642
  declare function resolveCaSigningKey(signingKey: CryptoKey | CaSigner, baseCrypto?: Pick<Crypto, "subtle" | "getRandomValues">): Promise<ResolvedCaSigningKey>;
643
+ /**
644
+ * What a `@peculiar/x509` generator needs when it must sign with a key AND
645
+ * embed that same key's public half — a CSR, or a self-signed certificate.
646
+ * {@link resolveCaSigningKey} is not enough there: it yields no public key.
647
+ */
648
+ interface ResolvedCaKeyPair {
649
+ crypto: Crypto;
650
+ keys: {
651
+ privateKey: CryptoKey;
652
+ publicKey: CryptoKey;
653
+ };
654
+ signingAlgorithm: CaSignAlgorithm;
655
+ }
656
+ /**
657
+ * Resolves a `CryptoKey | CaSigner` into the key pair and per-call `crypto`
658
+ * a CSR or self-signed-certificate generator needs.
659
+ *
660
+ * For a raw `CryptoKey` the public half is derived from the private one.
661
+ * For a {@link CaSigner} it comes from `getPublicKey()` — the only way to
662
+ * obtain it when the private key lives in an HSM and cannot be exported.
663
+ */
664
+ declare function resolveCaKeyPair(key: CryptoKey | CaSigner, baseCrypto?: Pick<Crypto, "subtle" | "getRandomValues">): Promise<ResolvedCaKeyPair>;
641
665
 
642
666
  /**
643
667
  * @param privateKeyInDerOrPem
@@ -657,8 +681,14 @@ declare function _coercePrivateKey(privateKey: unknown): Promise<KeyObject>;
657
681
  interface CreateCertificateFromCsrOptions {
658
682
  /** The CSR to sign, PEM or DER encoded. */
659
683
  csr: string | BufferSource;
660
- /** The issuing CA's subject, e.g. `"CN=MyCA"`. */
661
- issuerName: string;
684
+ /**
685
+ * The issuing CA's subject. Prefer passing the issuer's parsed
686
+ * `subjectName` (`X509Certificate.subjectName`, or the CSR's own for a
687
+ * self-signed root) over a string: re-parsing a string form can change
688
+ * the DER encoding, and an issuer that is not byte-identical to the
689
+ * issuer certificate's subject breaks chain building.
690
+ */
691
+ issuerName: x509.X509CertificateCreateParamsName;
662
692
  /** The issuing CA's public key, used to derive the Authority Key Identifier extension. */
663
693
  issuerPublicKey: PublicKeyType;
664
694
  /** The issuing CA's signing key — a raw key, or an HSM/KMS-backed {@link CaSigner}. */
@@ -691,7 +721,13 @@ declare function createCertificateFromCsr(options: CreateCertificateFromCsrOptio
691
721
  }>;
692
722
 
693
723
  interface CreateCertificateSigningRequestOptions {
694
- privateKey: CryptoKey;
724
+ /**
725
+ * The requester's key. A raw `CryptoKey`, or a {@link CaSigner} when the
726
+ * private key lives in an HSM/KMS and cannot be exported - the public
727
+ * half is then taken from `getPublicKey()` and the proof-of-possession
728
+ * signature is produced by `sign()`.
729
+ */
730
+ privateKey: CryptoKey | CaSigner;
695
731
  notBefore?: Date;
696
732
  notAfter?: Date;
697
733
  validity?: number;
@@ -714,8 +750,12 @@ interface CreateCrlEntryOptions {
714
750
  invalidity?: Date;
715
751
  }
716
752
  interface CreateCrlOptions {
717
- /** The issuing CA's subject, e.g. `"CN=MyCA"`. */
718
- issuerName: string;
753
+ /**
754
+ * The issuing CA's subject. Prefer the issuer certificate's parsed
755
+ * `subjectName` over a string: a CRL whose issuer is not byte-identical
756
+ * to the issuer certificate's subject will not be matched to it.
757
+ */
758
+ issuerName: x509.X509CertificateCreateParamsName;
719
759
  /** The issuing CA's public key, used to derive the Authority Key Identifier extension. */
720
760
  issuerPublicKey: PublicKeyType;
721
761
  /** The issuing CA's signing key — a raw key, or an HSM/KMS-backed {@link CaSigner}. */
@@ -952,4 +992,4 @@ declare const asn1: {
952
992
  readSignatureValueBin: typeof readSignatureValueBin;
953
993
  };
954
994
 
955
- export { type AttributeTypeAndValue, type AuthorityInformationAccess, type AuthorityKeyIdentifier, type BasicConstraints, type CaSignAlgorithm, type CaSigner, type Certificate, type CertificateExtension, type CertificateInfo, type CertificateInternals, type CertificatePEM, CertificatePurpose, type CertificateRevocationList, type CertificateRevocationListInfo, type CertificateSerialNumber, type CertificateSigningRequestInfo, type ComputeDerivedKeysOptions, type CreateCertificateFromCsrOptions, type CreateCrlEntryOptions, type CreateCrlOptions, type CreateSelfSignCertificateOptions, type DER, type DERContentType, type DerivedKeys, type DirectoryName, type DistinguishedNameAttribute, type ExtensionRequest, type Extensions, type KeyExportOptions, type KeyObject, LocalPrivateKeySigner, type Name, type Nonce, type PEM, PaddingAlgorithm, PfxIntegrityError, PfxMalformedError, Pkcs12UnsupportedAlgorithmError, type PrivateKey, type PrivateKeyInternals, type PrivateKeyPEM, PrivateKeyPassphraseRequiredError, type PublicKey, type PublicKeyLength, type PublicKeyPEM, RSA_PKCS1_OAEP_PADDING, RSA_PKCS1_PADDING, type ResolvedCaSigningKey, type RevocationExtensionsParams, type RevokedCertificate, type Signature, Subject, type SubjectAltName, type SubjectOptions, type SubjectPublicKey, type SubjectPublicKeyInfo, type TBSCertList, type TbsCertificate, type Validity, type VerifyChunkSignatureOptions, type VerifyMessageChunkSignatureOptions, type Version, type WebCryptoFromSigner, type X509ExtKeyUsage, type X509KeyUsage, type _VerifyStatus, _coercePrivateKey, asn1, buildAuthorityKeyIdentifierFromIssuer, buildCrlNumberExtension, buildRevocationExtensions, certificateMatchesPrivateKey, clearExploreCertificateCache, coerceCertificate, coerceCertificatePem, coercePEMorDerToPrivateKey, coercePrivateKeyPem, coercePublicKeyPem, coerceRsaPublicKeyPem, combine_der, computeDerivedKeys, computePaddingFooter, convertPEMtoDER, createCertificateFromCsr, createCertificateSigningRequest, createCrl, createPrivateKeyFromNodeJSCrypto, createSelfSignedCertificate, decryptBufferWithDerivedKeys, derToPrivateKey, encryptBufferWithDerivedKeys, exploreAsn1, exploreCertificate, exploreCertificateInfo, exploreCertificateRevocationList, exploreCertificateSigningRequest, explorePrivateKey, extractPublicKeyFromCertificate, extractPublicKeyFromCertificateSync, generateKeyPair, generatePrivateKey, getCrypto, hexDump, identifyDERContent, identifyPemType, isCrlIssuedByCertificate, isKeyObject, makeMessageChunkSignature, makeMessageChunkSignatureWithDerivedKeys, makePrivateKeyFromPem, makePrivateKeyThumbPrint, makePseudoRandomBuffer, makeSHA1Thumbprint, pemToPrivateKey, privateDecrypt, privateDecrypt_long, privateDecrypt_native, privateKeyToCryptoKey, privateKeyToPEM, publicEncrypt, publicEncrypt_long, publicEncrypt_native, publicKeyAndPrivateKeyMatches, readCertificationRequestInfo, readExtension, readNameForCrl, readTbsCertificate, reduceLength, removePadding, removeTrailingLF, resolveCaSigningKey, rsaLengthPrivateKey, rsaLengthPublicKey, rsaLengthRsaPublicKey, split_der, toPem, toPem2, verifyCertificateChain, verifyCertificateOrClrSignature, verifyCertificateRevocationListSignature, verifyCertificateSignature, verifyChunkSignature, verifyChunkSignatureWithDerivedKeys, verifyCrlIssuedByCertificate, verifyMessageChunkSignature, verify_certificate_der_structure, webCryptoFromSigner };
995
+ export { type AttributeTypeAndValue, type AuthorityInformationAccess, type AuthorityKeyIdentifier, type BasicConstraints, type CaSignAlgorithm, type CaSigner, type Certificate, type CertificateExtension, type CertificateInfo, type CertificateInternals, type CertificatePEM, CertificatePurpose, type CertificateRevocationList, type CertificateRevocationListInfo, type CertificateSerialNumber, type CertificateSigningRequestInfo, type ComputeDerivedKeysOptions, type CreateCertificateFromCsrOptions, type CreateCrlEntryOptions, type CreateCrlOptions, type CreateSelfSignCertificateOptions, type DER, type DERContentType, type DerivedKeys, type DirectoryName, type DistinguishedNameAttribute, type ExtensionRequest, type Extensions, type KeyExportOptions, type KeyObject, LocalPrivateKeySigner, type Name, type Nonce, type PEM, PaddingAlgorithm, PfxIntegrityError, PfxMalformedError, Pkcs12UnsupportedAlgorithmError, type PrivateKey, type PrivateKeyInternals, type PrivateKeyPEM, PrivateKeyPassphraseRequiredError, type PublicKey, type PublicKeyLength, type PublicKeyPEM, RSA_PKCS1_OAEP_PADDING, RSA_PKCS1_PADDING, type ResolvedCaKeyPair, type ResolvedCaSigningKey, type RevocationExtensionsParams, type RevokedCertificate, type Signature, Subject, type SubjectAltName, type SubjectOptions, type SubjectPublicKey, type SubjectPublicKeyInfo, type TBSCertList, type TbsCertificate, type Validity, type VerifyChunkSignatureOptions, type VerifyMessageChunkSignatureOptions, type Version, type WebCryptoFromSigner, type X509ExtKeyUsage, type X509KeyUsage, type _VerifyStatus, _coercePrivateKey, asn1, buildAuthorityKeyIdentifierFromIssuer, buildCrlNumberExtension, buildRevocationExtensions, certificateMatchesPrivateKey, clearExploreCertificateCache, coerceCertificate, coerceCertificatePem, coercePEMorDerToPrivateKey, coercePrivateKeyPem, coercePublicKeyPem, coerceRsaPublicKeyPem, combine_der, computeDerivedKeys, computePaddingFooter, convertPEMtoDER, createCertificateFromCsr, createCertificateSigningRequest, createCrl, createPrivateKeyFromNodeJSCrypto, createSelfSignedCertificate, decryptBufferWithDerivedKeys, derToPrivateKey, encryptBufferWithDerivedKeys, exploreAsn1, exploreCertificate, exploreCertificateInfo, exploreCertificateRevocationList, exploreCertificateSigningRequest, explorePrivateKey, extractPublicKeyFromCertificate, extractPublicKeyFromCertificateSync, generateKeyPair, generatePrivateKey, getCrypto, hexDump, identifyDERContent, identifyPemType, isCaSigner, isCrlIssuedByCertificate, isKeyObject, makeMessageChunkSignature, makeMessageChunkSignatureWithDerivedKeys, makePrivateKeyFromPem, makePrivateKeyThumbPrint, makePseudoRandomBuffer, makeSHA1Thumbprint, pemToPrivateKey, privateDecrypt, privateDecrypt_long, privateDecrypt_native, privateKeyToCryptoKey, privateKeyToPEM, publicEncrypt, publicEncrypt_long, publicEncrypt_native, publicKeyAndPrivateKeyMatches, readCertificationRequestInfo, readExtension, readNameForCrl, readTbsCertificate, reduceLength, removePadding, removeTrailingLF, resolveCaKeyPair, resolveCaSigningKey, rsaLengthPrivateKey, rsaLengthPublicKey, rsaLengthRsaPublicKey, split_der, toPem, toPem2, verifyCertificateChain, verifyCertificateOrClrSignature, verifyCertificateRevocationListSignature, verifyCertificateSignature, verifyChunkSignature, verifyChunkSignatureWithDerivedKeys, verifyCrlIssuedByCertificate, verifyMessageChunkSignature, verify_certificate_der_structure, webCryptoFromSigner };
package/dist/index_web.js CHANGED
@@ -61727,6 +61727,31 @@ function buildCrlNumberExtension(crlNumber) {
61727
61727
 
61728
61728
  // ../node-opcua-crypto/source/x509/ca_signer.ts
61729
61729
  init_inject_buffer();
61730
+
61731
+ // ../node-opcua-crypto/source/x509/_build_public_key.ts
61732
+ init_inject_buffer();
61733
+ async function buildPublicKey(privateKey) {
61734
+ const crypto4 = getCrypto();
61735
+ const jwk = await crypto4.subtle.exportKey("jwk", privateKey);
61736
+ delete jwk.d;
61737
+ delete jwk.dp;
61738
+ delete jwk.dq;
61739
+ delete jwk.q;
61740
+ delete jwk.qi;
61741
+ jwk.key_ops = [
61742
+ "encrypt",
61743
+ "sign"
61744
+ // "wrapKey"
61745
+ ];
61746
+ const publicKey = await crypto4.subtle.importKey("jwk", jwk, { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } }, true, [
61747
+ // "encrypt",
61748
+ // "sign",
61749
+ // "wrapKey",
61750
+ ]);
61751
+ return publicKey;
61752
+ }
61753
+
61754
+ // ../node-opcua-crypto/source/x509/ca_signer.ts
61730
61755
  var LocalPrivateKeySigner = class {
61731
61756
  algorithm;
61732
61757
  #privateKey;
@@ -61790,6 +61815,21 @@ async function resolveCaSigningKey(signingKey, baseCrypto = getCrypto()) {
61790
61815
  }
61791
61816
  return { crypto: baseCrypto, signingKey };
61792
61817
  }
61818
+ async function resolveCaKeyPair(key, baseCrypto = getCrypto()) {
61819
+ if (isCaSigner(key)) {
61820
+ const adapted = await webCryptoFromSigner(key, baseCrypto);
61821
+ return {
61822
+ crypto: adapted.crypto,
61823
+ keys: { privateKey: adapted.privateKeyHandle, publicKey: adapted.publicKey },
61824
+ signingAlgorithm: key.algorithm
61825
+ };
61826
+ }
61827
+ return {
61828
+ crypto: baseCrypto,
61829
+ keys: { privateKey: key, publicKey: await buildPublicKey(key) },
61830
+ signingAlgorithm: { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } }
61831
+ };
61832
+ }
61793
61833
 
61794
61834
  // ../node-opcua-crypto/source/x509/coerce_private_key.ts
61795
61835
  init_inject_buffer();
@@ -61938,11 +61978,17 @@ async function createCertificateFromCsr(options) {
61938
61978
  }
61939
61979
  const extensions = [
61940
61980
  basicConstraints,
61941
- new x509_es_exports.ExtendedKeyUsageExtension(keyUsageExtension, true),
61981
+ // RFC 5280 4.2.1.12: extendedKeyUsage SHALL contain at least one
61982
+ // KeyPurposeId. getAttributes yields an empty list for a CA
61983
+ // certificate, so emitting it unconditionally would produce an
61984
+ // empty critical extension that a strict validator must reject.
61985
+ ...keyUsageExtension.length > 0 ? [new x509_es_exports.ExtendedKeyUsageExtension(keyUsageExtension, true)] : [],
61942
61986
  new x509_es_exports.KeyUsagesExtension(usages, true),
61943
61987
  await x509_es_exports.SubjectKeyIdentifierExtension.create(csr.publicKey),
61944
61988
  await buildAuthorityKeyIdentifierFromIssuer(options.issuerPublicKey, crypto4),
61945
- new x509_es_exports.SubjectAlternativeNameExtension(alternativeNameExtensions),
61989
+ // an empty SubjectAlternativeName is an empty GeneralNames sequence,
61990
+ // which RFC 5280 4.2.1.6 does not permit - omit the extension instead
61991
+ ...alternativeNameExtensions.length > 0 ? [new x509_es_exports.SubjectAlternativeNameExtension(alternativeNameExtensions)] : [],
61946
61992
  ...options.revocation ? buildRevocationExtensions(options.revocation) : []
61947
61993
  ];
61948
61994
  const notBefore = options.notBefore ?? /* @__PURE__ */ new Date();
@@ -61968,31 +62014,6 @@ async function createCertificateFromCsr(options) {
61968
62014
 
61969
62015
  // ../node-opcua-crypto/source/x509/create_certificate_signing_request.ts
61970
62016
  init_inject_buffer();
61971
-
61972
- // ../node-opcua-crypto/source/x509/_build_public_key.ts
61973
- init_inject_buffer();
61974
- async function buildPublicKey(privateKey) {
61975
- const crypto4 = getCrypto();
61976
- const jwk = await crypto4.subtle.exportKey("jwk", privateKey);
61977
- delete jwk.d;
61978
- delete jwk.dp;
61979
- delete jwk.dq;
61980
- delete jwk.q;
61981
- delete jwk.qi;
61982
- jwk.key_ops = [
61983
- "encrypt",
61984
- "sign"
61985
- // "wrapKey"
61986
- ];
61987
- const publicKey = await crypto4.subtle.importKey("jwk", jwk, { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } }, true, [
61988
- // "encrypt",
61989
- // "sign",
61990
- // "wrapKey",
61991
- ]);
61992
- return publicKey;
61993
- }
61994
-
61995
- // ../node-opcua-crypto/source/x509/create_certificate_signing_request.ts
61996
62017
  async function createCertificateSigningRequest({
61997
62018
  privateKey,
61998
62019
  subject,
@@ -62001,19 +62022,7 @@ async function createCertificateSigningRequest({
62001
62022
  applicationUri,
62002
62023
  purpose
62003
62024
  }) {
62004
- const crypto4 = getCrypto();
62005
- const modulusLength = 2048;
62006
- const alg = {
62007
- name: "RSASSA-PKCS1-v1_5",
62008
- hash: { name: "SHA-256" },
62009
- publicExponent: new Uint8Array([1, 0, 1]),
62010
- modulusLength
62011
- };
62012
- const publicKey = await buildPublicKey(privateKey);
62013
- const keys = {
62014
- privateKey,
62015
- publicKey
62016
- };
62025
+ const { crypto: crypto4, keys, signingAlgorithm } = await resolveCaKeyPair(privateKey);
62017
62026
  const alternativeNameExtensions = [];
62018
62027
  for (const d of dns ?? []) {
62019
62028
  alternativeNameExtensions.push({ type: "dns", value: d });
@@ -62032,7 +62041,7 @@ async function createCertificateSigningRequest({
62032
62041
  {
62033
62042
  name,
62034
62043
  keys,
62035
- signingAlgorithm: alg,
62044
+ signingAlgorithm,
62036
62045
  extensions: [
62037
62046
  basicConstraints,
62038
62047
  new x509_es_exports.KeyUsagesExtension(usages, true),
@@ -62136,7 +62145,8 @@ async function createSelfSignedCertificate({
62136
62145
  new x509_es_exports.Extension(ID_NETSCAPE_COMMENT, false, AsnConvert.serialize(AsnUtf8StringConverter.toASN(nsComment))),
62137
62146
  // new x509.BasicConstraintsExtension(true, 2, true),
62138
62147
  basicConstraints,
62139
- new x509_es_exports.ExtendedKeyUsageExtension(keyUsageExtension, true),
62148
+ // see RFC 5280 4.2.1.12 - an empty critical EKU is invalid
62149
+ ...keyUsageExtension.length > 0 ? [new x509_es_exports.ExtendedKeyUsageExtension(keyUsageExtension, true)] : [],
62140
62150
  new x509_es_exports.KeyUsagesExtension(usages, true),
62141
62151
  await x509_es_exports.SubjectKeyIdentifierExtension.create(keys.publicKey),
62142
62152
  await x509_es_exports.AuthorityKeyIdentifierExtension.create(keys.publicKey),
@@ -62424,6 +62434,7 @@ export {
62424
62434
  hexDump,
62425
62435
  identifyDERContent,
62426
62436
  identifyPemType,
62437
+ isCaSigner,
62427
62438
  isCrlIssuedByCertificate,
62428
62439
  isKeyObject,
62429
62440
  makeMessageChunkSignature,
@@ -62449,6 +62460,7 @@ export {
62449
62460
  reduceLength,
62450
62461
  removePadding,
62451
62462
  removeTrailingLF,
62463
+ resolveCaKeyPair,
62452
62464
  resolveCaSigningKey,
62453
62465
  rsaLengthPrivateKey,
62454
62466
  rsaLengthPublicKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-opcua-crypto-web",
3
- "version": "5.7.2",
3
+ "version": "5.8.0",
4
4
  "type": "module",
5
5
  "description": "Pre-bundled, zero-config browser build of node-opcua-crypto",
6
6
  "//why": "This package exists so node-opcua-crypto can stay lean for Node. The pre-bundled browser build inlines its polyfills (crypto-browserify, stream-browserify, ...) and weighs ~344 KB gzip - more than the entire node-opcua-crypto tarball. Shipping it there would make every Node consumer download a browser bundle they never import. Browser consumers install this package explicitly, or let node-opcua-crypto's \"browser\" export condition redirect to it (see node-opcua-crypto/browser/index.js).",
@@ -67,5 +67,5 @@
67
67
  "LICENSE",
68
68
  "README.md"
69
69
  ],
70
- "gitHead": "1d0adf954c8c34cac820e05d982b029f05884e0c"
70
+ "gitHead": "20bf526d2b82b6cdff47ad96d4fdf5f1b9155758"
71
71
  }