001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.security.srp;
023:
024: import java.io.Serializable;
025: import java.util.Arrays;
026:
027: import org.jboss.security.Util;
028:
029: /** The RFC2945 algorithm session parameters that the client and server
030: agree to use. In addition to the base RFC2945 parameters, one can choose an
031: alternate hash algorithm for the private session key.
032:
033: @author Scott.Stark@jboss.org
034: @version $Revision: 57210 $
035: */
036: public class SRPParameters implements Cloneable, Serializable {
037: /** The serial version ID.
038: * @since 1.2.4.1
039: */
040: private static final long serialVersionUID = 6438772808805276693L;
041:
042: /** The algorithm safe-prime modulus */
043: public final byte[] N;
044: /** The algorithm primitive generator */
045: public final byte[] g;
046: /** The random password salt originally used to verify the password */
047: public final byte[] s;
048: /** The algorithm to hash the session key to produce K. To be consistent
049: with the RFC2945 description this must be SHA_Interleave as implemented
050: by the JBossSX security provider. For compatibility with earlier JBossSX
051: SRP releases the algorithm must be SHA_ReverseInterleave. This name is
052: passed to java.security.MessageDigest.getInstance(). */
053: public final String hashAlgorithm;
054: /** The algorithm to use for any encryption of data.
055: */
056: public final String cipherAlgorithm;
057: /** The cipher intialization vector bytes
058: */
059: public byte[] cipherIV;
060:
061: /** Creates a new instance of SRPParameters */
062: public SRPParameters(byte[] N, byte[] g, byte[] s) {
063: this (N, g, s, "SHA_Interleave", null);
064: }
065:
066: public SRPParameters(byte[] N, byte[] g, byte[] s,
067: String hashAlgorithm) {
068: this (N, g, s, hashAlgorithm, null);
069: }
070:
071: public SRPParameters(byte[] N, byte[] g, byte[] s,
072: String hashAlgorithm, String cipherAlgorithm) {
073: this (N, g, s, hashAlgorithm, cipherAlgorithm, null);
074: }
075:
076: public SRPParameters(byte[] N, byte[] g, byte[] s,
077: String hashAlgorithm, String cipherAlgorithm,
078: byte[] cipherIV) {
079: this .N = N;
080: this .g = g;
081: this .s = s;
082: if (hashAlgorithm == null)
083: hashAlgorithm = "SHA_Interleave";
084: this .hashAlgorithm = hashAlgorithm;
085: this .cipherAlgorithm = cipherAlgorithm;
086: this .cipherIV = cipherIV;
087: }
088:
089: public Object clone() {
090: Object clone = null;
091: try {
092: clone = super .clone();
093: } catch (CloneNotSupportedException e) {
094: }
095: return clone;
096: }
097:
098: public int hashCode() {
099: int hashCode = hashAlgorithm.hashCode();
100: for (int i = 0; i < N.length; i++)
101: hashCode += N[i];
102: for (int i = 0; i < g.length; i++)
103: hashCode += g[i];
104: for (int i = 0; i < s.length; i++)
105: hashCode += s[i];
106: return hashCode;
107: }
108:
109: public boolean equals(Object obj) {
110: boolean equals = false;
111: if (obj instanceof SRPParameters) {
112: SRPParameters p = (SRPParameters) obj;
113: equals = hashAlgorithm.equals(p.hashAlgorithm);
114: if (equals == true)
115: equals = Arrays.equals(N, p.N);
116: if (equals == true)
117: equals = Arrays.equals(g, p.g);
118: if (equals == true)
119: equals = Arrays.equals(s, p.s);
120: }
121: return equals;
122: }
123:
124: public String toString() {
125: StringBuffer tmp = new StringBuffer(super .toString());
126: tmp.append('{');
127: tmp.append("N: ");
128: tmp.append(Util.encodeBase64(N));
129: tmp.append("|g: ");
130: tmp.append(Util.encodeBase64(g));
131: tmp.append("|s: ");
132: tmp.append(Util.encodeBase64(s));
133: tmp.append("|hashAlgorithm: ");
134: tmp.append(hashAlgorithm);
135: tmp.append("|cipherAlgorithm: ");
136: tmp.append(cipherAlgorithm);
137: tmp.append("|cipherIV: ");
138: tmp.append(cipherIV);
139: tmp.append('}');
140: return tmp.toString();
141: }
142: }
|