2 * Copyright (C) 2014 Tobias Brunner
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 package org.strongswan.android.ui;
19 import android.app.Dialog;
20 import android.content.ActivityNotFoundException;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.widget.Toast;
27 import org.strongswan.android.R;
28 import org.strongswan.android.data.VpnProfileDataSource;
29 import org.strongswan.android.logic.TrustedCertificateManager;
31 import java.io.FileNotFoundException;
32 import java.io.InputStream;
33 import java.security.KeyStore;
34 import java.security.cert.CertificateException;
35 import java.security.cert.CertificateFactory;
36 import java.security.cert.X509Certificate;
38 import androidx.activity.result.ActivityResultLauncher;
39 import androidx.activity.result.contract.ActivityResultContracts;
40 import androidx.appcompat.app.AlertDialog;
41 import androidx.appcompat.app.AppCompatActivity;
42 import androidx.appcompat.app.AppCompatDialogFragment;
43 import androidx.fragment.app.FragmentTransaction;
45 public class TrustedCertificateImportActivity extends AppCompatActivity
47 private static final String DIALOG_TAG = "Dialog";
48 private Uri mCertificateUri;
50 private final ActivityResultLauncher<Intent> mOpenDocument = registerForActivityResult(
51 new ActivityResultContracts.StartActivityForResult(),
53 if (result.getResultCode() == RESULT_OK && result.getData() != null)
55 mCertificateUri = result.getData().getData();
63 public void onCreate(Bundle savedInstanceState)
65 super.onCreate(savedInstanceState);
67 if (savedInstanceState != null)
68 { /* do nothing when we are restoring */
72 Intent intent = getIntent();
73 String action = intent.getAction();
74 if (Intent.ACTION_VIEW.equals(action))
76 importCertificate(intent.getData());
80 Intent openIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
81 openIntent.setType("*/*");
84 mOpenDocument.launch(openIntent);
86 catch (ActivityNotFoundException e)
87 { /* some devices are unable to browse for files */
94 protected void onPostResume()
97 if (mCertificateUri != null)
99 importCertificate(mCertificateUri);
100 mCertificateUri = null;
105 * Import the file pointed to by the given URI as a certificate.
109 private void importCertificate(Uri uri)
111 X509Certificate certificate = parseCertificate(uri);
112 if (certificate == null)
114 Toast.makeText(this, R.string.cert_import_failed, Toast.LENGTH_LONG).show();
118 /* Ask the user whether to import the certificate. This is particularly
119 * necessary because the import activity can be triggered by any app on
120 * the system. Also, if our app is the only one that is registered to
121 * open certificate files by MIME type the user would have no idea really
122 * where the file was imported just by reading the Toast we display. */
123 ConfirmImportDialog dialog = new ConfirmImportDialog();
124 Bundle args = new Bundle();
125 args.putSerializable(VpnProfileDataSource.KEY_CERTIFICATE, certificate);
126 dialog.setArguments(args);
127 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
128 ft.add(dialog, DIALOG_TAG);
133 * Load the file from the given URI and try to parse it as X.509 certificate.
136 * @return certificate or null
138 private X509Certificate parseCertificate(Uri uri)
140 X509Certificate certificate = null;
143 CertificateFactory factory = CertificateFactory.getInstance("X.509");
144 InputStream in = getContentResolver().openInputStream(uri);
145 certificate = (X509Certificate)factory.generateCertificate(in);
146 /* we don't check whether it's actually a CA certificate or not */
148 catch (CertificateException e)
152 catch (FileNotFoundException e)
161 * Try to store the given certificate in the KeyStore.
164 * @return whether it was successfully stored
166 private boolean storeCertificate(X509Certificate certificate)
170 KeyStore store = KeyStore.getInstance("LocalCertificateStore");
171 store.load(null, null);
172 store.setCertificateEntry(null, certificate);
173 TrustedCertificateManager.getInstance().reset();
184 * Class that displays a confirmation dialog when a certificate should get
185 * imported. If the user confirms the import we try to store it.
187 public static class ConfirmImportDialog extends AppCompatDialogFragment
190 public Dialog onCreateDialog(Bundle savedInstanceState)
192 final X509Certificate certificate;
194 certificate = (X509Certificate)getArguments().getSerializable(VpnProfileDataSource.KEY_CERTIFICATE);
196 return new AlertDialog.Builder(getActivity())
197 .setIcon(R.mipmap.ic_app)
198 .setTitle(R.string.import_certificate)
199 .setMessage(certificate.getSubjectDN().toString())
200 .setPositiveButton(R.string.import_certificate, new DialogInterface.OnClickListener()
203 public void onClick(DialogInterface dialog, int whichButton)
205 TrustedCertificateImportActivity activity = (TrustedCertificateImportActivity)getActivity();
206 if (activity.storeCertificate(certificate))
208 Toast.makeText(getActivity(), R.string.cert_imported_successfully, Toast.LENGTH_LONG).show();
209 getActivity().setResult(RESULT_OK);
213 Toast.makeText(getActivity(), R.string.cert_import_failed, Toast.LENGTH_LONG).show();
215 getActivity().finish();
218 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
221 public void onClick(DialogInterface dialog, int which)
223 getActivity().finish();
229 public void onCancel(DialogInterface dialog)
231 getActivity().finish();