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