DERObject subPubkeyDerObj = ais.readObject();
SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(subPubkeyDerObj);
// Create certificate generator and initialize fields
// Certificate version is v3
V3TBSCertificateGenerator v3certGen = new V3TBSCertificateGenerator();
// Subject info
v3certGen.setSubject(subName);
v3certGen.setSubjectPublicKeyInfo(subPubKeyInfo);
// Issuer info
v3certGen.setIssuer(caName);
// serial number
v3certGen.setSerialNumber(new DERInteger(serialNum));
// validity
v3certGen.setStartDate(new Time(validFromDate));
v3certGen.setEndDate(new Time(validToDate));
// signature algorithm
v3certGen.setSignature(algId);
// Get the certificate info to be signed
TBSCertificateStructure tbsCert = v3certGen.generateTBSCertificate();
byte[] tobesigned = tbsCert.getEncoded();
// Create the signature
Signature signatureObj = Signature.getInstance(algorithm);
signatureObj.initSign(caPriKey);