| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi.gssapi.auth; |
| 16 |
|
| 17 |
import org.gridforum.jgss.ExtendedGSSManager; |
| 18 |
|
| 19 |
import org.ietf.jgss.GSSManager; |
| 20 |
import org.ietf.jgss.GSSContext; |
| 21 |
import org.ietf.jgss.GSSName; |
| 22 |
import org.ietf.jgss.GSSException; |
| 23 |
import org.ietf.jgss.GSSCredential; |
| 24 |
|
| 25 |
import org.apache.commons.logging.Log; |
| 26 |
import org.apache.commons.logging.LogFactory; |
| 27 |
|
| 28 |
/** |
| 29 |
* Implements a simple host authorization mechanism. |
| 30 |
* The peer's host name (in FQDN form) is compared with the |
| 31 |
* host name specified in the peer's certificate chain. |
| 32 |
*/ |
| 33 |
public class HostAuthorization |
| 34 |
extends GSSAuthorization { |
| 35 |
|
| 36 |
private static Log logger = |
| 37 |
LogFactory.getLog(HostAuthorization.class.getName()); |
| 38 |
|
| 39 |
public static final HostAuthorization ldapAuthorization = |
| 40 |
new HostAuthorization("ldap"); |
| 41 |
|
| 42 |
private String _service = null; |
| 43 |
|
| 44 |
private static HostAuthorization hostAuthorization; |
| 45 |
|
| 46 |
public HostAuthorization(String service) { |
| 47 |
_service = (service == null) ? "host" : service; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns an instance of host authentication. |
| 52 |
* |
| 53 |
* @return an instance of this class initialized with |
| 54 |
* <i>host</i> as a service. |
| 55 |
*/ |
| 56 |
public synchronized static HostAuthorization getInstance() { |
| 57 |
if (hostAuthorization == null) { |
| 58 |
hostAuthorization = new HostAuthorization("host"); |
| 59 |
} |
| 60 |
return hostAuthorization; |
| 61 |
} |
| 62 |
|
| 63 |
public GSSName getExpectedName(GSSCredential cred, String host) |
| 64 |
throws GSSException { |
| 65 |
GSSManager manager = ExtendedGSSManager.getInstance(); |
| 66 |
return manager.createName(_service + "@" + host, |
| 67 |
GSSName.NT_HOSTBASED_SERVICE); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Performs host authentication. The hostname of the peer is |
| 72 |
* compared with the hostname specified in the peer's (topmost) |
| 73 |
* certificate in the certificate chain. The hostnames must |
| 74 |
* match exactly (in case-insensitive way) |
| 75 |
* |
| 76 |
* @param context the security context |
| 77 |
* @param host host address of the peer. |
| 78 |
* @exception AuthorizationException if the hostnames |
| 79 |
* do not match. |
| 80 |
*/ |
| 81 |
public void authorize(GSSContext context, String host) |
| 82 |
throws AuthorizationException { |
| 83 |
logger.debug("Authorization: HOST"); |
| 84 |
|
| 85 |
try { |
| 86 |
GSSName expected = getExpectedName(null, host); |
| 87 |
|
| 88 |
GSSName target = null; |
| 89 |
if (context.isInitiator()) { |
| 90 |
target = context.getTargName(); |
| 91 |
} else { |
| 92 |
target = context.getSrcName(); |
| 93 |
} |
| 94 |
|
| 95 |
if (!expected.equals(target)) { |
| 96 |
generateAuthorizationException(expected, target); |
| 97 |
} |
| 98 |
} catch (GSSException e) { |
| 99 |
throw new AuthorizationException("Authorization failure", e); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public boolean equals(Object o) { |
| 104 |
if (o == null) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
if (this == o) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
if (o instanceof HostAuthorization) { |
| 111 |
HostAuthorization other = (HostAuthorization)o; |
| 112 |
if (this._service == null) { |
| 113 |
return (other._service == null); |
| 114 |
} else { |
| 115 |
return this._service.equals(other._service); |
| 116 |
} |
| 117 |
} |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
public int hashCode() { |
| 122 |
return (this._service == null) ? 0 : this._service.hashCode(); |
| 123 |
} |
| 124 |
|
| 125 |
} |