summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/utils/RSAHelper.java
blob: 2872139a85bf96bc953bcf988e5b6289c31c8d5b (plain)
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
package se.leap.bitmaskclient.base.utils;

import android.os.Build;

import androidx.annotation.VisibleForTesting;

import org.spongycastle.util.encoders.Base64;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

import de.blinkt.openvpn.core.NativeUtils;

public class RSAHelper {

    public interface RSAHelperInterface {
        RSAPrivateKey parseRsaKeyFromString(String rsaKeyString);
    }

    public static class DefaultRSAHelper implements RSAHelperInterface {

        @Override
        public RSAPrivateKey parseRsaKeyFromString(String rsaKeyString) {
            RSAPrivateKey key;
            try {
                KeyFactory kf;
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
                    kf = KeyFactory.getInstance("RSA", "BC");
                } else {
                    kf = KeyFactory.getInstance("RSA");
                }
                rsaKeyString = rsaKeyString.replaceFirst("-----BEGIN RSA PRIVATE KEY-----", "").replaceFirst("-----END RSA PRIVATE KEY-----", "");
                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(rsaKeyString));
                key = (RSAPrivateKey) kf.generatePrivate(keySpec);
            } catch (InvalidKeySpecException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            } catch (NullPointerException e) {
                e.printStackTrace();
                return null;
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
                return null;
            }

            return key;
        }
    }

    private static RSAHelperInterface instance = new DefaultRSAHelper();

    @VisibleForTesting
    public RSAHelper(RSAHelperInterface helperInterface) {
        if (!NativeUtils.isUnitTest()) {
            throw new IllegalStateException("RSAHelper injected with RSAHelperInterface outside of an unit test");
        }
        instance = helperInterface;
    }

    public static RSAPrivateKey parseRsaKeyFromString(String rsaKeyString) {
       return instance.parseRsaKeyFromString(rsaKeyString);
    }
}