1 /**
  2  *  ---------
  3  * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
  4  * |#       #|  
  5  * |#       #|  Copyright (c) 1999-2010 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 Connector implementing a web service interface to a SPOC for the
 25  * distribution of card verifiable certificates used for terminal authentication as defined in CSN 36 9791
 26  */
 27 
 28 
 29 
 30 /**
 31  * Creates a web service connector to access services of a SPOC as defined in CSN 36 9791
 32  *
 33  * @class Class implementing a SPOC web service connector
 34  * @constructor
 35  * @param {String} url the web service endpoint
 36  */
 37 function SPOCConnection(url) {
 38 	this.url = url;
 39 	this.soapcon = new SOAPConnection(SOAPConnection.SOAP11);
 40 	this.verbose = true;
 41 	this.lastReturnCode = null;
 42 }
 43 
 44 
 45 
 46 /**
 47  * Get the last return code
 48  *
 49  * @returns the last return code received or null if none defined
 50  * @type String
 51  */
 52 SPOCConnection.prototype.getLastReturnCode = function() {
 53 	return this.lastReturnCode;
 54 }
 55 
 56 
 57 
 58 /**
 59  * Close the connector and release allocated resources
 60  */
 61 SPOCConnection.prototype.close = function() {
 62 	this.soapcon.close();
 63 }
 64 
 65 
 66 
 67 /**
 68  * Obtain a list of certificates from the SPOC
 69  *
 70  * @param {String} callerID two letter country code of the calling CVCA
 71  * @param {String} messageID unique message id generated by the caller
 72  * @returns a lists of card verifiable certificates from the SPOC or null in case of error
 73  * @type ByteString[]
 74  */
 75 SPOCConnection.prototype.getCACertificates = function(callerID, messageID) {
 76 
 77 	this.lastReturnCode = null;
 78 
 79 	var ns = new Namespace("http://namespaces.unmz.cz/csn369791");
 80 
 81 	var request =
 82 		<csn:GetCACertificatesRequest xmlns:csn={ns}>
 83 			<csn:callerID>{callerID}</csn:callerID>
 84 			<csn:messageID>{messageID}</csn:messageID>
 85 		</csn:GetCACertificatesRequest>
 86 
 87 	if (this.verbose) {
 88 		GPSystem.trace(request.toXMLString());
 89 	}
 90 
 91 	this.request = request;
 92 
 93 	try	 {
 94 		var response = this.soapcon.call(this.url, request);
 95 		if (this.verbose) {
 96 			GPSystem.trace(response.toXMLString());
 97 		}
 98 	}
 99 	catch(e) {
100 		GPSystem.trace("SOAP call to " + this.url + " failed : " + e);
101 		throw new GPError("SPOCConnection", GPError.DEVICE_ERROR, 0, "getCACertificates failed with : " + e);
102 	}
103 	
104 	this.response = response;
105 	
106 	var certlist = [];
107 
108 	this.lastReturnCode = response.ns::result.toString();
109 
110 	if (this.lastReturnCode == "ok_cert_available") {
111 		for each (var c in response.ns::certificateSequence.ns::certificate) {
112 			var cvc = new ByteString(c, BASE64);
113 			certlist.push(cvc);
114 			if (this.verbose) {
115 				GPSystem.trace(cvc);
116 			}
117 		}
118 	} else {
119 		return null;
120 	}
121 
122 	return certlist;
123 }
124 
125 
126 
127 /**
128  * Request a certificate from the SPOC using a web service
129  *
130  * @param {String} callerID two letter country code of the calling CVCA
131  * @param {String} messageID unique message id generated by the caller
132  * @param {ByteString} certreq the certificate request
133  * @returns the new certificates
134  * @type ByteString[]
135  */
136 SPOCConnection.prototype.requestCertificate = function(certreq, callerID, messageID) {
137 
138 	var soapConnection = new SOAPConnection();
139 
140 	var ns = new Namespace("http://namespaces.unmz.cz/csn369791");
141 
142 	var request =
143 		<csn:RequestCertificateRequest xmlns:csn={ns}>
144 			<csn:callerID>{callerID}</csn:callerID>
145 			<csn:messageID>{messageID}</csn:messageID>
146 			<csn:certificateRequest>{certreq.toString(BASE64)}</csn:certificateRequest>
147 		</csn:RequestCertificateRequest>
148 
149 	if (this.verbose) {
150 		GPSystem.trace(request.toXMLString());
151 	}
152 
153 	this.request = request;
154 
155 	try	{
156 		var response = this.soapcon.call(this.url, request);
157 		if (this.verbose) {
158 			GPSystem.trace(response.toXMLString());
159 		}
160 	}
161 	catch(e) {
162 		GPSystem.trace("SOAP call to " + this.url + " failed : " + e);
163 		throw new GPError("SPOCConnection", GPError.DEVICE_ERROR, 0, "RequestCertificate failed with : " + e);
164 	}
165 	
166 	this.response = response;
167 
168 	var certlist = [];
169 
170 	this.lastReturnCode = response.ns::result.toString();
171 	
172 	if (this.lastReturnCode == "ok_cert_available") {
173 		for each (var c in response.ns::certificateSequence.ns::certificate) {
174 			var cvc = new ByteString(c, BASE64);
175 			certlist.push(cvc);
176 			if (this.verbose) {
177 				GPSystem.trace(cvc);
178 			}
179 		}
180 	} else {
181 		return null;
182 	}
183 
184 	return certlist;
185 }
186 
187 
188 
189 /**
190  * Send a certificate to the SPOC
191  *
192  * @param {ByteString[]} certificates the list of certificates to post or null
193  * @param {String} callerID two letter country code of the calling CVCA
194  * @param {String} messageID unique message id generated by the caller
195  * @param {String} statusInfo the status info provided by the sender
196  * @type String
197  * @return the returnCode
198  */
199 SPOCConnection.prototype.sendCertificates = function(certificates, callerID, messageID, statusInfo) {
200 
201 	var soapConnection = new SOAPConnection();
202 
203 	var ns = new Namespace("http://namespaces.unmz.cz/csn369791");
204 
205 	var request =
206 			<csn:SendCertificatesRequest xmlns:csn={ns}>
207 				<csn:callerID>{callerID}</csn:callerID>
208 				<!--Optional:-->
209 				<csn:messageID>{messageID}</csn:messageID>
210 				<!--Optional:-->
211 				<csn:certificateSequence>
212 				</csn:certificateSequence>
213 				<csn:statusInfo>{statusInfo}</csn:statusInfo>
214 			</csn:SendCertificatesRequest>
215 
216 	var list = request.ns::certificateSequence;
217 
218 	if (certificates) {
219 		for (var i = 0; i < certificates.length; i++) {
220 			var cvc = certificates[i];
221 			list.ns::certificate += <ns:certificate xmlns:ns={ns}>{cvc.toString(BASE64)}</ns:certificate>
222 		}
223 	}
224 
225 	if (this.verbose) {
226 		GPSystem.trace(request.toXMLString());
227 	}
228 
229 	this.request = request;
230 
231 	try	{
232 		var response = this.soapcon.call(this.url, request);
233 		if (this.verbose) {
234 			GPSystem.trace(response.toXMLString());
235 		}
236 	}
237 	catch(e) {
238 		GPSystem.trace("SOAP call to " + this.url + " failed : " + e);
239 		throw new GPError("SPOCConnection", GPError.DEVICE_ERROR, 0, "SendCertificates failed with : " + e);
240 	}
241 
242 	this.response = response;
243 
244 	this.lastReturnCode = response.ns::result.toString();
245 	
246 	return this.lastReturnCode;
247 }
248 
249 
250 
251 /**
252  * Obtain a list of certificates from the SPOC
253  *
254  * @param {String} callerID two letter country code of the calling CVCA
255  * @param {String} messageID unique message id generated by the caller
256  * @param {String} subject the subject of the message
257  * @param {String} body the body of the message
258  */
259 SPOCConnection.prototype.generalMessage = function(callerID, messageID, subject, body) {
260 
261 	this.lastReturnCode = null;
262 
263 	var ns = new Namespace("http://namespaces.unmz.cz/csn369791");
264 
265 	var request =
266 		<csn:GeneralMessageRequest xmlns:csn={ns}>
267 			<csn:callerID>{callerID}</csn:callerID>
268 			<csn:messageID>{messageID}</csn:messageID>
269 			<csn:subject>{subject}</csn:subject>
270 			<csn:body>{body}</csn:body>
271 		</csn:GeneralMessageRequest>
272 
273 	if (this.verbose) {
274 		GPSystem.trace(request.toXMLString());
275 	}
276 
277 	this.request = request;
278 
279 	try	 {
280 		var response = this.soapcon.call(this.url, request);
281 		if (this.verbose) {
282 			GPSystem.trace(response.toXMLString());
283 		}
284 	}
285 	catch(e) {
286 		GPSystem.trace("SOAP call to " + this.url + " failed : " + e);
287 		throw new GPError("SPOCConnection", GPError.DEVICE_ERROR, 0, "generalMessage failed with : " + e);
288 	}
289 	
290 	this.response = response;
291 	
292 	this.lastReturnCode = response.ns::result.toString();
293 }
294 
295 
296 
297 /**
298  * Convert a list of certificates in binary format to a list of CVC objects
299  *
300  * @param {ByteString[]} certlist the list of certificates
301  * @type CVC[]
302  * @return the list of certificate objects
303  */
304 SPOCConnection.toCVCList = function(certlist) {
305 	var certs = [];
306 	for each (var cvcbin in certlist) {
307 		certs.push(new CVC(cvcbin));
308 	}
309 	return certs;
310 }
311 
312 
313 
314 /**
315  * Convert a list of certificate objects into a list of certificates in binary format
316  *
317  * @param {CVC[]} certlist the list of certificate objects
318  * @type ByteString[]
319  * @return the list of certificates
320  */
321 SPOCConnection.fromCVCList = function(certlist) {
322 	var certs = [];
323 	for each (var cvc in certlist) {
324 		certs.push(cvc.getBytes());
325 	}
326 	return certs;
327 }
328 
329 
330 
331 SPOCConnection.test = function() {
332 	var c = new SPOCConnection("http://localhost:8080/se/spoc");
333 	c.verbose = true;
334 	var certlist = c.getCACertificates("UT", "4711