3939import com .owncloud .android .lib .resources .files .ReadFileRemoteOperation ;
4040import com .owncloud .android .lib .resources .status .OCCapability ;
4141import com .owncloud .android .lib .resources .status .OwnCloudVersion ;
42+ import com .owncloud .android .lib .resources .users .DeletePrivateKeyOperation ;
43+ import com .owncloud .android .lib .resources .users .DeletePublicKeyOperation ;
4244import com .owncloud .android .lib .resources .users .GetPrivateKeyOperation ;
4345import com .owncloud .android .lib .resources .users .GetPublicKeyOperation ;
4446import com .owncloud .android .lib .resources .users .SendCSROperation ;
5052import com .owncloud .android .utils .EncryptionUtils ;
5153import com .owncloud .android .utils .FileStorageUtils ;
5254
55+ import org .bouncycastle .operator .OperatorCreationException ;
56+ import org .conscrypt .OpenSSLRSAPublicKey ;
5357import org .junit .Before ;
5458import org .junit .BeforeClass ;
5559import org .junit .Rule ;
5862
5963import java .io .File ;
6064import java .io .IOException ;
65+ import java .math .BigInteger ;
6166import java .security .KeyPair ;
67+ import java .security .NoSuchAlgorithmException ;
68+ import java .security .cert .CertificateException ;
69+ import java .security .interfaces .RSAPrivateCrtKey ;
6270import java .util .ArrayList ;
6371import java .util .List ;
6472import java .util .Random ;
@@ -474,6 +482,34 @@ public void testUploadWithDelete() throws Exception {
474482 assertFalse (new File (uploadedFile .getStoragePath ()).exists ());
475483 }
476484
485+ @ Test
486+ public void testCheckCSR () throws NoSuchAlgorithmException , IOException , OperatorCreationException , CertificateException {
487+ deleteKeys ();
488+
489+ // Create public/private key pair
490+ KeyPair keyPair = EncryptionUtils .generateKeyPair ();
491+
492+ // create CSR
493+ AccountManager accountManager = AccountManager .get (targetContext );
494+ String userId = accountManager .getUserData (account , AccountUtils .Constants .KEY_USER_ID );
495+ String urlEncoded = CsrHelper .generateCsrPemEncodedString (keyPair , userId );
496+
497+ SendCSROperation operation = new SendCSROperation (urlEncoded );
498+ RemoteOperationResult result = operation .execute (account , targetContext );
499+
500+ assertTrue (result .isSuccess ());
501+ String publicKeyString = (String ) result .getData ().get (0 );
502+
503+ // check key
504+ RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey ) keyPair .getPrivate ();
505+ OpenSSLRSAPublicKey publicKey = EncryptionUtils .convertPublicKeyFromString (publicKeyString );
506+
507+ BigInteger modulusPublic = publicKey .getModulus ();
508+ BigInteger modulusPrivate = privateKey .getModulus ();
509+
510+ assertEquals (modulusPrivate , modulusPublic );
511+ }
512+
477513 private void deleteFile (int i ) {
478514 ArrayList <OCFile > files = new ArrayList <>();
479515 for (OCFile file : getStorageManager ().getFolderContent (currentFolder , false )) {
@@ -529,11 +565,11 @@ public void reInit() throws Exception {
529565 private void useExistingKeys () throws Exception {
530566 // download them from server
531567 GetPublicKeyOperation publicKeyOperation = new GetPublicKeyOperation ();
532- RemoteOperationResult publicKeyResult = publicKeyOperation .execute (account , targetContext );
568+ RemoteOperationResult < String > publicKeyResult = publicKeyOperation .execute (account , targetContext );
533569
534570 assertTrue ("Result code:" + publicKeyResult .getHttpCode (), publicKeyResult .isSuccess ());
535571
536- String publicKeyFromServer = ( String ) publicKeyResult .getData (). get ( 0 );
572+ String publicKeyFromServer = publicKeyResult .getResultData ( );
537573 arbitraryDataProvider .storeOrUpdateKeyValue (account .name ,
538574 EncryptionUtils .PUBLIC_KEY ,
539575 publicKeyFromServer );
@@ -559,7 +595,9 @@ private void useExistingKeys() throws Exception {
559595 TODO do not c&p code
560596 */
561597 private static void createKeys () throws Exception {
562- String publicKey ;
598+ deleteKeys ();
599+
600+ String publicKeyString ;
563601
564602 // Create public/private key pair
565603 KeyPair keyPair = EncryptionUtils .generateKeyPair ();
@@ -573,7 +611,18 @@ private static void createKeys() throws Exception {
573611 RemoteOperationResult result = operation .execute (account , targetContext );
574612
575613 if (result .isSuccess ()) {
576- publicKey = (String ) result .getData ().get (0 );
614+ publicKeyString = (String ) result .getData ().get (0 );
615+
616+ // check key
617+ RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey ) keyPair .getPrivate ();
618+ OpenSSLRSAPublicKey publicKey = EncryptionUtils .convertPublicKeyFromString (publicKeyString );
619+
620+ BigInteger modulusPublic = publicKey .getModulus ();
621+ BigInteger modulusPrivate = privateKey .getModulus ();
622+
623+ if (modulusPrivate .compareTo (modulusPublic ) != 0 ) {
624+ throw new RuntimeException ("Wrong CSR returned" );
625+ }
577626 } else {
578627 throw new Exception ("failed to send CSR" , result .getException ());
579628 }
@@ -591,14 +640,25 @@ private static void createKeys() throws Exception {
591640 if (storePrivateKeyResult .isSuccess ()) {
592641 arbitraryDataProvider .storeOrUpdateKeyValue (account .name , EncryptionUtils .PRIVATE_KEY ,
593642 privateKeyString );
594- arbitraryDataProvider .storeOrUpdateKeyValue (account .name , EncryptionUtils .PUBLIC_KEY , publicKey );
643+ arbitraryDataProvider .storeOrUpdateKeyValue (account .name , EncryptionUtils .PUBLIC_KEY , publicKeyString );
595644 arbitraryDataProvider .storeOrUpdateKeyValue (account .name , EncryptionUtils .MNEMONIC ,
596645 generateMnemonicString ());
597646 } else {
598647 throw new RuntimeException ("Error uploading private key!" );
599648 }
600649 }
601650
651+ private static void deleteKeys () {
652+ RemoteOperationResult <PrivateKey > privateKeyRemoteOperationResult = new GetPrivateKeyOperation ().execute (client );
653+ RemoteOperationResult <String > publicKeyRemoteOperationResult = new GetPublicKeyOperation ().execute (client );
654+
655+ if (privateKeyRemoteOperationResult .isSuccess () || publicKeyRemoteOperationResult .isSuccess ()) {
656+ // delete keys
657+ assertTrue (new DeletePrivateKeyOperation ().execute (client ).isSuccess ());
658+ assertTrue (new DeletePublicKeyOperation ().execute (client ).isSuccess ());
659+ }
660+ }
661+
602662 private static String generateMnemonicString () {
603663 return "1 2 3 4 5 6" ;
604664 }
0 commit comments