|
| 1 | +package com.rabbitmq.client.impl; |
| 2 | + |
| 3 | +import com.rabbitmq.client.ConnectionFactory; |
| 4 | +import com.rabbitmq.client.SaslConfig; |
| 5 | +import com.rabbitmq.client.UsernamePasswordCallbackHandler; |
| 6 | + |
| 7 | +import javax.security.auth.callback.Callback; |
| 8 | +import javax.security.auth.callback.CallbackHandler; |
| 9 | +import javax.security.auth.callback.NameCallback; |
| 10 | +import javax.security.auth.callback.PasswordCallback; |
| 11 | +import javax.security.auth.callback.UnsupportedCallbackException; |
| 12 | +import javax.security.sasl.SaslClient; |
| 13 | +import javax.security.sasl.SaslException; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.UnsupportedEncodingException; |
| 16 | +import java.security.MessageDigest; |
| 17 | +import java.security.NoSuchAlgorithmException; |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +/** |
| 21 | + START-OK: Username |
| 22 | + SECURE: {Salt1, Salt2} (where Salt1 is the salt from the db and |
| 23 | + Salt2 differs every time) |
| 24 | + SECURE-OK: md5(Salt2 ++ md5(Salt1 ++ Password)) |
| 25 | +
|
| 26 | + The second salt is there to defend against replay attacks. The |
| 27 | + first is needed since the passwords are salted in the db. |
| 28 | +
|
| 29 | + This is only somewhat improved security over PLAIN (if you can |
| 30 | + break MD5 you can still replay attack) but it's better than nothing |
| 31 | + and mostly there to prove the use of SECURE / SECURE-OK frames. |
| 32 | +*/ |
| 33 | + |
| 34 | +public class ScramMD5SaslClient implements SaslClient { |
| 35 | + private static final String NAME = "RABBIT-SCRAM-MD5"; |
| 36 | + |
| 37 | + private CallbackHandler handler; |
| 38 | + private int round = 0; |
| 39 | + |
| 40 | + public ScramMD5SaslClient(CallbackHandler handler) { |
| 41 | + this.handler = handler; |
| 42 | + } |
| 43 | + |
| 44 | + public String getMechanismName() { |
| 45 | + return NAME; |
| 46 | + } |
| 47 | + |
| 48 | + public boolean hasInitialResponse() { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + public byte[] evaluateChallenge(byte[] challenge) throws SaslException { |
| 53 | + byte[] resp; |
| 54 | + try { |
| 55 | + if (round == 0) { |
| 56 | + NameCallback nc = new NameCallback("Name:"); |
| 57 | + handler.handle(new Callback[]{nc}); |
| 58 | + resp = nc.getName().getBytes("utf-8"); |
| 59 | + } else { |
| 60 | + byte[] salt1 = Arrays.copyOfRange(challenge, 0, 4); |
| 61 | + byte[] salt2 = Arrays.copyOfRange(challenge, 4, 8); |
| 62 | + PasswordCallback pc = new PasswordCallback("Password:", false); |
| 63 | + handler.handle(new Callback[]{pc}); |
| 64 | + byte[] pw = new String(pc.getPassword()).getBytes("utf-8"); |
| 65 | + resp = digest(salt2, digest(salt1, pw)); |
| 66 | + } |
| 67 | + } catch (UnsupportedEncodingException e) { |
| 68 | + throw new RuntimeException(e); |
| 69 | + } catch (UnsupportedCallbackException e) { |
| 70 | + throw new SaslException("Bad callback", e); |
| 71 | + } catch (IOException e) { |
| 72 | + throw new SaslException("IO Exception", e); |
| 73 | + } |
| 74 | + |
| 75 | + round++; |
| 76 | + return resp; |
| 77 | + } |
| 78 | + |
| 79 | + public boolean isComplete() { |
| 80 | + return round == 2; |
| 81 | + } |
| 82 | + |
| 83 | + public byte[] unwrap(byte[] bytes, int i, int i1) throws SaslException { |
| 84 | + throw new UnsupportedOperationException(); |
| 85 | + } |
| 86 | + |
| 87 | + public byte[] wrap(byte[] bytes, int i, int i1) throws SaslException { |
| 88 | + throw new UnsupportedOperationException(); |
| 89 | + } |
| 90 | + |
| 91 | + public Object getNegotiatedProperty(String s) { |
| 92 | + throw new UnsupportedOperationException(); |
| 93 | + } |
| 94 | + |
| 95 | + public void dispose() throws SaslException { |
| 96 | + // NOOP |
| 97 | + } |
| 98 | + |
| 99 | + private static byte[] digest(byte[] arr1, byte[] arr2) { |
| 100 | + try { |
| 101 | + MessageDigest digest = MessageDigest.getInstance("MD5"); |
| 102 | + return digest.digest(concat(arr1, arr2)); |
| 103 | + |
| 104 | + } catch (NoSuchAlgorithmException e) { |
| 105 | + throw new RuntimeException(e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static byte[] concat(byte[] first, byte[] second) { |
| 110 | + byte[] result = Arrays.copyOf(first, first.length + second.length); |
| 111 | + System.arraycopy(second, 0, result, first.length, second.length); |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + public static class ScramMD5SaslConfig implements SaslConfig { |
| 116 | + private ConnectionFactory factory; |
| 117 | + |
| 118 | + public ScramMD5SaslConfig(ConnectionFactory factory) { |
| 119 | + this.factory = factory; |
| 120 | + } |
| 121 | + |
| 122 | + public SaslClient getSaslClient(String[] mechanisms) throws SaslException { |
| 123 | + if (Arrays.asList(mechanisms).contains(NAME)) { |
| 124 | + return new ScramMD5SaslClient(new UsernamePasswordCallbackHandler(factory)); |
| 125 | + } |
| 126 | + else { |
| 127 | + return null; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments