All files / src Extension.js

76.25% Statements 61/80
75.86% Branches 44/58
83.33% Functions 5/6
76.25% Lines 61/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272                                                                      9512x         9512x         9512x 313x   9199x   9512x         276x       9512x 9199x                   29222x     9512x   10235x   9199x   276x                                                   14510x   14510x                                       9199x                     9199x         9199x 9199x 2964x 9199x     9199x 9199x     9199x           1437x 1437x   1763x 1763x           42x 42x   1223x 1223x     679x 679x   413x 413x         48x 48x   16x 16x   93x 93x     83x 83x   1210x 1210x   96x 96x   1893x 1893x   153x 153x         24x 24x                                   723x   723x   723x 174x   723x       723x                                                        
import * as asn1js from "asn1js";
import { getParametersValue } from "pvutils";
import SubjectDirectoryAttributes from "./SubjectDirectoryAttributes.js";
import PrivateKeyUsagePeriod from "./PrivateKeyUsagePeriod.js";
import AltName from "./AltName.js";
import BasicConstraints from "./BasicConstraints.js";
import IssuingDistributionPoint from "./IssuingDistributionPoint.js";
import GeneralNames from "./GeneralNames.js";
import NameConstraints from "./NameConstraints.js";
import CRLDistributionPoints from "./CRLDistributionPoints.js";
import CertificatePolicies from "./CertificatePolicies.js";
import PolicyMappings from "./PolicyMappings.js";
import AuthorityKeyIdentifier from "./AuthorityKeyIdentifier.js";
import PolicyConstraints from "./PolicyConstraints.js";
import ExtKeyUsage from "./ExtKeyUsage.js";
import InfoAccess from "./InfoAccess.js";
//**************************************************************************************
/**
 * Class from RFC5280
 */
export default class Extension
{
	//**********************************************************************************
	/**
	 * Constructor for Extension class
	 * @param {Object} [parameters={}]
	 * @property {Object} [schema] asn1js parsed value
	 */
	constructor(parameters = {})
	{
		//region Internal properties of the object
		/**
		 * @type {string}
		 * @description extnID
		 */
		this.extnID = getParametersValue(parameters, "extnID", Extension.defaultValues("extnID"));
		/**
		 * @type {boolean}
		 * @description critical
		 */
		this.critical = getParametersValue(parameters, "critical", Extension.defaultValues("critical"));
		/**
		 * @type {OctetString}
		 * @description extnValue
		 */
		if("extnValue" in parameters)
			this.extnValue = new asn1js.OctetString({ valueHex: parameters.extnValue });
		else
			this.extnValue = Extension.defaultValues("extnValue");
 
		if("parsedValue" in parameters)
			/**
			 * @type {Object}
			 * @description parsedValue
			 */
			this.parsedValue = getParametersValue(parameters, "parsedValue", Extension.defaultValues("parsedValue"));
		//endregion
 
		//region If input argument array contains "schema" for this object
		if("schema" in parameters)
			this.fromSchema(parameters.schema);
		//endregion
	}
	//**********************************************************************************
	/**
	 * Return default values for all class members
	 * @param {string} memberName String name for a class member
	 */
	static defaultValues(memberName)
	{
		switch(memberName)
		{
			case "extnID":
				return "";
			case "critical":
				return false;
			case "extnValue":
				return new asn1js.OctetString();
			case "parsedValue":
				return {};
			default:
				throw new Error(`Invalid member name for Extension class: ${memberName}`);
		}
	}
	//**********************************************************************************
	/**
	 * Return value of asn1js schema for current class
	 * @param {Object} parameters Input parameters for the schema
	 * @returns {Object} asn1js schema object
	 */
	static schema(parameters = {})
	{
		//Extension  ::=  SEQUENCE  {
		//    extnID      OBJECT IDENTIFIER,
		//    critical    BOOLEAN DEFAULT FALSE,
		//    extnValue   OCTET STRING
		//}
 
		/**
		 * @type {Object}
		 * @property {string} [blockName]
		 * @property {string} [extnID]
		 * @property {string} [critical]
		 * @property {string} [extnValue]
		 */
		const names = getParametersValue(parameters, "names", {});
 
		return (new asn1js.Sequence({
			name: (names.blockName || ""),
			value: [
				new asn1js.ObjectIdentifier({ name: (names.extnID || "") }),
				new asn1js.Boolean({
					name: (names.critical || ""),
					optional: true
				}),
				new asn1js.OctetString({ name: (names.extnValue || "") })
			]
		}));
	}
	//**********************************************************************************
	/**
	 * Convert parsed asn1js object into current class
	 * @param {!Object} schema
	 */
	fromSchema(schema)
	{
		//region Check the schema is valid
		let asn1 = asn1js.compareSchema(schema,
			schema,
			Extension.schema({
				names: {
					extnID: "extnID",
					critical: "critical",
					extnValue: "extnValue"
				}
			})
		);
 
		Iif(asn1.verified === false)
			throw new Error("Object's schema was not verified against input data for EXTENSION");
		//endregion
 
		//region Get internal properties from parsed schema
		this.extnID = asn1.result.extnID.valueBlock.toString();
		if("critical" in asn1.result)
			this.critical = asn1.result.critical.valueBlock.value;
		this.extnValue = asn1.result.extnValue;
 
		//region Get "parsedValue" for well-known extensions
		asn1 = asn1js.fromBER(this.extnValue.valueBlock.valueHex);
		Iif(asn1.offset === (-1))
			return;
 
		switch(this.extnID)
		{
			case "2.5.29.9": // SubjectDirectoryAttributes
				this.parsedValue = new SubjectDirectoryAttributes({ schema: asn1.result });
				break;
			case "2.5.29.14": // SubjectKeyIdentifier
				this.parsedValue = asn1.result; // Should be just a simple OCTETSTRING
				break;
			case "2.5.29.15": // KeyUsage
				this.parsedValue = asn1.result; // Should be just a simple BITSTRING
				break;
			case "2.5.29.16": // PrivateKeyUsagePeriod
				this.parsedValue = new PrivateKeyUsagePeriod({ schema: asn1.result });
				break;
			case "2.5.29.17": // SubjectAltName
			case "2.5.29.18": // IssuerAltName
				this.parsedValue = new AltName({ schema: asn1.result });
				break;
			case "2.5.29.19": // BasicConstraints
				this.parsedValue = new BasicConstraints({ schema: asn1.result });
				break;
			case "2.5.29.20": // CRLNumber
			case "2.5.29.27": // BaseCRLNumber (delta CRL indicator)
				this.parsedValue = asn1.result; // Should be just a simple INTEGER
				break;
			case "2.5.29.21": // CRLReason
				this.parsedValue = asn1.result; // Should be just a simple ENUMERATED
				break;
			case "2.5.29.24": // InvalidityDate
				this.parsedValue = asn1.result; // Should be just a simple GeneralizedTime
				break;
			case "2.5.29.28": // IssuingDistributionPoint
				this.parsedValue = new IssuingDistributionPoint({ schema: asn1.result });
				break;
			case "2.5.29.29": // CertificateIssuer
				this.parsedValue = new GeneralNames({ schema: asn1.result }); // Should be just a simple
				break;
			case "2.5.29.30": // NameConstraints
				this.parsedValue = new NameConstraints({ schema: asn1.result });
				break;
			case "2.5.29.31": // CRLDistributionPoints
			case "2.5.29.46": // FreshestCRL
				this.parsedValue = new CRLDistributionPoints({ schema: asn1.result });
				break;
			case "2.5.29.32": // CertificatePolicies
				this.parsedValue = new CertificatePolicies({ schema: asn1.result });
				break;
			case "2.5.29.33": // PolicyMappings
				this.parsedValue = new PolicyMappings({ schema: asn1.result });
				break;
			case "2.5.29.35": // AuthorityKeyIdentifier
				this.parsedValue = new AuthorityKeyIdentifier({ schema: asn1.result });
				break;
			case "2.5.29.36": // PolicyConstraints
				this.parsedValue = new PolicyConstraints({ schema: asn1.result });
				break;
			case "2.5.29.37": // ExtKeyUsage
				this.parsedValue = new ExtKeyUsage({ schema: asn1.result });
				break;
			case "2.5.29.54": // InhibitAnyPolicy
				this.parsedValue = asn1.result; // Should be just a simple INTEGER
				break;
			case "1.3.6.1.5.5.7.1.1": // AuthorityInfoAccess
			case "1.3.6.1.5.5.7.1.11": // SubjectInfoAccess
				this.parsedValue = new InfoAccess({ schema: asn1.result });
				break;
			default:
		}
		//endregion
		//endregion
	}
	//**********************************************************************************
	/**
	 * Convert current object to asn1js object and set correct values
	 * @returns {Object} asn1js object
	 */
	toSchema()
	{
		//region Create array for output sequence
		const outputArray = [];
 
		outputArray.push(new asn1js.ObjectIdentifier({ value: this.extnID }));
 
		if(this.critical !== Extension.defaultValues("critical"))
			outputArray.push(new asn1js.Boolean({ value: this.critical }));
 
		outputArray.push(this.extnValue);
		//endregion
 
		//region Construct and return new ASN.1 schema for this object
		return (new asn1js.Sequence({
			value: outputArray
		}));
		//endregion
	}
	//**********************************************************************************
	/**
	 * Convertion for the class to JSON object
	 * @returns {Object}
	 */
	toJSON()
	{
		const object = {
			extnID: this.extnID,
			extnValue: this.extnValue.toJSON()
		};
 
		if(this.critical !== Extension.defaultValues("critical"))
			object.critical = this.critical;
 
		if("parsedValue" in this)
			object.parsedValue = this.parsedValue.toJSON();
 
		return object;
	}
	//**********************************************************************************
}
//**************************************************************************************