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

File BcPublicKeyParameters.java

 

Coverage histogram

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

Code metrics

8
17
3
1
100
57
8
0.47
5.67
3
2.67

Classes

Class Line # Actions
BcPublicKeyParameters 45 17 0% 8 6
0.7857142778.6%
 

Contributing tests

This file is covered by 32 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.internal.asymmetric;
21   
22    import java.io.IOException;
23   
24    import org.bouncycastle.asn1.ASN1Integer;
25    import org.bouncycastle.asn1.DERNull;
26    import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
27    import org.bouncycastle.asn1.pkcs.RSAPublicKey;
28    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
29    import org.bouncycastle.asn1.x509.DSAParameter;
30    import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
31    import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
32    import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
33    import org.bouncycastle.crypto.params.DSAParameters;
34    import org.bouncycastle.crypto.params.DSAPublicKeyParameters;
35    import org.bouncycastle.crypto.params.RSAKeyParameters;
36    import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
37    import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters;
38   
39    /**
40    * Encapsulate a Bouncy Castle asymmetric parameter of a public key.
41    *
42    * @version $Id: 44e9cef4a61d9aa982877eda1422097d2052463d $
43    * @since 5.4M1
44    */
 
45    public class BcPublicKeyParameters extends AbstractBcAsymmetricKeyParameters implements PublicKeyParameters
46    {
47    /**
48    * Encapsulate a given Bouncy Castle public key parameter.
49    *
50    * @param parameters a BC public key parameter.
51    */
 
52  63 toggle public BcPublicKeyParameters(AsymmetricKeyParameter parameters)
53    {
54  63 super(parameters);
55  63 if (isPrivate()) {
56  0 throw new IllegalArgumentException("Private key assigned to a public key: "
57    + parameters.getClass().getName());
58    }
59    }
60   
61    /**
62    * @return parameters converted to BC subject public key info structure.
63    * @throws IOException on error.
64    */
 
65  23 toggle public SubjectPublicKeyInfo getSubjectPublicKeyInfo() throws IOException
66    {
67  23 if (this.parameters instanceof RSAKeyParameters) {
68  14 RSAKeyParameters params = (RSAKeyParameters) this.parameters;
69   
70  14 return new SubjectPublicKeyInfo(
71    new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE),
72    new RSAPublicKey(params.getModulus(), params.getExponent()));
73  9 } else if (this.parameters instanceof DSAPublicKeyParameters) {
74  9 DSAPublicKeyParameters params = (DSAPublicKeyParameters) this.parameters;
75  9 DSAParameters dsaParams = params.getParameters();
76  9 DSAParameter algParams = null;
77   
78  9 if (dsaParams != null) {
79  9 algParams = new DSAParameter(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
80    }
81   
82  9 return new SubjectPublicKeyInfo(
83    new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, algParams),
84    new ASN1Integer(params.getY()));
85    } else {
86    // Fallback to Bouncy Castle, not sure it will do anything useful however.
87  0 return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(this.parameters);
88    }
89    }
90   
 
91  2 toggle @Override
92    public byte[] getEncoded()
93    {
94  2 try {
95  2 return getSubjectPublicKeyInfo().getEncoded();
96    } catch (IOException e) {
97  0 return null;
98    }
99    }
100    }