The Modelit Embedded HTTP server for Matlab can be set up as an HTTPS server. This HTTPS server has the same functionality as the normal HTTP server. But has the advantage that it can be used to secure incoming and outgoing requests. This is useful when sensitive information is sent and received, or when the server needs to be protected with authentication.
To set up an HTTPS server, you’ll need an SSL certificate. You can use a free service, e.g. "Let’s Encrypt", or create a self-signed certificate using OpenSSL tool, or Java’s "keytool". In this article we will show how you can configure the Embedded Server with a self signed certificate generated by using the Java "keytool".
Follow the steps below to configure the Modelit Embedded HTTP server for Matlab as an HTTPS server:
1. Prepare the SSL certificate and key with the Java "keytool":
To set up an HTTPS server, you’ll need an SSL certificate and its corresponding private key. This can be generated with the Java "keytool" which is part of the JRE. When Matlab is installed you can find this "keytool" in the following directory (Windows)
>> javadir = fullfile(matlabroot,'sys','java','jre','win64','jre','bin');
>> javadir = fullfile(matlabroot,'sys','java','jre','glnxa64','jre','bin');
>> alias = 'localhost';
>> keymanagerpassword = 'keypass';
>> keystorepassword = 'storepass';
>> keystore = fullfile(directory, 'keystore.jks');
>> cn = 'localhost';
>> o = 'your organization';
>> c = 'your country';
>> dos(sprintf('"%s\\keytool" -genkey -alias %s -keyalg RSA -keypass %s -storepass %s -keystore %s -dname "CN=%s,O=%s,C=%s" -keysize 2048 -ext san=dns:localhost,ip:127.0.0.1', javadir, alias, keymanagerpassword, keystorepassword, keystore, cn, o, c));
>> hostname = 'localhost';
>> port = 8081;
>> server = modelit.web.server.Server(port, @infoCallback, 'hostname', hostname, 'keystore', keystore, 'keystorepassword' keystorepassword, 'keymanagerpassword', keymanagerpassword);
>> server = start(server);
>> server = start(server);
>> request = modelit.web.client.HttpRequest('get','https://localhost:8081');
>> response = request.send();
function [keystore, keystorepassword, keymanagerpassword] = generateSelfSignedCertificate(directory) % generateSelfSignedCertificate - Generate a selfsigned certificate to use % https with the modelit.web.server.Server. This function generates the % following files: keystore.jks and saves them in the specified directory. % % To use the modelit modelit.web.server.Server as a https server it is necessary to generate % a keystore.jks file. This function does exactly this, you can use its % output arguments to call the modelit.web.server.Server constructor and % create a https with a (selfsigned) certificate. % % CALL: % [keystore, keystorepassword, keymanagerpassword] = generateSelfSignedCertificate(directory) % % INPUT: % directory: <string> % (optional) path where the keystore is saved. Default value: pwd. % % OUTPUT: % keystore: <string> % full path to the keystore.jks file % keystorepassword: <string> % keystore password % keymanagerpassword: <string> % keymanager password % % USAGE: % [keystore, keystorepassword, keymanagerpassword] = generateSelfSignedCertificate(directory) % % % Create the https server. % hostname = 'localhost'; % port = 8081; % server = modelit.web.server.Server(port, @(e)callback(e), 'hostname', hostname, 'keystore', keystore, 'keystorepassword' keystorepassword, 'keymanagerpassword', keymanagerpassword); % server = start(server); % % % Create an HTTP Request (in another Matlab session) or with Postman or % from a browser. % request = modelit.web.client.HttpRequest('get','https://localhost:8081'); % response = request.send(); % % REQUIRES: % keytool: % keytool is a tool to manage (public/private) security keys and certificates and store them in a Java KeyStore file (stored_file_name. jks). % It is provided with any standard JDK / JRE distributions. You can find it under the following folder %JAVA_HOME%\bin . if nargin < 1 directory = pwd; end if ispc javadir = fullfile(matlabroot,'sys','java','jre','win64','jre','bin'); else javadir = fullfile(matlabroot,'sys','java','jre','glnxa64','jre','bin'); end alias = 'localhost'; keymanagerpassword = 'keypass'; keystorepassword = 'storepass'; cername = fullfile(directory, 'server.cer'); keystore = fullfile(directory, 'keystore.jks'); cn = 'localhost'; o = 'Modelit'; % Organization c = 'NL'; % Country dos(sprintf('"%s\\keytool" -genkey -alias %s -keyalg RSA -keypass %s -storepass %s -keystore %s -dname "CN=%s,O=%s,C=%s" -keysize 2048 -ext san=dns:localhost,ip:127.0.0.1', javadir, alias, keymanagerpassword, keystorepassword, keystore, cn, o, c));