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

File BcExtensionUtils.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
54
7
1
195
112
17
0.31
7.71
7
2.43

Classes

Class Line # Actions
BcExtensionUtils 50 54 0% 17 12
0.830985983.1%
 

Contributing tests

This file is covered by 15 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.util.ArrayList;
23    import java.util.Collection;
24    import java.util.EnumSet;
25    import java.util.List;
26    import java.util.Set;
27   
28    import org.bouncycastle.asn1.ASN1ObjectIdentifier;
29    import org.bouncycastle.asn1.DERBitString;
30    import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
31    import org.bouncycastle.asn1.x509.GeneralName;
32    import org.bouncycastle.asn1.x509.GeneralNames;
33    import org.bouncycastle.asn1.x509.KeyPurposeId;
34    import org.xwiki.crypto.pkix.params.x509certificate.extension.ExtendedKeyUsages;
35    import org.xwiki.crypto.pkix.params.x509certificate.extension.KeyUsage;
36    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509DirectoryName;
37    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509DnsName;
38    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509GeneralName;
39    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509GenericName;
40    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509IpAddress;
41    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509Rfc822Name;
42    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509URI;
43   
44    /**
45    * Utility class for converting extension from/into Bouncy Castle equivalents.
46    *
47    * @version $Id: 3664306bf483ea878e35215df71b75dd577741e4 $
48    * @since 5.4
49    */
 
50    public final class BcExtensionUtils
51    {
 
52  0 toggle private BcExtensionUtils()
53    {
54    // Utility class.
55    }
56   
57    /**
58    * Convert general names from Bouncy Castle general names.
59    *
60    * @param genNames Bouncy castle general names.
61    * @return a list of X.509 general names.
62    */
 
63  1 toggle public static List<X509GeneralName> getX509GeneralNames(GeneralNames genNames)
64    {
65  1 if (genNames == null) {
66  0 return null;
67    }
68   
69  1 GeneralName[] names = genNames.getNames();
70  1 List<X509GeneralName> x509names = new ArrayList<X509GeneralName>(names.length);
71   
72  1 for (GeneralName name : names) {
73  15 switch (name.getTagNo()) {
74  2 case GeneralName.rfc822Name:
75  2 x509names.add(new X509Rfc822Name(name));
76  2 break;
77  1 case GeneralName.dNSName:
78  1 x509names.add(new X509DnsName(name));
79  1 break;
80  1 case GeneralName.directoryName:
81  1 x509names.add(new X509DirectoryName(name));
82  1 break;
83  2 case GeneralName.uniformResourceIdentifier:
84  2 x509names.add(new X509URI(name));
85  2 break;
86  9 case GeneralName.iPAddress:
87  9 x509names.add(new X509IpAddress(name));
88  9 break;
89  0 default:
90  0 x509names.add(new X509GenericName(name));
91  0 break;
92    }
93    }
94   
95  1 return x509names;
96    }
97   
98    /**
99    * Convert usages from Bouncy Castle.
100    *
101    * @param keyUsage the bouncy castle key usage to convert.
102    * @return the set of authorized usages.
103    */
 
104  27 toggle public static EnumSet<KeyUsage> getSetOfKeyUsage(org.bouncycastle.asn1.x509.KeyUsage keyUsage)
105    {
106  27 if (keyUsage == null) {
107  0 return null;
108    }
109   
110  27 Collection<KeyUsage> usages = new ArrayList<KeyUsage>();
111   
112  27 for (KeyUsage usage : KeyUsage.values()) {
113  243 if ((((DERBitString) keyUsage.toASN1Primitive()).intValue() & usage.value()) > 0) {
114  54 usages.add(usage);
115    }
116    }
117  27 return EnumSet.copyOf(usages);
118    }
119   
120    /**
121    * Convert extended usages from Bouncy Castle.
122    *
123    * @param usages the bouncy castle extended key usage to convert.
124    * @return the set of authorized usages.
125    */
 
126  4 toggle public static ExtendedKeyUsages getExtendedKeyUsages(ExtendedKeyUsage usages)
127    {
128  4 if (usages == null) {
129  0 return null;
130    }
131   
132  4 List<String> usageStr = new ArrayList<String>();
133   
134  4 for (KeyPurposeId keyPurposeId : usages.getUsages()) {
135  4 usageStr.add(keyPurposeId.getId());
136    }
137   
138  4 return new ExtendedKeyUsages(usageStr);
139    }
140   
141    /**
142    * Convert a collection of X.509 general names to Bouncy Castle general names.
143    *
144    * @param genNames a collection of X.509 general names.
145    * @return a bouncy castle general names.
146    */
 
147  2 toggle public static GeneralNames getGeneralNames(X509GeneralName[] genNames)
148    {
149  2 GeneralName[] names = new GeneralName[genNames.length];
150   
151  2 int i = 0;
152  2 for (X509GeneralName name : genNames) {
153  16 if (name instanceof BcGeneralName) {
154  16 names[i++] = ((BcGeneralName) name).getGeneralName();
155    } else {
156  0 throw new IllegalArgumentException("Unexpected general name: " + name.getClass().toString());
157    }
158    }
159   
160  2 return new GeneralNames(names);
161    }
162   
163    /**
164    * Convert a set of key usages to Bouncy Castle key usage.
165    *
166    * @param usages the set of authorized usages.
167    * @return a bit mask
168    */
 
169  6 toggle public static org.bouncycastle.asn1.x509.KeyUsage getKeyUsage(EnumSet<KeyUsage> usages)
170    {
171  6 int bitmask = 0;
172  6 for (KeyUsage usage : usages) {
173  12 bitmask |= usage.value();
174    }
175  6 return new org.bouncycastle.asn1.x509.KeyUsage(bitmask);
176    }
177   
178    /**
179    * Convert a set of extended key usages to Bouncy Castle extended key usage.
180    *
181    * @param usages the set of authorized usages.
182    * @return a bit mask
183    */
 
184  2 toggle public static ExtendedKeyUsage getExtendedKeyUsage(Set<String> usages)
185    {
186  2 KeyPurposeId[] keyUsages = new KeyPurposeId[usages.size()];
187  2 int i = 0;
188   
189  2 for (String usage : usages) {
190  2 keyUsages[i++] = KeyPurposeId.getInstance(new ASN1ObjectIdentifier(usage));
191    }
192   
193  2 return new ExtendedKeyUsage(keyUsages);
194    }
195    }