Enable search for certificate lists (via SearchView in ActionBar)
[strongswan.git] / src / frontends / android / src / org / strongswan / android / data / TrustedCertificateEntry.java
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.data;
17
18 import java.security.cert.X509Certificate;
19
20 import android.net.http.SslCertificate;
21
22 public class TrustedCertificateEntry implements Comparable<TrustedCertificateEntry>
23 {
24 private final X509Certificate mCert;
25 private final String mAlias;
26 private String mSubjectPrimary;
27 private String mSubjectSecondary = "";
28 private String mString;
29
30 /**
31 * Create an entry for certificate lists.
32 *
33 * @param alias alias of the certificate (as used in the KeyStore)
34 * @param cert certificate associated with that alias
35 */
36 public TrustedCertificateEntry(String alias, X509Certificate cert)
37 {
38 mCert = cert;
39 mAlias = alias;
40
41 SslCertificate ssl = new SslCertificate(mCert);
42 String o = ssl.getIssuedTo().getOName();
43 String ou = ssl.getIssuedTo().getUName();
44 String cn = ssl.getIssuedTo().getCName();
45 if (!o.isEmpty())
46 {
47 mSubjectPrimary = o;
48 if (!cn.isEmpty())
49 {
50 mSubjectSecondary = cn;
51 }
52 else if (!ou.isEmpty())
53 {
54 mSubjectSecondary = ou;
55 }
56 }
57 else if (!cn.isEmpty())
58 {
59 mSubjectPrimary = cn;
60 }
61 else
62 {
63 mSubjectPrimary = ssl.getIssuedTo().getDName();
64 }
65 }
66
67 /**
68 * The main subject of this certificate (O, CN or the complete DN, whatever
69 * is found first).
70 *
71 * @return the main subject
72 */
73 public String getSubjectPrimary()
74 {
75 return mSubjectPrimary;
76 }
77
78 /**
79 * Get the secondary subject of this certificate (either CN or OU if primary
80 * subject is O, empty otherwise)
81 *
82 * @return the secondary subject
83 */
84 public String getSubjectSecondary()
85 {
86 return mSubjectSecondary;
87 }
88
89 /**
90 * The alias associated with this certificate.
91 *
92 * @return KeyStore alias of this certificate
93 */
94 public String getAlias()
95 {
96 return mAlias;
97 }
98
99 /**
100 * The certificate.
101 *
102 * @return certificate
103 */
104 public X509Certificate getCertificate()
105 {
106 return mCert;
107 }
108
109 @Override
110 public String toString()
111 { /* combination of both subject lines, used for filtering lists */
112 if (mString == null)
113 {
114 mString = mSubjectPrimary;
115 if (!mSubjectSecondary.isEmpty())
116 {
117 mString += ", " + mSubjectSecondary;
118 }
119 }
120 return mString;
121 }
122
123 @Override
124 public int compareTo(TrustedCertificateEntry another)
125 {
126 int diff = mSubjectPrimary.compareToIgnoreCase(another.mSubjectPrimary);
127 if (diff == 0)
128 {
129 diff = mSubjectSecondary.compareToIgnoreCase(another.mSubjectSecondary);
130 }
131 return diff;
132 }
133 }