1 /* SRPTrustManagerFactory.java -- trust manager for SRP.
2 Copyright (C) 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
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
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
.net
.ssl
.provider
;
41 import java
.io
.IOException
;
42 import java
.math
.BigInteger
;
44 import java
.security
.InvalidAlgorithmParameterException
;
45 import java
.security
.KeyPair
;
46 import java
.security
.KeyStore
;
47 import java
.util
.HashMap
;
49 import javax
.net
.ssl
.ManagerFactoryParameters
;
50 import javax
.net
.ssl
.TrustManager
;
51 import javax
.net
.ssl
.TrustManagerFactorySpi
;
53 import gnu
.java
.security
.key
.IKeyPairGenerator
;
54 import gnu
.javax
.crypto
.key
.srp6
.SRPKeyPairGenerator
;
55 import gnu
.javax
.crypto
.sasl
.srp
.PasswordFile
;
56 import gnu
.javax
.crypto
.sasl
.srp
.SRP
;
58 import gnu
.javax
.net
.ssl
.SRPManagerParameters
;
59 import gnu
.javax
.net
.ssl
.SRPTrustManager
;
62 * This is an implementation of a {@link javax.net.ssl.TrustManagerFactory}
63 * engine for the ``SRP'' algorithm. You must initialize instances of this
64 * algorithm with {@link SRPManagerParameters}.
66 public class SRPTrustManagerFactory
extends TrustManagerFactorySpi
70 // -------------------------------------------------------------------------
72 private Manager current
;
75 // -------------------------------------------------------------------------
77 public SRPTrustManagerFactory()
83 // -------------------------------------------------------------------------
85 protected TrustManager
[] engineGetTrustManagers()
88 throw new IllegalStateException("not initialized");
89 return new TrustManager
[] { current
};
92 protected void engineInit(KeyStore ks
)
94 throw new IllegalArgumentException("only accepts SRPManagerParameters");
97 protected void engineInit(ManagerFactoryParameters params
)
98 throws InvalidAlgorithmParameterException
104 String srpPasswd
= Util
.getSecurityProperty("jessie.srp.password.file");
105 if (srpPasswd
== null)
107 current
= new Manager(new PasswordFile());
110 String srpPasswd2
= Util
.getSecurityProperty("jessie.srp.password.file2");
111 if (srpPasswd2
== null)
112 srpPasswd2
= srpPasswd
+ "2";
113 String srpConfig
= Util
.getSecurityProperty("jessie.srp.config");
114 if (srpConfig
== null)
115 srpConfig
= srpPasswd
+ ".conf";
116 current
= new Manager(new PasswordFile(srpPasswd
, srpPasswd2
, srpConfig
));
119 catch (IOException ioe
)
121 throw new InvalidAlgorithmParameterException("default initialization failed: "
125 if (params
instanceof SRPManagerParameters
)
127 current
= new Manager(((SRPManagerParameters
) params
).getPasswordFile());
130 throw new InvalidAlgorithmParameterException();
134 // -------------------------------------------------------------------------
136 private class Manager
implements SRPTrustManager
140 // -----------------------------------------------------------------------
142 private final PasswordFile file
;
145 // -----------------------------------------------------------------------
147 Manager(PasswordFile file
)
153 // -----------------------------------------------------------------------
155 public boolean contains(String user
)
159 return file
.contains(user
);
161 catch (IOException ioe
) { }
165 public KeyPair
getKeyPair(String user
)
169 if (file
.contains(user
))
171 SRP srp
= SRP
.instance("SHA");
172 String
[] ent
= file
.lookup(user
, "SHA");
173 String
[] cnf
= file
.lookupConfig(ent
[2]);
175 v
= new BigInteger(1, gnu
.java
.security
.util
.Util
.fromBase64(ent
[0]));
176 N
= new BigInteger(1, gnu
.java
.security
.util
.Util
.fromBase64(cnf
[0]));
177 g
= new BigInteger(1, gnu
.java
.security
.util
.Util
.fromBase64(cnf
[1]));
178 IKeyPairGenerator kpg
= new SRPKeyPairGenerator();
179 HashMap attr
= new HashMap();
180 attr
.put(SRPKeyPairGenerator
.SHARED_MODULUS
, N
);
181 attr
.put(SRPKeyPairGenerator
.GENERATOR
, g
);
182 attr
.put(SRPKeyPairGenerator
.USER_VERIFIER
, v
);
184 return kpg
.generate();
187 catch (IOException ioe
) { }
191 public byte[] getSalt(String user
)
195 if (file
.contains(user
))
197 return gnu
.java
.security
.util
.Util
.fromBase64(file
.lookup(user
, "SHA")[1]);
200 catch (IOException ioe
) { }
204 public BigInteger
getVerifier(String user
)
208 if (file
.contains(user
))
210 return new BigInteger(1,
211 gnu
.java
.security
.util
.Util
.fromBase64(file
.lookup(user
, "SHA")[0]));
214 catch (IOException ioe
) { }
218 public PasswordFile
getPasswordFile()