diff --git a/README.md b/README.md new file mode 100644 index 0000000..628a216 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Using the SSL Certificates Chain API web service + +[SSL Certificates Chain API](https://threatintelligenceplatform.com/threat-intelligence-apis/ssl-certificates-chain-api) +provides you an opportunity to get a chain of SSL certificates for any active +domain name. + +Here you'll find examples of querying the API implemented in multiple +languages. + +You'll need a +[Threat Intelligence Platform account](https://threatintelligenceplatform.com/signup) to +authenticate. + +Please, refer to the +[SSL Certificates Chain API Guide](https://threatintelligenceplatform.com/threat-intelligence-api-docs/ssl-certificates-chain-api) +for info on input parameters, request/response formats, authentication +instructions and more. diff --git a/java/pom.xml b/java/pom.xml new file mode 100644 index 0000000..df83a75 --- /dev/null +++ b/java/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + com.threatintelligenceplatform + ssl-certificates-chain-api-sample + 0.0.1 + jar + + ssl-certificates-chain-api-sample + Example of using the SSL Certificates Chain API + + + + com.google.code.gson + gson + 2.8.2 + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + SslCertificatesChainApi + + + + + \ No newline at end of file diff --git a/java/src/main/java/SslCertificatesChainApi.java b/java/src/main/java/SslCertificatesChainApi.java new file mode 100644 index 0000000..e7254b7 --- /dev/null +++ b/java/src/main/java/SslCertificatesChainApi.java @@ -0,0 +1,81 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLEncoder; + +import javax.net.ssl.HttpsURLConnection; + +import com.google.gson.*; + +public class SslCertificatesChainApi { + private static final String BASE_URL = + "https://api.threatintelligenceplatform.com/v1/sslCertificatesChain"; + + private String apiKey; + + public static void main(String[] args) + { + SslCertificatesChainApi api = new SslCertificatesChainApi(); + api.setApiKey("Your SSL Certificates Chain API key"); + + try { + System.out.println(api.sendGet("threatintelligenceplatform.com")); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public String sendGet(String domain) throws Exception + { + String userAgent = "Mozilla/5.0"; + String url = this.buildUrl(domain); + + URL obj = new URL(url); + + HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); + + con.setRequestMethod("GET"); + con.setRequestProperty("User-Agent", userAgent); + + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream())); + + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + return prettyJson(response.toString()); + } + + public void setApiKey(String apiKey) + { + this.apiKey = apiKey; + } + + private String buildUrl(String domain) throws IOException + { + return SslCertificatesChainApi.BASE_URL + + "?apiKey=" + URLEncoder.encode(getApiKey(), "UTF-8") + + "&domainName=" + URLEncoder.encode(domain, "UTF-8"); + } + + private String getApiKey() + { + return this.apiKey; + } + + private String prettyJson(String jsonString) + { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + JsonParser jp = new JsonParser(); + JsonElement je = jp.parse(jsonString); + + return gson.toJson(je); + } +} diff --git a/java/ssl-certificates-chain-api-sample.iml b/java/ssl-certificates-chain-api-sample.iml new file mode 100644 index 0000000..ae0ca17 --- /dev/null +++ b/java/ssl-certificates-chain-api-sample.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/js/ssl-certificates-chain-api-jquery.html b/js/ssl-certificates-chain-api-jquery.html new file mode 100644 index 0000000..a6725b0 --- /dev/null +++ b/js/ssl-certificates-chain-api-jquery.html @@ -0,0 +1,40 @@ + + + + SSL Certificates Chain API Sample + + + + + +
+

+
+
+
\ No newline at end of file
diff --git a/net/SslCertificatesChainApiSample.sln b/net/SslCertificatesChainApiSample.sln
new file mode 100644
index 0000000..3a8d2ca
--- /dev/null
+++ b/net/SslCertificatesChainApiSample.sln
@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2013
+VisualStudioVersion = 12.0.0.0
+MinimumVisualStudioVersion = 10.0.0.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SslCertificatesChainApiSample", "SslCertificatesChainApiSample/SslCertificatesChainApiSample.csproj", "{ADDD9223-A632-4968-BC23-50A20EAE935B}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{ADDD9223-A632-4968-BC23-50A20EAE935B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{ADDD9223-A632-4968-BC23-50A20EAE935B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{ADDD9223-A632-4968-BC23-50A20EAE935B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{ADDD9223-A632-4968-BC23-50A20EAE935B}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal
diff --git a/net/SslCertificatesChainApiSample/Properties/AssemblyInfo.cs b/net/SslCertificatesChainApiSample/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..1424c0a
--- /dev/null
+++ b/net/SslCertificatesChainApiSample/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("SslCertificatesChainApiSample")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("SslCertificatesChainApiSample")]
+[assembly: AssemblyCopyright("Copyright ©  2018")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("ADDD9223-A632-4968-BC23-50A20EAE935B")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
\ No newline at end of file
diff --git a/net/SslCertificatesChainApiSample/SslCertificatesChainApiSample.cs b/net/SslCertificatesChainApiSample/SslCertificatesChainApiSample.cs
new file mode 100644
index 0000000..3371d68
--- /dev/null
+++ b/net/SslCertificatesChainApiSample/SslCertificatesChainApiSample.cs
@@ -0,0 +1,63 @@
+using System;
+using System.Net;
+
+using Newtonsoft.Json;
+
+namespace SslCertificatesChainApiSample
+{
+    internal class SslCertificatesChainApiSample
+    {
+        private static void Main()
+        {
+            var client = new SslCertificatesChainApiClient
+            {
+                ApiKey = "Your SSL Certificates Chain API key"
+            };
+
+            const string domain = "threatintelligenceplatform.com";
+
+            // Download JSON
+            var result = client.SendGet(domain);
+
+            // Print a nice informative string
+            PrintResponse(result);
+        }
+
+        private static void PrintResponse(string response)
+        {
+            dynamic responseObject = JsonConvert.DeserializeObject(response);
+
+            if (responseObject != null)
+            {
+                Console.Write("Chain length: " + responseObject.Count);
+                Console.WriteLine("\n--------------------------------");
+                return;
+            }
+
+            Console.WriteLine();
+        }
+    }
+
+    public class SslCertificatesChainApiClient
+    {
+        public string ApiKey { private get; set; }
+
+        private const string Url =
+            "https://api.threatintelligenceplatform.com/v1/sslCertificatesChain";
+
+        public string SendGet(string domain)
+        {
+            var requestParams = "?domainName=" + Uri.EscapeDataString(domain)
+                                + "&apiKey=" + Uri.EscapeDataString(ApiKey);
+
+            var fullUrl = Url + requestParams;
+
+            Console.Write("Sending request to: " + fullUrl + "\n");
+
+            // Download JSON into a string
+            var result = new WebClient().DownloadString(fullUrl);
+
+            return result;
+        }
+    }
+}
\ No newline at end of file
diff --git a/net/SslCertificatesChainApiSample/SslCertificatesChainApiSample.csproj b/net/SslCertificatesChainApiSample/SslCertificatesChainApiSample.csproj
new file mode 100644
index 0000000..9963814
--- /dev/null
+++ b/net/SslCertificatesChainApiSample/SslCertificatesChainApiSample.csproj
@@ -0,0 +1,62 @@
+
+
+  
+  
+    Debug
+    AnyCPU
+    {ADDD9223-A632-4968-BC23-50A20EAE935B}
+    {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
+    Exe
+    Properties
+    SslCertificatesChainApiSample
+    SslCertificatesChainApiSample
+    v4.5
+    512
+  
+  
+    AnyCPU
+    true
+    full
+    false
+    bin\Debug\
+    DEBUG;TRACE
+    prompt
+    4
+  
+  
+    AnyCPU
+    pdbonly
+    true
+    bin\Release\
+    TRACE
+    prompt
+    4
+  
+  
+    
+    
+      ..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll
+    
+    
+    
+    
+    
+    
+    
+  
+  
+    
+    
+  
+  
+    
+  
+  
+  
+
\ No newline at end of file
diff --git a/net/SslCertificatesChainApiSample/packages.config b/net/SslCertificatesChainApiSample/packages.config
new file mode 100644
index 0000000..7f83af3
--- /dev/null
+++ b/net/SslCertificatesChainApiSample/packages.config
@@ -0,0 +1,4 @@
+
+
+  
+
\ No newline at end of file
diff --git a/node/ssl-certificates-chain-api-sample.js b/node/ssl-certificates-chain-api-sample.js
new file mode 100644
index 0000000..d34660e
--- /dev/null
+++ b/node/ssl-certificates-chain-api-sample.js
@@ -0,0 +1,36 @@
+let https = require('https');
+let querystring = require('querystring');
+
+const apiUrl = 'https://api.threatintelligenceplatform.com/v1/sslCertificatesChain';
+
+const parameters = {
+    apiKey: 'Your SSL Certificates Chain API key',
+    domainName: 'threatintelligenceplatform.com'
+};
+
+let url = apiUrl + '?' + querystring.stringify(parameters);
+
+https.get(url, function (res) {
+    let rawData = '';
+
+    res.on('data', function(chunk) {
+        rawData += chunk;
+    });
+
+    res.on('end', function () {
+        try {
+            let parsedData = JSON.parse(rawData);
+
+            if (parsedData.constructor.name === 'Array')
+                console.log(
+                    'Chain length: ' + parsedData.length);
+            else
+                console.dir(parsedData, {colors: true});
+        }
+        catch (e) {
+            console.log(e.message);
+        }
+    });
+}).on('error', function(e) {
+    console.log('Error: ' + e.message);
+});
\ No newline at end of file
diff --git a/perl/ssl-certificates-chain-sample.pl b/perl/ssl-certificates-chain-sample.pl
new file mode 100644
index 0000000..7f40191
--- /dev/null
+++ b/perl/ssl-certificates-chain-sample.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+use JSON qw( decode_json );       # From CPAN
+use LWP::UserAgent;               # From CPAN
+use URI::Escape qw( uri_escape ); # From CPAN
+
+use strict;
+use warnings;
+
+my $api_key = 'Your SSL Certificates Chain API key';
+my $base_url = 'https://api.threatintelligenceplatform.com/v1/sslCertificatesChain';
+my $domain_name = 'threatintelligenceplatform.com';
+
+my $url = $base_url
+        . '?apiKey=' . uri_escape($api_key)
+        . '&domainName=' . uri_escape($domain_name);
+
+print 'Querying the API: ', $url, '...', "\n\n";
+
+my $ua = LWP::UserAgent->new;
+my $response = $ua->get($url);
+
+die $response->status_line unless $response->is_success;
+
+my @decoded_json = @{decode_json($response->content)};
+my $length = scalar @decoded_json;
+print 'Domain name: ', $domain_name, "\n";
+print 'Chain length: ', ($length > 0) ? $length : 'Something went wrong.', "\n";
\ No newline at end of file
diff --git a/php/ssl-certificates-chain-api.php b/php/ssl-certificates-chain-api.php
new file mode 100644
index 0000000..550871b
--- /dev/null
+++ b/php/ssl-certificates-chain-api.php
@@ -0,0 +1,25 @@
+ 'Your SSL Certificates Chain API key',
+    'domainName' => 'threatintelligenceplatform.com'
+);
+
+$url = $apiUrl . '?' . http_build_query($params, '', '&');
+
+print($url . PHP_EOL . PHP_EOL);
+
+$response = file_get_contents($url);
+
+$res = json_decode($response);
+
+if (! $res)
+    return;
+
+$length = count($res);
+if (! $length > 0)
+    return;
+
+echo 'Chain length: ' . $length . PHP_EOL;
\ No newline at end of file
diff --git a/powershell/ssl-certificates-chain-sample.ps1 b/powershell/ssl-certificates-chain-sample.ps1
new file mode 100644
index 0000000..a738d22
--- /dev/null
+++ b/powershell/ssl-certificates-chain-sample.ps1
@@ -0,0 +1,12 @@
+$url = 'https://api.threatintelligenceplatform.com/v1/sslCertificatesChain?'
+
+$api_key = 'at_E4ZmlrEW8oe305w0gAuHPyTddqT00'
+$domain = 'threatintelligenceplatform.com'
+
+$uri = $url `
+     + 'apiKey=' + [uri]::EscapeDataString($api_key) `
+     + '&domainName=' + [uri]::EscapeDataString($domain)
+
+$res = Invoke-WebRequest -Uri $uri -UseBasicParsing | ConvertFrom-Json
+
+echo "`nChain length: $($res.Count)"
\ No newline at end of file
diff --git a/python/ssl-certificates-chain-api-sample.py b/python/ssl-certificates-chain-api-sample.py
new file mode 100644
index 0000000..4bcf6dd
--- /dev/null
+++ b/python/ssl-certificates-chain-api-sample.py
@@ -0,0 +1,26 @@
+try:
+    from urllib.request import urlopen, pathname2url
+except ImportError:
+    from urllib import pathname2url
+    from urllib2 import urlopen
+
+import json
+
+
+def print_response(txt):
+    response_json = json.loads(txt)
+    print('Chain length: ' + str(len(response_json)))
+
+
+api_key = 'Your SSL Certificates Chain API key'
+domain = 'threatintelligenceplatform.com'
+
+url = 'https://api.threatintelligenceplatform.com/v1/sslCertificatesChain'\
+    + '?apiKey=' + pathname2url(api_key)\
+    + '&domainName=' + pathname2url(domain)
+
+try:
+    response = urlopen(url).read().decode('utf8')
+    print_response(urlopen(url).read().decode('utf8'))
+except Exception as e:
+    print(e)
diff --git a/ruby/ssl-certificates-chain-api.rb b/ruby/ssl-certificates-chain-api.rb
new file mode 100644
index 0000000..ba736bb
--- /dev/null
+++ b/ruby/ssl-certificates-chain-api.rb
@@ -0,0 +1,24 @@
+require 'erb'
+require 'json'
+require 'net/https'
+require 'uri'
+require 'yaml'
+
+api_key = 'Your SSL Certificates Chain API key'
+domain = 'threatintelligenceplatform.com'
+
+api_url = 'https://api.threatintelligenceplatform.com/v1/sslCertificatesChain'
+
+url = api_url +
+      '?apiKey=' + ERB::Util.url_encode(api_key) +
+      '&domainName=' + ERB::Util.url_encode(domain)
+
+begin
+  buffer = Net::HTTP.get(URI.parse(url))
+  json = JSON.parse(buffer)
+  puts json.to_yaml
+rescue Errno::ECONNRESET, Errno::EINVAL, EOFError, JSON::ParserError,
+       Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
+       OpenSSL::SSL::SSLError, Timeout::Error, URI::InvalidURIError => e
+  puts e
+end
\ No newline at end of file