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 Public Key Reference, the value stored in CAR and CHR of CV-Certificates
 25  */
 26  
 27 
 28 
 29 
 30 /**
 31  * <p>Create a public key reference (CAR/CHR) from binary representation or individual fields.</p>
 32  *
 33  * <p>Use one of the following signatures:</p>
 34  *
 35  * <ul>
 36  *  <li>PublicKeyReference(ByteString value) - binary public key reference</li>
 37  *  <li>PublicKeyReference(String value) - string encoded public key reference</li>
 38  *  <li>PublicKeyReference(String countryCode, String holderMnemonic, String sequenceNumber) - string encoded public key reference</li>
 39  * </ul>
 40  * <p>@see PublicKeyReference.test() for an example.</P>
 41  *
 42  * @class <p>A class that implements a public key reference to be used as CAR and CHR in
 43  *        card verifiable certificates (CVC).</p>
 44  * @constructor
 45  */
 46 PublicKeyReference = function() {
 47 	if (arguments.length > 0) {
 48 		if (arguments.length == 1) {
 49 			if (typeof(arguments[0]) == "string") {
 50 				this.bin = new ByteString(arguments[0], ASCII);
 51 			} else {
 52 				this.bin = arguments[0];
 53 			}
 54 		} else {
 55 			var cc = arguments[0];
 56 			var mn = arguments[1];
 57 			var sq = arguments[2];
 58 			this.bin = new ByteString(cc + mn + sq, ASCII);
 59 		}
 60 	}
 61 }
 62 
 63 exports.PublicKeyReference = PublicKeyReference;
 64 
 65 
 66 
 67 /**
 68  * Returns the 2 character country code
 69  *
 70  * @return the country code
 71  * @type String
 72  */
 73 PublicKeyReference.prototype.getCountryCode = function() {
 74 	return this.bin.bytes(0, 2).toString(ASCII);
 75 }
 76 
 77 
 78 
 79 /**
 80  * Returns the variable length holder mnemonic
 81  *
 82  * @return the holder mnemonic
 83  * @type String
 84  */
 85 PublicKeyReference.prototype.getMnemonic = function() {
 86 	return this.bin.bytes(2, this.bin.length - 7).toString(ASCII);
 87 }
 88 
 89 
 90 
 91 /**
 92  * Returns the 5 character sequence number
 93  *
 94  * @return the sequence number
 95  * @type String
 96  */
 97 PublicKeyReference.prototype.getSequenceNo = function() {
 98 	return this.bin.bytes(this.bin.length - 5, 5).toString(ASCII);
 99 }
100 
101 
102 
103 /**
104  * Returns the certificate holder name, which is the concatenation of the country code and the
105  * holder mnemonic.
106  *
107  * @return the holder name
108  * @type String
109  */
110 PublicKeyReference.prototype.getHolder = function() {
111 	return this.getCountryCode() + this.getMnemonic();
112 }
113 
114 
115 
116 /**
117  * Returns the binary encoded public key reference
118  *
119  * @return the public key reference
120  * @type ByteString
121  */
122 PublicKeyReference.prototype.getBytes = function() {
123 	return this.bin;
124 }
125 
126 
127 
128 /**
129  * Returns the string representation of the public key reference
130  *
131  * @return the public key reference
132  * @type String
133  */
134 PublicKeyReference.prototype.toString = function() {
135 	return this.bin.toString(ASCII);
136 }
137 
138 
139 
140 /**
141  * Returns true if other public key reference equals this public key reference
142  *
143  * @return true if equals
144  * @type boolean
145  */
146 PublicKeyReference.prototype.equals = function(other) {
147 	return this.bin.equals(other.bin);
148 }
149 
150 
151 
152 /**
153  * Test function
154  */
155 PublicKeyReference.test = function() {
156 	var p = new PublicKeyReference(new ByteString("UTABCDF0000", ASCII));
157 	assert(p.getCountryCode() == "UT");
158 	assert(p.getMnemonic() == "ABCD");
159 	assert(p.getSequenceNo() == "F0000");
160 	assert(p.getHolder() == "UTABCD");
161 	
162 	var p = new PublicKeyReference("UT", "ABCD", "F0000");
163 	assert(p.getCountryCode() == "UT");
164 	assert(p.getMnemonic() == "ABCD");
165 	assert(p.getSequenceNo() == "F0000");
166 	assert(p.getHolder() == "UTABCD");
167 	
168 	var p = new PublicKeyReference("UTABCDF0000");
169 	assert(p.getCountryCode() == "UT");
170 	assert(p.getMnemonic() == "ABCD");
171 	assert(p.getSequenceNo() == "F0000");
172 	assert(p.getHolder() == "UTABCD");
173 	
174 	assert(p.getBytes().toString(ASCII) == "UTABCDF0000");
175 }
176 
177