Skip to content

Commit bee99e5

Browse files
pyryadvr
authored andcommitted
Use constant-time comparison functions when checking signatures
This limits the likeliness of timing attacks against the API. See http://codahale.com/a-lesson-in-timing-attacks/ for the full rationale. Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> Conflicts: server/src/com/cloud/api/ApiServer.java server/src/com/cloud/user/AccountManagerImpl.java (cherry picked from commit 9b4e39e) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> Conflicts: server/src/com/cloud/api/ApiServer.java server/src/com/cloud/user/AccountManagerImpl.java
1 parent 0f7ec98 commit bee99e5

4 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎server/src/com/cloud/api/ApiServer.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,11 @@ else if ("signatureversion".equalsIgnoreCase(paramName)) {
849849
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1");
850850
mac.init(keySpec);
851851
mac.update(unsignedRequest.getBytes());
852-
byte[] encryptedBytes = mac.doFinal();
853-
String computedSignature = Base64.encodeBase64String(encryptedBytes);
854-
boolean equalSig = signature.equals(computedSignature);
852+
853+
final byte[] encryptedBytes = mac.doFinal();
854+
final String computedSignature = Base64.encodeBase64String(encryptedBytes);
855+
final boolean equalSig = ConstantTimeComparator.compareStrings(signature, computedSignature);
856+
855857
if (!equalSig) {
856858
s_logger.info("User signature: " + signature + " is not equaled to computed signature: " + computedSignature);
857859
} else {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api;
18+
19+
public class ConstantTimeComparator {
20+
21+
public static boolean compareBytes(byte[] b1, byte[] b2) {
22+
if (b1.length != b2.length) {
23+
return false;
24+
}
25+
26+
int result = 0;
27+
for (int i = 0; i < b1.length; i++) {
28+
result |= b1[i] ^ b2[i];
29+
}
30+
return result == 0;
31+
}
32+
33+
public static boolean compareStrings(String s1, String s2) {
34+
return compareBytes(s1.getBytes(), s2.getBytes());
35+
}
36+
}

‎server/src/com/cloud/servlet/ConsoleProxyServlet.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
import org.apache.cloudstack.api.IdentityService;
4747

48+
import com.cloud.api.ConstantTimeComparator;
4849
import com.cloud.exception.PermissionDeniedException;
4950
import com.cloud.host.HostVO;
5051
import com.cloud.server.ManagementServer;
@@ -651,7 +652,7 @@ private boolean verifyRequest(Map<String, Object[]> requestParameters) {
651652
mac.update(unsignedRequest.getBytes());
652653
byte[] encryptedBytes = mac.doFinal();
653654
String computedSignature = Base64.encodeBase64String(encryptedBytes);
654-
boolean equalSig = signature.equals(computedSignature);
655+
boolean equalSig = ConstantTimeComparator.compareStrings(signature, computedSignature);
655656
if (!equalSig) {
656657
s_logger.debug("User signature: " + signature + " is not equaled to computed signature: " + computedSignature);
657658
}

‎server/src/com/cloud/user/AccountManagerImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.apache.log4j.Logger;
5858

5959
import com.cloud.api.ApiDBUtils;
60+
import com.cloud.api.ConstantTimeComparator;
6061
import com.cloud.api.query.vo.ControlledViewEntity;
6162
import com.cloud.configuration.Config;
6263
import com.cloud.configuration.ConfigurationManager;
@@ -381,7 +382,6 @@ public void checkAccess(Account caller, Domain domain) throws PermissionDeniedEx
381382

382383
@Override
383384
public void checkAccess(Account caller, AccessType accessType, boolean sameOwner, ControlledEntity... entities) {
384-
385385
//check for the same owner
386386
Long ownerId = null;
387387
ControlledEntity prevEntity = null;
@@ -1918,7 +1918,7 @@ public UserAccount authenticateUser(String username, String password, Long domai
19181918
mac.update(unsignedRequest.getBytes());
19191919
byte[] encryptedBytes = mac.doFinal();
19201920
String computedSignature = new String(Base64.encodeBase64(encryptedBytes));
1921-
boolean equalSig = signature.equals(computedSignature);
1921+
boolean equalSig = ConstantTimeComparator.compareStrings(signature, computedSignature);
19221922
if (!equalSig) {
19231923
s_logger.info("User signature: " + signature + " is not equaled to computed signature: " + computedSignature);
19241924
} else {

0 commit comments

Comments
 (0)