use of javax.security.auth.x500.X500Principal in project grpc-java by grpc.
the class TesterOkHttpChannelBuilder method getTrustManagers.
private static TrustManager[] getTrustManagers(InputStream testCa) throws Exception {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(testCa);
X500Principal principal = cert.getSubjectX500Principal();
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
// Set up trust manager factory to use our key store.
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks);
return trustManagerFactory.getTrustManagers();
}
use of javax.security.auth.x500.X500Principal in project grpc-java by grpc.
the class OkHostnameVerifier method verifyHostName.
/**
* Returns true if {@code certificate} matches {@code hostName}.
*/
private boolean verifyHostName(String hostName, X509Certificate certificate) {
hostName = hostName.toLowerCase(Locale.US);
boolean hasDns = false;
List<String> altNames = getSubjectAltNames(certificate, ALT_DNS_NAME);
for (int i = 0, size = altNames.size(); i < size; i++) {
hasDns = true;
if (verifyHostName(hostName, altNames.get(i))) {
return true;
}
}
if (!hasDns) {
X500Principal principal = certificate.getSubjectX500Principal();
// RFC 2818 advises using the most specific name for matching.
String cn = new DistinguishedNameParser(principal).findMostSpecific("cn");
if (cn != null) {
return verifyHostName(hostName, cn);
}
}
return false;
}
use of javax.security.auth.x500.X500Principal in project grpc-java by grpc.
the class TestUtils method newSslSocketFactoryForCa.
/**
* Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
*/
public static SSLSocketFactory newSslSocketFactoryForCa(Provider provider, File certChainFile) throws Exception {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(new BufferedInputStream(new FileInputStream(certChainFile)));
X500Principal principal = cert.getSubjectX500Principal();
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
// Set up trust manager factory to use our key store.
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks);
SSLContext context = SSLContext.getInstance("TLS", provider);
context.init(null, trustManagerFactory.getTrustManagers(), null);
return context.getSocketFactory();
}
use of javax.security.auth.x500.X500Principal in project XobotOS by xamarin.
the class X509CRLSelector method setIssuers.
/**
* Sets the criterion for the issuer distinguished names.
* <p>
* The CRL issuer must match at least one of the specified distinguished
* names.
*
* @param issuers
* the list of issuer distinguished names to match, or {@code
* null} if any issuer distinguished name will do.
*/
public void setIssuers(Collection<X500Principal> issuers) {
if (issuers == null) {
issuerNames = null;
issuerPrincipals = null;
return;
}
issuerNames = new ArrayList<String>(issuers.size());
issuerPrincipals = new ArrayList<X500Principal>(issuers);
for (X500Principal issuer : issuers) {
issuerNames.add(issuer.getName(X500Principal.CANONICAL));
}
}
use of javax.security.auth.x500.X500Principal in project XobotOS by xamarin.
the class X509CRLSelector method addIssuer.
/**
* Adds an issuer to the criterion for the issuer distinguished names.
* <p>
* The CRL issuer must match at least one of the specified distinguished
* names.
*
* @param issuer
* the issuer to add to the criterion
*/
public void addIssuer(X500Principal issuer) {
if (issuer == null) {
throw new NullPointerException("issuer == null");
}
if (issuerNames == null) {
issuerNames = new ArrayList<String>();
}
String name = issuer.getName(X500Principal.CANONICAL);
if (!issuerNames.contains(name)) {
issuerNames.add(name);
}
if (issuerPrincipals == null) {
issuerPrincipals = new ArrayList<X500Principal>(issuerNames.size());
}
// extend the list of issuer Principals
int size = issuerNames.size() - 1;
for (int i = issuerPrincipals.size(); i < size; i++) {
issuerPrincipals.add(new X500Principal(issuerNames.get(i)));
}
issuerPrincipals.add(issuer);
}
Aggregations