use of java.security.cert.CRLException in project robovm by robovm.
the class CertificateFactory4Test method test_generateCRLLjava_io_InputStream.
/**
* java.security.cert.CertificateFactory#generateCRL(java.io.InputStream)
*/
public void test_generateCRLLjava_io_InputStream() throws Exception {
CertificateFactory fact = CertificateFactory.getInstance("X.509");
for (int i = 0; i < CRL_URLS.length; i++) {
URL certUrl = new URL(BASE_URL + CRL_URLS[i]);
try {
InputStream is = certUrl.openStream();
CRL crl = fact.generateCRL(is);
assertNotNull("The CRL in \"" + certUrl.toExternalForm() + "\" were not parsed correctly", crl);
} catch (IOException e) {
// the certificate could not be found, skip it
} catch (CRLException e) {
fail("An exception was thrown while parsing \"" + certUrl.toExternalForm() + "\": " + e.getMessage());
}
}
}
use of java.security.cert.CRLException in project robovm by robovm.
the class CertificateFactorySpiTest method testCertificateFactorySpi01.
/**
* Test for <code>CertificateFactorySpi</code> constructor
* Assertion: constructs CertificateFactorySpi
*/
public void testCertificateFactorySpi01() throws CertificateException, CRLException {
CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi();
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
try {
certFactorySpi.engineGenerateCertPath(bais);
fail("UnsupportedOperationException must be thrown");
} catch (UnsupportedOperationException e) {
}
try {
certFactorySpi.engineGenerateCertPath(bais, "");
fail("UnsupportedOperationException must be thrown");
} catch (UnsupportedOperationException e) {
}
try {
List<Certificate> list = null;
certFactorySpi.engineGenerateCertPath(list);
fail("UnsupportedOperationException must be thrown");
} catch (UnsupportedOperationException e) {
}
try {
certFactorySpi.engineGetCertPathEncodings();
fail("UnsupportedOperationException must be thrown");
} catch (UnsupportedOperationException e) {
}
Certificate cc = certFactorySpi.engineGenerateCertificate(bais);
assertNull("Not null Cerificate", cc);
try {
certFactorySpi.engineGenerateCertificate(null);
fail("CertificateException must be thrown");
} catch (CertificateException e) {
}
Collection<? extends Certificate> col = certFactorySpi.engineGenerateCertificates(bais);
assertNull("Not null Collection", col);
try {
certFactorySpi.engineGenerateCertificates(null);
fail("CertificateException must be thrown");
} catch (CertificateException e) {
}
CRL ccCRL = certFactorySpi.engineGenerateCRL(bais);
assertNull("Not null CRL", ccCRL);
try {
certFactorySpi.engineGenerateCRL(null);
fail("CRLException must be thrown");
} catch (CRLException e) {
}
Collection<? extends CRL> colCRL = certFactorySpi.engineGenerateCRLs(bais);
assertNull("Not null CRL", colCRL);
try {
certFactorySpi.engineGenerateCRLs(null);
fail("CRLException must be thrown");
} catch (CRLException e) {
}
}
use of java.security.cert.CRLException in project robovm by robovm.
the class CertificateFactorySpiTest method testCertificateFactorySpi03.
/**
* Test for <code>CertificateFactorySpi</code> constructor
* Assertion: constructs CertificateFactorySpi
*/
public void testCertificateFactorySpi03() throws CertificateException, CRLException {
CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi();
MyCertificateFactorySpi.putMode(false);
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
DataInputStream dis = new DataInputStream(bais);
try {
certFactorySpi.engineGenerateCertPath(bais);
fail("CertificateException must be thrown");
} catch (CertificateException e) {
}
try {
certFactorySpi.engineGenerateCertPath(dis);
fail("CertificateException must be thrown");
} catch (CertificateException e) {
}
try {
certFactorySpi.engineGenerateCertPath(bais, "aa");
fail("CertificateException must be thrown");
} catch (CertificateException e) {
}
certFactorySpi.engineGenerateCertPath(dis, "");
certFactorySpi.engineGenerateCertPath(dis, "ss");
try {
certFactorySpi.engineGenerateCertificate(bais);
fail("CertificateException must be thrown");
} catch (CertificateException e) {
}
try {
certFactorySpi.engineGenerateCertificates(null);
fail("CertificateException must be thrown");
} catch (CertificateException e) {
}
Certificate cert = certFactorySpi.engineGenerateCertificate(dis);
assertNull("Result must be null", cert);
Collection<? extends Certificate> col = certFactorySpi.engineGenerateCertificates(dis);
assertNull("Result must be null", col);
try {
certFactorySpi.engineGenerateCRL(bais);
fail("CRLException must be thrown");
} catch (CRLException e) {
}
try {
certFactorySpi.engineGenerateCRLs(null);
fail("CRLException must be thrown");
} catch (CRLException e) {
}
CRL crl = certFactorySpi.engineGenerateCRL(dis);
assertNull("Result must be null", crl);
Collection<? extends CRL> colcrl = certFactorySpi.engineGenerateCRLs(dis);
assertNull("Result must be null", colcrl);
List<Certificate> list = null;
certFactorySpi.engineGenerateCertPath(list);
Iterator<String> enc = certFactorySpi.engineGetCertPathEncodings();
assertFalse("Incorrect Iterator", enc.hasNext());
}
use of java.security.cert.CRLException in project XobotOS by xamarin.
the class X509CertFactoryImpl method getCRL.
/**
* Returns the CRL object corresponding to the encoding provided
* by the stream.
* Resulting object is retrieved from the cache
* if it contains such correspondence
* and is constructed on the base of encoding
* and stored in the cache otherwise.
* @throws IOException if some decoding errors occur
* (in the case of cache miss).
*/
private static CRL getCRL(InputStream inStream) throws CRLException, IOException {
synchronized (CRL_CACHE) {
inStream.mark(CRL_CACHE_SEED_LENGTH);
byte[] buff = readBytes(inStream, CRL_CACHE_SEED_LENGTH);
// read the prefix of the encoding
inStream.reset();
if (buff == null) {
throw new CRLException("InputStream doesn't contain enough data");
}
long hash = CRL_CACHE.getHash(buff);
if (CRL_CACHE.contains(hash)) {
byte[] encoding = new byte[BerInputStream.getLength(buff)];
if (encoding.length < CRL_CACHE_SEED_LENGTH) {
throw new CRLException("Bad CRL encoding");
}
Streams.readFully(inStream, encoding);
CRL res = (CRL) CRL_CACHE.get(hash, encoding);
if (res != null) {
return res;
}
res = new X509CRLImpl(encoding);
CRL_CACHE.put(hash, encoding, res);
return res;
} else {
X509CRL res = new X509CRLImpl(inStream);
CRL_CACHE.put(hash, res.getEncoded(), res);
return res;
}
}
}
use of java.security.cert.CRLException in project XobotOS by xamarin.
the class X509CertFactoryImpl method engineGenerateCRLs.
/**
* @see java.security.cert.CertificateFactorySpi#engineGenerateCRLs(InputStream)
* method documentation for more info
*/
public Collection<? extends CRL> engineGenerateCRLs(InputStream inStream) throws CRLException {
if (inStream == null) {
throw new CRLException("inStream == null");
}
ArrayList<CRL> result = new ArrayList<CRL>();
try {
if (!inStream.markSupported()) {
inStream = new RestoringInputStream(inStream);
}
// if it is PEM encoded form this array will contain the encoding
// so ((it is PEM) <-> (encoding != null))
byte[] encoding = null;
// The following by SEQUENCE ASN.1 tag, used for
// recognizing the data format
// (is it PKCS7 ContentInfo structure, X.509 CRL, or
// unsupported encoding)
int second_asn1_tag = -1;
inStream.mark(1);
int ch;
while ((ch = inStream.read()) != -1) {
// check if it is PEM encoded form
if (ch == '-') {
// beginning of PEM encoding ('-' char)
// decode PEM chunk and store its content (ASN.1 encoding)
encoding = decodePEM(inStream, FREE_BOUND_SUFFIX);
} else if (ch == 0x30) {
// beginning of ASN.1 sequence (0x30)
encoding = null;
inStream.reset();
// prepare for data format determination
inStream.mark(CRL_CACHE_SEED_LENGTH);
} else {
// unsupported data
if (result.size() == 0) {
throw new CRLException("Unsupported encoding");
} else {
// it can be trailing user data,
// so keep it in the stream
inStream.reset();
return result;
}
}
// Check the data format
BerInputStream in = (encoding == null) ? new BerInputStream(inStream) : new BerInputStream(encoding);
// read the next ASN.1 tag
second_asn1_tag = in.next();
if (encoding == null) {
// keep whole structure in the stream
inStream.reset();
}
// check if it is a TBSCertList structure
if (second_asn1_tag != ASN1Constants.TAG_C_SEQUENCE) {
if (result.size() == 0) {
// whether it is PKCS7 structure
break;
} else {
// so return what we already read
return result;
}
} else {
if (encoding == null) {
result.add(getCRL(inStream));
} else {
result.add(getCRL(encoding));
}
}
inStream.mark(1);
}
if (result.size() != 0) {
// the stream was read out
return result;
} else if (ch == -1) {
throw new CRLException("There is no data in the stream");
}
// else: check if it is PKCS7
if (second_asn1_tag == ASN1Constants.TAG_OID) {
// it is PKCS7 ContentInfo structure, so decode it
ContentInfo info = (ContentInfo) ((encoding != null) ? ContentInfo.ASN1.decode(encoding) : ContentInfo.ASN1.decode(inStream));
// retrieve SignedData
SignedData data = info.getSignedData();
if (data == null) {
throw new CRLException("Invalid PKCS7 data provided");
}
List<CertificateList> crls = data.getCRLs();
if (crls != null) {
for (CertificateList crl : crls) {
result.add(new X509CRLImpl(crl));
}
}
return result;
}
// else: Unknown data format
throw new CRLException("Unsupported encoding");
} catch (IOException e) {
throw new CRLException(e);
}
}
Aggregations