| 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 | private static final Logger logger = LoggerFactory.getLogger(PrivilegedSaslClient.class); |
| 37 | |
| 38 | public static final Map<String, String> DEFAULT_PROPERTIES = new HashMap<String, String>() {{ |
| 39 | put(Sasl.SERVER_AUTH, "true"); |
| 40 | put(Sasl.QOP, "auth"); |
| 41 | }}; |
| 42 | private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
| 43 | |
| 44 | /** |
| 45 | * Used to respond to server challenges with SASL tokens |
| 46 | * represented as byte arrays. |
| 47 | */ |
| 48 | private final SaslClient saslClient; |
| 49 | |
| 50 | /** |
| 51 | * The identity we intend to use when we authenticate with the server |
| 52 | */ |
| 53 | private Subject clientIdentity; |
| 54 | |
| 55 | public PrivilegedSaslClient(final Subject clientIdentity, |
| 56 | final String[] mechanisms, |
| 57 | final String authzId, |
| 58 | final String protocol, |
| 59 | final String hostname, |
| 60 | final Map<String, String> properties, |
| 61 | final CallbackHandler callbackHandler) { |
| 62 | logger.trace("Initalising SASL client"); |
| 63 | this.clientIdentity = clientIdentity; |
| 64 | saslClient = Subject.doAs(this.clientIdentity, new PrivilegedAction<SaslClient>() { |
| 65 | @Override |
| 66 | public javax.security.sasl.SaslClient run() { |
| 67 | try { |
| 68 | return Sasl.createSaslClient( |
| 69 | mechanisms, |
| 70 | authzId, |
| 71 | protocol, |
| 72 | hostname, |
| 73 | properties, |
| 74 | callbackHandler); |
| 75 | } catch (Exception e) { |
| 76 | logger.error("Error initialising SASL client", e); |
| 77 | throw new RuntimeException(e); |
| 78 | } |
| 79 | } |
| 80 | }); |
| 81 | logger.trace("SASL client initialised"); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Generate an initial token to start the SASL handshake with server. |
| 87 | * |
| 88 | * @return SaslTokenRequestMessage message to be sent to server. |
| 89 | */ |
| 90 | public byte[] getInitialResponse() { |
| 91 | logger.trace("Initialising SASL handshake from client"); |
| 92 | byte[] initialResponse = null; |
| 93 | if (saslClient.hasInitialResponse()) { |
| 94 | initialResponse = Subject.doAs(clientIdentity, new PrivilegedAction<byte[]>() { |
| 95 | @Override |
| 96 | public byte[] run() { |
| 97 | try { |
| 98 | return saslClient.evaluateChallenge(EMPTY_BYTE_ARRAY); |
| 99 | } catch (Exception e) { |
| 100 | throw new RuntimeException(e); |
| 101 | } |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | return initialResponse; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Respond to server's SASL token |
| 110 | * |
| 111 | * @param challenge contains server's SASL token |
| 112 | * @return client's response SASL token, which will be null if authentication is complete |
| 113 | */ |
| 114 | public byte[] evaluateChallenge(final byte[] challenge) { |
| 115 | logger.trace("Evaluating SASL server token"); |
| 116 | if (saslClient.isComplete()) { |
| 117 | return null; |
| 118 | } |
| 119 | byte[] retval = Subject.doAs(clientIdentity, new PrivilegedAction<byte[]>() { |
| 120 | @Override |
| 121 | public byte[] run() { |
| 122 | try { |
| 123 | logger.trace("SASL client evaluating challenge"); |
| 124 | byte[] retval = saslClient.evaluateChallenge(challenge); |
| 125 | logger.trace("SASL client is complete = " + saslClient.isComplete()); |
| 126 | return retval; |
| 127 | } catch (SaslException e) { |
| 128 | logger.error("Failed to generate response to SASL server's token:", e); |
| 129 | throw new RuntimeException(e); |
| 130 | } |
| 131 | } |
| 132 | }); |
| 133 | return retval; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Is the SASL negotiation with the server complete |
| 138 | * |
| 139 | * @return true iff SASL authentication has successfully completed |
| 140 | */ |
| 141 | public boolean isComplete() { |
| 142 | return saslClient.isComplete(); |
| 143 | } |
| 144 | } |