use of javax.security.cert.X509Certificate in project perun by CESNET.
the class urn_perun_user_attribute_def_virt_userCertExpirations method getAttributeValue.
@Override
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
Attribute attribute = new Attribute(attributeDefinition);
HashMap<String, String> certsExpirations = new LinkedHashMap<String, String>();
try {
Attribute userCertsAttribute = getUserCertsAttribute(sess, user);
HashMap<String, String> certs = (LinkedHashMap<String, String>) userCertsAttribute.getValue();
if (certs != null) {
for (String certDN : certs.keySet()) {
String cert = certs.get(certDN);
// Remove --- BEGIN --- and --- END ----
String certWithoutBegin = cert.replaceFirst("-----BEGIN CERTIFICATE-----", "");
String rawCert = certWithoutBegin.replaceFirst("-----END CERTIFICATE-----", "");
X509Certificate x509 = X509Certificate.getInstance(Base64.decodeBase64(rawCert.getBytes()));
// TODO use some defined date/time format
DateFormat dateFormat = DateFormat.getDateInstance();
certsExpirations.put(certDN, dateFormat.format(x509.getNotAfter()));
}
attribute = Utils.copyAttributeToViAttributeWithoutValue(userCertsAttribute, attribute);
}
} catch (AttributeNotExistsException ex) {
// FIXME throw new WrongReferenceAttributeValueException("User " + user + " doesn't have assigned urn:perun:user:attribute-def:def:userCertificates attribute", ex);
} catch (CertificateException e) {
throw new InternalErrorException("CertificateException - user: " + user + ".", e);
}
attribute.setValue(certsExpirations);
return attribute;
}
use of javax.security.cert.X509Certificate in project j2objc by google.
the class X509CertificateTest method testGetInstance2.
/**
* getInstance(byte[] certData) method testing.
* @throws CertificateEncodingException
* @throws java.security.cert.CertificateEncodingException
*/
public void testGetInstance2() throws java.security.cert.CertificateEncodingException, CertificateEncodingException {
boolean certificateException = false;
X509Certificate c = null;
if (this.cert == null) {
// Test can not be applied.
return;
}
try {
c = X509Certificate.getInstance(cert.getEncoded());
} catch (java.security.cert.CertificateEncodingException e) {
fail("Unexpected CertificateEncodingException was thrown.");
} catch (CertificateException e) {
// The requested certificate type is not available.
// Test pass..
certificateException = true;
}
if (!certificateException) {
assertNotNull(c);
assertTrue(Arrays.equals(c.getEncoded(), cert.getEncoded()));
}
try {
X509Certificate.getInstance(new byte[] { (byte) 1 });
} catch (CertificateException e) {
//ok
}
// Regression for HARMONY-756
try {
X509Certificate.getInstance((byte[]) null);
fail("No expected CertificateException");
} catch (CertificateException e) {
// expected;
}
}
use of javax.security.cert.X509Certificate in project camel by apache.
the class NettyEndpoint method enrichWithClientCertInformation.
/**
* Enriches the message with client certificate details such as subject name, serial number etc.
* <p/>
* If the certificate is unverified then the headers is not enriched.
*
* @param sslSession the SSL session
* @param message the message to enrich
*/
protected void enrichWithClientCertInformation(SSLSession sslSession, Message message) {
try {
X509Certificate[] certificates = sslSession.getPeerCertificateChain();
if (certificates != null && certificates.length > 0) {
X509Certificate cert = certificates[0];
Principal subject = cert.getSubjectDN();
if (subject != null) {
message.setHeader(NettyConstants.NETTY_SSL_CLIENT_CERT_SUBJECT_NAME, subject.getName());
}
Principal issuer = cert.getIssuerDN();
if (issuer != null) {
message.setHeader(NettyConstants.NETTY_SSL_CLIENT_CERT_ISSUER_NAME, issuer.getName());
}
BigInteger serial = cert.getSerialNumber();
if (serial != null) {
message.setHeader(NettyConstants.NETTY_SSL_CLIENT_CERT_SERIAL_NO, serial.toString());
}
message.setHeader(NettyConstants.NETTY_SSL_CLIENT_CERT_NOT_BEFORE, cert.getNotBefore());
message.setHeader(NettyConstants.NETTY_SSL_CLIENT_CERT_NOT_AFTER, cert.getNotAfter());
}
} catch (SSLPeerUnverifiedException e) {
// ignore
}
}
use of javax.security.cert.X509Certificate in project robovm by robovm.
the class X509CertificateTest method testGetInstance2.
/**
* getInstance(byte[] certData) method testing.
* @throws CertificateEncodingException
* @throws java.security.cert.CertificateEncodingException
*/
public void testGetInstance2() throws java.security.cert.CertificateEncodingException, CertificateEncodingException {
boolean certificateException = false;
X509Certificate c = null;
if (this.cert == null) {
// Test can not be applied.
return;
}
try {
c = X509Certificate.getInstance(cert.getEncoded());
} catch (java.security.cert.CertificateEncodingException e) {
fail("Unexpected CertificateEncodingException was thrown.");
} catch (CertificateException e) {
// The requested certificate type is not available.
// Test pass..
certificateException = true;
}
if (!certificateException) {
assertNotNull(c);
assertTrue(Arrays.equals(c.getEncoded(), cert.getEncoded()));
}
try {
X509Certificate.getInstance(new byte[] { (byte) 1 });
} catch (CertificateException e) {
//ok
}
// Regression for HARMONY-756
try {
X509Certificate.getInstance((byte[]) null);
fail("No expected CertificateException");
} catch (CertificateException e) {
// expected;
}
}
use of javax.security.cert.X509Certificate in project undertow by undertow-io.
the class SslClientCertAttribute method readAttribute.
@Override
public String readAttribute(HttpServerExchange exchange) {
SSLSessionInfo ssl = exchange.getConnection().getSslSessionInfo();
if (ssl == null) {
return null;
}
X509Certificate[] certificates;
try {
certificates = ssl.getPeerCertificateChain();
if (certificates.length > 0) {
return Certificates.toPem(certificates[0]);
}
return null;
} catch (SSLPeerUnverifiedException e) {
return null;
} catch (CertificateEncodingException e) {
return null;
} catch (RenegotiationRequiredException e) {
return null;
}
}
Aggregations