1"use strict"; 2Object.defineProperty(exports, "__esModule", { value: true }); 3exports.ASN1Tag = exports.UNIVERSAL_TAG = void 0; 4/* 5Copyright 2023 The Sigstore Authors. 6 7Licensed under the Apache License, Version 2.0 (the "License"); 8you may not use this file except in compliance with the License. 9You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13Unless required by applicable law or agreed to in writing, software 14distributed under the License is distributed on an "AS IS" BASIS, 15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16See the License for the specific language governing permissions and 17limitations under the License. 18*/ 19const error_1 = require("./error"); 20exports.UNIVERSAL_TAG = { 21 BOOLEAN: 0x01, 22 INTEGER: 0x02, 23 BIT_STRING: 0x03, 24 OCTET_STRING: 0x04, 25 OBJECT_IDENTIFIER: 0x06, 26 SEQUENCE: 0x10, 27 SET: 0x11, 28 PRINTABLE_STRING: 0x13, 29 UTC_TIME: 0x17, 30 GENERALIZED_TIME: 0x18, 31}; 32const TAG_CLASS = { 33 UNIVERSAL: 0x00, 34 APPLICATION: 0x01, 35 CONTEXT_SPECIFIC: 0x02, 36 PRIVATE: 0x03, 37}; 38// https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-encoded-tag-bytes 39class ASN1Tag { 40 constructor(enc) { 41 // Bits 0 through 4 are the tag number 42 this.number = enc & 0x1f; 43 // Bit 5 is the constructed bit 44 this.constructed = (enc & 0x20) === 0x20; 45 // Bit 6 & 7 are the class 46 this.class = enc >> 6; 47 if (this.number === 0x1f) { 48 throw new error_1.ASN1ParseError('long form tags not supported'); 49 } 50 if (this.class === TAG_CLASS.UNIVERSAL && this.number === 0x00) { 51 throw new error_1.ASN1ParseError('unsupported tag 0x00'); 52 } 53 } 54 isUniversal() { 55 return this.class === TAG_CLASS.UNIVERSAL; 56 } 57 isContextSpecific(num) { 58 const res = this.class === TAG_CLASS.CONTEXT_SPECIFIC; 59 return num !== undefined ? res && this.number === num : res; 60 } 61 isBoolean() { 62 return this.isUniversal() && this.number === exports.UNIVERSAL_TAG.BOOLEAN; 63 } 64 isInteger() { 65 return this.isUniversal() && this.number === exports.UNIVERSAL_TAG.INTEGER; 66 } 67 isBitString() { 68 return this.isUniversal() && this.number === exports.UNIVERSAL_TAG.BIT_STRING; 69 } 70 isOctetString() { 71 return this.isUniversal() && this.number === exports.UNIVERSAL_TAG.OCTET_STRING; 72 } 73 isOID() { 74 return (this.isUniversal() && this.number === exports.UNIVERSAL_TAG.OBJECT_IDENTIFIER); 75 } 76 isUTCTime() { 77 return this.isUniversal() && this.number === exports.UNIVERSAL_TAG.UTC_TIME; 78 } 79 isGeneralizedTime() { 80 return this.isUniversal() && this.number === exports.UNIVERSAL_TAG.GENERALIZED_TIME; 81 } 82 toDER() { 83 return this.number | (this.constructed ? 0x20 : 0x00) | (this.class << 6); 84 } 85} 86exports.ASN1Tag = ASN1Tag; 87