Skip to content

Commit d59a9ba

Browse files
author
Olesja Aarma
committed
Merge branch 'RM-4813_key_label' into 'SID'
RM-4813: change SID/MID key label format See merge request cdoc2/cdoc2-java-ref-impl!104
2 parents c038bdf + 81b7bfa commit d59a9ba

File tree

7 files changed

+147
-46
lines changed

7 files changed

+147
-46
lines changed

cdoc2-lib/src/main/java/ee/cyber/cdoc2/crypto/AuthenticationIdentifier.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ public class AuthenticationIdentifier {
2121

2222
private final String identifier;
2323

24-
/**
25-
* Identifiers for Smart ID or Mobile ID.
26-
* @param authIdentifier authentication identifier string
27-
*/
28-
public AuthenticationIdentifier(String authIdentifier) {
29-
this.identifier = authIdentifier;
30-
}
31-
3224
/**
3325
* Identifiers for Smart ID or Mobile ID.
3426
* @param authType authentication method
@@ -49,7 +41,11 @@ protected AuthenticationIdentifier(
4941
SemanticsIdentifier etsiIdentifier,
5042
String mobileNumber
5143
) {
52-
this.identifier = authType + ":" + etsiIdentifier.getIdentifier() + ":" + mobileNumber;
44+
this.identifier = authType
45+
+ COLON_SEPARATOR
46+
+ etsiIdentifier.getIdentifier()
47+
+ COLON_SEPARATOR
48+
+ mobileNumber;
5349
}
5450

5551
public String getIdentifier() {

cdoc2-lib/src/main/java/ee/cyber/cdoc2/crypto/KeyLabelTools.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,15 @@ public static KeyLabelParams createSecretKeyLabelParams(String keyLabel) {
245245

246246
/**
247247
* Create key label parameters of key shares for data section of formatted key label.
248-
* @param keyLabel key label as (National) personal number
248+
* @param etsiIdentifier ETSI identifier in format 'etsi/PNOEE-30303039914'
249249
* @return KeyLabelParams key label parameters required for data section
250250
*/
251-
public static KeyLabelParams createKeySharesKeyLabelParams(String keyLabel) {
251+
public static KeyLabelParams createKeySharesKeyLabelParams(String etsiIdentifier) {
252252
KeyLabelParams keyLabelParams = createKeyLabelCommonParams(
253-
EncryptionKeyOrigin.KEY_SHARE, KeyLabelTools.KeyLabelDataVersion.V_2
253+
EncryptionKeyOrigin.KEY_SHARE, KeyLabelTools.KeyLabelDataVersion.V_1
254254
);
255-
keyLabelParams.addParam(KeyLabelDataFields.PNO.name(), keyLabel);
255+
256+
keyLabelParams.addParam(KeyLabelDataFields.SN.name(), etsiIdentifier);
256257

257258
return keyLabelParams;
258259
}
@@ -326,7 +327,7 @@ private static String extractKeyLabelByType(
326327
) {
327328
switch (keyLabelType) {
328329
case AUTH -> {
329-
return keyLabelParams.get(KeyLabelDataFields.PNO.name());
330+
return keyLabelParams.get(KeyLabelDataFields.SN.name());
330331
}
331332
case PW, SECRET -> {
332333
return keyLabelParams.get(KeyLabelDataFields.LABEL.name());
@@ -438,7 +439,7 @@ public enum KeyLabelDataFields {
438439
FIRST_NAME,
439440
LABEL,
440441
LAST_NAME,
441-
PNO, // for Smart id & Mobile id
442+
SN, // for Smart id & Mobile id
442443
SERIAL_NUMBER,
443444
TYPE,
444445
V

cdoc2-lib/src/main/java/ee/cyber/cdoc2/crypto/keymaterial/encrypt/EtsiIdentifierEncKeyMaterialBuilder.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ee.cyber.cdoc2.crypto.keymaterial.encrypt;
22

3+
import ee.sk.smartid.rest.dao.SemanticsIdentifier;
4+
35
import java.security.cert.CertificateException;
46
import java.util.Arrays;
57
import java.util.LinkedList;
@@ -89,10 +91,12 @@ private EtsiIdentifierEncKeyMaterialBuilder withKeyShares(
8991
if (null != idCodes) {
9092
List<EncryptionKeyMaterial> keyMaterials = Arrays.stream(idCodes)
9193
.map(idCode -> {
94+
SemanticsIdentifier semanticsIdentifier = createSemanticsIdentifier(idCode);
9295
AuthenticationIdentifier authIdentifier = AuthenticationIdentifier
93-
.forKeyShares(createSemanticsIdentifier(idCode), authType);
94-
KeyLabelParams keyLabelParams
95-
= createKeySharesKeyLabelParams(authIdentifier.getIdentifier());
96+
.forKeyShares(semanticsIdentifier, authType);
97+
KeyLabelParams keyLabelParams = createKeySharesKeyLabelParams(
98+
authIdentifier.getEtsiIdentifier()
99+
);
96100

97101
return EncryptionKeyMaterial.fromAuthMeans(authIdentifier, keyLabelParams);
98102
})

cdoc2-lib/src/test/java/ee/cyber/cdoc2/KeyLabelToolsTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ee.cyber.cdoc2;
22

3+
import ee.sk.smartid.rest.dao.SemanticsIdentifier;
4+
5+
import ee.cyber.cdoc2.crypto.AuthenticationIdentifier;
36
import ee.cyber.cdoc2.crypto.EncryptionKeyOrigin;
47
import ee.cyber.cdoc2.crypto.KeyLabelParams;
58
import ee.cyber.cdoc2.crypto.KeyLabelTools;
@@ -14,6 +17,7 @@
1417
import java.util.TreeMap;
1518

1619
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.KEY_LABEL_FILE_NAME_PROPERTY;
20+
import static ee.cyber.cdoc2.crypto.AuthenticationIdentifier.createSemanticsIdentifier;
1721
import static ee.cyber.cdoc2.crypto.KeyLabelTools.convertKeyLabelParamsMapToString;
1822
import static ee.cyber.cdoc2.crypto.KeyLabelTools.createCertKeyLabelParams;
1923
import static ee.cyber.cdoc2.crypto.KeyLabelTools.createEIdKeyLabelParams;
@@ -278,13 +282,17 @@ void testSecretKeyLabelParamsCreation() {
278282

279283
@Test
280284
void testKeySharesKeyLabelParamsCreation() {
281-
KeyLabelParams keyLabelParams = createKeySharesKeyLabelParams("keyLabel");
285+
SemanticsIdentifier semanticsIdentifier = createSemanticsIdentifier("30303039914");
286+
AuthenticationIdentifier authIdentifier = AuthenticationIdentifier
287+
.forKeyShares(semanticsIdentifier, AuthenticationIdentifier.AuthenticationType.SID);
288+
KeyLabelParams keyLabelParams
289+
= createKeySharesKeyLabelParams(authIdentifier.getEtsiIdentifier());
282290

283291
assertEquals(
284-
"keyLabel",
292+
"etsi/PNOEE-30303039914",
285293
getDecodedKeyLabelParamValue(
286294
keyLabelParams.keyLabelParams(),
287-
KeyLabelTools.KeyLabelDataFields.PNO
295+
KeyLabelTools.KeyLabelDataFields.SN
288296
)
289297
);
290298
}

cdoc2-lib/src/test/java/ee/cyber/cdoc2/container/EnvelopeTest.java

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@
8787
import static ee.cyber.cdoc2.KeyUtil.createSecretKey;
8888
import static ee.cyber.cdoc2.KeyUtil.getKeyPairRsaInstance;
8989
import static ee.cyber.cdoc2.container.EnvelopeTestUtils.checkContainerDecrypt;
90+
import static ee.cyber.cdoc2.container.EnvelopeTestUtils.createKeyLabelParams;
9091
import static ee.cyber.cdoc2.container.EnvelopeTestUtils.getPublicKeyLabelParams;
9192
import static ee.cyber.cdoc2.container.EnvelopeTestUtils.testContainer;
9293
import static ee.cyber.cdoc2.container.EnvelopeTestUtils.testContainerWithKeyShares;
9394
import static ee.cyber.cdoc2.crypto.AuthenticationIdentifier.createSemanticsIdentifier;
94-
import static ee.cyber.cdoc2.crypto.KeyLabelTools.createKeySharesKeyLabelParams;
9595
import static ee.cyber.cdoc2.fbs.header.Capsule.*;
9696
import static ee.cyber.cdoc2.fbs.header.Capsule.recipients_PBKDF2Capsule;
9797
import static ee.cyber.cdoc2.smartid.SmartIdClientTest.getDemoEnvConfiguration;
@@ -438,22 +438,26 @@ void testPasswordKeySerialization(@TempDir Path tempDir) throws Exception {
438438

439439
@Test
440440
void testKeySharesSerializationWithSmartId(@TempDir Path tempDir) throws Exception {
441+
AuthenticationIdentifier.AuthenticationType authType
442+
= AuthenticationIdentifier.AuthenticationType.SID;
441443
AuthenticationIdentifier keyLabel = AuthenticationIdentifier.forKeyShares(
442-
createSemanticsIdentifier("30303039914"),
443-
AuthenticationIdentifier.AuthenticationType.SID
444+
createSemanticsIdentifier("30303039914"), authType
445+
444446
);
445447

446-
testKeySharesSerialization(tempDir, keyLabel);
448+
testKeySharesSerialization(tempDir, keyLabel, authType, "30303039914");
447449
}
448450

449451
@Test
450452
void testKeySharesSerializationWithMobileId(@TempDir Path tempDir) throws Exception {
453+
AuthenticationIdentifier.AuthenticationType authType
454+
= AuthenticationIdentifier.AuthenticationType.MID;
451455
AuthenticationIdentifier keyLabel = AuthenticationIdentifier.forKeyShares(
452-
createSemanticsIdentifier("51307149560"),
453-
AuthenticationIdentifier.AuthenticationType.MID
456+
createSemanticsIdentifier("51307149560"), authType
457+
454458
);
455459

456-
testKeySharesSerialization(tempDir, keyLabel);
460+
testKeySharesSerialization(tempDir, keyLabel, authType, "51307149560");
457461
}
458462

459463
@Test
@@ -550,13 +554,22 @@ void testKeySharesScenarioWithSmartId(@TempDir Path tempDir) throws Exception {
550554
// SID demo env that authenticates automatically
551555
setupKeyShareClientMocks();
552556

557+
AuthenticationIdentifier.AuthenticationType authType
558+
= AuthenticationIdentifier.AuthenticationType.SID;
559+
String idCode = "30303039914";
560+
553561
AuthenticationIdentifier authIdentifier = AuthenticationIdentifier.forKeyShares(
554-
createSemanticsIdentifier("30303039914"),
555-
AuthenticationIdentifier.AuthenticationType.SID
562+
createSemanticsIdentifier(idCode), authType
556563
);
557564

558-
EnvelopeTestUtils.DecryptionData decryptionData
559-
= testContainerWithKeyShares(tempDir, authIdentifier, authIdentifier, shareClientFactory);
565+
EnvelopeTestUtils.DecryptionData decryptionData = testContainerWithKeyShares(
566+
tempDir,
567+
authIdentifier,
568+
authIdentifier,
569+
shareClientFactory,
570+
authType,
571+
idCode
572+
);
560573

561574
verifyMockedKeyShareClients();
562575

@@ -582,20 +595,23 @@ void testKeySharesScenarioWithSmartId(@TempDir Path tempDir) throws Exception {
582595
void testKeySharesScenarioWithMobileId(@TempDir Path tempDir) throws Exception {
583596
// MID demo env that authenticates automatically
584597
setupKeyShareClientMocks();
598+
String idCode = "51307149560";
585599
AuthenticationIdentifier encAuthIdentifier = AuthenticationIdentifier.forKeyShares(
586-
createSemanticsIdentifier("51307149560"),
600+
createSemanticsIdentifier(idCode),
587601
AuthenticationIdentifier.AuthenticationType.MID
588602
);
589603
AuthenticationIdentifier decryptAuthIdentifier = AuthenticationIdentifier.forMidDecryption(
590-
createSemanticsIdentifier("51307149560"),
604+
createSemanticsIdentifier(idCode),
591605
"+37269930366"
592606
);
593607

594608
EnvelopeTestUtils.DecryptionData decryptionData = testContainerWithKeyShares(
595609
tempDir,
596610
encAuthIdentifier,
597611
decryptAuthIdentifier,
598-
shareClientFactory
612+
shareClientFactory,
613+
AuthenticationIdentifier.AuthenticationType.MID,
614+
idCode
599615
);
600616

601617
verifyMockedKeyShareClients();
@@ -686,20 +702,23 @@ void testReEncryptionScenario(@TempDir Path tempDir) throws Exception {
686702
void testReEncryptionScenarioWithMobileId(@TempDir Path tempDir) throws Exception {
687703
// encrypt initial cdoc2 document
688704
setupKeyShareClientMocks();
705+
String idCode = "60001017869";
689706
AuthenticationIdentifier encAuthIdentifier = AuthenticationIdentifier.forKeyShares(
690-
createSemanticsIdentifier("60001017869"),
707+
createSemanticsIdentifier(idCode),
691708
AuthenticationIdentifier.AuthenticationType.MID
692709
);
693710
AuthenticationIdentifier decryptAuthIdentifier = AuthenticationIdentifier.forMidDecryption(
694-
createSemanticsIdentifier("60001017869"),
711+
createSemanticsIdentifier(idCode),
695712
"+37268000769"
696713
);
697714

698715
EnvelopeTestUtils.DecryptionData decryptionData = testContainerWithKeyShares(
699716
tempDir,
700717
encAuthIdentifier,
701718
decryptAuthIdentifier,
702-
shareClientFactory
719+
shareClientFactory,
720+
AuthenticationIdentifier.AuthenticationType.MID,
721+
idCode
703722
);
704723

705724
verify(mockKeySharesClient1).storeKeyShare(keyShareCaptor1.capture());
@@ -1265,7 +1284,9 @@ private void setUpKeyLabelFormat(boolean isFormatted) {
12651284

12661285
private void testKeySharesSerialization(
12671286
Path tempDir,
1268-
AuthenticationIdentifier authIdentifier
1287+
AuthenticationIdentifier authIdentifier,
1288+
AuthenticationIdentifier.AuthenticationType authType,
1289+
String idCode
12691290
) throws Exception {
12701291
setupKeyShareClientMocks();
12711292

@@ -1280,7 +1301,7 @@ private void testKeySharesSerialization(
12801301

12811302
Envelope envelope = Envelope.prepare(
12821303
List.of(EncryptionKeyMaterial.fromAuthMeans(
1283-
authIdentifier, createKeySharesKeyLabelParams(authIdentifier.getIdentifier()))
1304+
authIdentifier, createKeyLabelParams(idCode, authType))
12841305
),
12851306
null, shareClientFactory
12861307
);

cdoc2-lib/src/test/java/ee/cyber/cdoc2/container/EnvelopeTestUtils.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ee.cyber.cdoc2.container;
22

3+
import ee.sk.smartid.rest.dao.SemanticsIdentifier;
4+
35
import ee.cyber.cdoc2.client.KeyShareClientFactory;
46
import ee.cyber.cdoc2.crypto.Crypto;
57
import ee.cyber.cdoc2.crypto.EncryptionKeyOrigin;
@@ -59,6 +61,7 @@
5961
import java.util.UUID;
6062

6163
import static ee.cyber.cdoc2.config.Cdoc2ConfigurationProperties.isKeyLabelMachineReadableFormatEnabled;
64+
import static ee.cyber.cdoc2.crypto.AuthenticationIdentifier.createSemanticsIdentifier;
6265
import static ee.cyber.cdoc2.crypto.KeyLabelTools.createKeySharesKeyLabelParams;
6366
import static ee.cyber.cdoc2.crypto.KeyLabelTools.createPublicKeyLabelParams;
6467
import static ee.cyber.cdoc2.crypto.KeyLabelTools.createSymmetricKeyLabelParams;
@@ -256,7 +259,9 @@ public static byte[] createContainerWithKeyShares(
256259
File payloadFile,
257260
byte[] payloadData,
258261
AuthenticationIdentifier authIdentifier,
259-
KeyShareClientFactory shareClientFactory
262+
KeyShareClientFactory shareClientFactory,
263+
AuthenticationIdentifier.AuthenticationType authType,
264+
String idCode
260265
) throws IOException, GeneralSecurityException, ExtApiException {
261266

262267
try (FileOutputStream payloadFos = new FileOutputStream(payloadFile)) {
@@ -266,8 +271,7 @@ public static byte[] createContainerWithKeyShares(
266271
List<File> files = new LinkedList<>();
267272
files.add(payloadFile);
268273

269-
KeyLabelParams keyLabelParams
270-
= createKeySharesKeyLabelParams(authIdentifier.getIdentifier());
274+
KeyLabelParams keyLabelParams = createKeyLabelParams(idCode, authType);
271275

272276
EncryptionKeyMaterial encKeyMaterial
273277
= EncryptionKeyMaterial.fromAuthMeans(authIdentifier, keyLabelParams);
@@ -334,7 +338,9 @@ public static DecryptionData testContainerWithKeyShares(
334338
Path tempDir,
335339
AuthenticationIdentifier encryptAuthIdentifier,
336340
AuthenticationIdentifier decryptAuthIdentifier,
337-
KeyShareClientFactory shareClientFactory
341+
KeyShareClientFactory shareClientFactory,
342+
AuthenticationIdentifier.AuthenticationType authType,
343+
String idCode
338344
) throws Exception {
339345

340346
UUID uuid = UUID.randomUUID();
@@ -349,7 +355,9 @@ public static DecryptionData testContainerWithKeyShares(
349355
payloadFile,
350356
payloadData.getBytes(StandardCharsets.UTF_8),
351357
encryptAuthIdentifier,
352-
shareClientFactory
358+
shareClientFactory,
359+
authType,
360+
idCode
353361
);
354362

355363
assertTrue(cdocContainerBytes.length > 0);
@@ -496,4 +504,15 @@ record DecryptionData(
496504
) {
497505
}
498506

507+
static KeyLabelParams createKeyLabelParams(
508+
String idCode,
509+
AuthenticationIdentifier.AuthenticationType type
510+
) {
511+
SemanticsIdentifier semanticsIdentifier = createSemanticsIdentifier(idCode);
512+
AuthenticationIdentifier authIdentifier = AuthenticationIdentifier
513+
.forKeyShares(semanticsIdentifier, type);
514+
515+
return createKeySharesKeyLabelParams(authIdentifier.getEtsiIdentifier());
516+
}
517+
499518
}

0 commit comments

Comments
 (0)