5
5
import io .opentdf .platform .sdk .TDF .Reader ;
6
6
import io .opentdf .platform .sdk .nanotdf .ECKeyPair ;
7
7
import io .opentdf .platform .sdk .nanotdf .NanoTDFType ;
8
+ import org .apache .commons .codec .DecoderException ;
8
9
import org .apache .commons .compress .utils .SeekableInMemoryByteChannel ;
9
- import org .bouncycastle .jce .interfaces .ECPrivateKey ;
10
10
import org .junit .jupiter .api .BeforeAll ;
11
11
import org .junit .jupiter .api .Test ;
12
12
13
13
import javax .annotation .Nonnull ;
14
14
import java .io .ByteArrayInputStream ;
15
15
import java .io .ByteArrayOutputStream ;
16
+ import java .io .IOException ;
16
17
import java .io .InputStream ;
17
18
import java .io .OutputStream ;
18
19
import java .nio .charset .StandardCharsets ;
19
20
import java .security .KeyPair ;
21
+ import java .security .NoSuchAlgorithmException ;
20
22
import java .security .SecureRandom ;
23
+ import java .text .ParseException ;
21
24
import java .util .ArrayList ;
22
25
import java .util .Base64 ;
23
26
import java .util .List ;
24
27
import java .util .Random ;
28
+ import java .util .concurrent .ExecutionException ;
25
29
import java .util .concurrent .atomic .AtomicInteger ;
26
30
import java .util .function .Predicate ;
27
31
import java .util .stream .Collectors ;
28
32
29
33
import static io .opentdf .platform .sdk .TDF .GLOBAL_KEY_SALT ;
30
34
import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
35
+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
31
36
import static org .junit .jupiter .api .Assertions .assertThrows ;
32
37
33
38
public class TDFTest {
@@ -492,6 +497,48 @@ public void testCreateTDFWithMimeType() throws Exception {
492
497
assertThat (reader .getManifest ().payload .mimeType ).isEqualTo (mimeType );
493
498
}
494
499
500
+ @ Test
501
+ public void legacyTDFRoundTrips () throws DecoderException , IOException , ExecutionException , JOSEException , InterruptedException , ParseException , NoSuchAlgorithmException {
502
+ final String mimeType = "application/pdf" ;
503
+
504
+ Config .TDFConfig config = Config .newTDFConfig (
505
+ Config .withAutoconfigure (false ),
506
+ Config .withKasInformation (getRSAKASInfos ()),
507
+ Config .withTargetMode ("4.2.1" ),
508
+ Config .withMimeType (mimeType ));
509
+
510
+ byte [] data = new byte [129 ];
511
+ new Random ().nextBytes (data );
512
+ InputStream plainTextInputStream = new ByteArrayInputStream (data );
513
+ ByteArrayOutputStream tdfOutputStream = new ByteArrayOutputStream ();
514
+
515
+ TDF tdf = new TDF ();
516
+ tdf .createTDF (plainTextInputStream , tdfOutputStream , config , kas , null );
517
+
518
+ var dataOutputStream = new ByteArrayOutputStream ();
519
+
520
+ var reader = tdf .loadTDF (new SeekableInMemoryByteChannel (tdfOutputStream .toByteArray ()), kas );
521
+ var integrityInformation = reader .getManifest ().encryptionInformation .integrityInformation ;
522
+ assertThat (reader .getManifest ().tdfVersion ).isNull ();
523
+ var decodedSignature = Base64 .getDecoder ().decode (integrityInformation .rootSignature .signature );
524
+ for (var b : decodedSignature ) {
525
+ assertThat (isHexChar (b ))
526
+ .withFailMessage ("non-hex byte in signature: " + b )
527
+ .isTrue ();
528
+ }
529
+ for (var s : integrityInformation .segments ) {
530
+ var decodedSegmentSignature = Base64 .getDecoder ().decode (s .hash );
531
+ for (var b : decodedSegmentSignature ) {
532
+ assertThat (isHexChar (b ))
533
+ .withFailMessage ("non-hex byte in segment signature: " + b )
534
+ .isTrue ();
535
+ }
536
+ }
537
+ reader .readPayload (dataOutputStream );
538
+ assertThat (reader .getManifest ().payload .mimeType ).isEqualTo (mimeType );
539
+ assertArrayEquals (data , dataOutputStream .toByteArray (), "extracted data does not match" );
540
+ }
541
+
495
542
@ Nonnull
496
543
private static Config .KASInfo [] getKASInfos (Predicate <Integer > filter ) {
497
544
var kasInfos = new ArrayList <Config .KASInfo >();
@@ -515,4 +562,8 @@ private static Config.KASInfo[] getRSAKASInfos() {
515
562
private static Config .KASInfo [] getECKASInfos () {
516
563
return getKASInfos (i -> i % 2 != 0 );
517
564
}
565
+
566
+ private static boolean isHexChar (byte b ) {
567
+ return (b >= 'a' && b <= 'f' ) || (b >= '0' && b <= '9' );
568
+ }
518
569
}
0 commit comments