@peculiar/asn1-x509
Advanced tools
Comparing version 2.0.3 to 2.0.4
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ip = require("ipaddr.js"); | ||
const pvtsutils_1 = require("pvtsutils"); | ||
class IpConverter { | ||
static isV4(value) { | ||
return /^(\d{1,3}\.){3,3}\d{1,3}$/.test(value); | ||
static decodeIP(value) { | ||
if (value.length === 64 && parseInt(value, 16) === 0) { | ||
return "::/0"; | ||
} | ||
if (value.length !== 16) { | ||
return value; | ||
} | ||
const mask = parseInt(value.slice(8), 16) | ||
.toString(2) | ||
.split('') | ||
.reduce((a, k) => a + (+k), 0); | ||
let ip = value.slice(0, 8) | ||
.replace(/(.{2})/g, match => `${parseInt(match, 16)}.`); | ||
ip = ip.slice(0, -1); | ||
return `${ip}/${mask}`; | ||
} | ||
static isV6(value) { | ||
return /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i.test(value); | ||
} | ||
static toString(buf) { | ||
const data = new Uint8Array(buf); | ||
if (buf.byteLength === 4) { | ||
const res = []; | ||
data.forEach((octet) => { | ||
res.push(octet.toString()); | ||
}); | ||
return res.join("."); | ||
if (buf.byteLength === 4 || buf.byteLength === 16) { | ||
const uint8 = new Uint8Array(buf); | ||
const addr = ip.fromByteArray(Array.from(uint8)); | ||
return addr.toString(); | ||
} | ||
else { | ||
throw new Error("IP V6 is not implemented"); | ||
} | ||
return this.decodeIP(pvtsutils_1.Convert.ToHex(buf)); | ||
} | ||
static fromString(text) { | ||
if (this.isV4(text)) { | ||
const bytes = text.split(".").map((byte) => parseInt(byte, 10) & 0xff); | ||
return new Uint8Array(bytes).buffer; | ||
} | ||
else { | ||
throw new Error("IP V6 is not implemented"); | ||
} | ||
const addr = ip.parse(text); | ||
return new Uint8Array(addr.toByteArray()).buffer; | ||
} | ||
} | ||
exports.IpConverter = IpConverter; |
@@ -0,30 +1,32 @@ | ||
import * as ip from "ipaddr.js"; | ||
import { Convert } from "pvtsutils"; | ||
export class IpConverter { | ||
static isV4(value) { | ||
return /^(\d{1,3}\.){3,3}\d{1,3}$/.test(value); | ||
static decodeIP(value) { | ||
if (value.length === 64 && parseInt(value, 16) === 0) { | ||
return "::/0"; | ||
} | ||
if (value.length !== 16) { | ||
return value; | ||
} | ||
const mask = parseInt(value.slice(8), 16) | ||
.toString(2) | ||
.split('') | ||
.reduce((a, k) => a + (+k), 0); | ||
let ip = value.slice(0, 8) | ||
.replace(/(.{2})/g, match => `${parseInt(match, 16)}.`); | ||
ip = ip.slice(0, -1); | ||
return `${ip}/${mask}`; | ||
} | ||
static isV6(value) { | ||
return /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i.test(value); | ||
} | ||
static toString(buf) { | ||
const data = new Uint8Array(buf); | ||
if (buf.byteLength === 4) { | ||
const res = []; | ||
data.forEach((octet) => { | ||
res.push(octet.toString()); | ||
}); | ||
return res.join("."); | ||
if (buf.byteLength === 4 || buf.byteLength === 16) { | ||
const uint8 = new Uint8Array(buf); | ||
const addr = ip.fromByteArray(Array.from(uint8)); | ||
return addr.toString(); | ||
} | ||
else { | ||
throw new Error("IP V6 is not implemented"); | ||
} | ||
return this.decodeIP(Convert.ToHex(buf)); | ||
} | ||
static fromString(text) { | ||
if (this.isV4(text)) { | ||
const bytes = text.split(".").map((byte) => parseInt(byte, 10) & 0xff); | ||
return new Uint8Array(bytes).buffer; | ||
} | ||
else { | ||
throw new Error("IP V6 is not implemented"); | ||
} | ||
const addr = ip.parse(text); | ||
return new Uint8Array(addr.toByteArray()).buffer; | ||
} | ||
} |
export declare class IpConverter { | ||
static isV4(value: string): boolean; | ||
static isV6(value: string): boolean; | ||
private static decodeIP; | ||
static toString(buf: ArrayBuffer): string; | ||
static fromString(text: string): ArrayBuffer; | ||
} |
{ | ||
"name": "@peculiar/asn1-x509", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "ASN.1 schema of `Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile` (RFC5280)", | ||
@@ -35,8 +35,9 @@ "bugs": { | ||
"dependencies": { | ||
"@peculiar/asn1-schema": "^2.0.3", | ||
"@peculiar/asn1-schema": "^2.0.4", | ||
"asn1js": "^2.0.26", | ||
"ipaddr.js": "^1.9.1", | ||
"pvtsutils": "^1.0.10", | ||
"tslib": "^1.11.1" | ||
}, | ||
"gitHead": "af5e4f57c4db0c331f53d3bf22cebb1acf31ba30" | ||
"gitHead": "83feaaaec26bb73d64881d40c85c0595dd714f3a" | ||
} |
@@ -0,29 +1,40 @@ | ||
import * as ip from "ipaddr.js"; | ||
import { Convert } from "pvtsutils"; | ||
export class IpConverter { | ||
public static isV4(value: string) { | ||
return /^(\d{1,3}\.){3,3}\d{1,3}$/.test(value); | ||
private static decodeIP(value: string) { | ||
if (value.length === 64 && parseInt(value, 16) === 0) { | ||
return "::/0"; | ||
} | ||
if (value.length !== 16) { | ||
return value; | ||
} | ||
const mask = parseInt(value.slice(8), 16) | ||
.toString(2) | ||
.split('') | ||
.reduce((a, k) => a + (+k), 0); | ||
let ip = value.slice(0, 8) | ||
.replace(/(.{2})/g, match => `${parseInt(match, 16)}.`); | ||
ip = ip.slice(0, -1); | ||
return `${ip}/${mask}`; | ||
} | ||
public static isV6(value: string) { | ||
return /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i.test(value); | ||
} | ||
public static toString(buf: ArrayBuffer) { | ||
const data = new Uint8Array(buf); | ||
if (buf.byteLength === 4) { | ||
// v4 | ||
const res: string[] = []; | ||
data.forEach((octet) => { | ||
res.push(octet.toString()); | ||
}); | ||
return res.join("."); | ||
} else { | ||
throw new Error("IP V6 is not implemented"); | ||
if (buf.byteLength === 4 || buf.byteLength === 16) { | ||
const uint8 = new Uint8Array(buf); | ||
const addr = ip.fromByteArray(Array.from(uint8)); | ||
return addr.toString(); | ||
} | ||
return this.decodeIP(Convert.ToHex(buf)); | ||
} | ||
public static fromString(text: string): ArrayBuffer { | ||
if (this.isV4(text)) { | ||
const bytes = text.split(".").map((byte) => parseInt(byte, 10) & 0xff); | ||
return new Uint8Array(bytes).buffer; | ||
} else { | ||
throw new Error("IP V6 is not implemented"); | ||
} | ||
const addr = ip.parse(text) as any; | ||
return new Uint8Array(addr.toByteArray()).buffer; | ||
} | ||
} |
import * as assert from "assert"; | ||
import { AsnParser } from "@peculiar/asn1-schema"; | ||
import { AsnParser, AsnConvert } from "@peculiar/asn1-schema"; | ||
import { Convert } from "pvtsutils"; | ||
import { Certificate, id_ce_cRLDistributionPoints, CRLDistributionPoints, id_ce_keyUsage, KeyUsage } from "../src"; | ||
import { Certificate, id_ce_cRLDistributionPoints, CRLDistributionPoints, id_ce_keyUsage, KeyUsage, id_ce_extKeyUsage, ExtendedKeyUsage, NameConstraints, GeneralSubtrees, GeneralSubtree, GeneralName } from "../src"; | ||
import { CertificateTemplate } from "@peculiar/asn1-x509-microsoft"; | ||
@@ -26,3 +27,3 @@ context("x509", () => { | ||
const keyUsage = AsnParser.parse(extension.extnValue, KeyUsage); | ||
assert.equal(keyUsage.toString(),"[digitalSignature, keyEncipherment]"); | ||
assert.equal(keyUsage.toString(), "[digitalSignature, keyEncipherment]"); | ||
} | ||
@@ -33,2 +34,53 @@ }); | ||
it("Extended key usages", () => { | ||
const hex = `300c06042a030405060453040506`; | ||
const eku = AsnParser.parse(Convert.FromHex(hex), ExtendedKeyUsage); | ||
assert.equal(eku.join(", "), "1.2.3.4.5, 2.3.4.5.6"); | ||
}); | ||
it("Name constrains", () => { | ||
var nameConstrains = new NameConstraints({ | ||
permittedSubtrees: new GeneralSubtrees([ | ||
new GeneralSubtree({ | ||
base: new GeneralName({ | ||
dNSName: "some.dns.com", | ||
}) | ||
}), | ||
new GeneralSubtree({ | ||
base: new GeneralName({ | ||
iPAddress: "192.168.1.1", | ||
}) | ||
}), | ||
new GeneralSubtree({ | ||
base: new GeneralName({ | ||
iPAddress: "2001:0db8:11a3:09d7:1f34:8a2e:07a0:765d", | ||
}) | ||
}), | ||
]) | ||
}); | ||
const der = AsnConvert.serialize(nameConstrains); | ||
const test = AsnParser.parse(der, NameConstraints); | ||
assert.equal(test.permittedSubtrees![0].base.dNSName, "some.dns.com"); | ||
assert.equal(test.permittedSubtrees![1].base.iPAddress, "192.168.1.1"); | ||
assert.equal(test.permittedSubtrees![2].base.iPAddress, "2001:db8:11a3:9d7:1f34:8a2e:7a0:765d"); | ||
}); | ||
it("Certificate template", () => { | ||
const certTemplate = new CertificateTemplate({ | ||
templateID: "1.2.3.4.5.6.7.8.9", | ||
templateMajorVersion: 101, | ||
templateMinorVersion: 0, | ||
}); | ||
const der = AsnConvert.serialize(certTemplate); | ||
const test = AsnConvert.parse(der, CertificateTemplate); | ||
assert.equal(test.templateID, "1.2.3.4.5.6.7.8.9"); | ||
assert.equal(test.templateMajorVersion, 101); | ||
assert.equal(test.templateMinorVersion, 0); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
173689
4890
5
+ Addedipaddr.js@^1.9.1
+ Addedipaddr.js@1.9.1(transitive)
Updated@peculiar/asn1-schema@^2.0.4