/// <exception cref="System.IO.IOException"/> /// <exception cref="GeneralSecurityException"/> internal X509TrustManager LoadTrustManager() { X509TrustManager trustManager = null; KeyStore ks = KeyStore.GetInstance(type); lastLoaded = file.LastModified(); FileInputStream @in = new FileInputStream(file); try { ks.Load(@in, password.ToCharArray()); Log.Debug("Loaded truststore '" + file + "'"); } finally { @in.Close(); } TrustManagerFactory trustManagerFactory = TrustManagerFactory.GetInstance(SSLFactory .Sslcertificate); trustManagerFactory.Init(ks); TrustManager[] trustManagers = trustManagerFactory.GetTrustManagers(); foreach (TrustManager trustManager1 in trustManagers) { if (trustManager1 is X509TrustManager) { trustManager = (X509TrustManager)trustManager1; break; } } return(trustManager); }
private ITrustManager[] GetTrustManagers() { TrustManagerFactory trustManagerFactory = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); trustManagerFactory.Init((KeyStore)null); return(trustManagerFactory.GetTrustManagers()); }
/// <summary> /// Set certificates for the trusted Root Certificate Authorities (Android implementation) /// </summary> /// <param name="certificates">Certificates for the CAs to trust</param> public virtual void SetTrustedRoots(params byte[][] certificates) { if (certificates == null) { _trustMgrFactory = null; _x509TrustManager = null; return; } var keyStore = KeyStore.GetInstance(KeyStore.DefaultType); keyStore.Load(null); var certFactory = CertificateFactory.GetInstance("X.509"); foreach (var certificate in certificates) { var cert = (X509Certificate)certFactory.GenerateCertificate(new System.IO.MemoryStream(certificate)); keyStore.SetCertificateEntry(cert.SubjectDN.Name, cert); } _trustMgrFactory = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); _trustMgrFactory.Init(keyStore); foreach (var trustManager in TrustManagers) { _x509TrustManager = trustManager.JavaCast <IX509TrustManager>(); if (_x509TrustManager != null) { break; } } }
static void SetupTrustManager() { if (sslTrustManager != null) { return; } lock (lock_) { TrustManagerFactory factory = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm) !; factory.Init((KeyStore?)null); foreach (ITrustManager tm in factory.GetTrustManagers() !) { try { sslTrustManager = tm.JavaCast <IX509TrustManager>(); } catch { // ignore } if (sslTrustManager != null) { break; } } } }
/// <exception cref="Sharpen.NoSuchAlgorithmException"></exception> /// <exception cref="Sharpen.KeyStoreException"></exception> public virtual Apache.Http.Conn.Ssl.SSLContextBuilder LoadTrustMaterial(KeyStore truststore, TrustStrategy trustStrategy) { TrustManagerFactory tmfactory = TrustManagerFactory.GetInstance(TrustManagerFactory .GetDefaultAlgorithm()); tmfactory.Init(truststore); TrustManager[] tms = tmfactory.GetTrustManagers(); if (tms != null) { if (trustStrategy != null) { for (int i = 0; i < tms.Length; i++) { TrustManager tm = tms[i]; if (tm is X509TrustManager) { tms[i] = new SSLContextBuilder.TrustManagerDelegate((X509TrustManager)tm, trustStrategy ); } } } for (int i_1 = 0; i_1 < tms.Length; i_1++) { this.trustmanagers.AddItem(tms[i_1]); } } return(this); }
public CompleteX509TrustManager(IX509TrustManager localTrustManager) { this.localTrustManager = localTrustManager; var defaultTrustManagerFactory = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); defaultTrustManagerFactory.Init((KeyStore)null); defaultTrustManager = defaultTrustManagerFactory.GetTrustManagers()[0].JavaCast <IX509TrustManager>(); }
internal static IX509TrustManager GetLocalTrustManager() { var trustManager = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); trustManager.Init(null as KeyStore); var localTrustManager = trustManager.GetTrustManagers().First().JavaCast <IX509TrustManager>(); return(localTrustManager); }
public CustomX509TrustManager() { var algorithm = TrustManagerFactory.DefaultAlgorithm; var defaultTrustManagerFactory = TrustManagerFactory.GetInstance(algorithm); defaultTrustManagerFactory.Init((KeyStore)null); var trustManagers = defaultTrustManagerFactory.GetTrustManagers(); defaultTrustManager = trustManagers[0].JavaCast <IX509TrustManager>(); }
void SetupSSL(HttpsURLConnection httpsConnection) { if (httpsConnection == null) { return; } SSLSocketFactory socketFactory = ConfigureCustomSSLSocketFactory(httpsConnection); if (socketFactory != null) { httpsConnection.SSLSocketFactory = socketFactory; return; } KeyStore keyStore = KeyStore.GetInstance(KeyStore.DefaultType); keyStore.Load(null, null); bool gotCerts = TrustedCerts?.Count > 0; if (gotCerts) { for (int i = 0; i < TrustedCerts.Count; i++) { Certificate cert = TrustedCerts [i]; if (cert == null) { continue; } keyStore.SetCertificateEntry($"ca{i}", cert); } } keyStore = ConfigureKeyStore(keyStore); KeyManagerFactory kmf = ConfigureKeyManagerFactory(keyStore); TrustManagerFactory tmf = ConfigureTrustManagerFactory(keyStore); if (tmf == null) { // If there are no certs and no trust manager factory, we can't use a custom manager // because it will cause all the HTTPS requests to fail because of unverified trust // chain if (!gotCerts) { return; } tmf = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); tmf.Init(keyStore); } SSLContext context = SSLContext.GetInstance("TLS"); context.Init(kmf?.GetKeyManagers(), tmf.GetTrustManagers(), null); httpsConnection.SSLSocketFactory = context.SocketFactory; }
public SslPolicy(PrivateKey privateKey, X509Certificate[] keyCertChain, IList <string> tlsVersions, IList <string> ciphers, ClientAuth clientAuth, TrustManagerFactory trustManagerFactory, SslProvider sslProvider, bool verifyHostname, LogProvider logProvider) { this._privateKey = privateKey; this._keyCertChain = keyCertChain; this._tlsVersions = tlsVersions == null ? null : tlsVersions.ToArray(); this._ciphers = ciphers; this._clientAuth = clientAuth; this._trustManagerFactory = trustManagerFactory; this._sslProvider = sslProvider; this._verifyHostname = verifyHostname; this._log = logProvider.GetLog(typeof(SslPolicy)); }
protected override TrustManagerFactory ConfigureTrustManagerFactory(KeyStore keyStore) { if (_trustManagerFactory != null) { return(_trustManagerFactory); } _trustManagerFactory = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); _trustManagerFactory.Init(keyStore); return(_trustManagerFactory); }
//https://github.com/square/okhttp/issues/2372#issuecomment-244807676 public static OkHttpClient.Builder EnableTls12OnPreLollipopDevices(this OkHttpClient.Builder builder) { int currentVersion = (int)Build.VERSION.SdkInt; if (currentVersion >= 16 && currentVersion < 22) { try { //Creation of X509TrustManager : https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.Builder.html#sslSocketFactory-javax.net.ssl.SSLSocketFactory-javax.net.ssl.X509TrustManager- var trustManagerFactory = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); trustManagerFactory.Init((Java.Security.KeyStore)null); var trustManagers = trustManagerFactory.GetTrustManagers(); if (trustManagers.Length != 1) { throw new Java.Lang.IllegalStateException($"Unexpected default trust managers: {trustManagers}"); } var trustManager = trustManagers[0].JavaCast <IX509TrustManager>(); if (trustManager == null) { throw new Java.Lang.IllegalStateException($"Unexpected default trust managers: {trustManagers}"); } var context = SSLContext.GetInstance("TLS"); context.Init(null, new ITrustManager[] { trustManager }, null); builder.SslSocketFactory(new ImprovedSSLSocketFactory(context.SocketFactory, trustManager), trustManager); ConnectionSpec connectionSpec = new ConnectionSpec.Builder(ConnectionSpec.ModernTls) .TlsVersions(TlsVersion.Tls12) .Build(); List <ConnectionSpec> connexionSpecs = new List <ConnectionSpec> { new ConnectionSpec.Builder(ConnectionSpec.ModernTls).TlsVersions(TlsVersion.Tls12).Build(), ConnectionSpec.ModernTls, ConnectionSpec.CompatibleTls, ConnectionSpec.Cleartext, }; builder.ConnectionSpecs(connexionSpecs); } catch (Exception ex) { Android.Util.Log.Warn("ModernHttpClient", $"Unable to enable TLS 1.2 on okhttpclient: {ex}"); } } return(builder); }
private void SetHandler() { var algoritm = TrustManagerFactory.DefaultAlgorithm; var trustManagerFactory = TrustManagerFactory.GetInstance(algoritm); trustManagerFactory.Init((KeyStore)null); var tm = new ITrustManager[] { new PublicKeyManager() }; SSLContext sslContext = SSLContext.GetInstance("TLS"); sslContext.Init(null, tm, null); SSLContext.Default = sslContext; HttpsURLConnection.DefaultSSLSocketFactory = sslContext.SocketFactory; }
void IGetCertificates.DebugWrite(string text) { //KeyStore certStore = KeyStore.GetInstance("AndroidCAStore"); KeyStore certStore = KeyStore.GetInstance(KeyStore.DefaultType); // == BKS certStore.Load(null); // https://forums.xamarin.com/discussion/14938/certificate-pinning-in-monodroid var tmf = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); tmf.Init((KeyStore)null); foreach (var itm in tmf.GetTrustManagers()) { var tm = itm.JavaCast <IX509TrustManager>(); } Debug.WriteLine($"\n\nGetCertificatesAndroid says; {text}\n\n"); }
protected override SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection) { var algorithm = TrustManagerFactory.DefaultAlgorithm; var trustManagerFactory = TrustManagerFactory.GetInstance(algorithm); trustManagerFactory.Init((KeyStore)null); var trustManagers = trustManagerFactory.GetTrustManagers(); var context = SSLContext.GetInstance("TLS"); context.Init(null, trustManagers, null); SSLContext.Default = context; if (_trustManagerExt == null) { var x509TrustManager = trustManagers.FirstOrDefault(x => x is IX509TrustManager) as IX509TrustManager; _trustManagerExt = new X509TrustManagerExtensions(x509TrustManager); } return(context.SocketFactory); }
public CertificatePinning() { try { byte[] keyStoreBytes = Base64.Decode("H4sICIgmj1QAA2RlZmF1bHRfY2xpZW50X3RydXN0c3RvcmUuYmtzAN2aB1hUSbbHu5scJYPEBsnxNjlKziAgOSg0uQlNBgkiGVGyIogEQQQkqICIZBBQkSw5KxkRyQiI+BpBZ5xxxp19b+ftbn8fH9x7q4qqW+d3zv+cahAIBAaBQGRZ6Z6OVGc7OSWfRLZm21bvX5JmaQSBsFjBIIiQIKoNbBL3Fivo8INhxCsEiIFAEBkgBCIJhIDvQohcq2boiIY5BB+LvvRHa6+qBwgwcTgjVCI+EIDBEAwQYA7DA3Aw0fTRMUkh+rowSoD88AKXlNDAxh2hi7BDckNVkVa8MBFA6PABHimvvBPcwwMqANX2snRCWEG13RHOcHdfqLyNuyfCFmEF90S4IKGyXp72Lu4IT1+AnpJATBiA8YsBXz4mlAT8ooAo6oaAkJiQmMm/fgLBmb9eMxgDhBYcAwKCoyDBwaBWM+OMdZorYLJHRJPoMqPThg8cFGXiwvDxegSGh4m73kOMGFg+cOYrYBS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7vEUc8KNc+xYNboXas6aukLRjXr8WnMQNaGU1sUYV2u6jTpjoreWVuVU8dKT2ez4kW/uXyOnda2BavJRTSReXnAKNySmXgycZiXWy1a8oR/6EohaGBu6s8wZJH3J18bwNDnUFpsv6Vp9gTJfvlLyKsnUwVNcLp0577kKpYopgtz2/CYNY/2ouQVdM9q36auWFHh5reb4qxNPvTsffHBY35PNzRYga531s+rNsBTpD7fJIJzTZfakOU9Homv6nHs+VFWC/P4V+0rD5D9M0onJbjQWJmJ/kOZrLT2YMcm0nhhl1Iq4KegtbDjeTc7XUnyJnEHE0uN037x5JyXaqNAe1+h9CxxzevM4ZDWe1FsTUnp8C5fyhvJTTLxYeG33PNuUq+kaB+86tvO9kvukOp4X1aVbNQIyYa8PmdypIqXTjgj5FAvEcSg9pJcSGhG1pKBSK557rOM08cByCdxNz3zgF0dCTWL/5KPtTTIyRbP78/cRlMFcJiKt5zk3n01lmendytUDda2uNT5PAaglhClG7yDffioufTqyAlEmvdKwVgYr1qJ9o5X24AR++ocLBEObQgZBIxXOlHHp9MHWbw5aFKuKHStx/c5oqYe9V1+7OE4MqsDje/kgd8n78wklKwDo8r7rc1X0fwBxS2vGpDgAAA==", 0); KeyStore keyStore = KeyStore.GetInstance("BKS"); System.IO.MemoryStream ms = new System.IO.MemoryStream(keyStoreBytes); GZIPInputStream keyStoreInputStream = new GZIPInputStream(ms); //var readbytes = keyStoreInputStream.ToArray<byte>(); List <byte> values = new List <byte>(); bool notdone = true; while (notdone) { var input = keyStoreInputStream.Read(); if (input < 0 || input > 255) { break; } values.Add((byte)input); } try { keyStore.Load(new System.IO.MemoryStream(values.ToArray()), null); } finally { keyStoreInputStream.Close(); } TRUST_STORE = keyStore; String algorithm = TrustManagerFactory.DefaultAlgorithm; TrustManagerFactory trustManagerFactory = TrustManagerFactory.GetInstance(algorithm); trustManagerFactory.Init(keyStore); SSLContext context = SSLContext.GetInstance("TLS"); context.Init(null, trustManagerFactory.GetTrustManagers(), null); SSL_CONTEXT = context; } catch (Exception e) { } }
public static IX509TrustManager GetSystemDefaultTrustManager() { IX509TrustManager x509TrustManager = null; try { var trustManagerFactory = TrustManagerFactory.GetInstance(TrustManagerFactory.DefaultAlgorithm); trustManagerFactory.Init((KeyStore)null); foreach (var trustManager in trustManagerFactory.GetTrustManagers()) { var manager = trustManager.JavaCast <IX509TrustManager>(); if (manager != null) { x509TrustManager = manager; break; } } } catch (Exception ex) when(ex is NoSuchAlgorithmException || ex is KeyStoreException) { // move along... } return(x509TrustManager); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private javax.net.ssl.TrustManagerFactory createTrustManagerFactory(boolean trustAll, java.io.File trustedCertificatesDir, java.util.Collection<java.security.cert.X509CRL> crls, org.neo4j.ssl.ClientAuth clientAuth) throws Exception private TrustManagerFactory CreateTrustManagerFactory(bool trustAll, File trustedCertificatesDir, ICollection <X509CRL> crls, ClientAuth clientAuth) { if (trustAll) { return(InsecureTrustManagerFactory.INSTANCE); } KeyStore trustStore = KeyStore.getInstance(KeyStore.DefaultType); trustStore.load(null, null); File[] trustedCertFiles = trustedCertificatesDir.listFiles(); if (trustedCertFiles == null) { throw new Exception(format("Could not find or list files in trusted directory: %s", trustedCertificatesDir)); } else if (clientAuth == ClientAuth.REQUIRE && trustedCertFiles.Length == 0) { throw new Exception(format("Client auth is required but no trust anchors found in: %s", trustedCertificatesDir)); } int i = 0; foreach (File trustedCertFile in trustedCertFiles) { CertificateFactory certificateFactory = CertificateFactory.getInstance(PkiUtils.CERTIFICATE_TYPE); using (Stream input = Files.newInputStream(trustedCertFile.toPath())) { while (input.available() > 0) { try { X509Certificate cert = ( X509Certificate )certificateFactory.generateCertificate(input); trustStore.setCertificateEntry(Convert.ToString(i++), cert); } catch (Exception e) { throw new CertificateException("Error loading certificate file: " + trustedCertFile, e); } } } } TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.DefaultAlgorithm); if (crls.Count > 0) { PKIXBuilderParameters pkixParamsBuilder = new PKIXBuilderParameters(trustStore, new X509CertSelector()); pkixParamsBuilder.RevocationEnabled = true; pkixParamsBuilder.addCertStore(CertStore.getInstance("Collection", new CollectionCertStoreParameters(crls))); trustManagerFactory.init(new CertPathTrustManagerParameters(pkixParamsBuilder)); } else { trustManagerFactory.init(trustStore); } return(trustManagerFactory); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: public static void main(String[] paramArrayOfString) throws Exception public static void Main(string[] paramArrayOfString) { bool @bool; char[] arrayOfChar; char c; string str1; if (paramArrayOfString.Length == 1 || paramArrayOfString.Length == 2) { string[] arrayOfString = paramArrayOfString[0].Split(":", true); str1 = arrayOfString[0]; c = (arrayOfString.Length == 1) ? (char)443 : (char)int.Parse(arrayOfString[1]); string str = (paramArrayOfString.Length == 1) ? "changeit" : paramArrayOfString[1]; arrayOfChar = str.ToCharArray(); } else { Console.WriteLine("Usage: java InstallCert [:port] [passphrase]"); return; } File file = new File("jssecacerts"); if (!file.File) { char c1 = Path.DirectorySeparatorChar; File file1 = new File(System.getProperty("java.home") + c1 + "lib" + c1 + "security"); file = new File(file1, "jssecacerts"); if (!file.File) { file = new File(file1, "cacerts"); } } Console.WriteLine("Loading KeyStore " + file + "..."); FileStream fileInputStream = new FileStream(file, FileMode.Open, FileAccess.Read); KeyStore keyStore = KeyStore.getInstance(KeyStore.DefaultType); keyStore.load(fileInputStream, arrayOfChar); fileInputStream.Close(); SSLContext sSLContext = SSLContext.getInstance("TLS"); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.DefaultAlgorithm); trustManagerFactory.init(keyStore); X509TrustManager x509TrustManager = (X509TrustManager)trustManagerFactory.TrustManagers[0]; SavingTrustManager savingTrustManager = new SavingTrustManager(x509TrustManager); sSLContext.init(null, new TrustManager[] { savingTrustManager }, null); SSLSocketFactory sSLSocketFactory = sSLContext.SocketFactory; Console.WriteLine("Opening connection to " + str1 + ":" + c + "..."); SSLSocket sSLSocket = (SSLSocket)sSLSocketFactory.createSocket(str1, c); sSLSocket.SoTimeout = 10000; try { Console.WriteLine("Starting SSL handshake..."); sSLSocket.startHandshake(); sSLSocket.close(); Console.WriteLine(); Console.WriteLine("No errors, certificate is already trusted"); } catch (SSLException sSLException) { Console.WriteLine(); sSLException.printStackTrace(System.out); } X509Certificate[] arrayOfX509Certificate = savingTrustManager.chain; if (arrayOfX509Certificate == null) { Console.WriteLine("Could not obtain server certificate chain"); return; } StreamReader bufferedReader = new StreamReader(System.in); Console.WriteLine(); Console.WriteLine("Server sent " + arrayOfX509Certificate.Length + " certificate(s):"); Console.WriteLine(); MessageDigest messageDigest1; MessageDigest messageDigest2 = (messageDigest1 = MessageDigest.getInstance("SHA1")).getInstance("MD5"); for (sbyte b = 0; b < arrayOfX509Certificate.Length; b++) { X509Certificate x509Certificate1 = arrayOfX509Certificate[b]; Console.WriteLine(" " + (b + true) + " Subject " + x509Certificate1.SubjectDN); Console.WriteLine(" Issuer " + x509Certificate1.IssuerDN); messageDigest1.update(x509Certificate1.Encoded); Console.WriteLine(" sha1 " + toHexString(messageDigest1.digest())); messageDigest2.update(x509Certificate1.Encoded); Console.WriteLine(" md5 " + toHexString(messageDigest2.digest())); Console.WriteLine(); } Console.WriteLine("Enter certificate to add to trusted keystore or 'q' to quit: [1]"); string str2 = bufferedReader.ReadLine().Trim(); try { @bool = (str2.Length == 0) ? 0 : (int.Parse(str2) - 1); } catch (System.FormatException) { Console.WriteLine("KeyStore not changed"); return; } X509Certificate x509Certificate = arrayOfX509Certificate[@bool]; string str3 = str1 + "-" + (@bool + true); keyStore.setCertificateEntry(str3, x509Certificate); FileStream fileOutputStream = new FileStream("jssecacerts", FileMode.Create, FileAccess.Write); keyStore.store(fileOutputStream, arrayOfChar); fileOutputStream.Close(); Console.WriteLine(); Console.WriteLine(x509Certificate); Console.WriteLine(); Console.WriteLine("Added certificate to keystore 'jssecacerts' using alias '" + str3 + "'"); }
private SSLSocketFactory getSSLSocketFactory() { SSLSocketFactory factory = null; try { //reading the keyStore path and password from the environment properties string keyStorePath = java.lang.System.getProperty("javax.net.ssl.keyStore"); java.io.FileInputStream keyStoreStream = null; if (keyStorePath != null) { java.io.File file = new java.io.File(keyStorePath); if (file.exists()) { keyStoreStream = new java.io.FileInputStream(file); } else { keyStoreStream = searchDefaultCacerts(); } } else { keyStoreStream = searchDefaultCacerts(); } string keyStorePassWord = java.lang.System.getProperty("javax.net.ssl.keyStorePassword"); if (keyStorePassWord == null) { keyStorePassWord = "******"; } char[] passphrase = keyStorePassWord.ToCharArray(); //initiating SSLContext SSLContext ctx = SSLContext.getInstance("TLS"); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore ks = KeyStore.getInstance("JKS"); if (keyStoreStream != null) { ks.load(keyStoreStream, passphrase); } else { ks.load(null, null); } kmf.init(ks, passphrase); tmf.init(ks); ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); factory = ctx.getSocketFactory(); } catch (Exception e) { factory = null; #if DEBUG Console.WriteLine("Can't get SSL Socket Factory, the exception is {0}, {1}", e.GetType(), e.Message); #endif } return(factory); }
public static ITrustManager[] getTrustManagers(this TrustManagerFactory factory) { return(factory.GetTrustManagers()); }
public static void init(this TrustManagerFactory factory, KeyStore ks) { factory.Init(ks); }