byte[] request = Base64.decode(req.getBytes());
ASN1InputStream in = new ASN1InputStream(request);
ASN1Sequence crmfSeq = (ASN1Sequence) in.readObject();
ASN1Sequence reqSeq = (ASN1Sequence) ((ASN1Sequence) crmfSeq.getObjectAt(0)).getObjectAt(0);
CertRequest certReq = new CertRequest( reqSeq );
SubjectPublicKeyInfo pKeyInfo = certReq.getCertTemplate().getPublicKey();
KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC");
KeySpec keySpec = new X509EncodedKeySpec( pKeyInfo.getEncoded() );
PublicKey pubKey = keyFact.generatePublic(keySpec); // just check it's ok
imsg = new SimpleRequestMessage(pubKey, username, password);
// a simple crmf is not a complete PKI message, as desired by the CrmfRequestMessage class
//PKIMessage msg = PKIMessage.getInstance(new ASN1InputStream(new ByteArrayInputStream(request)).readObject());
//CrmfRequestMessage reqmsg = new CrmfRequestMessage(msg, null, true, null);
//imsg = reqmsg;
} else if (reqType == SecConst.CERT_REQ_TYPE_PUBLICKEY) {
byte[] request;
// Request can be Base64 encoded or in PEM format
try {
request = FileTools.getBytesFromPEM(req.getBytes(), CertTools.BEGIN_PUBLIC_KEY, CertTools.END_PUBLIC_KEY);
} catch (IOException ex) {
try {
request = Base64.decode(req.getBytes());
if (request == null) {
throw new IOException("Base64 decode of buffer returns null");
}
} catch (ArrayIndexOutOfBoundsException ae) {
throw new IOException("Base64 decode fails, message not base64 encoded: " + ae.getMessage());
}
}
final ASN1InputStream in = new ASN1InputStream(request);
final SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(in.readObject());
final AlgorithmIdentifier keyAlg = keyInfo.getAlgorithmId();
final X509EncodedKeySpec xKeySpec = new X509EncodedKeySpec(new DERBitString(keyInfo).getBytes());
final KeyFactory keyFact = KeyFactory.getInstance(keyAlg.getObjectId().getId(), "BC");
final PublicKey pubKey = keyFact.generatePublic(xKeySpec);
imsg = new SimpleRequestMessage(pubKey, username, password);
}