      1 /**
      2  *  ---------
      3  * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
      4  * |#       #|
      5  * |#       #|  Copyright (c) 1999-2018 CardContact Software & System Consulting
      6  * |'##> <##'|  Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
      7  *  ---------
      8  *
      9  *  This file is part of OpenSCDP.
     10  *
     11  *  OpenSCDP is free software; you can redistribute it and/or modify
     12  *  it under the terms of the GNU General Public License version 2 as
     13  *  published by the Free Software Foundation.
     14  *
     15  *  OpenSCDP is distributed in the hope that it will be useful,
     16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  *  GNU General Public License for more details.
     19  *
     20  *  You should have received a copy of the GNU General Public License
     21  *  along with OpenSCDP; if not, write to the Free Software
     22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     23  *
     24  * @fileoverview Security environment - a container for security related data elements
     25  */
     26
     27
     28
     29 /**
     30  * Creates a security environment container that collects cryptographic reference templates (CRT)
     31  *
     32  * @class Class implementing a security environment for cryptographic operations.
     33  * @constructor
     34  */
     35 function SecurityEnvironment() {
     36 	this.t = { AT:null, KAT: null, HT: null, CCT:null, DST:null, CT: null };
     37 }
     38
     39 exports.SecurityEnvironment = SecurityEnvironment;
     40
     41
     42
     43 /**
     44  * Adds CRT elements to a named template.
     45  *
     46  * @param {String} tname the CRT name one of AT, KAT, HT, CCT, DST or CT
     47  * @param {ASN1} tlv the tlv object containing the CRT elements
     48  **/
     49 SecurityEnvironment.prototype.addElements = function(tname, tlv) {
     50 	var t = this.t[tname];
     51 	if (t) {
     52 		for (var i = 0; i < tlv.elements; i++) {
     53 			var o = tlv.get(i);
     54 			SecurityEnvironment.decorateCRT(o);
     55 			var j = 0;
     56 			while(j < t.elements) {
     57 				if (t.get(j).tag == o.tag) {
     58 					t.remove(j);
     59 				} else {
     60 					j++;
     61 				}
     62 			}
     63 			t.add(o);
     64 		}
     65 	} else {
     66 		for (var i = 0; i < tlv.elements; i++) {
     67 			var o = tlv.get(i);
     68 			SecurityEnvironment.decorateCRT(o);
     69 		}
     70 		this.t[tname] = tlv;
     71 	}
     72 }
     73
     74
     75
     76 /**
     77  * Adds a CRT identified by it's tag
     78  *
     79  * @param {ASN1} tlv the tlv object
     80  */
     81 SecurityEnvironment.prototype.add = function(tlv) {
     82 	switch(tlv.tag) {
     83 	case 0xA4:
     84 		tlv.setName("AT");
     85 		break;
     86 	case 0xA6:
     87 		tlv.setName("KAT");
     88 		break;
     89 	case 0xAA:
     90 		tlv.setName("HT");
     91 		break;
     92 	case 0xB4:
     93 		tlv.setName("CCT");
     94 		break;
     95 	case 0xB6:
     96 		tlv.setName("DST");
     97 		break;
     98 	case 0xB8:
     99 		tlv.setName("CT");
    100 		break;
    101 	default:
    102 		throw new GPError("SecurityEnvironment", GPError.INVALID_DATA, tlv.tag, "Invalid tag for CRT");
    103 	}
    104 	this.addElements(tlv.name, tlv);
    105 }
    106
    107
    108
    109 /**
    110  * Return textual representation of security environment container
    111  */
    112 SecurityEnvironment.prototype.toString = function() {
    113 	var str = "";
    114
    115 	if (this.t.AT) {
    116 		str += "Authentication Template (AT)\n" + this.t.AT;
    117 	}
    118 	if (this.t.KAT) {
    119 		str += "Key Agreement Template (KAT)\n" + this.t.KAT;
    120 	}
    121 	if (this.t.HT) {
    122 		str += "Hash Template (HT)\n" + this.t.HT;
    123 	}
    124 	if (this.t.CCT) {
    125 		str += "Cryptographic Checksum Template (CCT)\n" + this.t.CCT;
    126 	}
    127 	if (this.t.DST) {
    128 		str += "Digital Signature Template (DST)\n" + this.t.DST;
    129 	}
    130 	if (this.t.CT) {
    131 		str += "Confidentiality Template (CT)\n" + this.t.CT;
    132 	}
    133 	return str;
    134 }
    135
    136
    137
    138 /**
    139  * Decorates a tlv object from the CRT
    140  */
    141 SecurityEnvironment.decorateCRT = function(asn1) {
    142 	switch(asn1.tag) {
    143 	case 0x80:
    144 		asn1.setName("cryptographicMechanism 80");
    145 		break;
    146 	case 0x81:
    147 		asn1.setName("fileIdentifierOrPath 81");
    148 		break;
    149 	case 0x82:
    150 		asn1.setName("dFName 82");
    151 		break;
    152 	case 0x83:
    153 		asn1.setName("secretOrPublicKeyReference 83");
    154 		break;
    155 	case 0x84:
    156 		asn1.setName("sessionOrPrivateKeyReference 84");
    157 		break;
    158 	case 0x85:
    159 		asn1.setName("nullBlock 85");
    160 		break;
    161 	case 0x86:
    162 		asn1.setName("chainingBlock 86");
    163 		break;
    164 	case 0x87:
    165 		asn1.setName("initialBlock 87");
    166 		break;
    167 	case 0x88:
    168 		asn1.setName("previousChallenge 88");
    169 		break;
    170 	case 0x89:
    171 		asn1.setName("proprietaryDataElementIndex 89");
    172 		break;
    173 	case 0x8A:
    174 		asn1.setName("proprietaryDataElementIndex 8A");
    175 		break;
    176 	case 0x8B:
    177 		asn1.setName("proprietaryDataElementIndex 8B");
    178 		break;
    179 	case 0x8C:
    180 		asn1.setName("proprietaryDataElementIndex 8C");
    181 		break;
    182 	case 0x8D:
    183 		asn1.setName("proprietaryDataElementIndex 8D");
    184 		break;
    185 	case 0x90:
    186 		asn1.setName("cardHashCode 90");
    187 		break;
    188 	case 0x91:
    189 		asn1.setName("ephemeralPublicKey 91");
    190 		break;
    191 	case 0x92:
    192 		asn1.setName("cardTimeStamp 92");
    193 		break;
    194 	case 0x93:
    195 		asn1.setName("dsiCounter 93");
    196 		break;
    197 	case 0x94:
    198 		asn1.setName("challengeOrDerivationParameter 94");
    199 		break;
    200 	case 0x95:
    201 		asn1.setName("usageQualifier 95");
    202 		break;
    203 	case 0x8E:
    204 		asn1.setName("cryptographicContentReference 8E");
    205 		break;
    206 	case 0x67:
    207 		asn1.setName("auxiliaryAuthenticatedData 67");
    208 		break;
    209 	case 0x67:
    210 		asn1.setName("auxiliaryAuthenticatedData 67");
    211 		break;
    212 	case 0x7F4C:
    213 		asn1.setName("certificateHolderAuthorisationTemplate 7F4C");
    214 		break;
    215 	}
    216 }
    217