use of com.github.zhenwei.pkix.operator.OperatorCreationException in project LinLong-Java by zhenwei1108.
the class ParentCertIssuedValidation method validate.
public void validate(CertPathValidationContext context, X509CertificateHolder certificate) throws CertPathValidationException {
if (workingIssuerName != null) {
if (!workingIssuerName.equals(certificate.getIssuer())) {
throw new CertPathValidationException("Certificate issue does not match parent");
}
}
if (workingPublicKey != null) {
try {
SubjectPublicKeyInfo validatingKeyInfo;
if (workingPublicKey.getAlgorithm().equals(workingAlgId)) {
validatingKeyInfo = workingPublicKey;
} else {
validatingKeyInfo = new SubjectPublicKeyInfo(workingAlgId, workingPublicKey.parsePublicKey());
}
if (!certificate.isSignatureValid(contentVerifierProvider.build(validatingKeyInfo))) {
throw new CertPathValidationException("Certificate signature not for public key in parent");
}
} catch (OperatorCreationException e) {
throw new CertPathValidationException("Unable to create verifier: " + e.getMessage(), e);
} catch (CertException e) {
throw new CertPathValidationException("Unable to validate signature: " + e.getMessage(), e);
} catch (IOException e) {
throw new CertPathValidationException("Unable to build public key: " + e.getMessage(), e);
}
}
workingIssuerName = certificate.getSubject();
workingPublicKey = certificate.getSubjectPublicKeyInfo();
if (workingAlgId != null) {
// check for inherited parameters
if (workingPublicKey.getAlgorithm().getAlgorithm().equals(workingAlgId.getAlgorithm())) {
if (!isNull(workingPublicKey.getAlgorithm().getParameters())) {
workingAlgId = workingPublicKey.getAlgorithm();
}
} else {
workingAlgId = workingPublicKey.getAlgorithm();
}
} else {
workingAlgId = workingPublicKey.getAlgorithm();
}
}
use of com.github.zhenwei.pkix.operator.OperatorCreationException in project LinLong-Java by zhenwei1108.
the class JceOpenSSLPKCS8EncryptorBuilder method build.
public OutputEncryptor build() throws OperatorCreationException {
final AlgorithmIdentifier algID;
if (random == null) {
random = new SecureRandom();
}
try {
this.cipher = helper.createCipher(algOID.getId());
if (PEMUtilities.isPKCS5Scheme2(algOID)) {
this.paramGen = helper.createAlgorithmParameterGenerator(algOID.getId());
}
} catch (GeneralSecurityException e) {
throw new OperatorCreationException(algOID + " not available: " + e.getMessage(), e);
}
if (PEMUtilities.isPKCS5Scheme2(algOID)) {
salt = new byte[PEMUtilities.getSaltSize(prf.getAlgorithm())];
random.nextBytes(salt);
params = paramGen.generateParameters();
try {
EncryptionScheme scheme = new EncryptionScheme(algOID, ASN1Primitive.fromByteArray(params.getEncoded()));
KeyDerivationFunc func = new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, iterationCount, prf));
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(func);
v.add(scheme);
algID = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBES2, PBES2Parameters.getInstance(new DERSequence(v)));
} catch (IOException e) {
throw new OperatorCreationException(e.getMessage(), e);
}
try {
if (PEMUtilities.isHmacSHA1(prf)) {
key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, algOID.getId(), password, salt, iterationCount);
} else {
key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, algOID.getId(), password, salt, iterationCount, prf);
}
cipher.init(Cipher.ENCRYPT_MODE, key, params);
} catch (GeneralSecurityException e) {
throw new OperatorCreationException(e.getMessage(), e);
}
} else if (PEMUtilities.isPKCS12(algOID)) {
ASN1EncodableVector v = new ASN1EncodableVector();
salt = new byte[20];
random.nextBytes(salt);
v.add(new DEROctetString(salt));
v.add(new ASN1Integer(iterationCount));
algID = new AlgorithmIdentifier(algOID, PKCS12PBEParams.getInstance(new DERSequence(v)));
try {
cipher.init(Cipher.ENCRYPT_MODE, new PKCS12KeyWithParameters(password, salt, iterationCount));
} catch (GeneralSecurityException e) {
throw new OperatorCreationException(e.getMessage(), e);
}
} else {
throw new OperatorCreationException("unknown algorithm: " + algOID, null);
}
return new OutputEncryptor() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return algID;
}
public OutputStream getOutputStream(OutputStream encOut) {
return new CipherOutputStream(encOut, cipher);
}
public GenericKey getKey() {
return new JceGenericKey(algID, key);
}
};
}
use of com.github.zhenwei.pkix.operator.OperatorCreationException in project LinLong-Java by zhenwei1108.
the class BcITSContentVerifierProvider method get.
public ContentVerifier get(final int verifierAlgorithmIdentifier) throws OperatorCreationException {
if (sigChoice != verifierAlgorithmIdentifier) {
throw new OperatorCreationException("wrong verifier for algorithm: " + verifierAlgorithmIdentifier);
}
final Digest digest = BcDefaultDigestProvider.INSTANCE.get(digestAlgo);
final byte[] parentDigest = new byte[digest.getDigestSize()];
digest.update(parentData, 0, parentData.length);
digest.doFinal(parentDigest, 0);
final byte[] parentTBSDigest = issuer.getIssuer().isSelf() ? new byte[digest.getDigestSize()] : null;
if (parentTBSDigest != null) {
byte[] enc = OEREncoder.toByteArray(issuer.toASN1Structure().getCertificateBase().getToBeSignedCertificate(), IEEE1609dot2.tbsCertificate);
digest.update(enc, 0, enc.length);
digest.doFinal(parentTBSDigest, 0);
}
final OutputStream os = new OutputStream() {
public void write(int b) throws IOException {
digest.update((byte) b);
}
public void write(byte[] b) throws IOException {
digest.update(b, 0, b.length);
}
public void write(byte[] b, int off, int len) throws IOException {
digest.update(b, off, len);
}
};
return new ContentVerifier() {
final DSADigestSigner signer = new DSADigestSigner(new ECDSASigner(), BcDefaultDigestProvider.INSTANCE.get(digestAlgo));
public AlgorithmIdentifier getAlgorithmIdentifier() {
return null;
}
public OutputStream getOutputStream() {
return os;
}
public boolean verify(byte[] expected) {
byte[] clientCertDigest = new byte[digest.getDigestSize()];
digest.doFinal(clientCertDigest, 0);
// System.out.println("Verify: "+ Hex.toHexString(clientCertDigest));
signer.init(false, pubParams);
signer.update(clientCertDigest, 0, clientCertDigest.length);
//
if (parentTBSDigest != null && Arrays.areEqual(clientCertDigest, parentTBSDigest)) {
byte[] empty = new byte[digest.getDigestSize()];
digest.doFinal(empty, 0);
// System.out.println("Empty: "+Hex.toHexString(empty));
signer.update(empty, 0, empty.length);
} else {
signer.update(parentDigest, 0, parentDigest.length);
}
return signer.verifySignature(expected);
}
};
}
use of com.github.zhenwei.pkix.operator.OperatorCreationException in project LinLong-Java by zhenwei1108.
the class CertificateConfirmationContentBuilder method build.
public CertificateConfirmationContent build(DigestCalculatorProvider digesterProvider) throws CMPException {
ASN1EncodableVector v = new ASN1EncodableVector();
for (int i = 0; i != acceptedCerts.size(); i++) {
X509CertificateHolder certHolder = (X509CertificateHolder) acceptedCerts.get(i);
BigInteger reqID = (BigInteger) acceptedReqIds.get(i);
AlgorithmIdentifier digAlg = digestAlgFinder.find(certHolder.toASN1Structure().getSignatureAlgorithm());
if (digAlg == null) {
throw new CMPException("cannot find algorithm for digest from signature");
}
DigestCalculator digester;
try {
digester = digesterProvider.get(digAlg);
} catch (OperatorCreationException e) {
throw new CMPException("unable to create digest: " + e.getMessage(), e);
}
CMPUtil.derEncodeToStream(certHolder.toASN1Structure(), digester.getOutputStream());
v.add(new CertStatus(digester.getDigest(), reqID));
}
return new CertificateConfirmationContent(CertConfirmContent.getInstance(new DERSequence(v)), digestAlgFinder);
}
use of com.github.zhenwei.pkix.operator.OperatorCreationException in project LinLong-Java by zhenwei1108.
the class CertificateStatus method isVerified.
public boolean isVerified(X509CertificateHolder certHolder, DigestCalculatorProvider digesterProvider) throws CMPException {
AlgorithmIdentifier digAlg = digestAlgFinder.find(certHolder.toASN1Structure().getSignatureAlgorithm());
if (digAlg == null) {
throw new CMPException("cannot find algorithm for digest from signature");
}
DigestCalculator digester;
try {
digester = digesterProvider.get(digAlg);
} catch (OperatorCreationException e) {
throw new CMPException("unable to create digester: " + e.getMessage(), e);
}
CMPUtil.derEncodeToStream(certHolder.toASN1Structure(), digester.getOutputStream());
return Arrays.areEqual(certStatus.getCertHash().getOctets(), digester.getDigest());
}
Aggregations