| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.auth; |
| 17 | |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import javax.security.auth.Subject; |
| 22 | import javax.security.auth.callback.CallbackHandler; |
| 23 | import javax.security.sasl.Sasl; |
| 24 | import javax.security.sasl.SaslClient; |
| 25 | import javax.security.sasl.SaslException; |
| 26 | import java.security.PrivilegedAction; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | /** |
| 31 | * Wrapper around {@code SaslClient}, which performs necessary parts of |
| 32 | * the authentication protocol as {@code PrivilegedAction}s with the |
| 33 | * assumed identity of the {@code Subject} supplied in the constructor. |
| 34 | */ |
| 35 | public class PrivilegedSaslClient |
| 36 | { |
| 37 | private static final Logger logger = LoggerFactory.getLogger(PrivilegedSaslClient.class); |
| 38 | |
| 39 | public static final Map<String, String> DEFAULT_PROPERTIES = new HashMap<String, String>() |
| 40 | {{ |
| 41 | put(Sasl.SERVER_AUTH, "true"); |
| 42 | put(Sasl.QOP, "auth"); |
| 43 | }}; |
| 44 | private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
| 45 | |
| 46 | /** |
| 47 | * Used to respond to server challenges with SASL tokens |
| 48 | * represented as byte arrays. |
| 49 | */ |
| 50 | private final SaslClient saslClient; |
| 51 | |
| 52 | /** |
| 53 | * The identity we intend to use when we authenticate with the server |
| 54 | */ |
| 55 | private Subject clientIdentity; |
| 56 | |
| 57 | public PrivilegedSaslClient(final Subject clientIdentity, |
| 58 | final String[] mechanisms, |
| 59 | final String authzId, |
| 60 | final String protocol, |
| 61 | final String hostname, |
| 62 | final Map<String, String> properties, |
| 63 | final CallbackHandler callbackHandler) |
| 64 | { |
| 65 | logger.trace("Initalising SASL client"); |
| 66 | this.clientIdentity = clientIdentity; |
| 67 | saslClient = Subject.doAs(this.clientIdentity, new PrivilegedAction<SaslClient>() |
| 68 | { |
| 69 | @Override |
| 70 | public javax.security.sasl.SaslClient run() |
| 71 | { |
| 72 | try |
| 73 | { |
| 74 | return Sasl.createSaslClient( |
| 75 | mechanisms, |
| 76 | authzId, |
| 77 | protocol, |
| 78 | hostname, |
| 79 | properties, |
| 80 | callbackHandler); |
| 81 | } catch (Exception e) |
| 82 | { |
| 83 | logger.error("Error initialising SASL client", e); |
| 84 | throw new RuntimeException(e); |
| 85 | } |
| 86 | } |
| 87 | }); |
| 88 | logger.trace("SASL client initialised"); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * Generate an initial token to start the SASL handshake with server. |
| 94 | * @return SaslTokenRequestMessage message to be sent to server. |
| 95 | */ |
| 96 | public byte[] getInitialResponse() |
| 97 | { |
| 98 | logger.trace("Initialising SASL handshake from client"); |
| 99 | byte[] initialResponse = null; |
| 100 | if (saslClient.hasInitialResponse()) |
| 101 | { |
| 102 | initialResponse = Subject.doAs(clientIdentity, new PrivilegedAction<byte[]>() |
| 103 | { |
| 104 | @Override |
| 105 | public byte[] run() |
| 106 | { |
| 107 | try |
| 108 | { |
| 109 | return saslClient.evaluateChallenge(EMPTY_BYTE_ARRAY); |
| 110 | } catch (Exception e) |
| 111 | { |
| 112 | throw new RuntimeException(e); |
| 113 | } |
| 114 | } |
| 115 | }); |
| 116 | } |
| 117 | return initialResponse; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Respond to server's SASL token |
| 122 | * @param challenge contains server's SASL token |
| 123 | * @return client's response SASL token, which will be null if authentication is complete |
| 124 | */ |
| 125 | public byte[] evaluateChallenge(final byte[] challenge) |
| 126 | { |
| 127 | logger.trace("Evaluating SASL server token"); |
| 128 | if (saslClient.isComplete()) |
| 129 | { |
| 130 | return null; |
| 131 | } |
| 132 | byte[] retval = Subject.doAs(clientIdentity, new PrivilegedAction<byte[]>() |
| 133 | { |
| 134 | @Override |
| 135 | public byte[] run() |
| 136 | { |
| 137 | try |
| 138 | { |
| 139 | logger.trace("SASL client evaluating challenge"); |
| 140 | byte[] retval = saslClient.evaluateChallenge(challenge); |
| 141 | logger.trace("SASL client is complete = " + saslClient.isComplete()); |
| 142 | return retval; |
| 143 | } |
| 144 | catch (SaslException e) |
| 145 | { |
| 146 | logger.error("Failed to generate response to SASL server's token:", e); |
| 147 | throw new RuntimeException(e); |
| 148 | } |
| 149 | } |
| 150 | }); |
| 151 | return retval; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Is the SASL negotiation with the server complete |
| 156 | * @return true iff SASL authentication has successfully completed |
| 157 | */ |
| 158 | public boolean isComplete() |
| 159 | { |
| 160 | return saslClient.isComplete(); |
| 161 | } |
| 162 | } |