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

File BcX509v3TBSCertificateBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
26
10
1
159
96
14
0.54
2.6
10
1.4

Classes

Class Line # Actions
BcX509v3TBSCertificateBuilder 44 26 0% 14 1
0.9761904597.6%
 

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;
21   
22    import java.io.IOException;
23    import java.math.BigInteger;
24    import java.util.Date;
25   
26    import org.bouncycastle.asn1.ASN1Integer;
27    import org.bouncycastle.asn1.x509.TBSCertificate;
28    import org.bouncycastle.asn1.x509.Time;
29    import org.bouncycastle.asn1.x509.V3TBSCertificateGenerator;
30    import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters;
31    import org.xwiki.crypto.pkix.internal.extension.BcX509Extensions;
32    import org.xwiki.crypto.pkix.internal.extension.DefaultX509ExtensionBuilder;
33    import org.xwiki.crypto.pkix.params.CertifiedPublicKey;
34    import org.xwiki.crypto.pkix.params.PrincipalIndentifier;
35    import org.xwiki.crypto.pkix.params.x509certificate.extension.X509Extensions;
36    import org.xwiki.crypto.signer.Signer;
37   
38    /**
39    * To Be Signed version 3 certificate builder.
40    *
41    * @version $Id: a058835d95a0ccf8f1da8a1000236bb1c263e57f $
42    * @since 5.4
43    */
 
44    public class BcX509v3TBSCertificateBuilder implements BcX509TBSCertificateBuilder
45    {
46    private final V3TBSCertificateGenerator tbsGen = new V3TBSCertificateGenerator();
47   
 
48  7 toggle @Override
49    public BcX509TBSCertificateBuilder setSerialNumber(BigInteger serial)
50    {
51  7 this.tbsGen.setSerialNumber(new ASN1Integer(serial));
52  7 return this;
53    }
54   
 
55  7 toggle @Override
56    public BcX509TBSCertificateBuilder setSubjectPublicKeyInfo(PublicKeyParameters subject)
57    {
58  7 this.tbsGen.setSubjectPublicKeyInfo(BcUtils.getSubjectPublicKeyInfo(subject));
59  7 return this;
60    }
61   
 
62  7 toggle @Override
63    public BcX509TBSCertificateBuilder setIssuer(PrincipalIndentifier issuer)
64    {
65  7 this.tbsGen.setIssuer(BcUtils.getX500Name(issuer));
66  7 return this;
67    }
68   
 
69  7 toggle @Override
70    public BcX509TBSCertificateBuilder setSubject(PrincipalIndentifier subject)
71    {
72  7 this.tbsGen.setSubject(BcUtils.getX500Name(subject));
73  7 return this;
74    }
75   
 
76  7 toggle @Override
77    public BcX509TBSCertificateBuilder setStartDate(Date time)
78    {
79  7 this.tbsGen.setStartDate(new Time(time));
80  7 return this;
81    }
82   
 
83  7 toggle @Override
84    public BcX509TBSCertificateBuilder setEndDate(Date time)
85    {
86  7 this.tbsGen.setEndDate(new Time(time));
87  7 return this;
88    }
89   
 
90  7 toggle @Override
91    public BcX509TBSCertificateBuilder setSignature(Signer signer)
92    {
93  7 this.tbsGen.setSignature(BcUtils.getSignerAlgoritmIdentifier(signer));
94  7 return this;
95    }
96   
 
97  7 toggle @Override
98    public TBSCertificate build()
99    {
100  7 return this.tbsGen.generateTBSCertificate();
101    }
102   
103    /**
104    * Set the extensions of a self-signed v3 certificate.
105    *
106    * @param subject the subject certified public key parameters to compute the Subject Key Identifier, or null for
107    * none.
108    * @param extensions1 the common extensions set.
109    * @param extensions2 the subject extensions set.
110    * @return this builder to allow chaining.
111    * @throws IOException on encoding error.
112    */
 
113  4 toggle public BcX509v3TBSCertificateBuilder setExtensions(PublicKeyParameters subject,
114    X509Extensions extensions1, X509Extensions extensions2) throws IOException
115    {
116  4 DefaultX509ExtensionBuilder extBuilder = new DefaultX509ExtensionBuilder();
117   
118  4 if (extensions1 != null || extensions2 != null) {
119  3 extBuilder.addAuthorityKeyIdentifier(subject)
120    .addSubjectKeyIdentifier(subject)
121    .addExtensions(extensions1)
122    .addExtensions(extensions2);
123    }
124   
125  4 if (!extBuilder.isEmpty())
126    {
127  3 this.tbsGen.setExtensions(((BcX509Extensions) extBuilder.build()).getExtensions());
128    }
129  4 return this;
130    }
131   
132    /**
133    * Set the extensions of a v3 certificate.
134    *
135    * @param issuer the issuer certified public key to compute the Authority Key Identifier, or null for none.
136    * @param subject the subject certified public key parameters to compute the Subject Key Identifier, or null for
137    * none.
138    * @param extensions1 the common extensions set.
139    * @param extensions2 the subject extensions set.
140    * @return this builder to allow chaining.
141    * @throws IOException on encoding error.
142    */
 
143  3 toggle public BcX509v3TBSCertificateBuilder setExtensions(CertifiedPublicKey issuer, PublicKeyParameters subject,
144    X509Extensions extensions1, X509Extensions extensions2) throws IOException
145    {
146  3 DefaultX509ExtensionBuilder extBuilder = new DefaultX509ExtensionBuilder();
147   
148  3 extBuilder.addAuthorityKeyIdentifier(issuer)
149    .addSubjectKeyIdentifier(subject)
150    .addExtensions(extensions1)
151    .addExtensions(extensions2);
152   
153  3 if (!extBuilder.isEmpty())
154    {
155  3 this.tbsGen.setExtensions(((BcX509Extensions) extBuilder.build()).getExtensions());
156    }
157  3 return this;
158    }
159    }