forked from getodk/collect
/
EncryptionUtils.java
605 lines (535 loc) 路 25.7 KB
/
EncryptionUtils.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
* Copyright (C) 2011 University of Washington
*
* 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 org.odk.collect.android.utilities;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Base64;
import org.apache.commons.io.IOUtils;
import org.kxml2.io.KXmlSerializer;
import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.dao.FormsDao;
import org.odk.collect.android.exception.EncryptionException;
import org.odk.collect.android.logic.FormController.InstanceMetadata;
import org.odk.collect.android.provider.FormsProviderAPI.FormsColumns;
import org.odk.collect.android.provider.InstanceProviderAPI.InstanceColumns;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import timber.log.Timber;
import static org.odk.collect.android.utilities.ApplicationConstants.Namespaces.XML_OPENROSA_NAMESPACE;
/**
* Utility class for encrypting submissions during the SaveToDiskTask.
*
* @author mitchellsundt@gmail.com
*/
public class EncryptionUtils {
public static final String RSA_ALGORITHM = "RSA";
// the symmetric key we are encrypting with RSA is only 256 bits... use SHA-256
public static final String ASYMMETRIC_ALGORITHM = "RSA/NONE/OAEPWithSHA256AndMGF1Padding";
public static final String SYMMETRIC_ALGORITHM = "AES/CFB/PKCS5Padding";
public static final String UTF_8 = "UTF-8";
public static final int SYMMETRIC_KEY_LENGTH = 256;
public static final int IV_BYTE_LENGTH = 16;
// tags in the submission manifest
private static final String XML_ENCRYPTED_TAG_NAMESPACE =
"http://www.opendatakit.org/xforms/encrypted";
private static final String DATA = "data";
private static final String ID = "id";
private static final String VERSION = "version";
private static final String ENCRYPTED = "encrypted";
private static final String BASE64_ENCRYPTED_KEY = "base64EncryptedKey";
private static final String ENCRYPTED_XML_FILE = "encryptedXmlFile";
private static final String META = "meta";
private static final String INSTANCE_ID = "instanceID";
private static final String MEDIA = "media";
private static final String FILE = "file";
private static final String BASE64_ENCRYPTED_ELEMENT_SIGNATURE =
"base64EncryptedElementSignature";
private static final String NEW_LINE = "\n";
private static final String ENCRYPTION_PROVIDER = "BC";
private EncryptionUtils() {
}
public static final class EncryptedFormInformation {
public final String formId;
public final String formVersion;
public final InstanceMetadata instanceMetadata;
public final PublicKey rsaPublicKey;
public final String base64RsaEncryptedSymmetricKey;
public final SecretKeySpec symmetricKey;
public final byte[] ivSeedArray;
private int ivCounter;
public final StringBuilder elementSignatureSource = new StringBuilder();
private boolean isNotBouncyCastle;
EncryptedFormInformation(String formId, String formVersion,
InstanceMetadata instanceMetadata, PublicKey rsaPublicKey) {
this.formId = formId;
this.formVersion = formVersion;
this.instanceMetadata = instanceMetadata;
this.rsaPublicKey = rsaPublicKey;
// generate the symmetric key from random bits...
SecureRandom r = new SecureRandom();
byte[] key = new byte[SYMMETRIC_KEY_LENGTH / 8];
r.nextBytes(key);
symmetricKey = new SecretKeySpec(key, SYMMETRIC_ALGORITHM);
// construct the fixed portion of the iv -- the ivSeedArray
// this is the md5 hash of the instanceID and the symmetric key
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(instanceMetadata.instanceId.getBytes(UTF_8));
md.update(key);
byte[] messageDigest = md.digest();
ivSeedArray = new byte[IV_BYTE_LENGTH];
for (int i = 0; i < IV_BYTE_LENGTH; ++i) {
ivSeedArray[i] = messageDigest[i % messageDigest.length];
}
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
Timber.e(e, "Unable to set md5 hash for instanceid and symmetric key.");
throw new IllegalArgumentException(e.getMessage());
}
// construct the base64-encoded RSA-encrypted symmetric key
try {
Cipher pkCipher;
pkCipher = Cipher.getInstance(ASYMMETRIC_ALGORITHM);
// write AES key
pkCipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
byte[] pkEncryptedKey = pkCipher.doFinal(key);
String alg = pkCipher.getAlgorithm();
Timber.i("Algorithm Used: %s", alg);
base64RsaEncryptedSymmetricKey = Base64.encodeToString(pkEncryptedKey, Base64.NO_WRAP);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
Timber.e(e, "Unable to encrypt the symmetric key.");
throw new IllegalArgumentException(e.getMessage());
}
// start building elementSignatureSource...
appendElementSignatureSource(formId);
if (formVersion != null) {
appendElementSignatureSource(formVersion);
}
appendElementSignatureSource(base64RsaEncryptedSymmetricKey);
appendElementSignatureSource(instanceMetadata.instanceId);
}
public void appendElementSignatureSource(String value) {
elementSignatureSource.append(value).append('\n');
}
public void appendFileSignatureSource(File file) {
String md5Hash = FileUtils.getMd5Hash(file);
appendElementSignatureSource(file.getName() + "::" + md5Hash);
}
public String getBase64EncryptedElementSignature() {
// Step 0: construct the text of the elements in elementSignatureSource (done)
// Where...
// * Elements are separated by newline characters.
// * Filename is the unencrypted filename (no .enc suffix).
// * Md5 hashes of the unencrypted files' contents are converted
// to zero-padded 32-character strings before concatenation.
// Assumes this is in the order:
// formId
// version (omitted if null)
// base64RsaEncryptedSymmetricKey
// instanceId
// for each media file { filename "::" md5Hash }
// submission.xml "::" md5Hash
// Step 1: construct the (raw) md5 hash of Step 0.
byte[] messageDigest;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(elementSignatureSource.toString().getBytes(UTF_8));
messageDigest = md.digest();
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
Timber.e(e, "Exception thrown while constructing md5 hash.");
throw new IllegalArgumentException(e.getMessage());
}
// Step 2: construct the base64-encoded RSA-encrypted md5
try {
Cipher pkCipher;
pkCipher = Cipher.getInstance(ASYMMETRIC_ALGORITHM);
// write AES key
pkCipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
byte[] pkEncryptedKey = pkCipher.doFinal(messageDigest);
return Base64.encodeToString(pkEncryptedKey, Base64.NO_WRAP);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
Timber.e(e, "Unable to encrypt the symmetric key.");
throw new IllegalArgumentException(e.getMessage());
}
}
public Cipher getCipher() throws InvalidKeyException,
InvalidAlgorithmParameterException, NoSuchAlgorithmException,
NoSuchPaddingException {
++ivSeedArray[ivCounter % ivSeedArray.length];
++ivCounter;
IvParameterSpec baseIv = new IvParameterSpec(ivSeedArray);
Cipher c = null;
try {
c = Cipher.getInstance(EncryptionUtils.SYMMETRIC_ALGORITHM, "BC");
isNotBouncyCastle = false;
} catch (NoSuchProviderException e) {
Timber.w(e, "Unable to obtain BouncyCastle provider! Decryption may fail.");
isNotBouncyCastle = true;
c = Cipher.getInstance(EncryptionUtils.SYMMETRIC_ALGORITHM);
}
c.init(Cipher.ENCRYPT_MODE, symmetricKey, baseIv);
return c;
}
public boolean isNotBouncyCastle() {
return isNotBouncyCastle;
}
}
/**
* Retrieve the encryption information for this uri.
*
* @param uri either an instance URI (if previously saved) or a form URI
*/
public static EncryptedFormInformation getEncryptedFormInformation(Uri uri,
InstanceMetadata instanceMetadata) throws EncryptionException {
ContentResolver cr = Collect.getInstance().getContentResolver();
// fetch the form information
String formId;
String formVersion;
PublicKey pk;
Cursor formCursor = null;
try {
if (InstanceColumns.CONTENT_ITEM_TYPE.equals(cr.getType(uri))) {
// chain back to the Form record...
String[] selectionArgs = null;
String selection = null;
Cursor instanceCursor = null;
try {
instanceCursor = cr.query(uri, null, null, null, null);
if (instanceCursor.getCount() != 1) {
String msg = Collect.getInstance().getString(R.string.not_exactly_one_record_for_this_instance);
Timber.e(msg);
throw new EncryptionException(msg, null);
}
instanceCursor.moveToFirst();
String jrFormId = instanceCursor.getString(
instanceCursor.getColumnIndex(InstanceColumns.JR_FORM_ID));
int idxJrVersion = instanceCursor.getColumnIndex(InstanceColumns.JR_VERSION);
if (!instanceCursor.isNull(idxJrVersion)) {
selectionArgs = new String[]{jrFormId, instanceCursor.getString(
idxJrVersion)};
selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION
+ "=?";
} else {
selectionArgs = new String[]{jrFormId};
selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION
+ " IS NULL";
}
} finally {
if (instanceCursor != null) {
instanceCursor.close();
}
}
formCursor = new FormsDao().getFormsCursor(selection, selectionArgs);
if (formCursor.getCount() != 1) {
String msg = Collect.getInstance().getString(R.string.not_exactly_one_blank_form_for_this_form_id);
Timber.e(msg);
throw new EncryptionException(msg, null);
}
formCursor.moveToFirst();
} else if (FormsColumns.CONTENT_ITEM_TYPE.equals(cr.getType(uri))) {
formCursor = cr.query(uri, null, null, null, null);
if (formCursor.getCount() != 1) {
String msg = Collect.getInstance().getString(R.string.not_exactly_one_blank_form_for_this_form_id);
Timber.e(msg);
throw new EncryptionException(msg, null);
}
formCursor.moveToFirst();
}
formId = formCursor.getString(formCursor.getColumnIndex(FormsColumns.JR_FORM_ID));
if (formId == null || formId.length() == 0) {
String msg = Collect.getInstance().getString(R.string.no_form_id_specified);
Timber.e(msg);
throw new EncryptionException(msg, null);
}
int idxVersion = formCursor.getColumnIndex(FormsColumns.JR_VERSION);
int idxBase64RsaPublicKey = formCursor.getColumnIndex(
FormsColumns.BASE64_RSA_PUBLIC_KEY);
formVersion = formCursor.isNull(idxVersion) ? null : formCursor.getString(idxVersion);
String base64RsaPublicKey = formCursor.isNull(idxBase64RsaPublicKey)
? null : formCursor.getString(idxBase64RsaPublicKey);
if (base64RsaPublicKey == null || base64RsaPublicKey.length() == 0) {
return null; // this is legitimately not an encrypted form
}
byte[] publicKey = Base64.decode(base64RsaPublicKey, Base64.NO_WRAP);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey);
KeyFactory kf;
try {
kf = KeyFactory.getInstance(RSA_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
String msg = Collect.getInstance().getString(R.string.phone_does_not_support_rsa);
Timber.e(e, "%s due to %s ", msg, e.getMessage());
throw new EncryptionException(msg, e);
}
try {
pk = kf.generatePublic(publicKeySpec);
} catch (InvalidKeySpecException e) {
String msg = Collect.getInstance().getString(R.string.invalid_rsa_public_key);
Timber.e(e, "%s due to %s ", msg, e.getMessage());
throw new EncryptionException(msg, e);
}
} finally {
if (formCursor != null) {
formCursor.close();
}
}
// submission must have an OpenRosa metadata block with a non-null
// instanceID value.
if (instanceMetadata.instanceId == null) {
Timber.e("No OpenRosa metadata block or no instanceId defined in that block");
return null;
}
// For now, prevent encryption if the BouncyCastle implementation is not present.
// https://code.google.com/p/opendatakit/issues/detail?id=918
try {
Cipher.getInstance(EncryptionUtils.SYMMETRIC_ALGORITHM, ENCRYPTION_PROVIDER);
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
String msg;
if (e instanceof NoSuchAlgorithmException) {
msg = "No BouncyCastle implementation of symmetric algorithm!";
} else if (e instanceof NoSuchProviderException) {
msg = "No BouncyCastle provider implementation of symmetric algorithm!";
} else {
msg = "No BouncyCastle provider for padding implementation of symmetric algorithm!";
}
Timber.e(msg);
return null;
}
return new EncryptedFormInformation(formId, formVersion, instanceMetadata, pk);
}
private static void encryptFile(File file, EncryptedFormInformation formInfo)
throws IOException, EncryptionException {
File encryptedFile = new File(file.getParentFile(), file.getName()
+ ".enc");
if (encryptedFile.exists() && !encryptedFile.delete()) {
throw new IOException("Cannot overwrite " + encryptedFile.getAbsolutePath()
+ ". Perhaps the file is locked?");
}
// add elementSignatureSource for this file...
formInfo.appendFileSignatureSource(file);
RandomAccessFile randomAccessFile = null;
CipherOutputStream cipherOutputStream = null;
try {
Cipher c = formInfo.getCipher();
randomAccessFile = new RandomAccessFile(encryptedFile, "rws");
ByteArrayOutputStream encryptedData = new ByteArrayOutputStream();
cipherOutputStream = new CipherOutputStream(encryptedData, c);
InputStream fin = new FileInputStream(file);
byte[] buffer = new byte[2048];
int len = fin.read(buffer);
while (len != -1) {
cipherOutputStream.write(buffer, 0, len);
len = fin.read(buffer);
}
fin.close();
cipherOutputStream.flush();
cipherOutputStream.close();
randomAccessFile.write(encryptedData.toByteArray());
Timber.i("Encrpyted:%s -> %s", file.getName(), encryptedFile.getName());
} catch (Exception e) {
String msg = "Error encrypting: " + file.getName() + " -> "
+ encryptedFile.getName();
Timber.e(e, "%s due to %s ", msg, e.getMessage());
throw new EncryptionException(msg, e);
} finally {
IOUtils.closeQuietly(cipherOutputStream);
if (randomAccessFile != null) {
randomAccessFile.close();
}
}
}
public static boolean deletePlaintextFiles(File instanceXml, @Nullable File lastSaved) {
// NOTE: assume the directory containing the instanceXml contains ONLY
// files related to this one instance.
File instanceDir = instanceXml.getParentFile();
boolean allSuccessful = true;
// Delete files that do not end with ".enc", and do not start with ".";
// ignore directories
File[] instanceFiles = instanceDir.listFiles();
for (File f : instanceFiles) {
if (f.equals(instanceXml)) {
continue; // don't touch instance file
}
if (f.isDirectory()) {
continue; // don't handle directories
}
if (!f.getName().endsWith(".enc")) {
// not an encrypted file -- delete it!
allSuccessful = allSuccessful & f.delete(); // DO NOT
// short-circuit
}
}
// Delete the last-saved instance, if one exists.
if (lastSaved != null && lastSaved.exists()) {
allSuccessful &= lastSaved.delete();
}
return allSuccessful;
}
private static List<File> encryptSubmissionFiles(File instanceXml,
File submissionXml, EncryptedFormInformation formInfo)
throws IOException, EncryptionException {
// NOTE: assume the directory containing the instanceXml contains ONLY
// files related to this one instance.
File instanceDir = instanceXml.getParentFile();
// encrypt files that do not end with ".enc", and do not start with ".";
// ignore directories
File[] allFiles = instanceDir.listFiles();
List<File> filesToProcess = new ArrayList<File>();
for (File f : allFiles) {
if (f.equals(instanceXml)) {
continue; // don't touch restore file
}
if (f.equals(submissionXml)) {
continue; // handled last
}
if (f.isDirectory()) {
continue; // don't handle directories
}
if (f.getName().startsWith(".")) {
continue; // MacOSX garbage
}
if (f.getName().endsWith(".enc")) {
f.delete(); // try to delete this (leftover junk)
} else {
filesToProcess.add(f);
}
}
// encrypt here...
for (File f : filesToProcess) {
encryptFile(f, formInfo);
}
// encrypt the submission.xml as the last file...
encryptFile(submissionXml, formInfo);
return filesToProcess;
}
/**
* Constructs the encrypted attachments, encrypted form xml, and the
* plaintext submission manifest (with signature) for the form submission.
*
* Does not delete any of the original files.
*/
public static void generateEncryptedSubmission(File instanceXml,
File submissionXml, EncryptedFormInformation formInfo)
throws IOException, EncryptionException {
// submissionXml is the submission data to be published to Aggregate
if (!submissionXml.exists() || !submissionXml.isFile()) {
throw new IOException("No submission.xml found");
}
// TODO: confirm that this xml is not already encrypted...
// Step 1: encrypt the submission and all the media files...
List<File> mediaFiles = encryptSubmissionFiles(instanceXml,
submissionXml, formInfo);
// Step 2: build the encrypted-submission manifest (overwrites
// submission.xml)...
writeSubmissionManifest(formInfo, submissionXml, mediaFiles);
}
private static void writeSubmissionManifest(
EncryptedFormInformation formInfo,
File submissionXml, List<File> mediaFiles) throws EncryptionException {
Document d = new Document();
d.setStandalone(true);
d.setEncoding(UTF_8);
Element e = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, DATA);
e.setPrefix(null, XML_ENCRYPTED_TAG_NAMESPACE);
e.setAttribute(null, ID, formInfo.formId);
if (formInfo.formVersion != null) {
e.setAttribute(null, VERSION, formInfo.formVersion);
}
e.setAttribute(null, ENCRYPTED, "yes");
d.addChild(0, Node.ELEMENT, e);
int idx = 0;
Element c;
c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_KEY);
c.addChild(0, Node.TEXT, formInfo.base64RsaEncryptedSymmetricKey);
e.addChild(idx++, Node.ELEMENT, c);
c = d.createElement(XML_OPENROSA_NAMESPACE, META);
c.setPrefix("orx", XML_OPENROSA_NAMESPACE);
{
Element instanceTag = d.createElement(XML_OPENROSA_NAMESPACE, INSTANCE_ID);
instanceTag.addChild(0, Node.TEXT, formInfo.instanceMetadata.instanceId);
c.addChild(0, Node.ELEMENT, instanceTag);
}
e.addChild(idx++, Node.ELEMENT, c);
e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);
if (mediaFiles != null) {
for (File file : mediaFiles) {
c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, MEDIA);
Element fileTag = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, FILE);
fileTag.addChild(0, Node.TEXT, file.getName() + ".enc");
c.addChild(0, Node.ELEMENT, fileTag);
e.addChild(idx++, Node.ELEMENT, c);
e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);
}
}
c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, ENCRYPTED_XML_FILE);
c.addChild(0, Node.TEXT, submissionXml.getName() + ".enc");
e.addChild(idx++, Node.ELEMENT, c);
c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_ELEMENT_SIGNATURE);
c.addChild(0, Node.TEXT, formInfo.getBase64EncryptedElementSignature());
e.addChild(idx++, Node.ELEMENT, c);
FileOutputStream fout = null;
OutputStreamWriter writer = null;
try {
fout = new FileOutputStream(submissionXml);
writer = new OutputStreamWriter(fout, UTF_8);
KXmlSerializer serializer = new KXmlSerializer();
serializer.setOutput(writer);
// setting the response content type emits the xml header.
// just write the body here...
d.writeChildren(serializer);
serializer.flush();
writer.flush();
fout.getChannel().force(true);
writer.close();
} catch (Exception ex) {
String msg = "Error writing submission.xml for encrypted submission: "
+ submissionXml.getParentFile().getName();
Timber.e(ex, "%s due to : %s ", msg, ex.getMessage());
throw new EncryptionException(msg, ex);
} finally {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(fout);
}
}
}