Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / key / srp6 / SRP6TLSServer.java
blob23e4440773b388dba74a3445e8f48010c134e9b3
1 /* SRP6TLSServer.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.key.srp6;
41 import gnu.java.security.Registry;
42 import gnu.java.security.util.Util;
43 import gnu.javax.crypto.key.KeyAgreementException;
44 import gnu.javax.crypto.key.OutgoingMessage;
45 import gnu.javax.crypto.key.IncomingMessage;
46 import gnu.javax.crypto.sasl.srp.SRP;
47 import gnu.javax.crypto.sasl.srp.SRPAuthInfoProvider;
48 import gnu.javax.crypto.sasl.srp.SRPRegistry;
50 import java.io.IOException;
51 import java.math.BigInteger;
52 import java.security.KeyPair;
53 import java.security.SecureRandom;
54 import java.util.HashMap;
55 import java.util.Map;
57 /**
58 * <p>A variation of the SRP6 key agreement protocol, for the server-side as
59 * proposed in
60 * <a href="http://www.ietf.org/internet-drafts/draft-ietf-tls-srp-05.txt">Using
61 * SRP for TLS Authentication</a>. The only difference between it and the SASL
62 * variant is that the shared secret is the entity <code>S</code> and not
63 * <code>H(S)</code>.</p>
65 * @version $Revision: 1.1 $
67 public class SRP6TLSServer extends SRP6KeyAgreement
70 // Constants and variables
71 // -------------------------------------------------------------------------
73 /** The user's ephemeral key pair. */
74 private KeyPair hostKeyPair;
76 /** The SRP password database. */
77 private SRPAuthInfoProvider passwordDB;
79 // Constructor(s)
80 // -------------------------------------------------------------------------
82 // default 0-arguments constructor
84 // Class methods
85 // -------------------------------------------------------------------------
87 // Instance methods
88 // -------------------------------------------------------------------------
90 // implementation of abstract methods in base class ------------------------
92 protected void engineInit(final Map attributes) throws KeyAgreementException
94 rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS);
96 final String md = (String) attributes.get(HASH_FUNCTION);
97 if (md == null || "".equals(md.trim()))
99 throw new KeyAgreementException("missing hash function");
101 srp = SRP.instance(md);
103 passwordDB = (SRPAuthInfoProvider) attributes.get(HOST_PASSWORD_DB);
104 if (passwordDB == null)
106 throw new KeyAgreementException("missing SRP password database");
110 protected OutgoingMessage engineProcessMessage(final IncomingMessage in)
111 throws KeyAgreementException
113 switch (step)
115 case 0:
116 return sendParameters(in);
117 case 1:
118 return computeSharedSecret(in);
119 default:
120 throw new IllegalStateException("unexpected state");
124 protected void engineReset()
126 hostKeyPair = null;
127 super.engineReset();
130 // own methods -------------------------------------------------------------
132 private OutgoingMessage sendParameters(final IncomingMessage in)
133 throws KeyAgreementException
135 final String I = in.readString();
137 // get s and v for user identified by I
138 // ----------------------------------------------------------------------
139 final Map credentials;
142 final Map userID = new HashMap();
143 userID.put(Registry.SASL_USERNAME, I);
144 userID.put(SRPRegistry.MD_NAME_FIELD, srp.getAlgorithm());
145 credentials = passwordDB.lookup(userID);
147 catch (IOException x)
149 throw new KeyAgreementException("computeSharedSecret()", x);
152 final BigInteger s = new BigInteger(
154 Util.fromBase64((String) credentials.get(SRPRegistry.SALT_FIELD)));
155 final BigInteger v = new BigInteger(
157 Util.fromBase64((String) credentials.get(SRPRegistry.USER_VERIFIER_FIELD)));
159 final Map configuration;
162 final String mode = (String) credentials.get(SRPRegistry.CONFIG_NDX_FIELD);
163 configuration = passwordDB.getConfiguration(mode);
165 catch (IOException x)
167 throw new KeyAgreementException("computeSharedSecret()", x);
170 N = new BigInteger(
172 Util.fromBase64((String) configuration.get(SRPRegistry.SHARED_MODULUS)));
173 g = new BigInteger(
175 Util.fromBase64((String) configuration.get(SRPRegistry.FIELD_GENERATOR)));
176 // ----------------------------------------------------------------------
178 // generate an ephemeral keypair
179 final SRPKeyPairGenerator kpg = new SRPKeyPairGenerator();
180 final Map attributes = new HashMap();
181 if (rnd != null)
183 attributes.put(SRPKeyPairGenerator.SOURCE_OF_RANDOMNESS, rnd);
185 attributes.put(SRPKeyPairGenerator.SHARED_MODULUS, N);
186 attributes.put(SRPKeyPairGenerator.GENERATOR, g);
187 attributes.put(SRPKeyPairGenerator.USER_VERIFIER, v);
188 kpg.setup(attributes);
189 hostKeyPair = kpg.generate();
191 final BigInteger B = ((SRPPublicKey) hostKeyPair.getPublic()).getY();
193 final OutgoingMessage result = new OutgoingMessage();
194 result.writeMPI(N);
195 result.writeMPI(g);
196 result.writeMPI(s);
197 result.writeMPI(B);
199 return result;
202 protected OutgoingMessage computeSharedSecret(final IncomingMessage in)
203 throws KeyAgreementException
205 final BigInteger A = in.readMPI();
207 final BigInteger B = ((SRPPublicKey) hostKeyPair.getPublic()).getY();
208 final BigInteger u = uValue(A, B); // u = H(A | B)
210 // compute S = (Av^u) ^ b
211 final BigInteger b = ((SRPPrivateKey) hostKeyPair.getPrivate()).getX();
212 final BigInteger v = ((SRPPrivateKey) hostKeyPair.getPrivate()).getV();
213 final BigInteger S = A.multiply(v.modPow(u, N)).modPow(b, N);
215 K = S;
217 complete = true;
218 return null;