1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.pkix.internal.extension

File BcX509Extensions.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
34
17
1
190
128
23
0.68
2
17
1.35

Classes

Class Line # Actions
BcX509Extensions 48 34 0% 23 14
0.777777877.8%
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.pkix.internal.extension;
21   
22    import java.io.IOException;
23    import java.util.ArrayList;
24    import java.util.EnumSet;
25    import java.util.Enumeration;
26    import java.util.List;
27   
28    import org.bouncycastle.asn1.ASN1ObjectIdentifier;
29    import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
30    import org.bouncycastle.asn1.x509.BasicConstraints;
31    import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
32    import org.bouncycastle.asn1.x509.Extension;
33    import org.bouncycastle.asn1.x509.Extensions;
34    import org.bouncycastle.asn1.x509.GeneralNames;
35    import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
36    import org.bouncycastle.asn1.x509.X509Extension;
37    import org.xwiki.crypto.pkix.params.x509certificate.extension.ExtendedKeyUsages;
38    import org.xwiki.crypto.pkix.params.x509certificate.extension.KeyUsage;
39    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509Extensions;
40    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509GeneralName;
41   
42    /**
43    * Set of X.509 extensions.
44    *
45    * @version $Id: 0b1cc9ea7ff91c197243942ef5c2986558717e88 $
46    * @since 5.4
47    */
 
48    public class BcX509Extensions implements X509Extensions
49    {
50    private final Extensions extensions;
51   
52    /**
53    * Build an extension set based on a Bouncy Castle one.
54    *
55    * @param extensions a Bouncy Castle extensions set.
56    */
 
57  300 toggle public BcX509Extensions(Extensions extensions)
58    {
59  300 this.extensions = extensions;
60    }
61   
62    /**
63    * @return the bouncy castle wrapped object.
64    */
 
65  14 toggle public Extensions getExtensions()
66    {
67  14 return this.extensions;
68    }
69   
 
70  0 toggle @Override
71    public byte[] getExtensionValue(String oid)
72    {
73  0 Extension ext = this.extensions.getExtension(new ASN1ObjectIdentifier(oid));
74   
75  0 if (ext == null) {
76  0 return null;
77    }
78   
79  0 return ext.getExtnValue().getOctets();
80    }
81   
 
82  12 toggle @Override
83    public boolean isCritical(String oid)
84    {
85  12 Extension ext = this.extensions.getExtension(new ASN1ObjectIdentifier(oid));
86   
87  12 return ext != null && ext.isCritical();
88    }
89   
 
90  1 toggle @Override
91    public String[] getExtensionOID()
92    {
93  1 List<String> oids = new ArrayList<String>();
94   
95  1 @SuppressWarnings("unchecked")
96    Enumeration<ASN1ObjectIdentifier> extOids = this.extensions.oids();
97  6 while (extOids.hasMoreElements()) {
98  5 oids.add(extOids.nextElement().getId());
99    }
100   
101  1 return oids.toArray(new String[oids.size()]);
102    }
103   
 
104  1 toggle @Override
105    public String[] getCriticalExtensionOID()
106    {
107  1 ASN1ObjectIdentifier[] asnoids = this.extensions.getCriticalExtensionOIDs();
108  1 return toStringArray(asnoids);
109    }
110   
 
111  1 toggle @Override
112    public String[] getNonCriticalExtensionOID()
113    {
114  1 ASN1ObjectIdentifier[] asnoids = this.extensions.getNonCriticalExtensionOIDs();
115  1 return toStringArray(asnoids);
116    }
117   
 
118  2 toggle private String[] toStringArray(ASN1ObjectIdentifier[] asnoids)
119    {
120  2 String[] oids = new String[asnoids.length];
121   
122  7 for (int i = 0; i < asnoids.length; i++) {
123  5 oids[i] = asnoids[i].getId();
124    }
125   
126  2 return oids;
127    }
128   
 
129  0 toggle @Override
130    public byte[] getEncoded() throws IOException
131    {
132  0 return this.extensions.getEncoded();
133    }
134   
 
135  30 toggle @Override
136    public boolean hasCertificateAuthorityBasicConstraints()
137    {
138  30 BasicConstraints bc = BasicConstraints.fromExtensions(this.extensions);
139   
140  30 return bc != null && bc.isCA();
141    }
142   
 
143  2 toggle @Override
144    public int getBasicConstraintsPathLen()
145    {
146  2 BasicConstraints bc = BasicConstraints.fromExtensions(this.extensions);
147   
148  2 return (bc != null) ? bc.getPathLenConstraint().intValue() : -1;
149    }
150   
 
151  27 toggle @Override
152    public EnumSet<KeyUsage> getKeyUsage()
153    {
154  27 return BcExtensionUtils.getSetOfKeyUsage(org.bouncycastle.asn1.x509.KeyUsage.fromExtensions(this.extensions));
155    }
156   
 
157  4 toggle @Override
158    public ExtendedKeyUsages getExtendedKeyUsage()
159    {
160  4 return BcExtensionUtils.getExtendedKeyUsages(ExtendedKeyUsage.fromExtensions(this.extensions));
161    }
162   
 
163  44 toggle @Override
164    public byte[] getAuthorityKeyIdentifier()
165    {
166  44 AuthorityKeyIdentifier id = AuthorityKeyIdentifier.fromExtensions(this.extensions);
167  44 return (id != null) ? id.getKeyIdentifier() : null;
168    }
169   
 
170  220 toggle @Override
171    public byte[] getSubjectKeyIdentifier()
172    {
173  220 SubjectKeyIdentifier id = SubjectKeyIdentifier.fromExtensions(this.extensions);
174  220 return (id != null) ? id.getKeyIdentifier() : null;
175    }
176   
 
177  1 toggle @Override
178    public List<X509GeneralName> getSubjectAltName()
179    {
180  1 return BcExtensionUtils.getX509GeneralNames(GeneralNames.fromExtensions(this.extensions,
181    X509Extension.subjectAlternativeName));
182    }
183   
 
184  0 toggle @Override
185    public List<X509GeneralName> getIssuerAltName()
186    {
187  0 return BcExtensionUtils.getX509GeneralNames(GeneralNames.fromExtensions(this.extensions,
188    X509Extension.issuerAlternativeName));
189    }
190    }