Revision 273d747c

View differences:

res/menu/main.xml
4 4
    tools:context="info.guardianproject.checkey.MainActivity" >
5 5

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

  
18 19
</resources>
src/info/guardianproject/checkey/MainActivity.java
78 78
                setResult(RESULT_CANCELED);
79 79
                finish();
80 80
                return true;
81
            case R.id.generate_pin:
82
                generatePin(appEntry, intent);
83
                return true;
81 84
            case R.id.save:
82 85
                saveCertificate(appEntry, intent);
83 86
                return true;
......
169 172
        }
170 173
    }
171 174

  
175
    @SuppressLint("WorldReadableFiles")
176
    @SuppressWarnings("deprecation")
177
    private void generatePin(AppEntry appEntry, Intent intent) {
178
        String packageName = appEntry.getPackageName();
179
        try {
180
            for (X509Certificate x509 : getX509Certificates(this, packageName)) {
181
                Properties prop = new Properties();
182
                prop.load(new StringBufferInputStream(x509.getSubjectDN().getName()
183
                        .replaceAll(",", "\n")));
184
                prop.list(System.out);
185
                String name;
186
                if (prop.containsKey("OU") && prop.containsKey("O"))
187
                    name = (String) prop.get("OU") + prop.get("O");
188
                else if (prop.containsKey("O"))
189
                    name = (String) prop.get("O");
190
                else if (prop.containsKey("OU"))
191
                    name = (String) prop.get("OU");
192
                else if (prop.containsKey("CN"))
193
                    name = (String) prop.get("CN");
194
                else
195
                    name = "Unknown";
196
                name = name.replaceAll("[^A-Za-z0-9]", "");
197
                String fileName = name + "Pin.java";
198

  
199
                Log.i("AppListFragment", "subjectdn: " + x509.getSubjectDN().getName());
200
                final FileOutputStream os = openFileOutput(fileName,
201
                        Context.MODE_WORLD_READABLE);
202
                os.write(("\n\n"
203
                        + "import info.guardianproject.secureintents.CertificatePin;\n\n"
204
                        + "public final class "
205
                        + name
206
                        + "Pin extends CertificatePin {\n\n"
207
                        + "\tprotected final static byte[] certificate = ").getBytes());
208
                os.write(Arrays.toString(x509.getEncoded())
209
                        .replaceAll("\\[", "{").replaceAll("\\]", "}")
210
                        .getBytes());
211
                os.write(";\n}\n".getBytes());
212
                os.close();
213
                Log.i("AppListFragment", "wrote " + fileName);
214

  
215
                String subject = packageName + " - " + x509.getIssuerDN().getName()
216
                        + " - " + x509.getNotAfter();
217
                Uri uri = Uri.fromFile(getFileStreamPath(fileName));
218
                Intent i = new Intent(Intent.ACTION_SEND);
219
                i.setType("text/x-java-source");
220
                i.putExtra(Intent.EXTRA_STREAM, uri);
221
                i.putExtra(Intent.EXTRA_TITLE, subject);
222
                i.putExtra(Intent.EXTRA_SUBJECT, subject);
223
                startActivity(Intent.createChooser(i, getString(R.string.save_cert_using)));
224
            }
225
        } catch (CertificateException e) {
226
            e.printStackTrace();
227
        } catch (FileNotFoundException e) {
228
            e.printStackTrace();
229
        } catch (UnsupportedEncodingException e) {
230
            e.printStackTrace();
231
        } catch (IOException e) {
232
            e.printStackTrace();
233
        }
234
    }
235

  
172 236
    private void virustotal(AppEntry appEntry, Intent intent) {
173 237
        String urlString = "https://www.virustotal.com/en/file/"
174 238
                + Utils.getBinaryHash(appEntry.getApkFile(), "sha256") + "/analysis/";

Also available in: Unified diff