| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.crypto.pkix.internal; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
import java.io.OutputStream; |
| 24 |
|
|
| 25 |
|
import org.bouncycastle.asn1.ASN1Encodable; |
| 26 |
|
import org.bouncycastle.asn1.ASN1EncodableVector; |
| 27 |
|
import org.bouncycastle.asn1.DERBitString; |
| 28 |
|
import org.bouncycastle.asn1.DERNull; |
| 29 |
|
import org.bouncycastle.asn1.DEROutputStream; |
| 30 |
|
import org.bouncycastle.asn1.DERSequence; |
| 31 |
|
import org.bouncycastle.asn1.x500.X500Name; |
| 32 |
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 33 |
|
import org.bouncycastle.asn1.x509.Certificate; |
| 34 |
|
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
| 35 |
|
import org.bouncycastle.asn1.x509.TBSCertificate; |
| 36 |
|
import org.bouncycastle.cert.X509CertificateHolder; |
| 37 |
|
import org.bouncycastle.crypto.params.AsymmetricKeyParameter; |
| 38 |
|
import org.bouncycastle.crypto.util.PublicKeyFactory; |
| 39 |
|
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory; |
| 40 |
|
import org.bouncycastle.operator.ContentSigner; |
| 41 |
|
import org.xwiki.crypto.internal.asymmetric.BcAsymmetricKeyParameters; |
| 42 |
|
import org.xwiki.crypto.internal.asymmetric.BcPublicKeyParameters; |
| 43 |
|
import org.xwiki.crypto.params.cipher.asymmetric.PublicKeyParameters; |
| 44 |
|
import org.xwiki.crypto.pkix.CertificateFactory; |
| 45 |
|
import org.xwiki.crypto.pkix.params.CertifiedPublicKey; |
| 46 |
|
import org.xwiki.crypto.pkix.params.PrincipalIndentifier; |
| 47 |
|
import org.xwiki.crypto.signer.Signer; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
@version |
| 53 |
|
@since |
| 54 |
|
|
| |
|
| 60% |
Uncovered Elements: 30 (75) |
Complexity: 24 |
Complexity Density: 0.53 |
|
| 55 |
|
public final class BcUtils |
| 56 |
|
{ |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 57 |
0 |
private BcUtils()... |
| 58 |
|
{ |
| 59 |
|
|
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@param |
| 66 |
|
@return |
| 67 |
|
|
| |
|
| 42.9% |
Uncovered Elements: 4 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 68 |
51 |
public static X509CertificateHolder getX509CertificateHolder(CertifiedPublicKey cert)... |
| 69 |
|
{ |
| 70 |
51 |
if (cert instanceof BcX509CertifiedPublicKey) { |
| 71 |
51 |
return ((BcX509CertifiedPublicKey) cert).getX509CertificateHolder(); |
| 72 |
|
} else { |
| 73 |
0 |
try { |
| 74 |
0 |
return new X509CertificateHolder(cert.getEncoded()); |
| 75 |
|
} catch (IOException e) { |
| 76 |
|
|
| 77 |
0 |
throw new IllegalArgumentException("Invalid certified public key, unable to encode."); |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
@param |
| 87 |
|
@return |
| 88 |
|
|
| |
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 89 |
0 |
public static AsymmetricKeyParameter getAsymmetricKeyParameter(PublicKeyParameters publicKey)... |
| 90 |
|
{ |
| 91 |
0 |
if (publicKey instanceof BcAsymmetricKeyParameters) { |
| 92 |
0 |
return ((BcAsymmetricKeyParameters) publicKey).getParameters(); |
| 93 |
|
} else { |
| 94 |
0 |
try { |
| 95 |
0 |
return PublicKeyFactory.createKey(publicKey.getEncoded()); |
| 96 |
|
} catch (IOException e) { |
| 97 |
|
|
| 98 |
0 |
throw new IllegalArgumentException("Invalid public key, unable to encode."); |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
@param |
| 108 |
|
@return |
| 109 |
|
|
| |
|
| 57.1% |
Uncovered Elements: 3 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 110 |
19 |
public static SubjectPublicKeyInfo getSubjectPublicKeyInfo(PublicKeyParameters publicKey)... |
| 111 |
|
{ |
| 112 |
19 |
try { |
| 113 |
19 |
if (publicKey instanceof BcPublicKeyParameters) { |
| 114 |
19 |
return ((BcPublicKeyParameters) publicKey).getSubjectPublicKeyInfo(); |
| 115 |
|
} else { |
| 116 |
0 |
return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(getAsymmetricKeyParameter(publicKey)); |
| 117 |
|
} |
| 118 |
|
} catch (IOException e) { |
| 119 |
|
|
| 120 |
0 |
throw new IllegalArgumentException("Invalid public key, unable to get subject info."); |
| 121 |
|
} |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
@param |
| 128 |
|
@param |
| 129 |
|
@return |
| 130 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 131 |
10 |
public static X509CertificateHolder getX509CertificateHolder(TBSCertificate tbsCert, byte[] signature)... |
| 132 |
|
{ |
| 133 |
10 |
ASN1EncodableVector v = new ASN1EncodableVector(); |
| 134 |
|
|
| 135 |
10 |
v.add(tbsCert); |
| 136 |
10 |
v.add(tbsCert.getSignature()); |
| 137 |
10 |
v.add(new DERBitString(signature)); |
| 138 |
|
|
| 139 |
10 |
return new X509CertificateHolder(Certificate.getInstance(new DERSequence(v))); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
@param |
| 146 |
|
@param |
| 147 |
|
@return |
| 148 |
|
|
| |
|
| 53.8% |
Uncovered Elements: 6 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 149 |
38 |
public static boolean isAlgorithlIdentifierEqual(AlgorithmIdentifier id1, AlgorithmIdentifier id2)... |
| 150 |
|
{ |
| 151 |
38 |
if (!id1.getAlgorithm().equals(id2.getAlgorithm())) |
| 152 |
|
{ |
| 153 |
0 |
return false; |
| 154 |
|
} |
| 155 |
|
|
| 156 |
38 |
if (id1.getParameters() == null) |
| 157 |
|
{ |
| 158 |
0 |
return !(id2.getParameters() != null && !id2.getParameters().equals(DERNull.INSTANCE)); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
38 |
if (id2.getParameters() == null) |
| 162 |
|
{ |
| 163 |
0 |
return !(id1.getParameters() != null && !id1.getParameters().equals(DERNull.INSTANCE)); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
38 |
return id1.getParameters().equals(id2.getParameters()); |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
@param |
| 173 |
|
@param |
| 174 |
|
@return |
| 175 |
|
@throws |
| 176 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 177 |
48 |
public static Signer updateDEREncodedObject(Signer signer, ASN1Encodable tbsObj)... |
| 178 |
|
throws IOException |
| 179 |
|
{ |
| 180 |
48 |
OutputStream sOut = signer.getOutputStream(); |
| 181 |
48 |
DEROutputStream dOut = new DEROutputStream(sOut); |
| 182 |
|
|
| 183 |
48 |
dOut.writeObject(tbsObj); |
| 184 |
|
|
| 185 |
48 |
sOut.close(); |
| 186 |
|
|
| 187 |
48 |
return signer; |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
@param |
| 194 |
|
@return |
| 195 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 196 |
34 |
public static X500Name getX500Name(PrincipalIndentifier principal)... |
| 197 |
|
{ |
| 198 |
34 |
if (principal instanceof BcPrincipalIdentifier) { |
| 199 |
34 |
return ((BcPrincipalIdentifier) principal).getX500Name(); |
| 200 |
|
} else { |
| 201 |
0 |
return new X500Name(principal.getName()); |
| 202 |
|
} |
| 203 |
|
} |
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
@param |
| 209 |
|
@return |
| 210 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 211 |
35 |
public static AlgorithmIdentifier getSignerAlgoritmIdentifier(Signer signer)... |
| 212 |
|
{ |
| 213 |
35 |
if (signer instanceof ContentSigner) { |
| 214 |
35 |
return ((ContentSigner) signer).getAlgorithmIdentifier(); |
| 215 |
|
} else { |
| 216 |
0 |
return AlgorithmIdentifier.getInstance(signer.getEncoded()); |
| 217 |
|
} |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
@param |
| 224 |
|
@param |
| 225 |
|
@return |
| 226 |
|
@since |
| 227 |
|
|
| |
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 228 |
61 |
public static CertifiedPublicKey convertCertificate(CertificateFactory certFactory, X509CertificateHolder cert)... |
| 229 |
|
{ |
| 230 |
61 |
if (cert == null) { |
| 231 |
19 |
return null; |
| 232 |
|
} |
| 233 |
|
|
| 234 |
42 |
if (certFactory instanceof BcX509CertificateFactory) { |
| 235 |
42 |
return ((BcX509CertificateFactory) certFactory).convert(cert); |
| 236 |
|
} else { |
| 237 |
0 |
try { |
| 238 |
0 |
return certFactory.decode(cert.getEncoded()); |
| 239 |
|
} catch (IOException e) { |
| 240 |
|
|
| 241 |
0 |
throw new IllegalArgumentException("Invalid Certificate, unable to encode", e); |
| 242 |
|
} |
| 243 |
|
} |
| 244 |
|
} |
| 245 |
|
} |