Skip to content

Commit 97e9f8b

Browse files
authored
Merge pull request #12 from Cosmian/fix/kmip_key_value
KeyValue structure changed: is not a choice anymore
2 parents 3654863 + f71bfa2 commit 97e9f8b

11 files changed

Lines changed: 87 additions & 109 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
---
6+
## [0.7.7] - 2022-07-28
7+
### Added
8+
### Changed
9+
### Fixed
10+
- KMIP KeyValue structure has changed in KMS from version 2.0.5
11+
### Removed
12+
---
13+

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This library free software and is available on Maven Central
3131
<dependency>
3232
<groupId>com.cosmian</groupId>
3333
<artifactId>cosmian_java_lib</artifactId>
34-
<version>0.7.6</version>
34+
<version>0.7.7</version>
3535
</dependency>
3636
```
3737

@@ -51,7 +51,7 @@ KMS Server | Java Lib | GPSW lib | CoverCrypt lib | Findex
5151
1.2.1 | 0.6.1 | 0.6.1 | N/A | N/A
5252
2.0.1 | 0.7.2 | 0.6.5 | 1.0.2 | N/A
5353
2.0.2 | 0.7.5 | 0.6.10 | 2.0.0 | N/A
54-
2.1.0 | 0.7.6 | 1.1.1 | 4.0.0 | 0.2.3
54+
2.1.0 | 0.7.7 | 1.1.1 | 4.0.0 | 0.2.3
5555

5656
## Update native libraries
5757

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.cosmian</groupId>
55
<artifactId>cosmian_java_lib</artifactId>
6-
<version>0.7.6</version>
6+
<version>0.7.7</version>
77

88
<name>cosmian_java_lib</name>
99
<description>The Cosmian Java Lib provides local encryption/decyption and access to the Cosmian public platform APIs</description>

src/main/java/com/cosmian/rest/kmip/KmipUtils.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.cosmian.CosmianException;
44
import com.cosmian.rest.kmip.data_structures.KeyBlock;
5+
import com.cosmian.rest.kmip.data_structures.KeyValue;
56
import com.cosmian.rest.kmip.data_structures.KeyWrappingData;
6-
import com.cosmian.rest.kmip.data_structures.PlainTextKeyValue;
77
import com.cosmian.rest.kmip.data_structures.TransparentSymmetricKey;
88

99
public class KmipUtils {
@@ -16,13 +16,8 @@ public class KmipUtils {
1616
* @throws CosmianException if the {@link KeyBlock} is malformed the bytes cannot be found
1717
*/
1818
public static byte[] bytesFromKeyBlock(KeyBlock keyBlock) throws CosmianException {
19-
Object keyValueContent = keyBlock.getKeyValue().get();
20-
if (keyValueContent instanceof byte[]) {
21-
return (byte[]) keyValueContent;
22-
} else if (!(keyValueContent instanceof PlainTextKeyValue)) {
23-
throw new CosmianException("Unknown KeyValue type: " + keyValueContent.getClass().getName());
24-
}
25-
Object keyMaterialContent = ((PlainTextKeyValue) keyValueContent).getKeyMaterial().get();
19+
Object keyValueContent = keyBlock.getKeyValue();
20+
Object keyMaterialContent = ((KeyValue) keyValueContent).getKeyMaterial().get();
2621
byte[] bytes;
2722
if (keyMaterialContent instanceof byte[]) {
2823
bytes = (byte[]) keyMaterialContent;
@@ -41,7 +36,7 @@ public static byte[] bytesFromKeyBlock(KeyBlock keyBlock) throws CosmianExceptio
4136
*/
4237
/**
4338
* Extract the nonce/iv/counter bytes from a {@link KeyWrappingData} for those made of byte arrays
44-
*
39+
*
4540
* @param keyWrappingData the {@link KeyWrappingData}
4641
* @return the bytes of the Nonce
4742
* @throws CosmianException if no Noce is available

src/main/java/com/cosmian/rest/kmip/data_structures/KeyBlock.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.cosmian.rest.kmip.types.KeyFormatType;
1111
import com.cosmian.rest.kmip.types.ObjectType;
1212
import com.fasterxml.jackson.annotation.JsonProperty;
13-
1413
/**
1514
* A Key Block object is a structure used to encapsulate all of the information that is closely associated with a
1615
* cryptographic key.
@@ -37,14 +36,14 @@ public class KeyBlock implements KmipStruct {
3736

3837
/**
3938
* Return {@link KeyBlock} {@link Attributes} or a set of empty {@link Attributes} for the given {@link ObjectType}
40-
*
39+
*
4140
* @param objectType the {@link ObjectType}
4241
* @return the {@link Attributes}
4342
*/
4443
public Attributes attributes(ObjectType objectType) {
45-
Object kv = this.keyValue.get();
46-
if (kv instanceof PlainTextKeyValue) {
47-
Optional<Attributes> oa = ((PlainTextKeyValue) kv).getAttributes();
44+
Object kv = this.keyValue;
45+
if (kv instanceof KeyValue) {
46+
Optional<Attributes> oa = ((KeyValue) kv).getAttributes();
4847
if (oa.isPresent()) {
4948
return oa.get();
5049
}
Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.cosmian.rest.kmip.data_structures;
22

3-
import com.cosmian.rest.kmip.json.KmipChoice2;
3+
import java.util.Objects;
4+
import java.util.Optional;
5+
6+
import com.cosmian.rest.kmip.json.KmipStruct;
7+
import com.cosmian.rest.kmip.types.Attributes;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
49

510
/**
611
* The Key Value is used only inside a Key Block and is either a Byte String or a: • The Key Value structure contains
@@ -10,25 +15,67 @@
1015
* encapsulated with (and possibly wrapped with) the key material itself. • The Key Value Byte String is either the
1116
* wrapped TTLV-encoded Key Value structure, or the wrapped un-encoded value of the Byte String Key Material field.
1217
*/
13-
public class KeyValue extends KmipChoice2<PlainTextKeyValue, byte[]> {
18+
public class KeyValue implements KmipStruct {
19+
20+
@JsonProperty(value = "KeyMaterial")
21+
private KeyMaterial keyMaterial;
22+
23+
@JsonProperty(value = "Attributes")
24+
private Optional<Attributes> attributes;
25+
26+
public KeyValue() {
27+
}
28+
29+
public KeyValue(KeyMaterial keyMaterial, Optional<Attributes> attributes) {
30+
this.keyMaterial = keyMaterial;
31+
this.attributes = attributes;
32+
}
33+
34+
public KeyMaterial getKeyMaterial() {
35+
return this.keyMaterial;
36+
}
37+
38+
public void setKeyMaterial(KeyMaterial keyMaterial) {
39+
this.keyMaterial = keyMaterial;
40+
}
41+
42+
public Optional<Attributes> getAttributes() {
43+
return this.attributes;
44+
}
45+
46+
public void setAttributes(Optional<Attributes> attributes) {
47+
this.attributes = attributes;
48+
}
49+
50+
public KeyValue keyMaterial(KeyMaterial keyMaterial) {
51+
setKeyMaterial(keyMaterial);
52+
return this;
53+
}
1454

15-
public KeyValue(Object value) {
16-
super(value);
55+
public KeyValue attributes(Optional<Attributes> attributes) {
56+
setAttributes(attributes);
57+
return this;
1758
}
1859

1960
@Override
2061
public boolean equals(Object o) {
21-
return super.equals(o);
62+
if (o == this)
63+
return true;
64+
if (!(o instanceof KeyValue)) {
65+
return false;
66+
}
67+
KeyValue keyValue = (KeyValue) o;
68+
return Objects.equals(keyMaterial, keyValue.keyMaterial) && Objects.equals(attributes, keyValue.attributes);
2269
}
2370

2471
@Override
2572
public int hashCode() {
26-
return super.hashCode();
73+
return Objects.hash(keyMaterial, attributes);
2774
}
2875

2976
@Override
3077
public String toString() {
31-
return super.toString();
78+
return "{" + " keyMaterial='" + getKeyMaterial() + "'" + ", attributes='" + getAttributes() + "'" + "}";
3279
}
3380

3481
}

src/main/java/com/cosmian/rest/kmip/data_structures/PlainTextKeyValue.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/test/java/com/cosmian/TestCoverCrypt.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.cosmian.rest.abe.access_policy.Attr;
2222
import com.cosmian.rest.abe.access_policy.Or;
2323
import com.cosmian.rest.abe.policy.Policy;
24-
import com.cosmian.rest.kmip.data_structures.PlainTextKeyValue;
24+
import com.cosmian.rest.kmip.data_structures.KeyValue;
2525
import com.cosmian.rest.kmip.objects.PrivateKey;
2626
import com.cosmian.rest.kmip.objects.PublicKey;
2727
import com.cosmian.rest.kmip.types.Attributes;
@@ -137,8 +137,8 @@ public void test_keys_import_export() throws Exception {
137137
PrivateKey userDecryptionKey = abe.retrieveUserDecryptionKey(userDecryptionKeyUniqueIdentifier);
138138
assertEquals(KeyFormatType.CoverCryptSecretKey, userDecryptionKey.getKeyBlock().getKeyFormatType());
139139
assertEquals(CryptographicAlgorithm.CoverCrypt, userDecryptionKey.getKeyBlock().getCryptographicAlgorithm());
140-
PlainTextKeyValue plainTextKeyValue = (PlainTextKeyValue) userDecryptionKey.getKeyBlock().getKeyValue().get();
141-
VendorAttribute[] vendorAttributes = plainTextKeyValue.getAttributes().get().getVendorAttributes().get();
140+
KeyValue keyValue = (KeyValue) userDecryptionKey.getKeyBlock().getKeyValue();
141+
VendorAttribute[] vendorAttributes = keyValue.getAttributes().get().getVendorAttributes().get();
142142
// TODO better check on Vendor Attributes
143143
logger.info(() -> Arrays.asList(vendorAttributes).toString());
144144

src/test/java/com/cosmian/TestGpsw.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.cosmian.rest.abe.access_policy.Attr;
2222
import com.cosmian.rest.abe.access_policy.Or;
2323
import com.cosmian.rest.abe.policy.Policy;
24-
import com.cosmian.rest.kmip.data_structures.PlainTextKeyValue;
24+
import com.cosmian.rest.kmip.data_structures.KeyValue;
2525
import com.cosmian.rest.kmip.objects.PrivateKey;
2626
import com.cosmian.rest.kmip.objects.PublicKey;
2727
import com.cosmian.rest.kmip.types.Attributes;
@@ -136,8 +136,8 @@ public void test_keys_import_export() throws Exception {
136136
PrivateKey userDecryptionKey = abe.retrieveUserDecryptionKey(userDecryptionKeyUniqueIdentifier);
137137
assertEquals(KeyFormatType.AbeUserDecryptionKey, userDecryptionKey.getKeyBlock().getKeyFormatType());
138138
assertEquals(CryptographicAlgorithm.ABE, userDecryptionKey.getKeyBlock().getCryptographicAlgorithm());
139-
PlainTextKeyValue plainTextKeyValue = (PlainTextKeyValue) userDecryptionKey.getKeyBlock().getKeyValue().get();
140-
VendorAttribute[] vendorAttributes = plainTextKeyValue.getAttributes().get().getVendorAttributes().get();
139+
KeyValue keyValue = (KeyValue) userDecryptionKey.getKeyBlock().getKeyValue();
140+
VendorAttribute[] vendorAttributes = keyValue.getAttributes().get().getVendorAttributes().get();
141141
// TODO better check on Vendor Attributes
142142
logger.info(() -> Arrays.asList(vendorAttributes).toString());
143143

src/test/java/com/cosmian/TestKmip.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.cosmian.rest.kmip.data_structures.KeyBlock;
1414
import com.cosmian.rest.kmip.data_structures.KeyMaterial;
1515
import com.cosmian.rest.kmip.data_structures.KeyValue;
16-
import com.cosmian.rest.kmip.data_structures.PlainTextKeyValue;
1716
import com.cosmian.rest.kmip.data_structures.TransparentSymmetricKey;
1817
import com.cosmian.rest.kmip.json.KmipStruct;
1918
import com.cosmian.rest.kmip.objects.SymmetricKey;
@@ -36,7 +35,6 @@
3635
import com.fasterxml.classmate.members.ResolvedField;
3736
import com.fasterxml.jackson.databind.JsonMappingException;
3837
import com.fasterxml.jackson.databind.ObjectMapper;
39-
4038
public class TestKmip {
4139

4240
@BeforeAll
@@ -45,10 +43,10 @@ public static void before_all() {
4543
}
4644

4745
private SymmetricKey symmetricKey() {
48-
PlainTextKeyValue pt_kv =
49-
new PlainTextKeyValue(new KeyMaterial(new TransparentSymmetricKey("bytes".getBytes())), Optional.empty());
46+
KeyValue kv =
47+
new KeyValue(new KeyMaterial(new TransparentSymmetricKey("bytes".getBytes())), Optional.empty());
5048
SymmetricKey symmetricKey = new SymmetricKey(new KeyBlock(KeyFormatType.TransparentSymmetricKey,
51-
Optional.empty(), new KeyValue(pt_kv), CryptographicAlgorithm.AES, 256, Optional.empty()
49+
Optional.empty(), kv, CryptographicAlgorithm.AES, 256, Optional.empty()
5250

5351
));
5452
return symmetricKey;

0 commit comments

Comments
 (0)