Revision 24a4d539

View differences:

res/menu/context.xml
2 2
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
3 3

  
4 4
    <item
5
        android:id="@+id/virustotal"
5
        android:id="@+id/save"
6
        android:icon="@android:drawable/ic_menu_save"
6 7
        android:showAsAction="ifRoom|withText"
8
        android:title="@string/save"/>
9
    <item
10
        android:id="@+id/virustotal"
7 11
        android:icon="@drawable/virustotal"
12
        android:showAsAction="ifRoom|withText"
8 13
        android:title="@string/virustotal"/>
9 14
    <item
10 15
        android:id="@+id/by_apk_hash"
res/values/strings.xml
13 13
    <string name="share">Share</string>
14 14
    <string name="share_url_using">Share URL using:</string>
15 15
    <string name="save">Save…</string>
16
    <string name="save_cert_using">Save certificate using:</string>
16 17

  
17 18
</resources>
src/info/guardianproject/checkey/AppListFragment.java
20 20

  
21 21
package info.guardianproject.checkey;
22 22

  
23
import android.app.Activity;
24
import android.content.Context;
23 25
import android.content.Intent;
26
import android.content.pm.PackageInfo;
27
import android.content.pm.PackageManager;
28
import android.content.pm.PackageManager.NameNotFoundException;
29
import android.content.pm.Signature;
24 30
import android.net.Uri;
25 31
import android.os.Bundle;
26 32
import android.support.v4.app.ListFragment;
......
36 42
import android.widget.ListView;
37 43
import android.widget.Toast;
38 44

  
45
import java.io.ByteArrayInputStream;
46
import java.io.FileNotFoundException;
47
import java.io.FileOutputStream;
48
import java.io.IOException;
49
import java.io.InputStream;
50
import java.io.UnsupportedEncodingException;
39 51
import java.security.NoSuchAlgorithmException;
52
import java.security.cert.CertificateException;
53
import java.security.cert.CertificateFactory;
54
import java.security.cert.X509Certificate;
40 55
import java.util.List;
41 56

  
42 57
public class AppListFragment extends ListFragment implements LoaderCallbacks<List<AppEntry>> {
43 58

  
59
    private PackageManager pm;
44 60
    private AppListAdapter adapter;
45 61
    private ActionMode actionMode;
46 62
    private ListView listView;
......
91 107
        selectedItem = position;
92 108
    }
93 109

  
110
    private void saveCertificate(AppEntry appEntry, Intent intent) {
111
        if (pm == null)
112
            pm = getActivity().getApplicationContext().getPackageManager();
113
        Activity activity = getActivity();
114
        String packageName = appEntry.getPackageName();
115
        PackageInfo pkgInfo;
116
        try {
117
            pkgInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
118
            CertificateFactory certFactory = CertificateFactory.getInstance("X509");
119
            for (Signature sig : pkgInfo.signatures) {
120
                byte[] cert = sig.toByteArray();
121
                InputStream inStream = new ByteArrayInputStream(cert);
122
                X509Certificate x509 = (X509Certificate) certFactory.generateCertificate(inStream);
123

  
124
                String fileName = packageName + ".cer";
125
                final FileOutputStream os = activity.openFileOutput(fileName,
126
                        Context.MODE_WORLD_READABLE);
127
                os.write(cert);
128
                os.close();
129

  
130
                String subject = packageName + " - " + x509.getIssuerDN().getName()
131
                        + " - " + x509.getNotAfter();
132
                Uri uri = Uri.fromFile(activity.getFileStreamPath(fileName));
133
                Intent i = new Intent(Intent.ACTION_SEND);
134
                i.setType("application/pkix-cert");
135
                i.putExtra(Intent.EXTRA_STREAM, uri);
136
                i.putExtra(Intent.EXTRA_TITLE, subject);
137
                i.putExtra(Intent.EXTRA_SUBJECT, subject);
138
                startActivity(Intent.createChooser(i, getString(R.string.save_cert_using)));
139
            }
140
        } catch (NameNotFoundException e) {
141
            e.printStackTrace();
142
        } catch (CertificateException e) {
143
            e.printStackTrace();
144
        } catch (FileNotFoundException e) {
145
            e.printStackTrace();
146
        } catch (UnsupportedEncodingException e) {
147
            e.printStackTrace();
148
        } catch (IOException e) {
149
            e.printStackTrace();
150
        }
151
    }
152

  
94 153
    private void virustotal(AppEntry appEntry, Intent intent) {
95 154
        String urlString = "https://www.virustotal.com/en/file/"
96 155
                + Utils.getBinaryHash(appEntry.getApkFile(), "sha256") + "/analysis/";
......
178 237
            Intent intent = new Intent(getActivity(), WebViewActivity.class);
179 238
            intent.putExtra(Intent.EXTRA_TEXT, appEntry.getLabel());
180 239
            switch (item.getItemId()) {
240
                case R.id.save:
241
                    saveCertificate(appEntry, intent);
242
                    break;
181 243
                case R.id.virustotal:
182 244
                    virustotal(appEntry, intent);
183 245
                    break;

Also available in: Unified diff