use of java.security.cert.X509Certificate in project gitblit by gitblit.
the class GitblitTrustManager method checkClientTrusted.
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
X509Certificate cert = chain[0];
if (isRevoked(cert)) {
String message = MessageFormat.format("Rejecting revoked certificate {0,number,0} for {1}", cert.getSerialNumber(), cert.getSubjectDN().getName());
logger.warn(message);
throw new CertificateException(message);
}
delegate.checkClientTrusted(chain, authType);
}
use of java.security.cert.X509Certificate in project gitblit by gitblit.
the class CertificatesTableModel method getValueAt.
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
X509Certificate cert = ucm.certs.get(rowIndex);
Columns col = Columns.values()[columnIndex];
switch(col) {
case Status:
return ucm.getStatus(cert);
case SerialNumber:
return cert.getSerialNumber();
case Issued:
return cert.getNotBefore();
case Expires:
return cert.getNotAfter();
case Reason:
if (ucm.getStatus(cert).equals(CertificateStatus.revoked)) {
RevocationReason r = ucm.getRevocationReason(cert.getSerialNumber());
return Translation.get("gb." + r.name());
}
}
return null;
}
use of java.security.cert.X509Certificate in project gitblit by gitblit.
the class X509UtilsTest method testCertificateUserMapping.
@Test
public void testCertificateUserMapping() throws Exception {
File storeFile = new File(folder, X509Utils.CA_KEY_STORE);
PrivateKey caPrivateKey = X509Utils.getPrivateKey(X509Utils.CA_ALIAS, storeFile, caPassword);
X509Certificate caCert = X509Utils.getCertificate(X509Utils.CA_ALIAS, storeFile, caPassword);
X509Metadata userMetadata = new X509Metadata("james", "james");
userMetadata.serverHostname = "www.myserver.com";
userMetadata.userDisplayname = "James Moger";
userMetadata.passwordHint = "your name";
userMetadata.oids.put("C", "US");
X509Certificate cert1 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
UserModel userModel1 = HttpUtils.getUserModelFromCertificate(cert1);
assertEquals(userMetadata.commonName, userModel1.username);
assertEquals(userMetadata.emailAddress, userModel1.emailAddress);
assertEquals("C=US,O=Gitblit,OU=Gitblit,CN=james", cert1.getSubjectDN().getName());
X509Certificate cert2 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
UserModel userModel2 = HttpUtils.getUserModelFromCertificate(cert2);
assertEquals(userMetadata.commonName, userModel2.username);
assertEquals(userMetadata.emailAddress, userModel2.emailAddress);
assertEquals("C=US,O=Gitblit,OU=Gitblit,CN=james", cert2.getSubjectDN().getName());
assertNotSame("Serial numbers are the same!", cert1.getSerialNumber().longValue(), cert2.getSerialNumber().longValue());
}
use of java.security.cert.X509Certificate in project gitblit by gitblit.
the class X509UtilsTest method testCertificateRevocation.
@Test
public void testCertificateRevocation() throws Exception {
File storeFile = new File(folder, X509Utils.CA_KEY_STORE);
PrivateKey caPrivateKey = X509Utils.getPrivateKey(X509Utils.CA_ALIAS, storeFile, caPassword);
X509Certificate caCert = X509Utils.getCertificate(X509Utils.CA_ALIAS, storeFile, caPassword);
X509Metadata userMetadata = new X509Metadata("james", "james");
userMetadata.serverHostname = "www.myserver.com";
userMetadata.userDisplayname = "James Moger";
userMetadata.passwordHint = "your name";
// generate a new client certificate
X509Certificate cert1 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
// confirm this certificate IS NOT revoked
File caRevocationList = new File(folder, X509Utils.CA_REVOCATION_LIST);
assertFalse(X509Utils.isRevoked(cert1, caRevocationList));
// revoke certificate and then confirm it IS revoked
X509Utils.revoke(cert1, RevocationReason.ACompromise, caRevocationList, storeFile, caPassword, log);
assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
// generate a second certificate
X509Certificate cert2 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
// confirm second certificate IS NOT revoked
assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
assertFalse(X509Utils.isRevoked(cert2, caRevocationList));
// revoke second certificate and then confirm it IS revoked
X509Utils.revoke(cert2, RevocationReason.ACompromise, caRevocationList, caPrivateKey, log);
assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
assertTrue(X509Utils.isRevoked(cert2, caRevocationList));
// generate a third certificate
X509Certificate cert3 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
// confirm third certificate IS NOT revoked
assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
assertTrue(X509Utils.isRevoked(cert2, caRevocationList));
assertFalse(X509Utils.isRevoked(cert3, caRevocationList));
// revoke third certificate and then confirm it IS revoked
X509Utils.revoke(cert3, RevocationReason.ACompromise, caRevocationList, caPrivateKey, log);
assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
assertTrue(X509Utils.isRevoked(cert2, caRevocationList));
assertTrue(X509Utils.isRevoked(cert3, caRevocationList));
}
use of java.security.cert.X509Certificate in project spring-security-oauth by spring-projects.
the class CoreOAuthSignatureMethodFactory method getSignatureMethod.
public OAuthSignatureMethod getSignatureMethod(String methodName, SignatureSecret signatureSecret, String tokenSecret) throws UnsupportedSignatureMethodException {
if (supportPlainText && PlainTextSignatureMethod.SIGNATURE_NAME.equals(methodName)) {
if (!(signatureSecret instanceof SharedConsumerSecret)) {
throw new IllegalArgumentException("Invalid secret for signature method " + methodName + ". Expected a " + SharedConsumerSecret.class.getName() + ", got " + (signatureSecret == null ? "null" : signatureSecret.getClass().getName()) + ".");
}
String consumerSecret = ((SharedConsumerSecret) signatureSecret).getConsumerSecret();
if (consumerSecret == null) {
consumerSecret = "";
}
if (tokenSecret == null) {
tokenSecret = "";
}
consumerSecret = oauthEncode(consumerSecret);
tokenSecret = oauthEncode(tokenSecret);
Object salt = null;
if (signatureSecret instanceof SaltedConsumerSecret) {
salt = ((SaltedConsumerSecret) signatureSecret).getSalt();
}
return new PlainTextSignatureMethod(oauthEncode(new StringBuilder(consumerSecret).append('&').append(tokenSecret).toString()), this.plainTextPasswordEncoder, salt);
} else if (supportHMAC_SHA1 && HMAC_SHA1SignatureMethod.SIGNATURE_NAME.equals(methodName)) {
if (!(signatureSecret instanceof SharedConsumerSecret)) {
throw new IllegalArgumentException("Invalid secret for signature method " + methodName + ". Expected a " + SharedConsumerSecret.class.getName() + ", got " + (signatureSecret == null ? "null" : signatureSecret.getClass().getName()) + ".");
}
String consumerSecret = ((SharedConsumerSecret) signatureSecret).getConsumerSecret();
if (consumerSecret == null) {
consumerSecret = "";
}
if (tokenSecret == null) {
tokenSecret = "";
}
consumerSecret = oauthEncode(consumerSecret);
tokenSecret = oauthEncode(tokenSecret);
byte[] keyBytes;
try {
keyBytes = new StringBuilder(consumerSecret).append('&').append(tokenSecret).toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage());
}
SecretKeySpec spec = new SecretKeySpec(keyBytes, HMAC_SHA1SignatureMethod.MAC_NAME);
return new HMAC_SHA1SignatureMethod(spec);
} else if (supportRSA_SHA1 && RSA_SHA1SignatureMethod.SIGNATURE_NAME.equals(methodName)) {
if (signatureSecret instanceof RSAKeySecret) {
PublicKey publicKey = ((RSAKeySecret) signatureSecret).getPublicKey();
PrivateKey privateKey = ((RSAKeySecret) signatureSecret).getPrivateKey();
return new RSA_SHA1SignatureMethod(privateKey, publicKey);
} else {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication.getCredentials() instanceof X509Certificate) {
X509Certificate certificate = (X509Certificate) authentication.getCredentials();
if (certificate != null) {
return new RSA_SHA1SignatureMethod(certificate.getPublicKey());
}
}
}
}
throw new UnsupportedSignatureMethodException("Unsupported signature method: " + methodName);
}
Aggregations