Skip to content

support sm4 encryption #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,9 @@ public byte[] marshall(SetBucketEncryptionRequest setBucketEncryptionRequest) {
} else {
xmlBody.append("<KMSMasterKeyID></KMSMasterKeyID>");
}
if (sseByDefault.getKMSDataEncryption() != null) {
xmlBody.append("<KMSDataEncryption>" + sseByDefault.getKMSDataEncryption() + "</KMSDataEncryption>");
}

xmlBody.append("</ApplyServerSideEncryptionByDefault>");
xmlBody.append("</ServerSideEncryptionRule>");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/aliyun/oss/internal/OSSHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface OSSHeaders extends HttpHeaders {

static final String OSS_SERVER_SIDE_ENCRYPTION = "x-oss-server-side-encryption";
static final String OSS_SERVER_SIDE_ENCRYPTION_KEY_ID = "x-oss-server-side-encryption-key-id";
static final String OSS_SERVER_SIDE_DATA_ENCRYPTION = "x-oss-server-side-data-encryption";

static final String GET_OBJECT_IF_MODIFIED_SINCE = "If-Modified-Since";
static final String GET_OBJECT_IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws O
result.setETag(trimQuotes(response.getHeaders().get(OSSHeaders.ETAG)));
result.setRequestId(response.getRequestId());
result.setPartSize(uploadPartRequest.getPartSize());
result.setResponse(response);
ResponseParsers.setCRC(result, response);

if (getInnerClient().getClientConfiguration().isCrcCheckEnabled()) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/aliyun/oss/internal/ResponseParsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ public AppendObjectResult parse(ResponseMessage response) throws ResponseParseEx
result.setNextPosition(Long.valueOf(nextPosition));
}
result.setObjectCRC(response.getHeaders().get(OSSHeaders.OSS_HASH_CRC64_ECMA));
result.setResponse(response);
setCRC(result, response);
return result;
} finally {
Expand Down Expand Up @@ -904,6 +905,7 @@ public CopyObjectResult parse(ResponseMessage response) throws ResponseParseExce
CopyObjectResult result = parseCopyObjectResult(response.getContent());
result.setVersionId(response.getHeaders().get(OSSHeaders.OSS_HEADER_VERSION_ID));
result.setRequestId(response.getRequestId());
result.setResponse(response);
return result;
} finally {
safeCloseResponse(response);
Expand Down Expand Up @@ -999,6 +1001,7 @@ public InitiateMultipartUploadResult parse(ResponseMessage response) throws Resp
try {
InitiateMultipartUploadResult result = parseInitiateMultipartUpload(response.getContent());
result.setRequestId(response.getRequestId());
result.setResponse(response);
return result;
} finally {
safeCloseResponse(response);
Expand Down Expand Up @@ -1052,6 +1055,7 @@ public UploadPartCopyResult parse(ResponseMessage response) throws ResponseParse
result.setPartNumber(partNumber);
result.setETag(trimQuotes(parseUploadPartCopy(response.getContent())));
result.setRequestId(response.getRequestId());
result.setResponse(response);
return result;
} finally {
safeCloseResponse(response);
Expand Down Expand Up @@ -2382,6 +2386,9 @@ public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws Res
if (sseElem.getChild("KMSMasterKeyID") != null) {
applyServerSideEncryptionByDefault.setKMSMasterKeyID(sseElem.getChildText("KMSMasterKeyID"));
}
if (sseElem.getChild("KMSDataEncryption") != null) {
applyServerSideEncryptionByDefault.setKMSDataEncryption(sseElem.getChildText("KMSDataEncryption"));
}
serverSideEncryptionConfiguration
.setApplyServerSideEncryptionByDefault(applyServerSideEncryptionByDefault);

Expand Down Expand Up @@ -2679,6 +2686,7 @@ public static ServerSideEncryptionConfiguration parseGetBucketEncryption(InputSt
Element sseElem = root.getChild("ApplyServerSideEncryptionByDefault");
sseByDefault.setSSEAlgorithm(sseElem.getChildText("SSEAlgorithm"));
sseByDefault.setKMSMasterKeyID(sseElem.getChildText("KMSMasterKeyID"));
sseByDefault.setKMSDataEncryption(sseElem.getChildText("KMSDataEncryption"));
configuration.setApplyServerSideEncryptionByDefault(sseByDefault);

return configuration;
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/aliyun/oss/model/DataEncryptionAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.aliyun.oss.model;

/**
* Server-side Data Encryption Algorithm.
*/
public enum DataEncryptionAlgorithm {
SM4("SM4");

private final String algorithm;

public String getAlgorithm() {
return algorithm;
}

private DataEncryptionAlgorithm(String algorithm) {
this.algorithm = algorithm;
}

@Override
public String toString() {
return algorithm;
}

public static DataEncryptionAlgorithm fromString(String algorithm) {
if (algorithm == null)
return null;
for (DataEncryptionAlgorithm e: values()) {
if (e.getAlgorithm().equals(algorithm))
return e;
}
throw new IllegalArgumentException("Unsupported data encryption algorithm " + algorithm);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/aliyun/oss/model/ObjectMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,25 @@ public void setServerSideEncryptionKeyId(String serverSideEncryptionKeyId) {
metadata.put(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION_KEY_ID, serverSideEncryptionKeyId);
}

/**
* Sets the object's server side data encryption.
*
* @param serverSideDataEncryption
* The server side data encryption.
*/
public void setServerSideDataEncryption(String serverSideDataEncryption) {
metadata.put(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION, serverSideDataEncryption);
}

/**
* Gets the object's server side data encryption.
*
* @return The server side data encryption. Null means no data encryption.
*/
public String getServerSideDataEncryption() {
return (String) metadata.get(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION);
}

/**
* Gets the object's storage type, which only supports "normal" and
* "appendable" for now.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/aliyun/oss/model/SSEAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public enum SSEAlgorithm {
AES256("AES256"),
KMS("KMS"),
SM4("SM4"),
;

private final String algorithm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ServerSideEncryptionByDefault {

private String sseAlgorithm;
private String kmsMasterKeyID;

private String kmsDataEncryption;
/**
* Creates a default instance.
*
Expand Down Expand Up @@ -126,5 +126,31 @@ public ServerSideEncryptionByDefault withKMSMasterKeyID(String kmsMasterKeyID) {
setKMSMasterKeyID(kmsMasterKeyID);
return this;
}


/**
* @return This parameter is allowed if SSEAlgorithm is kms.
*/
public String getKMSDataEncryption() {
return kmsDataEncryption;
}

/**
* Sets the KMS data encryption. This parameter is allowed if SSEAlgorithm is kms.
*
* @param kmsDataEncryption KMS data encryption to use.
*/
public void setKMSDataEncryption(String kmsDataEncryption) {
this.kmsDataEncryption = kmsDataEncryption;
}

/**
* Sets the KMS data encryption. This parameter is allowed if SSEAlgorithm is kms.
*
* @param kmsDataEncryption KMS data encryption to use.
* @return This object for method chaining.
*/
public ServerSideEncryptionByDefault withKMSDataEncryption(String kmsDataEncryption) {
setKMSDataEncryption(kmsDataEncryption);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@

package com.aliyun.oss.integrationtests;

import com.aliyun.oss.internal.OSSHeaders;
import com.aliyun.oss.model.*;
import junit.framework.Assert;

import org.junit.Test;

import static com.aliyun.oss.integrationtests.TestUtils.waitForCacheExpiration;

import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.BucketInfo;
import com.aliyun.oss.model.SSEAlgorithm;
import com.aliyun.oss.model.ServerSideEncryptionByDefault;
import com.aliyun.oss.model.ServerSideEncryptionConfiguration;
import com.aliyun.oss.model.SetBucketEncryptionRequest;

import java.io.File;
import java.util.Map;

public class BucketEncryptionTest extends TestBase {

@Test
public void testSetBucketEncryption() {
private void testSetBucketEncryptionInternal(SSEAlgorithm algorithm, DataEncryptionAlgorithm dataEncryptionAlgorithm) {

try {
// set
ServerSideEncryptionByDefault applyServerSideEncryptionByDefault =
new ServerSideEncryptionByDefault(SSEAlgorithm.AES256.toString());
new ServerSideEncryptionByDefault(algorithm.toString());
if (algorithm == SSEAlgorithm.KMS && dataEncryptionAlgorithm != null) {
applyServerSideEncryptionByDefault.setKMSDataEncryption(dataEncryptionAlgorithm.toString());
}
ServerSideEncryptionConfiguration setConfiguration = new ServerSideEncryptionConfiguration();
setConfiguration.setApplyServerSideEncryptionByDefault(applyServerSideEncryptionByDefault);
SetBucketEncryptionRequest setRequest = new SetBucketEncryptionRequest(bucketName, setConfiguration);
Expand All @@ -49,23 +51,45 @@ public void testSetBucketEncryption() {

// get
ServerSideEncryptionConfiguration getConfiguration = ossClient.getBucketEncryption(bucketName);
Assert.assertEquals(SSEAlgorithm.AES256.toString(),
Assert.assertEquals(algorithm.toString(),
getConfiguration.getApplyServerSideEncryptionByDefault().getSSEAlgorithm());
Assert.assertNull(getConfiguration.getApplyServerSideEncryptionByDefault().getKMSMasterKeyID());
Assert.assertEquals(dataEncryptionAlgorithm,
DataEncryptionAlgorithm.fromString(getConfiguration.getApplyServerSideEncryptionByDefault().getKMSDataEncryption()));
String fileName = TestUtils.genFixedLengthFile(1024);
String objectName = "encryption-" + TestUtils.genRandomString(10);
ossClient.putObject(bucketName, objectName, new File(fileName));

Map<String, String> headers = ossClient.getObject(bucketName, objectName).getResponse().getHeaders();
Assert.assertEquals(algorithm.toString(), headers.get(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION));
if (algorithm == SSEAlgorithm.KMS && dataEncryptionAlgorithm != null) {
Assert.assertEquals(dataEncryptionAlgorithm.toString(), headers.get(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION));
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}

@Test
public void testDeleteBucketEncryption() {
public void testSetBucketEncryption() {
testSetBucketEncryptionInternal(SSEAlgorithm.AES256, null);
testSetBucketEncryptionInternal(SSEAlgorithm.SM4, null);
testSetBucketEncryptionInternal(SSEAlgorithm.KMS, null);
testSetBucketEncryptionInternal(SSEAlgorithm.KMS, DataEncryptionAlgorithm.SM4);
}

private void testDeleteBucketEncryptionInternal(SSEAlgorithm algorithm, DataEncryptionAlgorithm dataEncryptionAlgorithm) {

try {
// set
ServerSideEncryptionByDefault applyServerSideEncryptionByDefault =
new ServerSideEncryptionByDefault().withSSEAlgorithm(SSEAlgorithm.KMS);
applyServerSideEncryptionByDefault.setKMSMasterKeyID("test-kms-master-key-id");
new ServerSideEncryptionByDefault().withSSEAlgorithm(algorithm);
if (algorithm == SSEAlgorithm.KMS)
applyServerSideEncryptionByDefault.setKMSMasterKeyID("test-kms-master-key-id");
if (algorithm == SSEAlgorithm.KMS && dataEncryptionAlgorithm != null) {
applyServerSideEncryptionByDefault.setKMSDataEncryption(dataEncryptionAlgorithm.toString());
}
ServerSideEncryptionConfiguration setConfiguration = new ServerSideEncryptionConfiguration()
.withApplyServerSideEncryptionByDefault(applyServerSideEncryptionByDefault);
setConfiguration.setApplyServerSideEncryptionByDefault(applyServerSideEncryptionByDefault);
Expand All @@ -76,11 +100,13 @@ public void testDeleteBucketEncryption() {

// get
ServerSideEncryptionConfiguration getConfiguration = ossClient.getBucketEncryption(bucketName);
Assert.assertEquals(SSEAlgorithm.KMS.toString(),
Assert.assertEquals(algorithm.toString(),
getConfiguration.getApplyServerSideEncryptionByDefault().getSSEAlgorithm());
Assert.assertEquals("test-kms-master-key-id",
getConfiguration.getApplyServerSideEncryptionByDefault().getKMSMasterKeyID());

if (algorithm == SSEAlgorithm.KMS)
Assert.assertEquals("test-kms-master-key-id",
getConfiguration.getApplyServerSideEncryptionByDefault().getKMSMasterKeyID());
Assert.assertEquals(dataEncryptionAlgorithm,
DataEncryptionAlgorithm.fromString(getConfiguration.getApplyServerSideEncryptionByDefault().getKMSDataEncryption()));
// delete
ossClient.deleteBucketEncryption(bucketName);
waitForCacheExpiration(3);
Expand All @@ -98,12 +124,22 @@ public void testDeleteBucketEncryption() {
}

@Test
public void testBucketInfo() {
public void testDeleteBucketEncryption() {
testDeleteBucketEncryptionInternal(SSEAlgorithm.AES256, null);
testDeleteBucketEncryptionInternal(SSEAlgorithm.SM4, null);
testDeleteBucketEncryptionInternal(SSEAlgorithm.KMS, null);
testDeleteBucketEncryptionInternal(SSEAlgorithm.KMS, DataEncryptionAlgorithm.SM4);
}

public void testBucketInfoInternal(SSEAlgorithm algorithm, DataEncryptionAlgorithm dataEncryptionAlgorithm) {

try {
// set 1
ServerSideEncryptionByDefault applyServerSideEncryptionByDefault =
new ServerSideEncryptionByDefault(SSEAlgorithm.AES256);
new ServerSideEncryptionByDefault(algorithm);
if (algorithm == SSEAlgorithm.KMS && dataEncryptionAlgorithm != null) {
applyServerSideEncryptionByDefault.setKMSDataEncryption(dataEncryptionAlgorithm.toString());
}
ServerSideEncryptionConfiguration setConfiguration = new ServerSideEncryptionConfiguration();
setConfiguration.setApplyServerSideEncryptionByDefault(applyServerSideEncryptionByDefault);
SetBucketEncryptionRequest setRequest = new SetBucketEncryptionRequest(bucketName, setConfiguration);
Expand All @@ -112,10 +148,14 @@ public void testBucketInfo() {

// get
BucketInfo bucketInfo = ossClient.getBucketInfo(bucketName);
Assert.assertEquals(SSEAlgorithm.AES256.toString(), bucketInfo.getServerSideEncryptionConfiguration()
Assert.assertEquals(algorithm.toString(), bucketInfo.getServerSideEncryptionConfiguration()
.getApplyServerSideEncryptionByDefault().getSSEAlgorithm());
Assert.assertNull(bucketInfo.getServerSideEncryptionConfiguration()
if (algorithm != SSEAlgorithm.KMS)
Assert.assertNull(bucketInfo.getServerSideEncryptionConfiguration()
.getApplyServerSideEncryptionByDefault().getKMSMasterKeyID());
Assert.assertEquals(dataEncryptionAlgorithm,
DataEncryptionAlgorithm.fromString(bucketInfo.getServerSideEncryptionConfiguration()
.getApplyServerSideEncryptionByDefault().getKMSDataEncryption()));

// delete
ossClient.deleteBucketEncryption(bucketName);
Expand Down Expand Up @@ -151,4 +191,11 @@ public void testBucketInfo() {
}
}

@Test
public void testBucketInfo() {
testBucketInfoInternal(SSEAlgorithm.AES256, null);
testBucketInfoInternal(SSEAlgorithm.SM4, null);
testBucketInfoInternal(SSEAlgorithm.KMS, null);
testBucketInfoInternal(SSEAlgorithm.KMS, DataEncryptionAlgorithm.SM4);
}
}
Loading