1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57:
58: import ;
59: import ;
60:
61:
66: public class SRPTrustManagerFactory extends TrustManagerFactorySpi
67: {
68:
69:
70:
71:
72: private Manager current;
73:
74:
75:
76:
77: public SRPTrustManagerFactory()
78: {
79: super();
80: }
81:
82:
83:
84:
85: protected TrustManager[] engineGetTrustManagers()
86: {
87: if (current == null)
88: throw new IllegalStateException("not initialized");
89: return new TrustManager[] { current };
90: }
91:
92: protected void engineInit(KeyStore ks)
93: {
94: throw new IllegalArgumentException("only accepts SRPManagerParameters");
95: }
96:
97: protected void engineInit(ManagerFactoryParameters params)
98: throws InvalidAlgorithmParameterException
99: {
100: if (params == null)
101: {
102: try
103: {
104: String srpPasswd = Util.getSecurityProperty("jessie.srp.password.file");
105: if (srpPasswd == null)
106: {
107: current = new Manager(new PasswordFile());
108: return;
109: }
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));
117: return;
118: }
119: catch (IOException ioe)
120: {
121: throw new InvalidAlgorithmParameterException("default initialization failed: "
122: + ioe.toString());
123: }
124: }
125: if (params instanceof SRPManagerParameters)
126: {
127: current = new Manager(((SRPManagerParameters) params).getPasswordFile());
128: return;
129: }
130: throw new InvalidAlgorithmParameterException();
131: }
132:
133:
134:
135:
136: private class Manager implements SRPTrustManager
137: {
138:
139:
140:
141:
142: private final PasswordFile file;
143:
144:
145:
146:
147: Manager(PasswordFile file)
148: {
149: this.file = file;
150: }
151:
152:
153:
154:
155: public boolean contains(String user)
156: {
157: try
158: {
159: return file.contains(user);
160: }
161: catch (IOException ioe) { }
162: return false;
163: }
164:
165: public KeyPair getKeyPair(String user)
166: {
167: try
168: {
169: if (file.contains(user))
170: {
171: SRP srp = SRP.instance("SHA");
172: String[] ent = file.lookup(user, "SHA");
173: String[] cnf = file.lookupConfig(ent[2]);
174: BigInteger v, N, g;
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);
183: kpg.setup(attr);
184: return kpg.generate();
185: }
186: }
187: catch (IOException ioe) { }
188: return null;
189: }
190:
191: public byte[] getSalt(String user)
192: {
193: try
194: {
195: if (file.contains(user))
196: {
197: return gnu.java.security.util.Util.fromBase64(file.lookup(user, "SHA")[1]);
198: }
199: }
200: catch (IOException ioe) { }
201: return null;
202: }
203:
204: public BigInteger getVerifier(String user)
205: {
206: try
207: {
208: if (file.contains(user))
209: {
210: return new BigInteger(1,
211: gnu.java.security.util.Util.fromBase64(file.lookup(user, "SHA")[0]));
212: }
213: }
214: catch (IOException ioe) { }
215: return null;
216: }
217:
218: public PasswordFile getPasswordFile()
219: {
220: return file;
221: }
222: }
223: }