Welcome to mirror list, hosted at ThFree Co, Russian Federation.

X509AttributeCertificateHolderSelector.java « selector « cert « bouncycastle « org « java « main « src « pkix - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c325fbad2200614877bba554650b5c9379273ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package org.bouncycastle.cert.selector;

import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;

import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.Target;
import org.bouncycastle.asn1.x509.TargetInformation;
import org.bouncycastle.asn1.x509.Targets;
import org.bouncycastle.cert.AttributeCertificateHolder;
import org.bouncycastle.cert.AttributeCertificateIssuer;
import org.bouncycastle.cert.X509AttributeCertificateHolder;
import org.bouncycastle.util.Selector;

/**
 * This class is an <code>Selector</code> like implementation to select
 * attribute certificates from a given set of criteria.
 */
public class X509AttributeCertificateHolderSelector
    implements Selector
{

    // TODO: name constraints???

    private final AttributeCertificateHolder holder;

    private final AttributeCertificateIssuer issuer;

    private final BigInteger serialNumber;

    private final Date attributeCertificateValid;

    private final X509AttributeCertificateHolder attributeCert;

    private final Collection targetNames;

    private final Collection targetGroups;

    X509AttributeCertificateHolderSelector(
        AttributeCertificateHolder holder,
        AttributeCertificateIssuer issuer,
        BigInteger serialNumber,
        Date attributeCertificateValid,
        X509AttributeCertificateHolder attributeCert,
        Collection targetNames,
        Collection targetGroups)
    {
        this.holder = holder;
        this.issuer = issuer;
        this.serialNumber = serialNumber;
        this.attributeCertificateValid = attributeCertificateValid;
        this.attributeCert = attributeCert;
        this.targetNames = targetNames;
        this.targetGroups = targetGroups;
    }

    /**
     * Decides if the given attribute certificate should be selected.
     *
     * @param obj The X509AttributeCertificateHolder which should be checked.
     * @return <code>true</code> if the attribute certificate is a match
     *         <code>false</code> otherwise.
     */
    public boolean match(Object obj)
    {
        if (!(obj instanceof X509AttributeCertificateHolder))
        {
            return false;
        }

        X509AttributeCertificateHolder attrCert = (X509AttributeCertificateHolder)obj;

        if (this.attributeCert != null)
        {
            if (!this.attributeCert.equals(attrCert))
            {
                return false;
            }
        }
        if (serialNumber != null)
        {
            if (!attrCert.getSerialNumber().equals(serialNumber))
            {
                return false;
            }
        }
        if (holder != null)
        {
            if (!attrCert.getHolder().equals(holder))
            {
                return false;
            }
        }
        if (issuer != null)
        {
            if (!attrCert.getIssuer().equals(issuer))
            {
                return false;
            }
        }

        if (attributeCertificateValid != null)
        {
            if (!attrCert.isValidOn(attributeCertificateValid))
            {
                return false;
            }
        }
        if (!targetNames.isEmpty() || !targetGroups.isEmpty())
        {
            Extension targetInfoExt = attrCert.getExtension(Extension.targetInformation);
            if (targetInfoExt != null)
            {
                TargetInformation targetinfo;
                try
                {
                    targetinfo = TargetInformation.getInstance(targetInfoExt.getParsedValue());
                }
                catch (IllegalArgumentException e)
                {
                    return false;
                }
                Targets[] targetss = targetinfo.getTargetsObjects();
                if (!targetNames.isEmpty())
                {
                    boolean found = false;

                    for (int i=0; i<targetss.length; i++)
                    {
                        Targets t = targetss[i];
                        Target[] targets = t.getTargets();
                        for (int j=0; j<targets.length; j++)
                        {
                            if (targetNames.contains(GeneralName.getInstance(targets[j]
                                                       .getTargetName())))
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
                        return false;
                    }
                }
                if (!targetGroups.isEmpty())
                {
                    boolean found = false;

                    for (int i=0; i<targetss.length; i++)
                    {
                        Targets t = targetss[i];
                        Target[] targets = t.getTargets();
                        for (int j=0; j<targets.length; j++)
                        {
                            if (targetGroups.contains(GeneralName.getInstance(targets[j]
                                                        .getTargetGroup())))
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * Returns a clone of this object.
     *
     * @return the clone.
     */
    public Object clone()
    {
        X509AttributeCertificateHolderSelector sel = new X509AttributeCertificateHolderSelector(
            holder, issuer, serialNumber, attributeCertificateValid, attributeCert, targetNames, targetGroups);

        return sel;
    }

    /**
     * Returns the attribute certificate holder which must be matched.
     *
     * @return Returns an X509AttributeCertificateHolder
     */
    public X509AttributeCertificateHolder getAttributeCert()
    {
        return attributeCert;
    }

    /**
     * Get the criteria for the validity.
     *
     * @return Returns the attributeCertificateValid.
     */
    public Date getAttributeCertificateValid()
    {
        if (attributeCertificateValid != null)
        {
            return new Date(attributeCertificateValid.getTime());
        }

        return null;
    }

    /**
     * Gets the holder.
     *
     * @return Returns the holder.
     */
    public AttributeCertificateHolder getHolder()
    {
        return holder;
    }

    /**
     * Returns the issuer criterion.
     *
     * @return Returns the issuer.
     */
    public AttributeCertificateIssuer getIssuer()
    {
        return issuer;
    }

    /**
     * Gets the serial number the attribute certificate must have.
     *
     * @return Returns the serialNumber.
     */
    public BigInteger getSerialNumber()
    {
        return serialNumber;
    }

    /**
     * Gets the target names. The collection consists of GeneralName objects.
     * <p>
     * The returned collection is immutable.
     *
     * @return The collection of target names
     */
    public Collection getTargetNames()
    {
        return targetNames;
    }

    /**
     * Gets the target groups. The collection consists of GeneralName objects.
     * <p>
     * The returned collection is immutable.
     *
     * @return The collection of target groups.
     */
    public Collection getTargetGroups()
    {
        return targetGroups;
    }
}