summaryrefslogtreecommitdiff
path: root/tlsexternalcertprovider/src/main/java/de/blinkt/externalcertprovider/ExternalCertService.java
blob: caf382dd4bda2f627603e32becc885a6b80dc9e0 (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
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
/*
 * Copyright (c) 2012-2018 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.externalcertprovider;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import de.blinkt.openvpn.api.ExternalCertificateProvider;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.StringReader;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

import static de.blinkt.externalcertprovider.SelectCertificateActivity.EXTRA_ALIAS;
import static de.blinkt.externalcertprovider.SelectCertificateActivity.EXTRA_DESCRIPTION;

/**
 * This is a VERY basic implementation.
 * It does not even check if the service is even allowed to use the API
 * see ExternalOpenVPNService for an example of checking caller's creditionals
 */
public class ExternalCertService extends Service {

    private final ExternalCertificateProvider.Stub mBinder = new ExternalCertificateProvider.Stub() {

        @Override
        public byte[] getSignedData(String alias, byte[] data) throws RemoteException {
            try {
                return SimpleSigner.signData(data);


            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            }
            // Something failed, return null
            return null;

        }

        @Override
        public byte[] getCertificateChain(String alias) throws RemoteException {

            return TextUtils.join("\n", SimpleSigner.certchain).getBytes();
        }

        @Override
        public Bundle getCertificateMetaData(String alias){
            Bundle b = new Bundle();
            b.putString(EXTRA_ALIAS, "mynicecert");
            b.putString(EXTRA_DESCRIPTION, "Super secret example key!");
            return b;
        }
    };



    @Override
    public void onCreate() {
        super.onCreate();

    }


    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}