Skip to content

Commit 1cd0681

Browse files
wujinhuhuiguangjun
authored andcommitted
support sm4 encryption (#311)
support sm4 encryption
1 parent 0f14b81 commit 1cd0681

File tree

12 files changed

+783
-104
lines changed

12 files changed

+783
-104
lines changed

src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,9 @@ public byte[] marshall(SetBucketEncryptionRequest setBucketEncryptionRequest) {
937937
} else {
938938
xmlBody.append("<KMSMasterKeyID></KMSMasterKeyID>");
939939
}
940+
if (sseByDefault.getKMSDataEncryption() != null) {
941+
xmlBody.append("<KMSDataEncryption>" + sseByDefault.getKMSDataEncryption() + "</KMSDataEncryption>");
942+
}
940943

941944
xmlBody.append("</ApplyServerSideEncryptionByDefault>");
942945
xmlBody.append("</ServerSideEncryptionRule>");

src/main/java/com/aliyun/oss/internal/OSSHeaders.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public interface OSSHeaders extends HttpHeaders {
3232

3333
static final String OSS_SERVER_SIDE_ENCRYPTION = "x-oss-server-side-encryption";
3434
static final String OSS_SERVER_SIDE_ENCRYPTION_KEY_ID = "x-oss-server-side-encryption-key-id";
35+
static final String OSS_SERVER_SIDE_DATA_ENCRYPTION = "x-oss-server-side-data-encryption";
3536

3637
static final String GET_OBJECT_IF_MODIFIED_SINCE = "If-Modified-Since";
3738
static final String GET_OBJECT_IF_UNMODIFIED_SINCE = "If-Unmodified-Since";

src/main/java/com/aliyun/oss/internal/OSSMultipartOperation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws O
363363
result.setETag(trimQuotes(response.getHeaders().get(OSSHeaders.ETAG)));
364364
result.setRequestId(response.getRequestId());
365365
result.setPartSize(uploadPartRequest.getPartSize());
366+
result.setResponse(response);
366367
ResponseParsers.setCRC(result, response);
367368

368369
if (getInnerClient().getClientConfiguration().isCrcCheckEnabled()) {

src/main/java/com/aliyun/oss/internal/ResponseParsers.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ public AppendObjectResult parse(ResponseMessage response) throws ResponseParseEx
808808
result.setNextPosition(Long.valueOf(nextPosition));
809809
}
810810
result.setObjectCRC(response.getHeaders().get(OSSHeaders.OSS_HASH_CRC64_ECMA));
811+
result.setResponse(response);
811812
setCRC(result, response);
812813
return result;
813814
} finally {
@@ -941,6 +942,7 @@ public CopyObjectResult parse(ResponseMessage response) throws ResponseParseExce
941942
CopyObjectResult result = parseCopyObjectResult(response.getContent());
942943
result.setVersionId(response.getHeaders().get(OSSHeaders.OSS_HEADER_VERSION_ID));
943944
result.setRequestId(response.getRequestId());
945+
result.setResponse(response);
944946
return result;
945947
} finally {
946948
safeCloseResponse(response);
@@ -1036,6 +1038,7 @@ public InitiateMultipartUploadResult parse(ResponseMessage response) throws Resp
10361038
try {
10371039
InitiateMultipartUploadResult result = parseInitiateMultipartUpload(response.getContent());
10381040
result.setRequestId(response.getRequestId());
1041+
result.setResponse(response);
10391042
return result;
10401043
} finally {
10411044
safeCloseResponse(response);
@@ -1089,6 +1092,7 @@ public UploadPartCopyResult parse(ResponseMessage response) throws ResponseParse
10891092
result.setPartNumber(partNumber);
10901093
result.setETag(trimQuotes(parseUploadPartCopy(response.getContent())));
10911094
result.setRequestId(response.getRequestId());
1095+
result.setResponse(response);
10921096
return result;
10931097
} finally {
10941098
safeCloseResponse(response);
@@ -2424,6 +2428,9 @@ public static BucketInfo parseGetBucketInfo(InputStream responseBody) throws Res
24242428
if (sseElem.getChild("KMSMasterKeyID") != null) {
24252429
applyServerSideEncryptionByDefault.setKMSMasterKeyID(sseElem.getChildText("KMSMasterKeyID"));
24262430
}
2431+
if (sseElem.getChild("KMSDataEncryption") != null) {
2432+
applyServerSideEncryptionByDefault.setKMSDataEncryption(sseElem.getChildText("KMSDataEncryption"));
2433+
}
24272434
serverSideEncryptionConfiguration
24282435
.setApplyServerSideEncryptionByDefault(applyServerSideEncryptionByDefault);
24292436

@@ -2721,6 +2728,9 @@ public static ServerSideEncryptionConfiguration parseGetBucketEncryption(InputSt
27212728
Element sseElem = root.getChild("ApplyServerSideEncryptionByDefault");
27222729
sseByDefault.setSSEAlgorithm(sseElem.getChildText("SSEAlgorithm"));
27232730
sseByDefault.setKMSMasterKeyID(sseElem.getChildText("KMSMasterKeyID"));
2731+
if (sseElem.getChild("KMSDataEncryption") != null) {
2732+
sseByDefault.setKMSDataEncryption(sseElem.getChildText("KMSDataEncryption"));
2733+
}
27242734
configuration.setApplyServerSideEncryptionByDefault(sseByDefault);
27252735

27262736
return configuration;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.aliyun.oss.model;
2+
3+
/**
4+
* Server-side Data Encryption Algorithm.
5+
*/
6+
public enum DataEncryptionAlgorithm {
7+
SM4("SM4");
8+
9+
private final String algorithm;
10+
11+
public String getAlgorithm() {
12+
return algorithm;
13+
}
14+
15+
private DataEncryptionAlgorithm(String algorithm) {
16+
this.algorithm = algorithm;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return algorithm;
22+
}
23+
24+
public static DataEncryptionAlgorithm fromString(String algorithm) {
25+
if (algorithm == null)
26+
return null;
27+
for (DataEncryptionAlgorithm e: values()) {
28+
if (e.getAlgorithm().equals(algorithm))
29+
return e;
30+
}
31+
throw new IllegalArgumentException("Unsupported data encryption algorithm " + algorithm);
32+
}
33+
}

src/main/java/com/aliyun/oss/model/ObjectMetadata.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,25 @@ public void setServerSideEncryptionKeyId(String serverSideEncryptionKeyId) {
320320
metadata.put(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION_KEY_ID, serverSideEncryptionKeyId);
321321
}
322322

323+
/**
324+
* Sets the object's server side data encryption.
325+
*
326+
* @param serverSideDataEncryption
327+
* The server side data encryption.
328+
*/
329+
public void setServerSideDataEncryption(String serverSideDataEncryption) {
330+
metadata.put(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION, serverSideDataEncryption);
331+
}
332+
333+
/**
334+
* Gets the object's server side data encryption.
335+
*
336+
* @return The server side data encryption. Null means no data encryption.
337+
*/
338+
public String getServerSideDataEncryption() {
339+
return (String) metadata.get(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION);
340+
}
341+
323342
/**
324343
* Gets the object's storage type, which only supports "normal" and
325344
* "appendable" for now.

src/main/java/com/aliyun/oss/model/SSEAlgorithm.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public enum SSEAlgorithm {
2626
AES256("AES256"),
2727
KMS("KMS"),
28+
SM4("SM4"),
2829
;
2930

3031
private final String algorithm;

src/main/java/com/aliyun/oss/model/ServerSideEncryptionByDefault.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class ServerSideEncryptionByDefault {
2727

2828
private String sseAlgorithm;
2929
private String kmsMasterKeyID;
30-
30+
private String kmsDataEncryption;
3131
/**
3232
* Creates a default instance.
3333
*
@@ -126,5 +126,31 @@ public ServerSideEncryptionByDefault withKMSMasterKeyID(String kmsMasterKeyID) {
126126
setKMSMasterKeyID(kmsMasterKeyID);
127127
return this;
128128
}
129-
129+
130+
/**
131+
* @return This parameter is allowed if SSEAlgorithm is kms.
132+
*/
133+
public String getKMSDataEncryption() {
134+
return kmsDataEncryption;
135+
}
136+
137+
/**
138+
* Sets the KMS data encryption. This parameter is allowed if SSEAlgorithm is kms.
139+
*
140+
* @param kmsDataEncryption KMS data encryption to use.
141+
*/
142+
public void setKMSDataEncryption(String kmsDataEncryption) {
143+
this.kmsDataEncryption = kmsDataEncryption;
144+
}
145+
146+
/**
147+
* Sets the KMS data encryption. This parameter is allowed if SSEAlgorithm is kms.
148+
*
149+
* @param kmsDataEncryption KMS data encryption to use.
150+
* @return This object for method chaining.
151+
*/
152+
public ServerSideEncryptionByDefault withKMSDataEncryption(String kmsDataEncryption) {
153+
setKMSDataEncryption(kmsDataEncryption);
154+
return this;
155+
}
130156
}

src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3852,4 +3852,101 @@ public void testparseListBucketInventoryConfigurations() {
38523852
Assert.assertTrue(false);
38533853
}
38543854
}
3855+
3856+
@Test
3857+
public void testGetBucketEncryptionResponseParser() {
3858+
InputStream instream = null;
3859+
String respBody;
3860+
3861+
respBody = "" +
3862+
"<ServerSideEncryptionRule>\n" +
3863+
" <ApplyServerSideEncryptionByDefault>\n" +
3864+
" <SSEAlgorithm>KMS</SSEAlgorithm>\n" +
3865+
" <KMSMasterKeyID>id</KMSMasterKeyID>\n" +
3866+
" <KMSDataEncryption>SM4</KMSDataEncryption>\n" +
3867+
" </ApplyServerSideEncryptionByDefault>\n" +
3868+
"</ServerSideEncryptionRule>";
3869+
3870+
try {
3871+
instream = new ByteArrayInputStream(respBody.getBytes("utf-8"));
3872+
} catch (UnsupportedEncodingException e) {
3873+
Assert.fail("UnsupportedEncodingException");
3874+
}
3875+
3876+
try {
3877+
ResponseMessage response = new ResponseMessage(null);
3878+
response.setContent(instream);
3879+
ResponseParsers.GetBucketEncryptionResponseParser parser = new ResponseParsers.GetBucketEncryptionResponseParser();
3880+
ServerSideEncryptionConfiguration config = parser.parse(response);
3881+
Assert.assertEquals(config.getApplyServerSideEncryptionByDefault().getSSEAlgorithm(), "KMS");
3882+
Assert.assertEquals(config.getApplyServerSideEncryptionByDefault().getKMSMasterKeyID(), "id");
3883+
Assert.assertEquals(config.getApplyServerSideEncryptionByDefault().getKMSDataEncryption(), "SM4");
3884+
Assert.assertTrue(true);
3885+
} catch (ResponseParseException e) {
3886+
Assert.assertTrue(false);
3887+
} catch (Exception e) {
3888+
Assert.assertTrue(false);
3889+
}
3890+
3891+
respBody = "" +
3892+
"<ServerSideEncryptionRule>\n" +
3893+
" <ApplyServerSideEncryptionByDefault>\n" +
3894+
" <SSEAlgorithm>KMS</SSEAlgorithm>\n" +
3895+
" <KMSMasterKeyID>id</KMSMasterKeyID>\n" +
3896+
" </ApplyServerSideEncryptionByDefault>\n" +
3897+
"</ServerSideEncryptionRule>";
3898+
3899+
try {
3900+
instream = new ByteArrayInputStream(respBody.getBytes("utf-8"));
3901+
} catch (UnsupportedEncodingException e) {
3902+
Assert.fail("UnsupportedEncodingException");
3903+
}
3904+
3905+
try {
3906+
ResponseMessage response = new ResponseMessage(null);
3907+
response.setContent(instream);
3908+
ResponseParsers.GetBucketEncryptionResponseParser parser = new ResponseParsers.GetBucketEncryptionResponseParser();
3909+
ServerSideEncryptionConfiguration config = parser.parse(response);
3910+
Assert.assertEquals(config.getApplyServerSideEncryptionByDefault().getSSEAlgorithm(), "KMS");
3911+
Assert.assertEquals(config.getApplyServerSideEncryptionByDefault().getKMSMasterKeyID(), "id");
3912+
Assert.assertEquals(config.getApplyServerSideEncryptionByDefault().getKMSDataEncryption(), null);
3913+
Assert.assertTrue(true);
3914+
} catch (ResponseParseException e) {
3915+
Assert.assertTrue(false);
3916+
} catch (Exception e) {
3917+
Assert.assertTrue(false);
3918+
}
3919+
3920+
respBody = "invalid";
3921+
3922+
try {
3923+
instream = new ByteArrayInputStream(respBody.getBytes("utf-8"));
3924+
} catch (UnsupportedEncodingException e) {
3925+
Assert.fail("UnsupportedEncodingException");
3926+
}
3927+
3928+
try {
3929+
ResponseMessage response = new ResponseMessage(null);
3930+
response.setContent(instream);
3931+
ResponseParsers.GetBucketEncryptionResponseParser parser = new ResponseParsers.GetBucketEncryptionResponseParser();
3932+
parser.parse(response);
3933+
Assert.assertTrue(false);
3934+
} catch (ResponseParseException e) {
3935+
Assert.assertTrue(true);
3936+
} catch (Exception e) {
3937+
Assert.assertTrue(false);
3938+
}
3939+
3940+
try {
3941+
ResponseMessage response = new ResponseMessage(null);
3942+
response.setContent(null);
3943+
ResponseParsers.GetBucketEncryptionResponseParser parser = new ResponseParsers.GetBucketEncryptionResponseParser();
3944+
parser.parse(response);
3945+
Assert.assertTrue(false);
3946+
} catch (ResponseParseException e) {
3947+
Assert.assertTrue(true);
3948+
} catch (Exception e) {
3949+
Assert.assertTrue(false);
3950+
}
3951+
}
38553952
}

0 commit comments

Comments
 (0)