1 /**
  2  *  ---------
  3  * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
  4  * |#       #|  
  5  * |#       #|  Copyright (c) 1999-2009 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 
 40 
 41 /**
 42  * Adds CRT elements to a named template.
 43  *
 44  * @param {String} tname the CRT name one of AT, KAT, HT, CCT, DST or CT
 45  * @param {ASN1} tlv the tlv object containing the CRT elements
 46  **/
 47 SecurityEnvironment.prototype.addElements = function(tname, tlv) {
 48 	var t = this.t[tname];
 49 	if (t) {
 50 		for (var i = 0; i < tlv.elements; i++) {
 51 			var o = tlv.get(i);
 52 			SecurityEnvironment.decorateCRT(o);
 53 			var j = 0;
 54 			while(j < t.elements) {
 55 				if (t.get(j).tag == o.tag) {
 56 					t.remove(j);
 57 				} else {
 58 					j++;
 59 				}
 60 			}
 61 			t.add(o);
 62 		}
 63 	} else {
 64 		for (var i = 0; i < tlv.elements; i++) {
 65 			var o = tlv.get(i);
 66 			SecurityEnvironment.decorateCRT(o);
 67 		}
 68 		this.t[tname] = tlv;
 69 	}
 70 }
 71 
 72 
 73 
 74 /**
 75  * Adds a CRT identified by it's tag
 76  *
 77  * @param {ASN1} tlv the tlv object
 78  */
 79 SecurityEnvironment.prototype.add = function(tlv) {
 80 	switch(tlv.tag) {
 81 	case 0xA4:
 82 		tlv.setName("AT");
 83 		break;
 84 	case 0xA6:
 85 		tlv.setName("KAT");
 86 		break;
 87 	case 0xAA:
 88 		tlv.setName("HT");
 89 		break;
 90 	case 0xB4:
 91 		tlv.setName("CCT");
 92 		break;
 93 	case 0xB6:
 94 		tlv.setName("DST");
 95 		break;
 96 	case 0xB8:
 97 		tlv.setName("CT");
 98 		break;
 99 	default:
100 		throw new GPError("SecurityEnvironment", GPError.INVALID_DATA, tlv.tag, "Invalid tag for CRT");
101 	}
102 	this.addElements(tlv.name, tlv);
103 }
104 
105 
106 
107 /**
108  * Return textual representation of security environment container
109  */
110 SecurityEnvironment.prototype.toString = function() {
111 	var str = "";
112 	
113 	if (this.t.AT) {
114 		str += "Authentication Template (AT)\n" + this.t.AT;
115 	}
116 	if (this.t.KAT) {
117 		str += "Key Agreement Template (KAT)\n" + this.t.KAT;
118 	}
119 	if (this.t.HT) {
120 		str += "Hash Template (HT)\n" + this.t.HT;
121 	}
122 	if (this.t.CCT) {
123 		str += "Cryptographic Checksum Template (CCT)\n" + this.t.CCT;
124 	}
125 	if (this.t.DST) {
126 		str += "Digital Signature Template (DST)\n" + this.t.DST;
127 	}
128 	if (this.t.CT) {
129 		str += "Confidentiality Template (CT)\n" + this.t.CT;
130 	}
131 	return str;	
132 }	
133 
134 
135 
136 /**
137  * Decorates a tlv object from the CRT
138  */
139 SecurityEnvironment.decorateCRT = function(asn1) {
140 	switch(asn1.tag) {
141 	case 0x80:
142 		asn1.setName("cryptographicMechanism 80");
143 		break;
144 	case 0x81:
145 		asn1.setName("fileIdentifierOrPath 81");
146 		break;
147 	case 0x82:
148 		asn1.setName("dFName 82");
149 		break;
150 	case 0x83:
151 		asn1.setName("secretOrPublicKeyReference 83");
152 		break;
153 	case 0x84:
154 		asn1.setName("sessionOrPrivateKeyReference 84");
155 		break;
156 	case 0x85:
157 		asn1.setName("nullBlock 85");
158 		break;
159 	case 0x86:
160 		asn1.setName("chainingBlock 86");
161 		break;
162 	case 0x87:
163 		asn1.setName("initialBlock 87");
164 		break;
165 	case 0x88:
166 		asn1.setName("previousChallenge 88");
167 		break;
168 	case 0x89:
169 		asn1.setName("proprietaryDataElementIndex 89");
170 		break;
171 	case 0x8A:
172 		asn1.setName("proprietaryDataElementIndex 8A");
173 		break;
174 	case 0x8B:
175 		asn1.setName("proprietaryDataElementIndex 8B");
176 		break;
177 	case 0x8C:
178 		asn1.setName("proprietaryDataElementIndex 8C");
179 		break;
180 	case 0x8D:
181 		asn1.setName("proprietaryDataElementIndex 8D");
182 		break;
183 	case 0x90:
184 		asn1.setName("cardHashCode 90");
185 		break;
186 	case 0x91:
187 		asn1.setName("ephemeralPublicKey 91");
188 		break;
189 	case 0x92:
190 		asn1.setName("cardTimeStamp 92");
191 		break;
192 	case 0x93:
193 		asn1.setName("dsiCounter 93");
194 		break;
195 	case 0x94:
196 		asn1.setName("challengeOrDerivationParameter 94");
197 		break;
198 	case 0x95:
199 		asn1.setName("usageQualifier 95");
200 		break;
201 	case 0x8E:
202 		asn1.setName("cryptographicContentReference 8E");
203 		break;
204 	case 0x67:
205 		asn1.setName("auxiliaryAuthenticatedData 67");
206 		break;
207 	case 0x67:
208 		asn1.setName("auxiliaryAuthenticatedData 67");
209 		break;
210 	case 0x7F4C:
211 		asn1.setName("certificateHolderAuthorisationTemplate 7F4C");
212 		break;
213 	}
214 }
215