diff options
Diffstat (limited to 'src/org/apache/harmony/javax/security/sasl')
10 files changed, 584 insertions, 0 deletions
diff --git a/src/org/apache/harmony/javax/security/sasl/AuthenticationException.java b/src/org/apache/harmony/javax/security/sasl/AuthenticationException.java new file mode 100644 index 0000000..38703ef --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/AuthenticationException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +public class AuthenticationException extends SaslException { + + private static final long serialVersionUID = -3579708765071815007L; + + public AuthenticationException() { + super(); + } + + public AuthenticationException(String detail) { + super(detail); + } + + public AuthenticationException(String detail, Throwable ex) { + super(detail, ex); + } +} diff --git a/src/org/apache/harmony/javax/security/sasl/AuthorizeCallback.java b/src/org/apache/harmony/javax/security/sasl/AuthorizeCallback.java new file mode 100644 index 0000000..2ba90a2 --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/AuthorizeCallback.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +import java.io.Serializable; +import org.apache.harmony.javax.security.auth.callback.Callback; + +public class AuthorizeCallback implements Callback, Serializable { + + private static final long serialVersionUID = -2353344186490470805L; + + /** + * Serialized field for storing authenticationID. + */ + private final String authenticationID; + + /** + * Serialized field for storing authorizationID. + */ + private final String authorizationID; + + /** + * Serialized field for storing authorizedID. + */ + private String authorizedID; + + /** + * Store authorized Serialized field. + */ + private boolean authorized; + + public AuthorizeCallback(String authnID, String authzID) { + super(); + authenticationID = authnID; + authorizationID = authzID; + authorizedID = authzID; + } + + public String getAuthenticationID() { + return authenticationID; + } + + public String getAuthorizationID() { + return authorizationID; + } + + public String getAuthorizedID() { + return (authorized ? authorizedID : null); + } + + public boolean isAuthorized() { + return authorized; + } + + public void setAuthorized(boolean ok) { + authorized = ok; + } + + public void setAuthorizedID(String id) { + if (id != null) { + authorizedID = id; + } + } +} diff --git a/src/org/apache/harmony/javax/security/sasl/RealmCallback.java b/src/org/apache/harmony/javax/security/sasl/RealmCallback.java new file mode 100644 index 0000000..65b5d15 --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/RealmCallback.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +import org.apache.harmony.javax.security.auth.callback.TextInputCallback; + +public class RealmCallback extends TextInputCallback { + + private static final long serialVersionUID = -4342673378785456908L; + + public RealmCallback(String prompt) { + super(prompt); + } + + public RealmCallback(String prompt, String defaultRealmInfo) { + super(prompt, defaultRealmInfo); + } +} diff --git a/src/org/apache/harmony/javax/security/sasl/RealmChoiceCallback.java b/src/org/apache/harmony/javax/security/sasl/RealmChoiceCallback.java new file mode 100644 index 0000000..079ea07 --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/RealmChoiceCallback.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +import org.apache.harmony.javax.security.auth.callback.ChoiceCallback; + +public class RealmChoiceCallback extends ChoiceCallback { + + private static final long serialVersionUID = -8588141348846281332L; + + public RealmChoiceCallback(String prompt, String[] choices, int defaultChoice, + boolean multiple) { + super(prompt, choices, defaultChoice, multiple); + } +} diff --git a/src/org/apache/harmony/javax/security/sasl/Sasl.java b/src/org/apache/harmony/javax/security/sasl/Sasl.java new file mode 100644 index 0000000..4d827f8 --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/Sasl.java @@ -0,0 +1,204 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +import java.security.Provider; +import java.security.Security; +import org.apache.harmony.javax.security.auth.callback.CallbackHandler; + + + +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Map; +import java.util.HashSet; +import java.util.Iterator; + +public class Sasl { + // SaslClientFactory service name + private static final String CLIENTFACTORYSRV = "SaslClientFactory"; //$NON-NLS-1$ + + // SaslServerFactory service name + private static final String SERVERFACTORYSRV = "SaslServerFactory"; //$NON-NLS-1$ + + public static final String POLICY_NOPLAINTEXT = "javax.security.sasl.policy.noplaintext"; //$NON-NLS-1$ + + public static final String POLICY_NOACTIVE = "javax.security.sasl.policy.noactive"; //$NON-NLS-1$ + + public static final String POLICY_NODICTIONARY = "javax.security.sasl.policy.nodictionary"; //$NON-NLS-1$ + + public static final String POLICY_NOANONYMOUS = "javax.security.sasl.policy.noanonymous"; //$NON-NLS-1$ + + public static final String POLICY_FORWARD_SECRECY = "javax.security.sasl.policy.forward"; //$NON-NLS-1$ + + public static final String POLICY_PASS_CREDENTIALS = "javax.security.sasl.policy.credentials"; //$NON-NLS-1$ + + public static final String MAX_BUFFER = "javax.security.sasl.maxbuffer"; //$NON-NLS-1$ + + public static final String RAW_SEND_SIZE = "javax.security.sasl.rawsendsize"; //$NON-NLS-1$ + + public static final String REUSE = "javax.security.sasl.reuse"; //$NON-NLS-1$ + + public static final String QOP = "javax.security.sasl.qop"; //$NON-NLS-1$ + + public static final String STRENGTH = "javax.security.sasl.strength"; //$NON-NLS-1$ + + public static final String SERVER_AUTH = "javax.security.sasl.server.authentication"; //$NON-NLS-1$ + + // Default public constructor is overridden + private Sasl() { + super(); + } + + // Forms new instance of factory + private static Object newInstance(String factoryName, Provider prv) throws SaslException { + String msg = "auth.31"; //$NON-NLS-1$ + Object factory; + ClassLoader cl = prv.getClass().getClassLoader(); + if (cl == null) { + cl = ClassLoader.getSystemClassLoader(); + } + try { + factory = (Class.forName(factoryName, true, cl)).newInstance(); + return factory; + } catch (IllegalAccessException e) { + throw new SaslException(msg + factoryName, e); + } catch (ClassNotFoundException e) { + throw new SaslException(msg + factoryName, e); + } catch (InstantiationException e) { + throw new SaslException(msg + factoryName, e); + } + } + + /** + * This method forms the list of SaslClient/SaslServer factories which are + * implemented in used providers + */ + private static Collection<?> findFactories(String service) { + HashSet<Object> fact = new HashSet<Object>(); + Provider[] pp = Security.getProviders(); + if ((pp == null) || (pp.length == 0)) { + return fact; + } + HashSet<String> props = new HashSet<String>(); + for (int i = 0; i < pp.length; i++) { + String prName = pp[i].getName(); + Enumeration<Object> keys = pp[i].keys(); + while (keys.hasMoreElements()) { + String s = (String) keys.nextElement(); + if (s.startsWith(service)) { + String prop = pp[i].getProperty(s); + try { + if (props.add(prName.concat(prop))) { + fact.add(newInstance(prop, pp[i])); + } + } catch (SaslException e) { + // ignore this factory + e.printStackTrace(); + } + } + } + } + return fact; + } + + @SuppressWarnings("unchecked") + public static Enumeration<SaslClientFactory> getSaslClientFactories() { + Collection<SaslClientFactory> res = (Collection<SaslClientFactory>) findFactories(CLIENTFACTORYSRV); + return Collections.enumeration(res); + + } + + @SuppressWarnings("unchecked") + public static Enumeration<SaslServerFactory> getSaslServerFactories() { + Collection<SaslServerFactory> res = (Collection<SaslServerFactory>) findFactories(SERVERFACTORYSRV); + return Collections.enumeration(res); + } + + public static SaslServer createSaslServer(String mechanism, String protocol, + String serverName, Map<String, ?> prop, CallbackHandler cbh) throws SaslException { + if (mechanism == null) { + throw new NullPointerException("auth.32"); //$NON-NLS-1$ + } + Collection<?> res = findFactories(SERVERFACTORYSRV); + if (res.isEmpty()) { + return null; + } + + Iterator<?> iter = res.iterator(); + while (iter.hasNext()) { + SaslServerFactory fact = (SaslServerFactory) iter.next(); + String[] mech = fact.getMechanismNames(null); + boolean is = false; + if (mech != null) { + for (int j = 0; j < mech.length; j++) { + if (mech[j].equals(mechanism)) { + is = true; + break; + } + } + } + if (is) { + SaslServer saslS = fact.createSaslServer(mechanism, protocol, serverName, prop, + cbh); + if (saslS != null) { + return saslS; + } + } + } + return null; + } + + public static SaslClient createSaslClient(String[] mechanisms, String authanticationID, + String protocol, String serverName, Map<String, ?> prop, CallbackHandler cbh) + throws SaslException { + if (mechanisms == null) { + throw new NullPointerException("auth.33"); //$NON-NLS-1$ + } + Collection<?> res = findFactories(CLIENTFACTORYSRV); + if (res.isEmpty()) { + return null; + } + + Iterator<?> iter = res.iterator(); + while (iter.hasNext()) { + SaslClientFactory fact = (SaslClientFactory) iter.next(); + String[] mech = fact.getMechanismNames(null); + boolean is = false; + if (mech != null) { + for (int j = 0; j < mech.length; j++) { + for (int n = 0; n < mechanisms.length; n++) { + if (mech[j].equals(mechanisms[n])) { + is = true; + break; + } + } + } + } + if (is) { + SaslClient saslC = fact.createSaslClient(mechanisms, authanticationID, + protocol, serverName, prop, cbh); + if (saslC != null) { + return saslC; + } + } + } + return null; + } +} diff --git a/src/org/apache/harmony/javax/security/sasl/SaslClient.java b/src/org/apache/harmony/javax/security/sasl/SaslClient.java new file mode 100644 index 0000000..e07ff53 --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/SaslClient.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +public interface SaslClient { + + void dispose() throws SaslException; + + byte[] evaluateChallenge(byte[] challenge) throws SaslException; + + String getMechanismName(); + + Object getNegotiatedProperty(String propName); + + boolean hasInitialResponse(); + + boolean isComplete(); + + byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException; + + byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException; +} diff --git a/src/org/apache/harmony/javax/security/sasl/SaslClientFactory.java b/src/org/apache/harmony/javax/security/sasl/SaslClientFactory.java new file mode 100644 index 0000000..e567ed3 --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/SaslClientFactory.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +import java.util.Map; +import org.apache.harmony.javax.security.auth.callback.CallbackHandler; + +public interface SaslClientFactory { + + SaslClient createSaslClient(String[] mechanisms, String authorizationId, String protocol, + String serverName, Map<String, ?> props, CallbackHandler cbh) throws SaslException; + + String[] getMechanismNames(Map<String, ?> props); + +} diff --git a/src/org/apache/harmony/javax/security/sasl/SaslException.java b/src/org/apache/harmony/javax/security/sasl/SaslException.java new file mode 100644 index 0000000..1ab7b12 --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/SaslException.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +import java.io.IOException; + +public class SaslException extends IOException { + + private static final long serialVersionUID = 4579784287983423626L; + + /** + * Serialized field for storing initial cause + */ + private Throwable _exception; + + public SaslException() { + super(); + } + + public SaslException(String detail) { + super(detail); + } + + public SaslException(String detail, Throwable ex) { + super(detail); + if (ex != null) { + super.initCause(ex); + _exception = ex; + } + } + + @Override + public Throwable getCause() { + return _exception; + } + + @Override + public Throwable initCause(Throwable cause) { + super.initCause(cause); + _exception = cause; + return this; + } + + @Override + public String toString() { + if (_exception == null) { + return super.toString(); + } + StringBuilder sb = new StringBuilder(super.toString()); + sb.append(", caused by: "); //$NON-NLS-1$ + sb.append(_exception.toString()); + return sb.toString(); + } +} diff --git a/src/org/apache/harmony/javax/security/sasl/SaslServer.java b/src/org/apache/harmony/javax/security/sasl/SaslServer.java new file mode 100644 index 0000000..f057a4b --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/SaslServer.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +public interface SaslServer { + + void dispose() throws SaslException; + + byte[] evaluateResponse(byte[] response) throws SaslException; + + String getAuthorizationID(); + + String getMechanismName(); + + Object getNegotiatedProperty(String propName); + + boolean isComplete(); + + byte[] unwrap(byte[] incoming, int offset, int len) throws SaslException; + + byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException; +} diff --git a/src/org/apache/harmony/javax/security/sasl/SaslServerFactory.java b/src/org/apache/harmony/javax/security/sasl/SaslServerFactory.java new file mode 100644 index 0000000..d59530e --- /dev/null +++ b/src/org/apache/harmony/javax/security/sasl/SaslServerFactory.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.javax.security.sasl; + +import java.util.Map; +import org.apache.harmony.javax.security.auth.callback.CallbackHandler; + +public interface SaslServerFactory { + + SaslServer createSaslServer(String mechanisms, String protocol, String serverName, + Map<String, ?> props, CallbackHandler cbh) throws SaslException; + + String[] getMechanismNames(Map<String, ?> props); + +} |