1 package iaik.pkcs.pkcs11.parameters;
2 
3 import iaik.pkcs.pkcs11.TokenRuntimeException;
4 import iaik.pkcs.pkcs11.wrapper.CK_SSL3_RANDOM_DATA;
5 import iaik.pkcs.pkcs11.wrapper.Constants;
6 import iaik.pkcs.pkcs11.wrapper.Functions;
7 
8 /**
9  * This class encapsulates parameters for the Mechanism.SSL3_MASTER_KEY_DERIVE
10  * and Mechanism.SSL3_KEY_AND_MAC_DERIVE mechanisms.
11  *
12  * @author Karl Scheibelhofer
13  * @version 1.0
14  * @invariants (clientRandom_ <> null)
15  *             and (serverRandom_ <> null)
16  */
17 public class SSL3RandomDataParameters implements Parameters {
18 
19 	/**
20 	 * The client's random data.
21 	 */
22 	protected byte[] clientRandom_;
23 
24 	/**
25 	 * The server's random data.
26 	 */
27 	protected byte[] serverRandom_;
28 
29 	/**
30 	 * Create a new SSL3RandomDataParameters object with the given
31 	 * cleint and server random.
32 	 *
33 	 * @param clientRandom The client's random data.
34 	 * @param serverRandom The server's random data.
35 	 * @preconditions (clientRandom <> null)
36 	 *                and (serverRandom <> null)
37 	 * @postconditions
38 	 */
SSL3RandomDataParameters(byte[] clientRandom, byte[] serverRandom)39 	public SSL3RandomDataParameters(byte[] clientRandom, byte[] serverRandom) {
40 		if (clientRandom == null) {
41 			throw new NullPointerException("Argument \"clientRandom\" must not be null.");
42 		}
43 		if (serverRandom == null) {
44 			throw new NullPointerException("Argument \"serverRandom\" must not be null.");
45 		}
46 		clientRandom_ = clientRandom;
47 		serverRandom_ = serverRandom;
48 	}
49 
50 	/**
51 	 * Create a (deep) clone of this object.
52 	 *
53 	 * @return A clone of this object.
54 	 * @preconditions
55 	 * @postconditions (result <> null)
56 	 *                 and (result instanceof SSL3RandomDataParameters)
57 	 *                 and (result.equals(this))
58 	 */
clone()59 	public java.lang.Object clone() {
60 		SSL3RandomDataParameters clone;
61 
62 		try {
63 			clone = (SSL3RandomDataParameters) super.clone();
64 
65 			clone.clientRandom_ = (byte[]) this.clientRandom_.clone();
66 			clone.serverRandom_ = (byte[]) this.serverRandom_.clone();
67 		} catch (CloneNotSupportedException ex) {
68 			// this must not happen, because this class is cloneable
69 			throw new TokenRuntimeException("An unexpected clone exception occurred.", ex);
70 		}
71 
72 		return clone;
73 	}
74 
75 	/**
76 	 * Get this parameters object as a CK_SSL3_RANDOM_DATA object.
77 	 *
78 	 * @return This object as a CK_SSL3_RANDOM_DATA object.
79 	 * @preconditions
80 	 * @postconditions (result <> null)
81 	 */
getPKCS11ParamsObject()82 	public Object getPKCS11ParamsObject() {
83 		CK_SSL3_RANDOM_DATA params = new CK_SSL3_RANDOM_DATA();
84 
85 		params.pClientRandom = clientRandom_;
86 		params.pServerRandom = serverRandom_;
87 
88 		return params;
89 	}
90 
91 	/**
92 	 * Get the client's random data.
93 	 *
94 	 * @return The client's random data.
95 	 * @preconditions
96 	 * @postconditions (result <> null)
97 	 */
getClientRandom()98 	public byte[] getClientRandom() {
99 		return clientRandom_;
100 	}
101 
102 	/**
103 	 * Get the server's random data.
104 	 *
105 	 * @return The server's random data.
106 	 * @preconditions
107 	 * @postconditions (result <> null)
108 	 */
getServerRandom()109 	public byte[] getServerRandom() {
110 		return serverRandom_;
111 	}
112 
113 	/**
114 	 * Set the client's random data.
115 	 *
116 	 * @param clientRandom The client's random data.
117 	 * @preconditions (clientRandom <> null)
118 	 * @postconditions
119 	 */
setClientRandom(byte[] clientRandom)120 	public void setClientRandom(byte[] clientRandom) {
121 		if (clientRandom == null) {
122 			throw new NullPointerException("Argument \"clientRandom\" must not be null.");
123 		}
124 		clientRandom_ = clientRandom;
125 	}
126 
127 	/**
128 	 * Set the server's random data.
129 	 *
130 	 * @param serverRandom The server's random data.
131 	 * @preconditions (serverRandom <> null)
132 	 * @postconditions
133 	 */
setServerRandom(byte[] serverRandom)134 	public void setServerRandom(byte[] serverRandom) {
135 		if (serverRandom == null) {
136 			throw new NullPointerException("Argument \"serverRandom\" must not be null.");
137 		}
138 		serverRandom_ = serverRandom;
139 	}
140 
141 	/**
142 	 * Returns the string representation of this object. Do not parse data from
143 	 * this string, it is for debugging only.
144 	 *
145 	 * @return A string representation of this object.
146 	 */
toString()147 	public String toString() {
148 		StringBuffer buffer = new StringBuffer();
149 
150 		buffer.append(Constants.INDENT);
151 		buffer.append("Client Random (hex): ");
152 		buffer.append(Functions.toHexString(clientRandom_));
153 		buffer.append(Constants.NEWLINE);
154 
155 		buffer.append(Constants.INDENT);
156 		buffer.append("Server Random (hex): ");
157 		buffer.append(Functions.toHexString(serverRandom_));
158 		// buffer.append(Constants.NEWLINE);
159 
160 		return buffer.toString();
161 	}
162 
163 	/**
164 	 * Compares all member variables of this object with the other object.
165 	 * Returns only true, if all are equal in both objects.
166 	 *
167 	 * @param otherObject The other object to compare to.
168 	 * @return True, if other is an instance of this class and all member
169 	 *         variables of both objects are equal. False, otherwise.
170 	 * @preconditions
171 	 * @postconditions
172 	 */
equals(java.lang.Object otherObject)173 	public boolean equals(java.lang.Object otherObject) {
174 		boolean equal = false;
175 
176 		if (otherObject instanceof SSL3RandomDataParameters) {
177 			SSL3RandomDataParameters other = (SSL3RandomDataParameters) otherObject;
178 			equal = (this == other)
179 			    || (Functions.equals(this.clientRandom_, other.clientRandom_) && Functions
180 			        .equals(this.serverRandom_, other.serverRandom_));
181 		}
182 
183 		return equal;
184 	}
185 
186 	/**
187 	 * The overriding of this method should ensure that the objects of this class
188 	 * work correctly in a hashtable.
189 	 *
190 	 * @return The hash code of this object.
191 	 * @preconditions
192 	 * @postconditions
193 	 */
hashCode()194 	public int hashCode() {
195 		return Functions.hashCode(clientRandom_) ^ Functions.hashCode(serverRandom_);
196 	}
197 
198 }
199