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