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

File AbstractBcX509ExtensionBuilder.java

 

Coverage histogram

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

Code metrics

8
22
5
1
111
66
10
0.45
4.4
5
2

Classes

Class Line # Actions
AbstractBcX509ExtensionBuilder 40 22 0% 10 9
0.7428571674.3%
 

Contributing tests

This file is covered by 4 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.Enumeration;
24   
25    import org.bouncycastle.asn1.ASN1Encodable;
26    import org.bouncycastle.asn1.ASN1Encoding;
27    import org.bouncycastle.asn1.ASN1ObjectIdentifier;
28    import org.bouncycastle.asn1.x509.Extension;
29    import org.bouncycastle.asn1.x509.Extensions;
30    import org.bouncycastle.asn1.x509.ExtensionsGenerator;
31    import org.xwiki.crypto.pkix.X509ExtensionBuilder;
32    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509Extensions;
33   
34    /**
35    * Base class for an extension builder based on Bouncy Castle.
36    *
37    * @version $Id: b78c74502d6063126b65cf5bbcf61ed546bfe469 $
38    * @since 5.4
39    */
 
40    public abstract class AbstractBcX509ExtensionBuilder implements X509ExtensionBuilder
41    {
42    private final ExtensionsGenerator extensions = new ExtensionsGenerator();
43   
 
44  0 toggle @Override
45    public X509ExtensionBuilder addExtension(String oid, boolean critical, byte[] value) throws IOException
46    {
47  0 this.extensions.addExtension(new ASN1ObjectIdentifier(oid), critical, value);
48  0 return this;
49    }
50   
 
51  12 toggle @Override
52    public X509ExtensionBuilder addExtensions(X509Extensions extensionSet) throws IOException
53    {
54  12 if (extensionSet == null) {
55  4 return this;
56    }
57   
58    // Optimisation
59  8 if (extensionSet instanceof BcX509Extensions) {
60  8 Extensions exts = ((BcX509Extensions) extensionSet).getExtensions();
61  8 @SuppressWarnings("unchecked")
62    Enumeration<ASN1ObjectIdentifier> oids = exts.oids();
63  22 while (oids.hasMoreElements()) {
64  14 ASN1ObjectIdentifier oid = oids.nextElement();
65  14 Extension ext = exts.getExtension(oid);
66  14 this.extensions.addExtension(ext.getExtnId(), ext.isCritical(), ext.getParsedValue());
67    }
68    } else {
69    // Fallback
70  0 for (String oid : extensionSet.getExtensionOID()) {
71  0 this.extensions.addExtension(new ASN1ObjectIdentifier(oid), extensionSet.isCritical(oid),
72    extensionSet.getExtensionValue(oid));
73    }
74    }
75  8 return this;
76    }
77   
78    /**
79    * Add an extension.
80    *
81    * @param oid the extension oid.
82    * @param critical true if the extension is critical.
83    * @param value the value of the extension.
84    * @return this extensions builder to allow chaining.
85    */
 
86  26 toggle public X509ExtensionBuilder addExtension(ASN1ObjectIdentifier oid, boolean critical, ASN1Encodable value)
87    {
88  26 try {
89  26 this.extensions.addExtension(oid, critical, value.toASN1Primitive().getEncoded(ASN1Encoding.DER));
90    } catch (IOException e) {
91    // Very unlikely
92  0 throw new IllegalArgumentException("Invalid extension value, it could not be properly DER encoded.");
93    }
94  26 return this;
95    }
96   
 
97  14 toggle @Override
98    public X509Extensions build()
99    {
100  14 if (this.extensions.isEmpty()) {
101  0 return null;
102    }
103  14 return new BcX509Extensions(this.extensions.generate());
104    }
105   
 
106  7 toggle @Override
107    public boolean isEmpty()
108    {
109  7 return this.extensions.isEmpty();
110    }
111    }