1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.data;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
20 import io.github.tonywasher.joceanus.gordianknot.api.keyset.GordianKeySet;
21 import io.github.tonywasher.joceanus.gordianknot.api.keyset.GordianKeySetFactory;
22 import io.github.tonywasher.joceanus.gordianknot.util.GordianUtilities;
23 import io.github.tonywasher.joceanus.metis.data.MetisDataResource;
24 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
25 import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionedSet;
26 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
27 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
28 import io.github.tonywasher.joceanus.prometheus.data.PrometheusCryptographyData.PrometheusControlKeyCtl;
29 import io.github.tonywasher.joceanus.prometheus.data.PrometheusCryptographyData.PrometheusControlKeySetCtl;
30 import io.github.tonywasher.joceanus.prometheus.data.PrometheusCryptographyData.PrometheusDataKeySetCtl;
31 import io.github.tonywasher.joceanus.prometheus.data.PrometheusData.PrometheusDataSetCtl;
32 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusDataException;
33 import io.github.tonywasher.joceanus.prometheus.exc.PrometheusSecurityException;
34
35 import java.util.Objects;
36
37
38
39
40
41
42
43 public class PrometheusDataKeySet
44 extends PrometheusDataItem
45 implements PrometheusDataKeySetCtl {
46
47
48
49 public static final String OBJECT_NAME = PrometheusCryptographyDataType.DATAKEYSET.getItemName();
50
51
52
53
54 public static final String LIST_NAME = PrometheusCryptographyDataType.DATAKEYSET.getListName();
55
56
57
58
59 public static final int WRAPLEN = GordianUtilities.getMaximumKeySetWrapLength();
60
61
62
63
64 private static final MetisFieldVersionedSet<PrometheusDataKeySet> FIELD_DEFS = MetisFieldVersionedSet.newVersionedFieldSet(PrometheusDataKeySet.class);
65
66
67
68
69 static {
70 FIELD_DEFS.declareLinkField(PrometheusCryptographyDataType.CONTROLKEYSET);
71 FIELD_DEFS.declareByteArrayField(PrometheusDataResource.KEYSET_KEYSETDEF, WRAPLEN);
72 FIELD_DEFS.declareDerivedVersionedField(PrometheusDataResource.KEYSET_KEYSET);
73 FIELD_DEFS.declareDerivedVersionedField(PrometheusDataResource.KEYSET_ENCRYPTOR);
74 }
75
76
77
78
79
80
81
82 protected PrometheusDataKeySet(final PrometheusDataKeySetList pList,
83 final PrometheusDataKeySet pSource) {
84
85 super(pList, pSource);
86
87
88 if (Objects.requireNonNull(getStyle()) == PrometheusListStyle.CLONE) {
89 final GordianKeySet myKeySet = pSource.getKeySet();
90 setValueKeySet(myKeySet);
91 setValueEncryptor(pSource.getEncryptor());
92 }
93 }
94
95
96
97
98
99
100
101
102 private PrometheusDataKeySet(final PrometheusDataKeySetList pList,
103 final PrometheusDataValues pValues) throws OceanusException {
104
105 super(pList, pValues);
106
107
108 final PrometheusDataSetCtl myData = getDataSet();
109 final OceanusDataFormatter myFormatter = myData.getDataFormatter();
110
111
112 Object myValue = pValues.getValue(PrometheusCryptographyDataType.CONTROLKEYSET);
113 if (myValue instanceof Integer i) {
114
115 setValueControlKeySet(i);
116
117
118 resolveDataLink(PrometheusCryptographyDataType.CONTROLKEYSET, PrometheusCryptographyDataType.CONTROLKEYSET);
119 } else if (myValue instanceof PrometheusControlKeySetCtl ks) {
120
121 setValueControlKeySet(ks);
122 }
123
124
125 final PrometheusControlKeySetCtl myControlKeySet = getControlKeySet();
126
127
128 myValue = pValues.getValue(PrometheusDataResource.KEYSET_KEYSETDEF);
129 if (myValue instanceof byte[] ba) {
130 setValueSecuredKeySetDef(ba);
131 }
132
133
134 myValue = pValues.getValue(PrometheusDataResource.KEYSET_KEYSET);
135 if (myValue instanceof GordianKeySet ks) {
136 setValueKeySet(ks);
137 setValueEncryptor(new PrometheusEncryptor(myFormatter, ks));
138 } else if (getSecuredKeySetDef() != null) {
139
140 try {
141 final GordianKeySet myKeySet = myControlKeySet.getKeySet().deriveKeySet(getSecuredKeySetDef());
142 setValueKeySet(myKeySet);
143 setValueEncryptor(new PrometheusEncryptor(myFormatter, myKeySet));
144 } catch (GordianException e) {
145 throw new PrometheusSecurityException(e);
146 }
147 }
148
149
150 myControlKeySet.registerDataKeySet(this);
151 }
152
153
154
155
156
157
158
159
160 protected PrometheusDataKeySet(final PrometheusDataKeySetList pList,
161 final PrometheusControlKeySetCtl pControlKeySet) throws OceanusException {
162
163 super(pList, 0);
164
165
166 try {
167
168 setValueControlKeySet(pControlKeySet);
169
170
171 final PrometheusDataSetCtl myData = getDataSet();
172 final OceanusDataFormatter myFormatter = myData.getDataFormatter();
173
174
175 final GordianKeySetFactory myKeySets = pControlKeySet.getSecurityFactory().getKeySetFactory();
176 final GordianKeySet myKeySet = myKeySets.generateKeySet(getDataSet().getKeySetSpec());
177 setValueKeySet(myKeySet);
178 setValueEncryptor(new PrometheusEncryptor(myFormatter, myKeySet));
179
180
181 setValueSecuredKeySetDef(pControlKeySet.getKeySet().secureKeySet(myKeySet));
182
183
184 } catch (GordianException
185 | OceanusException e) {
186
187 throw new PrometheusDataException(this, ERROR_CREATEITEM, e);
188 }
189 }
190
191 @Override
192 public MetisFieldSetDef getDataFieldSet() {
193 return FIELD_DEFS;
194 }
195
196
197
198
199
200
201 public final PrometheusControlKeyCtl getControlKey() {
202 return getControlKeySet().getControlKey();
203 }
204
205
206
207
208
209
210 public final PrometheusControlKeySetCtl getControlKeySet() {
211 return getValues().getValue(PrometheusCryptographyDataType.CONTROLKEYSET, PrometheusControlKeySetCtl.class);
212 }
213
214
215
216
217
218
219 public Integer getControlKeySetId() {
220 final PrometheusControlKeySetCtl myKeySet = getControlKeySet();
221 return myKeySet == null
222 ? null
223 : myKeySet.getIndexedId();
224 }
225
226
227
228
229
230
231 public final byte[] getSecuredKeySetDef() {
232 return getValues().getValue(PrometheusDataResource.KEYSET_KEYSETDEF, byte[].class);
233 }
234
235
236
237
238
239
240 public GordianKeySet getKeySet() {
241 return getValues().getValue(PrometheusDataResource.KEYSET_KEYSET, GordianKeySet.class);
242 }
243
244
245
246
247
248
249 public PrometheusEncryptor getEncryptor() {
250 return getValues().getValue(PrometheusDataResource.KEYSET_ENCRYPTOR, PrometheusEncryptor.class);
251 }
252
253
254
255
256
257
258
259 private void setValueControlKeySet(final Integer pId) throws OceanusException {
260 getValues().setValue(PrometheusCryptographyDataType.CONTROLKEYSET, pId);
261 }
262
263
264
265
266
267
268
269 private void setValueControlKeySet(final PrometheusControlKeySetCtl pKeySet) throws OceanusException {
270 getValues().setValue(PrometheusCryptographyDataType.CONTROLKEYSET, pKeySet);
271 }
272
273
274
275
276
277
278 private void setValueSecuredKeySetDef(final byte[] pValue) {
279 getValues().setUncheckedValue(PrometheusDataResource.KEYSET_KEYSETDEF, pValue);
280 }
281
282
283
284
285
286
287 private void setValueKeySet(final GordianKeySet pValue) {
288 getValues().setUncheckedValue(PrometheusDataResource.KEYSET_KEYSET, pValue);
289 }
290
291
292
293
294
295
296 private void setValueEncryptor(final PrometheusEncryptor pValue) {
297 getValues().setUncheckedValue(PrometheusDataResource.KEYSET_ENCRYPTOR, pValue);
298 }
299
300 @Override
301 public PrometheusDataKeySet getBase() {
302 return (PrometheusDataKeySet) super.getBase();
303 }
304
305 @Override
306 public PrometheusDataKeySetList getList() {
307 return (PrometheusDataKeySetList) super.getList();
308 }
309
310 @Override
311 public int compareValues(final PrometheusDataItem pThat) {
312
313 return 0;
314 }
315
316 @Override
317 public void resolveDataSetLinks() throws OceanusException {
318
319 final PrometheusDataSetCtl myData = getDataSet();
320 resolveDataLink(PrometheusCryptographyDataType.CONTROLKEYSET, PrometheusCryptographyDataType.CONTROLKEYSET);
321 final PrometheusControlKeySetCtl myControlKeySet = getControlKeySet();
322
323
324 myControlKeySet.registerDataKeySet(this);
325 }
326
327
328
329
330 protected void deleteDataKeySet() {
331
332 setDeleted(true);
333 }
334
335
336
337
338 public static class PrometheusDataKeySetList
339 extends PrometheusDataList<PrometheusDataKeySet> {
340
341
342
343 private static final MetisFieldSet<PrometheusDataKeySetList> FIELD_DEFS = MetisFieldSet.newFieldSet(PrometheusDataKeySetList.class);
344
345
346
347
348
349
350 protected PrometheusDataKeySetList(final PrometheusDataSetCtl pData) {
351 this(pData, PrometheusListStyle.CORE);
352 }
353
354
355
356
357
358
359
360 protected PrometheusDataKeySetList(final PrometheusDataSetCtl pData,
361 final PrometheusListStyle pStyle) {
362 super(PrometheusDataKeySet.class, pData, PrometheusCryptographyDataType.DATAKEYSET, pStyle);
363 }
364
365
366
367
368
369
370 private PrometheusDataKeySetList(final PrometheusDataKeySetList pSource) {
371 super(pSource);
372 }
373
374 @Override
375 public MetisFieldSet<PrometheusDataKeySetList> getDataFieldSet() {
376 return FIELD_DEFS;
377 }
378
379 @Override
380 public String listName() {
381 return LIST_NAME;
382 }
383
384 @Override
385 public MetisFieldSet<PrometheusDataKeySet> getItemFields() {
386 return PrometheusDataKeySet.FIELD_DEFS;
387 }
388
389 @Override
390 public boolean includeDataXML() {
391 return false;
392 }
393
394 @Override
395 protected PrometheusDataKeySetList getEmptyList(final PrometheusListStyle pStyle) {
396 final PrometheusDataKeySetList myList = new PrometheusDataKeySetList(this);
397 myList.setStyle(pStyle);
398 return myList;
399 }
400
401 @Override
402 public PrometheusDataKeySetList deriveList(final PrometheusListStyle pStyle) throws OceanusException {
403 return (PrometheusDataKeySetList) super.deriveList(pStyle);
404 }
405
406 @Override
407 public PrometheusDataKeySetList deriveDifferences(final PrometheusDataSetCtl pDataSet,
408 final PrometheusDataList<?> pOld) {
409 return (PrometheusDataKeySetList) super.deriveDifferences(pDataSet, pOld);
410 }
411
412 @Override
413 public PrometheusDataKeySet addCopyItem(final PrometheusDataItem pItem) {
414
415 if (!(pItem instanceof PrometheusDataKeySet)) {
416 return null;
417 }
418
419
420 final PrometheusDataKeySet mySet = new PrometheusDataKeySet(this, (PrometheusDataKeySet) pItem);
421 add(mySet);
422 return mySet;
423 }
424
425 @Override
426 public PrometheusDataKeySet addNewItem() {
427 throw new UnsupportedOperationException();
428 }
429
430 @Override
431 public PrometheusDataKeySet addValuesItem(final PrometheusDataValues pValues) throws OceanusException {
432
433 final PrometheusDataKeySet mySet = new PrometheusDataKeySet(this, pValues);
434
435
436 if (!isIdUnique(mySet.getIndexedId())) {
437 mySet.addError(ERROR_DUPLICATE, MetisDataResource.DATA_ID);
438 throw new PrometheusDataException(mySet, ERROR_VALIDATION);
439 }
440
441
442 add(mySet);
443
444
445 return mySet;
446 }
447
448
449
450
451
452
453
454
455
456 protected PrometheusDataKeySet cloneDataKeySet(final PrometheusDataItem pControlKeySet,
457 final PrometheusDataKeySet pKeySet) throws OceanusException {
458
459 final PrometheusDataValues myValues = new PrometheusDataValues(OBJECT_NAME);
460 myValues.addValue(MetisDataResource.DATA_ID, pKeySet.getIndexedId());
461 myValues.addValue(PrometheusCryptographyDataType.CONTROLKEYSET, pControlKeySet);
462 myValues.addValue(PrometheusDataResource.KEYSET_KEYSETDEF, pKeySet.getSecuredKeySetDef());
463 myValues.addValue(PrometheusDataResource.KEYSET_KEYSET, pKeySet.getKeySet());
464
465
466 return addValuesItem(myValues);
467 }
468
469 @Override
470 public void postProcessOnLoad() throws OceanusException {
471
472 reSort();
473 }
474
475 @Override
476 protected PrometheusDataMapItem allocateDataMap() {
477
478 throw new UnsupportedOperationException();
479 }
480 }
481 }