codekingpro/portable-devtools
114k
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseInteger = parseInteger;
4exports.parseStringASCII = parseStringASCII;
5exports.parseTime = parseTime;
6exports.parseOID = parseOID;
7exports.parseBoolean = parseBoolean;
8exports.parseBitString = parseBitString;
9/*
10Copyright 2023 The Sigstore Authors.
11
12Licensed under the Apache License, Version 2.0 (the "License");
13you may not use this file except in compliance with the License.
14You may obtain a copy of the License at
15
16 http://www.apache.org/licenses/LICENSE-2.0
17
18Unless required by applicable law or agreed to in writing, software
19distributed under the License is distributed on an "AS IS" BASIS,
20WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21See the License for the specific language governing permissions and
22limitations under the License.
23*/
24const RE_TIME_SHORT_YEAR = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\.\d{3})?Z$/;
25const RE_TIME_LONG_YEAR = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\.\d{3})?Z$/;
26// Parse a BigInt from the DER-encoded buffer
27// https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-integer
28function parseInteger(buf) {
29 let pos = 0;
30 const end = buf.length;
31 let val = buf[pos];
32 const neg = val > 0x7f;
33 // Consume any padding bytes
34 const pad = neg ? 0xff : 0x00;
35 while (val == pad && ++pos < end) {
36 val = buf[pos];
37 }
38 // Calculate remaining bytes to read
39 const len = end - pos;
40 if (len === 0)
41 return BigInt(neg ? -1 : 0);
42 // Handle two's complement for negative numbers
43 val = neg ? val - 256 : val;
44 // Parse remaining bytes
45 let n = BigInt(val);
46 for (let i = pos + 1; i < end; ++i) {
47 n = n * BigInt(256) + BigInt(buf[i]);
48 }
49 return n;
50}
51// Parse an ASCII string from the DER-encoded buffer
52// https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-basic-types#boolean
53function parseStringASCII(buf) {
54 return buf.toString('ascii');
55}
56// Parse a Date from the DER-encoded buffer
57// https://www.rfc-editor.org/rfc/rfc5280#section-4.1.2.5.1
58function parseTime(buf, shortYear) {
59 const timeStr = parseStringASCII(buf);
60 // Parse the time string into matches - captured groups start at index 1
61 const m = shortYear
62 ? RE_TIME_SHORT_YEAR.exec(timeStr)
63 : RE_TIME_LONG_YEAR.exec(timeStr);
64 if (!m) {
65 throw new Error('invalid time');
66 }
67 // Translate dates with a 2-digit year to 4 digits per the spec
68 if (shortYear) {
69 let year = Number(m[1]);
70 year += year >= 50 ? 1900 : 2000;
71 m[1] = year.toString();
72 }
73 // Translate to ISO8601 format and parse
74 return new Date(`${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}Z`);
75}
76// Parse an OID from the DER-encoded buffer
77// https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-object-identifier
78function parseOID(buf) {
79 let pos = 0;
80 const end = buf.length;
81 // Consume first byte which encodes the first two OID components
82 let n = buf[pos++];
83 const first = Math.floor(n / 40);
84 const second = n % 40;
85 let oid = `${first}.${second}`;
86 // Consume remaining bytes
87 let val = 0;
88 for (; pos < end; ++pos) {
89 n = buf[pos];
90 val = (val << 7) + (n & 0x7f);
91 // If the left-most bit is NOT set, then this is the last byte in the
92 // sequence and we can add the value to the OID and reset the accumulator
93 if ((n & 0x80) === 0) {
94 oid += `.${val}`;
95 val = 0;
96 }
97 }
98 return oid;
99}
100// Parse a boolean from the DER-encoded buffer
101// https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-basic-types#boolean
102function parseBoolean(buf) {
103 return buf[0] !== 0;
104}
105// Parse a bit string from the DER-encoded buffer
106// https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-bit-string
107function parseBitString(buf) {
108 // First byte tell us how many unused bits are in the last byte
109 const unused = buf[0];
110 const start = 1;
111 const end = buf.length;
112 const bits = [];
113 for (let i = start; i < end; ++i) {
114 const byte = buf[i];
115 // The skip value is only used for the last byte
116 const skip = i === end - 1 ? unused : 0;
117 // Iterate over each bit in the byte (most significant first)
118 for (let j = 7; j >= skip; --j) {
119 // Read the bit and add it to the bit string
120 bits.push((byte >> j) & 0x01);
121 }
122 }
123 return bits;
124}
125 