-
Notifications
You must be signed in to change notification settings - Fork 188
/
ProtectedDataType.java
524 lines (462 loc) · 15.3 KB
/
ProtectedDataType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.prism.xml.ns._public.types_3;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.util.CloneUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.xml.security.exceptions.Base64DecodingException;
import org.apache.xml.security.utils.Base64;
import org.w3c.dom.Element;
import com.evolveum.midpoint.prism.crypto.ProtectedData;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.QNameUtil;
/**
* This class was originally generated. But it was heavily modified by hand.
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ProtectedDataType", propOrder = {
"content"
})
@XmlSeeAlso({
ProtectedByteArrayType.class,
ProtectedStringType.class
})
public abstract class ProtectedDataType<T> implements ProtectedData<T>, Serializable {
public static final QName COMPLEX_TYPE = new QName("http://prism.evolveum.com/xml/ns/public/types-3", "ProtectedDataType");
public final static QName F_ENCRYPTED_DATA = new QName("http://prism.evolveum.com/xml/ns/public/types-3", "encryptedData");
public final static QName F_HASHED_DATA = new QName("http://prism.evolveum.com/xml/ns/public/types-3", "hashedData");
public final static QName F_CLEAR_VALUE = new QName("http://prism.evolveum.com/xml/ns/public/types-3", "clearValue");
public static final String NS_XML_ENC = "http://www.w3.org/2001/04/xmlenc#";
public static final String NS_XML_DSIG = "http://www.w3.org/2000/09/xmldsig#";
public static final QName F_XML_ENC_ENCRYPTED_DATA = new QName(NS_XML_ENC, "EncryptedData");
public static final QName F_XML_ENC_ENCRYPTION_METHOD = new QName(NS_XML_ENC, "EncryptionMethod");
public static final String ATTRIBUTE_XML_ENC_ALGORITHM = "Algorithm";
public static final QName F_XML_ENC_ALGORITHM = new QName(NS_XML_ENC, ATTRIBUTE_XML_ENC_ALGORITHM);
public static final QName F_XML_ENC_CIPHER_DATA = new QName(NS_XML_ENC, "CipherData");
public static final QName F_XML_ENC_CIPHER_VALUE = new QName(NS_XML_ENC, "CipherValue");
public static final QName F_XML_DSIG_KEY_INFO = new QName(NS_XML_DSIG, "KeyInfo");
public static final QName F_XML_DSIG_KEY_NAME = new QName(NS_XML_DSIG, "KeyName");
@XmlTransient
private EncryptedDataType encryptedDataType;
@XmlTransient
private HashedDataType hashedDataType;
@XmlTransient
private T clearValue;
@XmlElementRef(name = "encryptedData", namespace = "http://prism.evolveum.com/xml/ns/public/types-3", type = JAXBElement.class)
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
/**
*
* TODO
* May be either encrypted or hashed or provided in the clear (e.g. for debugging).
*
* This type is marked as "mixed" because it may have alternative representation where
* just the plaintext value is presented as the only value.
*
* This is considered to be primitive built-in type for prism objects.
* Gets the value of the content property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the content property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContent().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Object }
* {@link String }
* {@link JAXBElement }{@code <}{@link EncryptedDataType }{@code >}
*
*
*/
public List<Object> getContent() {
if (content == null) {
content = new ContentList();
}
return this.content;
}
@Override
public EncryptedDataType getEncryptedDataType() {
return encryptedDataType;
}
@Override
public void setEncryptedData(EncryptedDataType encryptedDataType) {
this.encryptedDataType = encryptedDataType;
}
@Override
public boolean isEncrypted() {
return encryptedDataType != null;
}
@Override
public HashedDataType getHashedDataType() {
return hashedDataType;
}
@Override
public void setHashedData(HashedDataType hashedDataType) {
this.hashedDataType = hashedDataType;
}
@Override
public boolean isHashed() {
return hashedDataType != null;
}
@Override
public T getClearValue() {
return clearValue;
}
@Override
public void setClearValue(T clearValue) {
this.clearValue = clearValue;
}
@Override
public boolean canGetCleartext() {
return clearValue != null || encryptedDataType != null;
}
@Override
public void destroyCleartext() {
// Not perfect. But OK for now.
clearValue = null;
}
private JAXBElement<EncryptedDataType> toJaxbElement(EncryptedDataType encryptedDataType) {
return new JAXBElement<EncryptedDataType>(F_ENCRYPTED_DATA, EncryptedDataType.class, encryptedDataType);
}
private JAXBElement<HashedDataType> toJaxbElement(HashedDataType hashedDataType) {
return new JAXBElement<HashedDataType>(F_ENCRYPTED_DATA, HashedDataType.class, hashedDataType);
}
private void clearContent() {
encryptedDataType = null;
hashedDataType = null;
}
private boolean addContent(Object newObject) {
if (newObject instanceof String){
String s = (String) newObject;
if (StringUtils.isNotBlank(s)) {
clearValue = (T) s;
}
return true;
} else
if (newObject instanceof JAXBElement<?>) {
JAXBElement<?> jaxbElement = (JAXBElement<?>)newObject;
if (QNameUtil.match(F_ENCRYPTED_DATA, jaxbElement.getName())) {
encryptedDataType = (EncryptedDataType) jaxbElement.getValue();
return true;
} else if (QNameUtil.match(F_HASHED_DATA, jaxbElement.getName())) {
hashedDataType = (HashedDataType) jaxbElement.getValue();
return true;
} else {
throw new IllegalArgumentException("Attempt to add unknown JAXB element "+jaxbElement);
}
} else if (newObject instanceof Element) {
Element element = (Element)newObject;
QName elementName = DOMUtil.getQName(element);
if (QNameUtil.match(F_XML_ENC_ENCRYPTED_DATA, elementName)) {
encryptedDataType = convertXmlEncToEncryptedDate(element);
return true;
} else if (QNameUtil.match(F_CLEAR_VALUE, elementName)){
clearValue = (T) element.getTextContent();
return true;
} else {
throw new IllegalArgumentException("Attempt to add unknown DOM element "+elementName);
}
} else {
throw new IllegalArgumentException("Attempt to add unknown object "+newObject+" ("+newObject.getClass()+")");
}
}
private EncryptedDataType convertXmlEncToEncryptedDate(Element eEncryptedData) {
EncryptedDataType encryptedDataType = new EncryptedDataType();
Element eEncryptionMethod = DOMUtil.getChildElement(eEncryptedData, F_XML_ENC_ENCRYPTION_METHOD);
if (eEncryptionMethod != null) {
String algorithm = eEncryptionMethod.getAttribute(ATTRIBUTE_XML_ENC_ALGORITHM);
EncryptionMethodType encryptionMethodType = new EncryptionMethodType();
encryptionMethodType.setAlgorithm(algorithm);
encryptedDataType.setEncryptionMethod(encryptionMethodType);
}
Element eKeyInfo = DOMUtil.getChildElement(eEncryptedData, F_XML_DSIG_KEY_INFO);
if (eKeyInfo != null) {
KeyInfoType keyInfoType = new KeyInfoType();
encryptedDataType.setKeyInfo(keyInfoType);
Element eKeyName = DOMUtil.getChildElement(eKeyInfo, F_XML_DSIG_KEY_NAME);
if (eKeyName != null) {
keyInfoType.setKeyName(eKeyName.getTextContent());
}
}
Element eCipherData = DOMUtil.getChildElement(eEncryptedData, F_XML_ENC_CIPHER_DATA);
if (eCipherData != null) {
CipherDataType cipherDataType = new CipherDataType();
encryptedDataType.setCipherData(cipherDataType);
Element eCipherValue = DOMUtil.getChildElement(eCipherData, F_XML_ENC_CIPHER_VALUE);
if (eCipherValue != null) {
String cipherValue = eCipherValue.getTextContent();
byte[] cipherValueBytes;
try {
cipherValueBytes = Base64.decode(cipherValue);
} catch (Base64DecodingException e) {
throw new IllegalArgumentException("Bad base64 encoding in CipherValue element: "+e.getMessage(),e);
}
cipherDataType.setCipherValue(cipherValueBytes);
}
}
return encryptedDataType;
}
public boolean isEmpty() {
return encryptedDataType == null && hashedDataType == null && clearValue == null;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((clearValue == null) ? 0 : clearValue.hashCode());
result = prime * result + ((encryptedDataType == null) ? 0 : encryptedDataType.hashCode());
result = prime * result + ((hashedDataType == null) ? 0 : hashedDataType.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ProtectedDataType other = (ProtectedDataType) obj;
if (clearValue == null) {
if (other.clearValue != null) {
return false;
}
} else if (!clearValue.equals(other.clearValue)) {
return false;
}
if (encryptedDataType == null) {
if (other.encryptedDataType != null) {
return false;
}
} else if (!encryptedDataType.equals(other.encryptedDataType)) {
return false;
}
if (hashedDataType == null) {
if (other.hashedDataType != null) {
return false;
}
} else if (!hashedDataType.equals(other.hashedDataType)) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
sb.append("(");
if (encryptedDataType != null) {
sb.append("encrypted=");
sb.append(encryptedDataType);
}
if (hashedDataType != null) {
sb.append("hashed=");
sb.append(hashedDataType);
}
if (clearValue != null) {
sb.append("clearValue=");
sb.append(clearValue);
}
sb.append(")");
return sb.toString();
}
protected void cloneTo(ProtectedDataType<T> cloned) {
cloned.clearValue = CloneUtil.clone(clearValue);
cloned.encryptedDataType = CloneUtil.clone(encryptedDataType);
cloned.hashedDataType = CloneUtil.clone(hashedDataType);
// content is virtual, there is no point in copying it
}
class ContentList implements List<Object>, Serializable {
@Override
public int size() {
if (encryptedDataType != null || hashedDataType != null) {
return 1;
} else {
return 0;
}
}
@Override
public boolean isEmpty() {
return encryptedDataType == null && hashedDataType == null;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterator<Object> iterator() {
return new Iterator<Object>() {
private int index = 0;
@Override
public boolean hasNext() {
return index == 0;
}
@Override
public Object next() {
if (index == 0) {
index++;
if (encryptedDataType == null) {
return toJaxbElement(hashedDataType);
} else {
return toJaxbElement(encryptedDataType);
}
} else {
return null;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public Object[] toArray() {
if (encryptedDataType == null && hashedDataType == null) {
return new Object[0];
} else {
Object[] a = new Object[1];
if (encryptedDataType == null) {
a[0] = toJaxbElement(hashedDataType);
} else {
a[0] = toJaxbElement(encryptedDataType);
}
return a;
}
}
@Override
public <T> T[] toArray(T[] a) {
return (T[]) toArray();
}
@Override
public boolean add(Object e) {
return addContent(e);
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object e: c) {
if (!contains(e)) {
return false;
}
}
return true;
}
@Override
public boolean addAll(Collection<? extends Object> c) {
boolean changed = false;
for (Object e: c) {
if (add(e)) {
changed = true;
}
}
return changed;
}
@Override
public boolean addAll(int index, Collection<? extends Object> c) {
throw new UnsupportedOperationException("we are too lazy for this");
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
// We would normally throw an exception here. but JAXB is actually using it.
clearContent();
}
@Override
public Object get(int index) {
if (index == 0) {
// what if encryptedDataType is null and clearValue is set? [pm]
if (encryptedDataType == null) {
return toJaxbElement(hashedDataType);
} else {
return toJaxbElement(encryptedDataType);
}
} else {
return null;
}
}
@Override
public Object set(int index, Object element) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, Object element) {
throw new UnsupportedOperationException();
}
@Override
public Object remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public int indexOf(Object o) {
throw new UnsupportedOperationException("we are too lazy for this");
}
@Override
public int lastIndexOf(Object o) {
throw new UnsupportedOperationException("we are too lazy for this");
}
@Override
public ListIterator<Object> listIterator() {
throw new UnsupportedOperationException("we are too lazy for this");
}
@Override
public ListIterator<Object> listIterator(int index) {
throw new UnsupportedOperationException("we are too lazy for this");
}
@Override
public List<Object> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException("we are too lazy for this");
}
}
}