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.Build;
25 import android.os.Bundle;
26 import android.widget.Toast;
28 import org.strongswan.android.R;
29 import org.strongswan.android.data.VpnProfileDataSource;
30 import org.strongswan.android.logic.TrustedCertificateManager;
32 import java.io.FileNotFoundException;
33 import java.io.InputStream;
34 import java.security.KeyStore;
35 import java.security.cert.CertificateException;
36 import java.security.cert.CertificateFactory;
37 import java.security.cert.X509Certificate;
39 import androidx.activity.result.ActivityResultLauncher;
40 import androidx.activity.result.contract.ActivityResultContracts;
41 import androidx.appcompat.app.AlertDialog;
42 import androidx.appcompat.app.AppCompatActivity;
43 import androidx.appcompat.app.AppCompatDialogFragment;
44 import androidx.fragment.app.FragmentTransaction;
46 public class TrustedCertificateImportActivity extends AppCompatActivity
48 private static final String DIALOG_TAG = "Dialog";
49 private Uri mCertificateUri;
51 private final ActivityResultLauncher<Intent> mOpenDocument = registerForActivityResult(
52 new ActivityResultContracts.StartActivityForResult(),
54 if (result.getResultCode() == RESULT_OK && result.getData() != null)
56 mCertificateUri = result.getData().getData();
64 public void onCreate(Bundle savedInstanceState)
66 super.onCreate(savedInstanceState);
68 if (savedInstanceState != null)
69 { /* do nothing when we are restoring */
73 Intent intent = getIntent();
74 String action = intent.getAction();
75 if (Intent.ACTION_VIEW.equals(action))
77 importCertificate(intent.getData());
81 Intent openIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
82 openIntent.setType("*/*");
85 mOpenDocument.launch(openIntent);
87 catch (ActivityNotFoundException e)
88 { /* some devices are unable to browse for files */
95 protected void onPostResume()
98 if (mCertificateUri != null)
100 importCertificate(mCertificateUri);
101 mCertificateUri = null;
106 * Import the file pointed to by the given URI as a certificate.
110 private void importCertificate(Uri uri)
112 X509Certificate certificate = parseCertificate(uri);
113 if (certificate == null)
115 Toast.makeText(this, R.string.cert_import_failed, Toast.LENGTH_LONG).show();
119 /* Ask the user whether to import the certificate. This is particularly
120 * necessary because the import activity can be triggered by any app on
121 * the system. Also, if our app is the only one that is registered to
122 * open certificate files by MIME type the user would have no idea really
123 * where the file was imported just by reading the Toast we display. */
124 ConfirmImportDialog dialog = new ConfirmImportDialog();
125 Bundle args = new Bundle();
126 args.putSerializable(VpnProfileDataSource.KEY_CERTIFICATE, certificate);
127 dialog.setArguments(args);
128 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
129 ft.add(dialog, DIALOG_TAG);
134 * Load the file from the given URI and try to parse it as X.509 certificate.
137 * @return certificate or null
139 private X509Certificate parseCertificate(Uri uri)
141 X509Certificate certificate = null;
144 CertificateFactory factory = CertificateFactory.getInstance("X.509");
145 InputStream in = getContentResolver().openInputStream(uri);
146 certificate = (X509Certificate)factory.generateCertificate(in);
147 /* we don't check whether it's actually a CA certificate or not */
149 catch (CertificateException e)
153 catch (FileNotFoundException e)
162 * Try to store the given certificate in the KeyStore.
165 * @return whether it was successfully stored
167 private boolean storeCertificate(X509Certificate certificate)
171 KeyStore store = KeyStore.getInstance("LocalCertificateStore");
172 store.load(null, null);
173 store.setCertificateEntry(null, certificate);
174 TrustedCertificateManager.getInstance().reset();
185 * Class that displays a confirmation dialog when a certificate should get
186 * imported. If the user confirms the import we try to store it.
188 public static class ConfirmImportDialog extends AppCompatDialogFragment
191 public Dialog onCreateDialog(Bundle savedInstanceState)
193 final X509Certificate certificate;
195 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
197 certificate = getCertificateCompat(getArguments());
201 certificate = getArguments().getSerializable(VpnProfileDataSource.KEY_CERTIFICATE, X509Certificate.class);
204 return new AlertDialog.Builder(getActivity())
205 .setIcon(R.mipmap.ic_app)
206 .setTitle(R.string.import_certificate)
207 .setMessage(certificate.getSubjectDN().toString())
208 .setPositiveButton(R.string.import_certificate, new DialogInterface.OnClickListener()
211 public void onClick(DialogInterface dialog, int whichButton)
213 TrustedCertificateImportActivity activity = (TrustedCertificateImportActivity)getActivity();
214 if (activity.storeCertificate(certificate))
216 Toast.makeText(getActivity(), R.string.cert_imported_successfully, Toast.LENGTH_LONG).show();
217 getActivity().setResult(RESULT_OK);
221 Toast.makeText(getActivity(), R.string.cert_import_failed, Toast.LENGTH_LONG).show();
223 getActivity().finish();
226 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
229 public void onClick(DialogInterface dialog, int which)
231 getActivity().finish();
237 public void onCancel(DialogInterface dialog)
239 getActivity().finish();
242 @SuppressWarnings("deprecation")
243 private static X509Certificate getCertificateCompat(Bundle bundle)
245 return (X509Certificate)bundle.getSerializable(VpnProfileDataSource.KEY_CERTIFICATE);