Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PolicyAttribute |
|
| 1.5;1.5 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.cert; | |
11 | ||
12 | import org.bouncycastle.asn1.ASN1Encodable; | |
13 | import org.bouncycastle.asn1.DERUTF8String; | |
14 | import org.bouncycastle.util.StreamParsingException; | |
15 | import org.bouncycastle.x509.X509Attribute; | |
16 | ||
17 | /** | |
18 | * A policy attribute of an attribute certificate contains a permis xml policy. | |
19 | * @since 0.1.0 | |
20 | */ | |
21 | public class PolicyAttribute | |
22 | implements Attribute | |
23 | { | |
24 | ||
25 | //---- Static | |
26 | ||
27 | /** | |
28 | * Policy attribute object identifier of an attribute certificate. | |
29 | * @since 0.3.0 | |
30 | */ | |
31 | public static final String OID = "1.2.826.0.1.3344810.1.1.13"; | |
32 | ||
33 | //---- State | |
34 | ||
35 | private final String policy; | |
36 | ||
37 | private final X509Attribute attribute; | |
38 | ||
39 | //---- Constructors | |
40 | ||
41 | /** | |
42 | * Creates a policy attribute from {@link X509Attribute}. | |
43 | * @throws StreamParsingException If the policy attribute could not be decoded correctly. | |
44 | * @since 0.3.0 | |
45 | */ | |
46 | 0 | public PolicyAttribute (X509Attribute attribute) throws StreamParsingException { |
47 | 0 | this.attribute = attribute; |
48 | 0 | this.policy = decode(attribute); |
49 | 0 | } |
50 | ||
51 | /** | |
52 | * Creates a policy attribute from object. | |
53 | * @since 0.3.0 | |
54 | */ | |
55 | 0 | public PolicyAttribute (String policy) { |
56 | 0 | this.policy = policy; |
57 | 0 | this.attribute = encode(this.policy); |
58 | 0 | } |
59 | ||
60 | //---- Methods | |
61 | ||
62 | /** | |
63 | * Returns the policy stored in this attribute. | |
64 | * @return the policy stored in this attribute. | |
65 | * @since 0.1.0 | |
66 | */ | |
67 | public String getPolicy () { | |
68 | 0 | return this.policy; |
69 | } | |
70 | ||
71 | /** | |
72 | * @since 0.3.0 | |
73 | */ | |
74 | private static String decode (X509Attribute attribute) throws StreamParsingException { | |
75 | 0 | final ASN1Encodable o = attribute.getValues()[0]; |
76 | 0 | if (o instanceof DERUTF8String) { |
77 | 0 | return ((DERUTF8String) o).getString(); |
78 | } | |
79 | 0 | throw new StreamParsingException("cannot decode policy attribute value", null); |
80 | } | |
81 | ||
82 | /** | |
83 | * @since 0.3.0 | |
84 | */ | |
85 | private static X509Attribute encode (String policy) { | |
86 | 0 | return new X509Attribute(OID, new DERUTF8String(policy)); |
87 | } | |
88 | ||
89 | //---- Attribute | |
90 | ||
91 | /** | |
92 | * Returns the attribute. | |
93 | * @return the attribute. | |
94 | * @since 0.3.0 | |
95 | */ | |
96 | public X509Attribute getAttribute () { | |
97 | 0 | return this.attribute; |
98 | } | |
99 | ||
100 | } |