@blamejs/pki 0.1.31 → 0.2.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.
package/lib/oid.js CHANGED
@@ -39,6 +39,10 @@ var guard = require("./guard-all");
39
39
 
40
40
  var OidError = frameworkError.OidError;
41
41
 
42
+ // (code, message) -> OidError, the factory shape the composed guards throw
43
+ // through so a malformed identifier keeps the oid/* typed verdict.
44
+ function _oidError(c, m) { return new OidError(c, m); }
45
+
42
46
  // FAMILIES -- OIDs grouped by their shared base arc (the "similar starting
43
47
  // variable" that defines a class). A member is `name: leaf`, where leaf is a
44
48
  // trailing arc (number) or a short arc array for a multi-level leaf; the full
@@ -302,11 +306,18 @@ Object.keys(FAMILIES).forEach(function (fam) {
302
306
  });
303
307
 
304
308
  function _assertDotted(dotted, who) {
305
- // No leading-zero components: "01.2.840" is a key no decoded OID can match,
306
- // and the arc converters round-trip it to a DIFFERENT OID ("1.2.840").
307
- if (typeof dotted !== "string" || !/^(0|[1-9]\d*)(\.(0|[1-9]\d*))+$/.test(dotted)) {
308
- throw new OidError("oid/bad-input", who + ": expected a dotted-decimal OID string (no leading-zero components)");
309
- }
309
+ // SYNTAX only (canonical dotted form, no leading-zero arc that would round-trip
310
+ // to a DIFFERENT OID). For the LOOKUP entry points (name / has): a well-formed
311
+ // but non-encodable OID is simply not registered (a miss), not an error, so the
312
+ // X.660 arc bounds are waived (boundsCode null).
313
+ guard.identifier.assertCanonicalOid(dotted, _oidError, "oid/bad-input", who, null);
314
+ }
315
+
316
+ function _assertEncodable(dotted, who) {
317
+ // SYNTAX and the X.660 arc bounds -- for the paths that assert / convert a real
318
+ // encodable OID (toArcs / toDER / register), where an out-of-bounds arc is a
319
+ // hard reject (oid/bad-arc), so the string form agrees with the DER form.
320
+ guard.identifier.assertCanonicalOid(dotted, _oidError, "oid/bad-input", who, "oid/bad-arc");
310
321
  }
311
322
 
312
323
  // X.660 encodability: the root arc is 0..2 and, under roots 0 and 1, the
@@ -370,9 +381,11 @@ function has(dotted) {
370
381
  * pki.oid.register("1.3.6.1.4.1.99999.1", "acmeWidgetPolicy");
371
382
  */
372
383
  function register(dotted, n) {
373
- _assertDotted(dotted, "register");
384
+ // _assertDotted now enforces the X.660 arc bounds on the string form too, so
385
+ // the separate arc-based check register used to run is redundant here (it is
386
+ // kept for registerFamily, which validates arcs it assembles, not a string).
387
+ _assertEncodable(dotted, "register");
374
388
  if (typeof n !== "string" || n.length === 0) throw new OidError("oid/bad-input", "register: name must be a non-empty string");
375
- _assertEncodableArcs(dotted.split(".").map(BigInt), "register");
376
389
  _index(dotted, n);
377
390
  }
378
391
 
@@ -432,7 +445,7 @@ function all() {
432
445
  // Number where safe and BigInt where an arc exceeds 2^53 (rare, but a
433
446
  // UUID-based OID arc can), so the round-trip never loses precision.
434
447
  function toArcs(dotted) {
435
- _assertDotted(dotted, "toArcs");
448
+ _assertEncodable(dotted, "toArcs");
436
449
  return dotted.split(".").map(function (p) {
437
450
  var b = BigInt(p);
438
451
  return b <= 9007199254740991n ? Number(b) : b;
@@ -453,7 +466,7 @@ function fromArcs(arcs) {
453
466
 
454
467
  // DER convenience -- thin pass-throughs to the codec so callers reach for
455
468
  // one namespace when they have a dotted string in hand.
456
- function toDER(dotted) { _assertDotted(dotted, "toDER"); return asn1.build.oid(dotted); }
469
+ function toDER(dotted) { _assertEncodable(dotted, "toDER"); return asn1.build.oid(dotted); }
457
470
  function fromDER(input) {
458
471
  var buf = guard.bytes.view(input, OidError, "oid/bad-input", "fromDER");
459
472
  var node = asn1.decode(buf);