summaryrefslogtreecommitdiffstats
path: root/proxy/code/src/com/redhat/rhn/common/cert/Certificate.java
blob: 3b5f22a8558e3ff8fac67b515cec38ae79d5e1f9 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
 * Copyright (c) 2009 Red Hat, Inc.
 *
 * This software is licensed to you under the GNU General Public License,
 * version 2 (GPLv2). There is NO WARRANTY for this software, express or
 * implied, including the implied warranties of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
 * along with this software; if not, see
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 * Red Hat trademarks are not licensed under GPLv2. No permission is
 * granted to use or replicate Red Hat trademarks that are incorporated
 * in this software or its documentation.
 */
package com.redhat.rhn.common.cert;

import com.redhat.rhn.common.cert.XmlTag;

import java.security.SignatureException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * A server certificate.
 *
 * Borrowed from project Spacewalk: http://spacewalk.redhat.com
 */
public class Certificate {

    public static final String CURRENT_GENERATION = "2";
    // Some of these properties should probably be int's or Date's
    // But until that is really needed, we'll stick with Strings

    private String product;
    private String owner;
    private String issued;
    private String expires;
    private String slots;
    private String provisioningSlots;
    private String monitoringSlots;
    private String virtualizationSlots;
    private String virtualizationPlatformSlots;
    private String nonlinuxSlots;
    private String satelliteVersion;
    private String generation;
    private String signature;
    private List channelFamilies;
    private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

    /**
     * Construct an empty certificate.
     */
    Certificate() {
        channelFamilies = new ArrayList();
    }

    /**
     * States whether or not this certificate is expired.
     * @return true iff <code>expires</code> is after current system time
     * @throws ParseException this shouldn't happen, means the date format in
     * the cert is not what we expect
     */
    public boolean isExpired() throws ParseException {
        Date now = Calendar.getInstance().getTime();
        return now.after(this.getExpiresDate());
    }

    /**
     * Convert this certificate into the canonical form used for signing.
     * 
     * @return the certificate in the canonical form used for signing
     */
    public String asChecksumString() {
        StringBuffer result = new StringBuffer();
        // Fields must appear in the output in alphabetical order
        // The channelFamilies are sorted in their very own way
        // (see ChannelFamily.compareTo)
        Collections.sort(channelFamilies);
        for (int i = 0; i < channelFamilies.size(); i++) {
            ChannelFamilyDescriptor cf = (ChannelFamilyDescriptor) channelFamilies.get(i);
            result.append(cf.asChecksumString()).append("\n");
        }
        appendField(result, "expires", getExpires());
        appendField(result, "generation", getGeneration());
        appendField(result, "issued", getIssued());
        appendField(result, "monitoring-slots", getMonitoringSlots());
        appendField(result, "nonlinux-slots", getNonlinuxSlots());
        appendField(result, "owner", getOwner());
        appendField(result, "product", getProduct());
        appendField(result, "provisioning-slots", getProvisioningSlots());
        appendField(result, "satellite-version", getSatelliteVersion());
        appendField(result, "slots", getSlots());
        appendField(result, "virtualization_host", this.getVirtualizationSlots());
        appendField(result, "virtualization_host_platform", this
                .getVirtualizationPlatformSlots());
        return result.toString();
    }

    /**
     * Returns the XML representation of the cert.
     * @return the XML representation of the cert.
     */
    public String asXmlString() {
        StringBuffer buf = new StringBuffer();

        buf.append(XML_HEADER).append("\n");

        XmlTag t = new XmlTag("rhn-cert");
        t.setAttribute("version", "0.1");
        buf.append(t.renderOpenTag()).append("\n");

        appendXmlField(buf, "product", getProduct());
        appendXmlField(buf, "owner", getOwner());
        appendXmlField(buf, "issued", getIssued());
        appendXmlField(buf, "expires", getExpires());
        appendXmlField(buf, "slots", getSlots());
        appendXmlField(buf, "monitoring-slots", getMonitoringSlots());
        appendXmlField(buf, "provisioning-slots", getProvisioningSlots());
        appendXmlField(buf, "nonlinux-slots", getNonlinuxSlots());
        appendXmlField(buf, "virtualization_host", this.getVirtualizationSlots());
        appendXmlField(buf, "virtualization_host_platform", this
                .getVirtualizationPlatformSlots());

        for (int i = 0; i < channelFamilies.size(); i++) {
            ChannelFamilyDescriptor cf = (ChannelFamilyDescriptor) channelFamilies.get(i);
            buf.append("  ").append(cf.asXmlString()).append("\n");
        }

        appendXmlField(buf, "satellite-version", getSatelliteVersion());
        appendXmlField(buf, "generation", getGeneration());

        XmlTag sig = new XmlTag("rhn-cert-signature");
        sig.addBody(getSignature());
        buf.append("  ").append(sig.render()).append("\n");

        buf.append(t.renderCloseTag()).append("\n");
        return buf.toString();
    }

    private void appendXmlField(StringBuffer result, String fieldName, String value) {
        if (value != null) {
            XmlTag t = new XmlTag("rhn-cert-field", false);
            t.setAttribute("name", fieldName);
            t.addBody(value);
            result.append("  ").append(t.render()).append('\n');
        }
    }

    /**
     * Check that this certificate was signed with a private key whose public
     * counterpart is on <code>keyRing</code>
     * @param keyRing the public keys with which to check the signatures
     * @return <code>true</code> if this certificate was signed with a private
     * key whose public counterpart is on <code>keyRing</code>,
     * <code>false</code> otherwise
     * @throws SignatureException if processing the sigature fails
     */
    public boolean verifySignature(PublicKeyRing keyRing) throws SignatureException {
        return keyRing.verifySignature(asChecksumString(), getSignature());
    }

    // Setters. They shouldn't be public, but have to be
    // since we use reflection to populate the certificate
    // when reading from XML.

    /**
     * Add a channel family
     * @param family the channel family to add
     */
    public void addChannelFamily(ChannelFamilyDescriptor family) {
        channelFamilies.add(family);
    }

    /**
     * Set the expiration date for the certificate
     * @param expires0 the expiration date
     */
    public void setExpires(String expires0) {
        expires = expires0;
    }

    /**
     * Set the generation
     * @param generation0 the generation
     */
    public void setGeneration(String generation0) {
        generation = generation0;
    }

    /**
     * Set the date of issue
     * @param issued0 date of issue
     */
    public void setIssued(String issued0) {
        issued = issued0;
    }

    /**
     * Set the number of non-linux slots
     * @param nonlinuxSlots0 the number of non-linux slots
     */
    public void setNonlinuxSlots(String nonlinuxSlots0) {
        nonlinuxSlots = nonlinuxSlots0;
    }

    /**
     * Set the number of monitoring slots
     * @param monitoringSlots0 the number of monitoring slots
     */
    public void setMonitoringSlots(String monitoringSlots0) {
        monitoringSlots = monitoringSlots0;
    }

    /**
     * Set the owner
     * @param owner0 the owner
     */
    public void setOwner(String owner0) {
        owner = owner0;
    }

    /**
     * Set the product
     * @param product0 the product
     */
    public void setProduct(String product0) {
        product = product0;
    }

    /**
     * Set the number of provisioning slots
     * @param provisioningSlots0 the number of provisioning slots
     */
    public void setProvisioningSlots(String provisioningSlots0) {
        provisioningSlots = provisioningSlots0;
    }

    /**
     * Set the satellite version
     * @param satelliteVersion0 the satellite version
     */
    public void setSatelliteVersion(String satelliteVersion0) {
        satelliteVersion = satelliteVersion0;
    }

    /**
     * Set the signature as an ASCII armored string
     * @param signature0 the ASCII armored signature
     */
    public void setSignature(String signature0) {
        signature = signature0;
    }

    /**
     * Set the number of slots
     * @param slots0 the number of slots
     */
    public void setSlots(String slots0) {
        slots = slots0;
    }

    /**
     * Set the number of virtualization slots
     * @param virtualizationSlots0 the number of virtualization slots
     */
    public void setVirtualizationSlots(String virtualizationSlots0) {
        virtualizationSlots = virtualizationSlots0;
    }

    /**
     * Set the number of virtualization platform slots
     * @param virtualizationSlots0 the number of virtualization platform slots
     */
    public void setVirtualizationPlatformSlots(String virtualizationSlots0) {
        virtualizationPlatformSlots = virtualizationSlots0;
    }

    // Getters

    /**
     * Return an unmodifiable list of the channel families
     * @return an unmodifiable list of the channel families
     */
    public List getChannelFamilies() {
        return Collections.unmodifiableList(channelFamilies);
    }

    /**
     * Return the channel family with name <code>family</code>, or
     * <code>null</code> if no such family exists.
     * @param family the name of the family
     * @return the channel family with name <code>family</code>, or
     * <code>null</code> if no such family exists.
     */
    public ChannelFamilyDescriptor getChannelFamily(String family) {
        for (int i = 0; i < channelFamilies.size(); i++) {
            ChannelFamilyDescriptor f = (ChannelFamilyDescriptor) channelFamilies.get(i);
            if (f.getFamily().equals(family)) {
                return f;
            }
        }
        return null;
    }

    /**
     * Get the expiration date
     * @return the expiration date
     */
    public String getExpires() {
        return expires;
    }

    /**
     * Convenience function, returns java.util.Date equivalent of
     * <code>expires</code>
     * @return Date obj equivalent of <code>expires</code>
     * @throws ParseException format of <code>expires</code> not what we
     * expected
     */
    public Date getExpiresDate() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(expires);
    }

    /**
     * Get the generation
     * @return the generation
     */
    public String getGeneration() {
        return generation;
    }

    /**
     * Get the issue date
     * @return the issue date
     */
    public String getIssued() {
        return issued;
    }

    /**
     * Get the nonlinux slots
     * @return the nonlinux slots
     */
    public String getNonlinuxSlots() {
        return nonlinuxSlots;
    }

    /**
     * Get the monitoring slots
     * @return the monitoring slots
     */
    public String getMonitoringSlots() {
        return monitoringSlots;
    }

    /**
     * Get the owner
     * @return the owner
     */
    public String getOwner() {
        return owner;
    }

    /**
     * Get the product
     * @return the product
     */
    public String getProduct() {
        return product;
    }

    /**
     * Get the number of provisioning slots
     * @return the number of provisioning slots
     */
    public String getProvisioningSlots() {
        return provisioningSlots;
    }

    /**
     * Get the satellite version
     * @return the satellite version
     */
    public String getSatelliteVersion() {
        return satelliteVersion;
    }

    /**
     * Get the ASCII armoured signature
     * @return the ASCII armoured signature
     */
    public String getSignature() {
        return signature;
    }

    /**
     * Get the number of slots
     * @return the number of slots
     */
    public String getSlots() {
        return slots;
    }

    /**
     * Get the number of virtualization slots
     * @return the number of virtualization slots
     */
    public String getVirtualizationSlots() {
        return virtualizationSlots;
    }

    /**
     * Get the number of virtualization platform slots
     * @return the number of virtualizationPlatform slots
     */
    public String getVirtualizationPlatformSlots() {
        return virtualizationPlatformSlots;
    }

    private void appendField(StringBuffer result, String fieldName, String value) {
        if (value != null) {
            result.append(fieldName).append("-").append(value).append('\n');
        }
    }
}