use of java.security.cert.CertificateFactory in project robovm by robovm.
the class CertPathValidatorTestPKIX method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
X509Certificate selfSignedcertificate = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(selfSignedCert.getBytes()));
keyStore.setCertificateEntry("selfSignedCert", selfSignedcertificate);
X509CertSelector targetConstraints = new X509CertSelector();
targetConstraints.setCertificate(selfSignedcertificate);
List<Certificate> certList = new ArrayList<Certificate>();
certList.add(selfSignedcertificate);
CertStoreParameters storeParams = new CollectionCertStoreParameters(certList);
CertStore certStore = CertStore.getInstance("Collection", storeParams);
PKIXBuilderParameters parameters = new PKIXBuilderParameters(keyStore, targetConstraints);
parameters.addCertStore(certStore);
parameters.setRevocationEnabled(false);
CertPathBuilder pathBuilder = CertPathBuilder.getInstance("PKIX");
CertPathBuilderResult builderResult = pathBuilder.build(parameters);
certPath = builderResult.getCertPath();
params = new PKIXParameters(keyStore);
params.setRevocationEnabled(false);
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class CipherTest method test_wrap_java_security_Key.
public void test_wrap_java_security_Key() throws Exception {
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
assertNotNull(c.wrap(CIPHER_KEY_DES));
assertNotNull(c.wrap(CIPHER_KEY_3DES));
String certName = Support_Resources.getURL("test.cert");
InputStream is = new URL(certName).openConnection().getInputStream();
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(is);
assertNotNull(c.wrap(cert.getPublicKey()));
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
assertNotNull(c.wrap(cert.getPublicKey()));
fail();
} catch (IllegalBlockSizeException expected) {
}
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
c.wrap(CIPHER_KEY_DES);
fail();
} catch (IllegalStateException expected) {
}
c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
try {
c.wrap(new Mock_Key());
fail();
} catch (InvalidKeyException expected) {
}
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class CipherTest method test_initWithCertificate.
public void test_initWithCertificate() throws Exception {
/* Certificate creation notes: certificate should be valid 37273 starting
* from 13 Nov 2008
* If it brcomes invalidated regenerate it using following commands:
* 1. openssl genrsa -des3 -out test.key 1024
* 2. openssl req -new -key test.key -out test.csr
* 3. cp test.key test.key.org
* 4. openssl rsa -in test.key.org -out test.key
* 5. openssl x509 -req -days 37273 -in test.csr -signkey test.key -out test.cert
* */
String certName = Support_Resources.getURL("test.cert");
InputStream is = new URL(certName).openConnection().getInputStream();
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(is);
is.close();
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, cert);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
try {
c.init(Cipher.ENCRYPT_MODE, cert);
fail();
} catch (InvalidKeyException expected) {
}
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class X509Certificate method getInstance.
/**
* Creates a new {@code X509Certificate} and initializes it from the
* specified input stream.
*
* @param inStream
* input stream containing data to initialize the certificate.
* @return the certificate initialized from the specified input stream
* @throws CertificateException
* if the certificate cannot be created or initialized.
*/
public static final X509Certificate getInstance(InputStream inStream) throws CertificateException {
if (inStream == null) {
throw new CertificateException("inStream == null");
}
if (constructor != null) {
try {
return (X509Certificate) constructor.newInstance(new Object[] { inStream });
} catch (Throwable e) {
throw new CertificateException(e.getMessage());
}
}
final java.security.cert.X509Certificate cert;
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
cert = (java.security.cert.X509Certificate) cf.generateCertificate(inStream);
} catch (java.security.cert.CertificateException e) {
throw new CertificateException(e.getMessage());
}
return new X509Certificate() {
public byte[] getEncoded() throws CertificateEncodingException {
try {
return cert.getEncoded();
} catch (java.security.cert.CertificateEncodingException e) {
throw new CertificateEncodingException(e.getMessage());
}
}
public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
try {
cert.verify(key);
} catch (java.security.cert.CertificateException e) {
throw new CertificateException(e.getMessage());
}
}
public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
try {
cert.verify(key, sigProvider);
} catch (java.security.cert.CertificateException e) {
throw new CertificateException(e.getMessage());
}
}
public String toString() {
return cert.toString();
}
public PublicKey getPublicKey() {
return cert.getPublicKey();
}
public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException {
try {
cert.checkValidity();
} catch (java.security.cert.CertificateNotYetValidException e) {
throw new CertificateNotYetValidException(e.getMessage());
} catch (java.security.cert.CertificateExpiredException e) {
throw new CertificateExpiredException(e.getMessage());
}
}
public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException {
try {
cert.checkValidity(date);
} catch (java.security.cert.CertificateNotYetValidException e) {
throw new CertificateNotYetValidException(e.getMessage());
} catch (java.security.cert.CertificateExpiredException e) {
throw new CertificateExpiredException(e.getMessage());
}
}
public int getVersion() {
return 2;
}
public BigInteger getSerialNumber() {
return cert.getSerialNumber();
}
public Principal getIssuerDN() {
return cert.getIssuerDN();
}
public Principal getSubjectDN() {
return cert.getSubjectDN();
}
public Date getNotBefore() {
return cert.getNotBefore();
}
public Date getNotAfter() {
return cert.getNotAfter();
}
public String getSigAlgName() {
return cert.getSigAlgName();
}
public String getSigAlgOID() {
return cert.getSigAlgOID();
}
public byte[] getSigAlgParams() {
return cert.getSigAlgParams();
}
};
}
use of java.security.cert.CertificateFactory in project robovm by robovm.
the class X509CertificateTest method setUp.
@Override
protected void setUp() throws Exception {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(base64cert.getBytes());
CertificateFactory cf = CertificateFactory.getInstance("X.509");
this.cert = (java.security.cert.X509Certificate) cf.generateCertificate(bais);
this.tbt_cert = X509Certificate.getInstance(cert.getEncoded());
// non self signed cert
this.javaCert = (java.security.cert.X509Certificate) cf.generateCertificate(new ByteArrayInputStream(selfSignedCert.getBytes()));
this.javaxCert = X509Certificate.getInstance(javaCert.getEncoded());
myProvider = cf.getProvider();
Security.addProvider(myProvider);
// self signed cert
this.javaSSCert = cf.generateCertificate(new ByteArrayInputStream(selfSignedCert.getBytes()));
this.javaxSSCert = X509Certificate.getInstance(javaCert.getEncoded());
mySSProvider = cf.getProvider();
Security.addProvider(mySSProvider);
} catch (java.security.cert.CertificateException e) {
// The requested certificate type is not available.
// Test pass..
this.cert = null;
Logger.global.warning("Error in test setup: Certificate type not supported");
} catch (javax.security.cert.CertificateException e) {
// The requested certificate type is not available.
// Test pass..
this.cert = null;
Logger.global.warning("Error in test setup: Certificate type not supported");
}
}
Aggregations