new AlgorithmIdentifier("1.2.840.113549.1.1.2"), new byte[4]);
List attributes = new ArrayList();
// 1.2.840.113549.1.9.1 is OID of EMAILADDRESS
attributes.add(new AttributeTypeAndValue("1.2.840.113549.1.9.1",
new AttributeValue("a@b.com", false)));
CertificationRequestInfo certReqInfo = new CertificationRequestInfo(
version, subject, spki, attributes);
AlgorithmIdentifier signatureAlgId = new AlgorithmIdentifier(
"1.2.3.44.555");
byte[] signature = { (byte) 0x01, (byte) 0x02, (byte) 0x03,
(byte) 0x04, (byte) 0x05 };
CertificationRequest certReq = new CertificationRequest(certReqInfo,
signatureAlgId, signature);
// check what we have constructed
assertEquals(certReqInfo, certReq.getInfo());
assertEquals(signatureAlgId, certReq.getAlgId());
assertTrue(Arrays.equals(signature, certReq.getSignature()));
// decode the encoded CSR
byte[] encoding = certReq.getEncoded();
CertificationRequest decoded = (CertificationRequest) CertificationRequest.ASN1
.decode(encoding);
// check what was decoded
CertificationRequestInfo decodedCRinfo = certReq.getInfo();
assertEquals(certReqInfo.getSubject(), decodedCRinfo.getSubject());
assertEquals(certReqInfo.getSubjectPublicKeyInfo(), decodedCRinfo
.getSubjectPublicKeyInfo());
assertEquals(certReqInfo.getVersion(), decodedCRinfo.getVersion());
assertEquals(certReqInfo.getAttributes(), decodedCRinfo.getAttributes());
assertEquals(certReq.getAlgId(), decoded.getAlgId());
assertTrue(Arrays.equals(certReq.getSignature(), decoded.getSignature()));
}