|
44 | 44 | public class LoginClient { |
45 | 45 | private static final SecureRandom SECURE_RANDOM = new SecureRandom(); |
46 | 46 |
|
| 47 | + static final int EXPECTED_ENVELOPE_SIZE = |
| 48 | + OpaqueUtil.PUBLIC_KEY_LENGTH + OpaqueUtil.NONCE_LENGTH + OpaqueUtil.MAC_TAG_LENGTH; |
| 49 | + |
47 | 50 | private final LoginServiceGrpc.LoginServiceStub stub; |
48 | 51 |
|
49 | 52 | public LoginClient(ManagedChannel channel) { |
@@ -132,7 +135,7 @@ public AccessToken login(String username, SecretBytes password) throws SpannerEx |
132 | 135 | initialResponse.getOpaqueResponse().getInitialResponse(); |
133 | 136 |
|
134 | 137 | ByteString envelope = initialOpaqueResponse.getMaskedResponse(); |
135 | | - if (envelope.size() != 65) { |
| 138 | + if (envelope.size() != EXPECTED_ENVELOPE_SIZE) { |
136 | 139 | throw new GeneralSecurityException("Invalid envelope size: " + envelope.size()); |
137 | 140 | } |
138 | 141 |
|
@@ -236,17 +239,21 @@ private byte[] generateClientMac( |
236 | 239 | OpaqueUtil.concat( |
237 | 240 | initialOpaqueResponse.getMaskingNonce().toByteArray(), |
238 | 241 | "CredentialResponsePad".getBytes(java.nio.charset.StandardCharsets.UTF_8)), |
239 | | - 16 + 33 + 16); |
| 242 | + OpaqueUtil.NONCE_LENGTH + OpaqueUtil.PUBLIC_KEY_LENGTH + OpaqueUtil.MAC_TAG_LENGTH); |
240 | 243 | byte[] serializedEnvelope = |
241 | 244 | OpaqueUtil.xorBytes( |
242 | 245 | initialOpaqueResponse.getMaskedResponse().toByteArray(), credentialResponsePad); |
243 | 246 | ByteString envelope = ByteString.copyFrom(serializedEnvelope); |
244 | | - if (envelope.size() != 65) { |
| 247 | + if (envelope.size() != EXPECTED_ENVELOPE_SIZE) { |
245 | 248 | throw new GeneralSecurityException("Invalid envelope size: " + envelope.size()); |
246 | 249 | } |
247 | | - ByteString serverPublicKey = envelope.substring(0, 33); |
248 | | - ByteString envelopeNonce = envelope.substring(33, 33 + 16); |
249 | | - ByteString authTag = envelope.substring(33 + 16, 33 + 16 + 16); |
| 250 | + ByteString serverPublicKey = envelope.substring(0, OpaqueUtil.PUBLIC_KEY_LENGTH); |
| 251 | + ByteString envelopeNonce = |
| 252 | + envelope.substring( |
| 253 | + OpaqueUtil.PUBLIC_KEY_LENGTH, OpaqueUtil.PUBLIC_KEY_LENGTH + OpaqueUtil.NONCE_LENGTH); |
| 254 | + ByteString authTag = |
| 255 | + envelope.substring( |
| 256 | + OpaqueUtil.PUBLIC_KEY_LENGTH + OpaqueUtil.NONCE_LENGTH, EXPECTED_ENVELOPE_SIZE); |
250 | 257 |
|
251 | 258 | authKey = |
252 | 259 | OpaqueUtil.expand( |
|
0 commit comments