|
52 | 52 | * |
53 | 53 | */ |
54 | 54 | public class WebSocket { |
| 55 | + |
| 56 | + public static class SecureRandomProvider { |
| 57 | + |
| 58 | + private boolean mError = false; |
| 59 | + private SecureRandom mSecureRandom; |
| 60 | + |
| 61 | + |
| 62 | + public synchronized Optional<SecureRandom> getSecureRandom() { |
| 63 | + if (mError) { |
| 64 | + return Optional.absent(); |
| 65 | + } |
| 66 | + if (mSecureRandom != null) { |
| 67 | + return Optional.of(mSecureRandom); |
| 68 | + } |
| 69 | + try { |
| 70 | + mSecureRandom = SecureRandom.getInstance("SHA1PRNG"); |
| 71 | + return Optional.of(mSecureRandom); |
| 72 | + } catch (NoSuchAlgorithmException e) { |
| 73 | + // if we do not have secure random we have to leave data unmasked |
| 74 | + mError = true; |
| 75 | + return Optional.absent(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public synchronized String generateHandshakeSecret() { |
| 80 | + final Optional<SecureRandom> secureRandom = getSecureRandom(); |
| 81 | + |
| 82 | + byte[] nonce = new byte[16]; |
| 83 | + if (secureRandom.isPresent()) { |
| 84 | + secureRandom.get().nextBytes(nonce); |
| 85 | + } else { |
| 86 | + Arrays.fill(nonce, (byte) 0); |
| 87 | + } |
| 88 | + return Base64.encodeToString(nonce, Base64.NO_WRAP); |
| 89 | + } |
| 90 | + |
| 91 | + public synchronized Optional<byte[]> generateMask() { |
| 92 | + final Optional<SecureRandom> secureRandom = getSecureRandom(); |
| 93 | + if (!secureRandom.isPresent()) |
| 94 | + return Optional.absent(); |
| 95 | + |
| 96 | + byte[] bytes = new byte[4]; |
| 97 | + secureRandom.get().nextBytes(bytes); |
| 98 | + return Optional.of(bytes); |
| 99 | + } |
| 100 | + } |
55 | 101 |
|
56 | 102 | // WebSocket states |
57 | 103 | private enum State { |
@@ -82,8 +128,7 @@ private enum State { |
82 | 128 | // Not need to be locked |
83 | 129 | private final WebSocketListener mListener; |
84 | 130 |
|
85 | | - // Should be locked via self |
86 | | - private final Optional<SecureRandom> mSecureRandom; |
| 131 | + private final SecureRandomProvider mSecureRandomProvider; |
87 | 132 |
|
88 | 133 | private final Object mLockObj = new Object(); // 1 |
89 | 134 |
|
@@ -116,19 +161,7 @@ private enum State { |
116 | 161 | public WebSocket(WebSocketListener listener) { |
117 | 162 | checkArgument(listener != null, "Lister cannot be null"); |
118 | 163 | this.mListener = listener; |
119 | | - |
120 | | - SecureRandom secureRandom; |
121 | | - try { |
122 | | - secureRandom = SecureRandom.getInstance("SHA1PRNG"); |
123 | | - } catch (NoSuchAlgorithmException e) { |
124 | | - // if we do not have secure random we have to leave data unmasked |
125 | | - secureRandom = null; |
126 | | - } |
127 | | - if (secureRandom == null) { |
128 | | - mSecureRandom = Optional.absent(); |
129 | | - } else { |
130 | | - mSecureRandom = Optional.of(secureRandom); |
131 | | - } |
| 164 | + mSecureRandomProvider = new SecureRandomProvider(); |
132 | 165 | } |
133 | 166 |
|
134 | 167 | /** |
@@ -421,14 +454,7 @@ private void readPayload(boolean fin, int opcode, |
421 | 454 | * @return 4 bit random mask |
422 | 455 | */ |
423 | 456 | private Optional<byte[]> generateMask() { |
424 | | - synchronized (mSecureRandom) { |
425 | | - if (!mSecureRandom.isPresent()) |
426 | | - return Optional.absent(); |
427 | | - |
428 | | - byte[] bytes = new byte[4]; |
429 | | - mSecureRandom.get().nextBytes(bytes); |
430 | | - return Optional.of(bytes); |
431 | | - } |
| 457 | + return mSecureRandomProvider.generateMask(); |
432 | 458 | } |
433 | 459 |
|
434 | 460 | /** |
@@ -699,15 +725,7 @@ private static boolean verifyHandshakeAcceptValue(String key, |
699 | 725 | * @return random handshake key |
700 | 726 | */ |
701 | 727 | private String generateHandshakeSecret() { |
702 | | - synchronized (mSecureRandom) { |
703 | | - byte[] nonce = new byte[16]; |
704 | | - if (mSecureRandom.isPresent()) { |
705 | | - mSecureRandom.get().nextBytes(nonce); |
706 | | - } else { |
707 | | - Arrays.fill(nonce, (byte) 0); |
708 | | - } |
709 | | - return Base64.encodeToString(nonce, Base64.NO_WRAP); |
710 | | - } |
| 728 | + return mSecureRandomProvider.generateHandshakeSecret(); |
711 | 729 | } |
712 | 730 |
|
713 | 731 | /** |
|
0 commit comments