3 import java.util.Hashtable;
11 public abstract class GenericCommon
14 /* These are the jni functions called by the routines in common
15 * see javasasl.c for their implementations
18 private native void jni_sasl_set_prop_string(int ptr, int propnum, String value);
19 private native void jni_sasl_set_prop_int(int ptr, int propnum, int value);
20 private native void jni_sasl_set_prop_bytes(int ptr, int propnum, byte[] value);
21 private native void jni_sasl_set_server(int ptr, byte []ipnum, int port);
22 private native void jni_sasl_set_client(int ptr, byte []ipnum, int port);
23 private native void jni_sasl_setSecurity(int ptr, int minssf, int maxssf);
24 private native int jni_sasl_getSecurity(int ptr);
25 private native byte[] jni_sasl_encode(int ptr, byte[] in,int len);
26 private native byte[] jni_sasl_decode(int ptr, byte[] in,int len);
27 private native void jni_sasl_dispose(int ptr);
30 * security layer security strength factor
32 public static int SASL_SSF =1;
34 public static int SASL_MAXOUTBUF=2; /* security layer max output buf unsigned */
35 public static int SASL_REALM =3; /* server authentication realm used */
36 public static int SASL_GETOPTCTX=4; /* context for getopt callback */
40 * Local sockaddr_in (use setServer and setClient to set this)
42 public static int SASL_IP_LOCAL =5;
45 * Remote sockaddr_in (use setClient and setServer to set this)
48 public static int SASL_IP_REMOTE =6;
51 * External security factor (use setSecurity to set this)
53 public static int SASL_SSF_EXTERNAL=100;
54 public static int SASL_SEC_PROPS =101; /* sasl_security_properties_t */
57 int ptr; // this is the actual pointer to sasl_conn_t
58 int ssfactive; // active ssf on this connection
62 public boolean done() { return finished; }
65 * Set a SASL property that takes a string value
67 * @param PROPERTY one of the property constants
68 * @param value string value
71 public void setproperty(int PROPERTY, String value)
73 jni_sasl_set_prop_string(ptr,PROPERTY,value);
77 * Set a SASL property that takes a integer value
79 * @param PROPERTY one of the property constants
80 * @param value integer value
83 public void setproperty(int PROPERTY, int value)
85 jni_sasl_set_prop_int(ptr,PROPERTY,value);
89 * Set a SASL property that takes a byte[] value
91 * @param PROPERTY one of the property constants
92 * @param value byte[] value
95 public void setproperty(int PROPERTY, byte[] value)
97 jni_sasl_set_prop_bytes(ptr,PROPERTY,value);
101 * Set the SASL properties for the server
102 * This sets the IP address and port
104 * @param name String of name of server (e.g. cyrus.andrew.cmu.edu)
105 * @param port port connected to on that server
108 private boolean setRemoteIP(String name,int port)
112 InetAddress server=InetAddress.getByName(name);
113 ip=server.getAddress();
114 } catch (UnknownHostException e) {
118 jni_sasl_set_server(ptr, ip, port);
123 * Set the SASL properties for the client
124 * This sets the IP address and port
126 * @param name String of local cannonical name (e.g. myhostname.andrew.cmu.edu)
127 * @param port port connecting
130 private boolean setLocalIP(String name, int port)
134 InetAddress server=InetAddress.getByName(name);
135 ip=server.getAddress();
136 } catch (UnknownHostException e) {
140 jni_sasl_set_client(ptr, ip, port);
145 * Set the SASL properties for the client
146 * This sets the IP address and port
148 * @param local local InetAdress
149 * @param port port connecting
152 public boolean setClient(InetAddress local,int port)
154 byte[]ip=local.getAddress();
156 jni_sasl_set_client(ptr, ip, port);
162 * Set the SASL properties for the client
163 * This sets the IP address and port
164 * The local IP address is determined with InetAddress.getLocalHost()
166 * @param port port connecting
169 public boolean setClient(int port)
172 return setClient(InetAddress.getLocalHost(),port);
173 } catch (UnknownHostException e) {
179 * Sets the security properties for the session
181 * @param external external security strength
182 * @param minssf minimum security needed
183 * @param maxssf maximum security to negotiate
185 * @return if the propery was set sucessfully or not
189 public boolean setSecurity(int external, int minssf, int maxssf)
191 /* setproperty(SASL_SSF_EXTERNAL, external); */
193 jni_sasl_setSecurity(ptr,minssf,maxssf);
198 public int getSecurity() {
199 return jni_sasl_getSecurity(ptr);
203 * Encode a String with the negotiated layer
205 * @param in String to be encoded
206 * @return the encoded string represented at a byte[]
208 public byte[] encode(byte[] in)
211 byte[] out=jni_sasl_encode(ptr,in,in.length);
217 * Decode a byte[] with the negotiated layer
219 * @param in byte[] to be decoded
220 * @param len number of bytes to be decoded
221 * @return the decoded string represented at a byte[]
223 public byte[] decode(byte[] in, int len)
226 byte[] out=jni_sasl_decode(ptr,in,len);
232 * Decode a String with the negotiated layer. NOTE: Be careful with
233 * this function. International or high ascii characters may do strange
234 * things. The byte[] method is preferred
236 * @param in String to be decoded
237 * @return the decoded string represented at a byte[]
239 public byte[] decode(String in)
241 return decode(in.getBytes(),in.length());
244 protected void setcommonproperties(Hashtable props)
247 String s_ssfmin=(String) props.get("javax.security.sasl.encryption.minimum");
248 if (s_ssfmin!=null) i_ssfmin = Integer.parseInt(s_ssfmin);
251 String s_ssfmax=(String) props.get("javax.security.sasl.encryption.maximum");
252 if (s_ssfmax!=null) i_ssfmax = Integer.parseInt(s_ssfmax);
256 /* String external=(String) props.getProperty("security.policy.encryption.external",
259 setSecurity(i_external,
263 String iplocal = (String) props.get("javax.security.sasl.ip.local");
264 if (iplocal!=null) setLocalIP(iplocal,0);
266 String ipremote = (String) props.get("javax.security.sasl.ip.remote");
267 if (ipremote!=null) setRemoteIP(ipremote,0);
269 /* String maxbuf=props.getProperty("security.maxbuf","65000"); */
270 /* xxx this raises an exception for some reason
271 setproperty(SASL_MAXOUTBUF,Integer.parseInt(maxbuf)); */
275 final protected void finalize () throws Throwable
277 jni_sasl_dispose(ptr);
280 protected boolean complete = false;
282 public boolean isComplete()
287 /* called by JNI layer */
288 public void setcomplete(int a)