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 function PublicKeyReference() {
 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 
 64 
 65 /**
 66  * Returns the 2 character country code
 67  *
 68  * @return the country code
 69  * @type String
 70  */
 71 PublicKeyReference.prototype.getCountryCode = function() {
 72 	return this.bin.bytes(0, 2).toString(ASCII);
 73 }
 74 
 75 
 76 
 77 /**
 78  * Returns the variable length holder mnemonic
 79  *
 80  * @return the holder mnemonic
 81  * @type String
 82  */
 83 PublicKeyReference.prototype.getMnemonic = function() {
 84 	return this.bin.bytes(2, this.bin.length - 7).toString(ASCII);
 85 }
 86 
 87 
 88 
 89 /**
 90  * Returns the 5 character sequence number
 91  *
 92  * @return the sequence number
 93  * @type String
 94  */
 95 PublicKeyReference.prototype.getSequenceNo = function() {
 96 	return this.bin.bytes(this.bin.length - 5, 5).toString(ASCII);
 97 }
 98 
 99 
100 
101 /**
102  * Returns the certificate holder name, which is the concatenation of the country code and the
103  * holder mnemonic.
104  *
105  * @return the holder name
106  * @type String
107  */
108 PublicKeyReference.prototype.getHolder = function() {
109 	return this.getCountryCode() + this.getMnemonic();
110 }
111 
112 
113 
114 /**
115  * Returns the binary encoded public key reference
116  *
117  * @return the public key reference
118  * @type ByteString
119  */
120 PublicKeyReference.prototype.getBytes = function() {
121 	return this.bin;
122 }
123 
124 
125 
126 /**
127  * Returns the string representation of the public key reference
128  *
129  * @return the public key reference
130  * @type String
131  */
132 PublicKeyReference.prototype.toString = function() {
133 	return this.bin.toString(ASCII);
134 }
135 
136 
137 
138 /**
139  * Returns true if other public key reference equals this public key reference
140  *
141  * @return true if equals
142  * @type boolean
143  */
144 PublicKeyReference.prototype.equals = function(other) {
145 	return this.bin.equals(other.bin);
146 }
147 
148 
149 
150 /**
151  * Test function
152  */
153 PublicKeyReference.test = function() {
154 	var p = new PublicKeyReference(new ByteString("UTABCDF0000", ASCII));
155 	assert(p.getCountryCode() == "UT");
156 	assert(p.getMnemonic() == "ABCD");
157 	assert(p.getSequenceNo() == "F0000");
158 	assert(p.getHolder() == "UTABCD");
159 	
160 	var p = new PublicKeyReference("UT", "ABCD", "F0000");
161 	assert(p.getCountryCode() == "UT");
162 	assert(p.getMnemonic() == "ABCD");
163 	assert(p.getSequenceNo() == "F0000");
164 	assert(p.getHolder() == "UTABCD");
165 	
166 	var p = new PublicKeyReference("UTABCDF0000");
167 	assert(p.getCountryCode() == "UT");
168 	assert(p.getMnemonic() == "ABCD");
169 	assert(p.getSequenceNo() == "F0000");
170 	assert(p.getHolder() == "UTABCD");
171 	
172 	assert(p.getBytes().toString(ASCII) == "UTABCDF0000");
173 }
174 
175