HTTPS/Client-authenticated: Difference between revisions

m
→‎{{header|Wren}}: Another minor change
(Omitted EasyLang)
m (→‎{{header|Wren}}: Another minor change)
 
(5 intermediate revisions by 3 users not shown)
Line 4:
 
This task is in general useful for use with [[Creating a SOAP Client|webservice client]]s as it offers a high level of assurance that the client is an acceptable counterparty for the server. For example, [http://aws.amazon.com/ Amazon Web Services] uses this style of authentication.
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">r: request.get.certificate:"mycert.pem" "https://www.example.com" ø</syntaxhighlight>
 
=={{header|C sharp|C#}}==
Line 72 ⟶ 76:
log.Print(string(contents))
 
}
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URL;
import java.security.KeyStore;
import java.util.Scanner;
 
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
 
public final class HTTPSClientAuthenticated {
 
public static void main(String[] aArgs) throws Exception {
final String keyStorePath = "the/path/to/keystore"; // The key store contains the client's certificate
final String keyStorePassword = "my-password";
SSLContext sslContext = getSSLContext(keyStorePath, keyStorePassword);
URL url = new URI("https://somehost.com").toURL();
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(sslContext.getSocketFactory());
// Obtain response from the url
BufferedInputStream response = (BufferedInputStream) connection.getInputStream();
try ( Scanner scanner = new Scanner(response) ) {
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
}
}
private static SSLContext getSSLContext(String aPath, String aPassword) throws Exception {
KeyStore keyStore = KeyStore.getInstance("pkcs12");
keyStore.load( new FileInputStream(aPath), aPassword.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("PKIX");
keyManagerFactory.init(keyStore, aPassword.toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
return sslContext;
}
}
</syntaxhighlight>
Line 362 ⟶ 412:
{{libheader|libcurl}}
An embedded program so we can ask the C host to communicate with libcurl for us.
<syntaxhighlight lang="ecmascriptwren">/* https_clientHTTPS_Client-authenticated.wren */
 
var CURLOPT_URL = 10002
Line 398 ⟶ 448:
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc https_clientHTTPS_Client-authenticated.c -o https_clientHTTPS_Client-authenticated -lcurl -lwren -lm */
 
#include <stdio.h>
Line 498 ⟶ 548:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "https_clientHTTPS_Client-authenticated.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
9,476

edits