]> git.basschouten.com Git - openhab-addons.git/blob
b22a31aa19aeab1c607b8795431d2b429093c2d5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.digitalstrom.internal.lib.serverconnection.impl;
14
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.HttpURLConnection;
22 import java.net.MalformedURLException;
23 import java.net.SocketTimeoutException;
24 import java.net.URL;
25 import java.nio.charset.StandardCharsets;
26 import java.security.KeyManagementException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.SecureRandom;
29 import java.security.Security;
30 import java.security.cert.CertificateEncodingException;
31 import java.security.cert.CertificateException;
32 import java.security.cert.CertificateFactory;
33 import java.security.cert.X509Certificate;
34 import java.util.Base64;
35
36 import javax.net.ssl.HostnameVerifier;
37 import javax.net.ssl.HttpsURLConnection;
38 import javax.net.ssl.SSLContext;
39 import javax.net.ssl.SSLHandshakeException;
40 import javax.net.ssl.SSLSession;
41 import javax.net.ssl.SSLSocketFactory;
42 import javax.net.ssl.TrustManager;
43 import javax.net.ssl.X509TrustManager;
44
45 import org.apache.commons.io.IOUtils;
46 import org.apache.commons.lang.StringUtils;
47 import org.openhab.binding.digitalstrom.internal.lib.config.Config;
48 import org.openhab.binding.digitalstrom.internal.lib.manager.ConnectionManager;
49 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.HttpTransport;
50 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.simpledsrequestbuilder.constants.ParameterKeys;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * The {@link HttpTransportImpl} executes an request to the digitalSTROM-Server.
56  * <p>
57  * If a {@link Config} is given at the constructor. It sets the SSL-Certificate what is set in
58  * {@link Config#getCert()}. If there is no SSL-Certificate, but an path to an external SSL-Certificate file what is set
59  * in {@link Config#getTrustCertPath()} this will be set. If no SSL-Certificate is set in the {@link Config} it will be
60  * red out from the server and set in {@link Config#setCert(String)}.
61  *
62  * <p>
63  * If no {@link Config} is given the SSL-Certificate will be stored locally.
64  *
65  * <p>
66  * The method {@link #writePEMCertFile(String)} saves the SSL-Certificate in a file at the given path. If all
67  * SSL-Certificates shout be ignored the flag <i>exeptAllCerts</i> have to be true at the constructor
68  * </p>
69  * <p>
70  * If a {@link ConnectionManager} is given at the constructor, the session-token is not needed by requests and the
71  * {@link ConnectionListener}, which is registered at the {@link ConnectionManager}, will be automatically informed
72  * about
73  * connection state changes through the {@link #execute(String, int, int)} method.
74  * </p>
75  *
76  * @author Michael Ochel - Initial contribution
77  * @author Matthias Siegele - Initial contribution
78  */
79 public class HttpTransportImpl implements HttpTransport {
80
81     private static final String LINE_SEPERATOR = System.getProperty("line.separator");
82     private static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----" + LINE_SEPERATOR;
83     private static final String END_CERT = LINE_SEPERATOR + "-----END CERTIFICATE-----" + LINE_SEPERATOR;
84
85     private final Logger logger = LoggerFactory.getLogger(HttpTransportImpl.class);
86     private static final short MAY_A_NEW_SESSION_TOKEN_IS_NEEDED = 1;
87
88     private String uri;
89
90     private int connectTimeout;
91     private int readTimeout;
92
93     private Config config;
94
95     private ConnectionManager connectionManager;
96
97     private String cert;
98     private SSLSocketFactory sslSocketFactory;
99     private final HostnameVerifier hostnameVerifier = new HostnameVerifier() {
100
101         @Override
102         public boolean verify(String arg0, SSLSession arg1) {
103             return arg0.equals(arg1.getPeerHost()) || arg0.contains("dss.local.");
104         }
105     };
106
107     /**
108      * Creates a new {@link HttpTransportImpl} with registration of the given {@link ConnectionManager} and set ignore
109      * all SSL-Certificates. The {@link Config} will be automatically added from the configurations of the given
110      * {@link ConnectionManager}.
111      *
112      * @param connectionManager to check connection, can be null
113      * @param exeptAllCerts (true = all will ignore)
114      */
115     public HttpTransportImpl(ConnectionManager connectionManager, boolean exeptAllCerts) {
116         this.connectionManager = connectionManager;
117         this.config = connectionManager.getConfig();
118         init(config.getHost(), config.getConnectionTimeout(), config.getReadTimeout(), exeptAllCerts);
119     }
120
121     /**
122      * Creates a new {@link HttpTransportImpl} with configurations of the given {@link Config} and set ignore all
123      * SSL-Certificates.
124      *
125      * @param config to get configurations, must not be null
126      * @param exeptAllCerts (true = all will ignore)
127      */
128     public HttpTransportImpl(Config config, boolean exeptAllCerts) {
129         this.config = config;
130         init(config.getHost(), config.getConnectionTimeout(), config.getReadTimeout(), exeptAllCerts);
131     }
132
133     /**
134      * Creates a new {@link HttpTransportImpl} with configurations of the given {@link Config}.
135      *
136      * @param config to get configurations, must not be null
137      */
138     public HttpTransportImpl(Config config) {
139         this.config = config;
140         init(config.getHost(), config.getConnectionTimeout(), config.getReadTimeout(), false);
141     }
142
143     /**
144      * Creates a new {@link HttpTransportImpl}.
145      *
146      * @param uri of the server, must not be null
147      */
148     public HttpTransportImpl(String uri) {
149         init(uri, Config.DEFAULT_CONNECTION_TIMEOUT, Config.DEFAULT_READ_TIMEOUT, false);
150     }
151
152     /**
153      * Creates a new {@link HttpTransportImpl} and set ignore all SSL-Certificates.
154      *
155      * @param uri of the server, must not be null
156      * @param exeptAllCerts (true = all will ignore)
157      */
158     public HttpTransportImpl(String uri, boolean exeptAllCerts) {
159         init(uri, Config.DEFAULT_CONNECTION_TIMEOUT, Config.DEFAULT_READ_TIMEOUT, exeptAllCerts);
160     }
161
162     /**
163      * Creates a new {@link HttpTransportImpl}.
164      *
165      * @param uri of the server, must not be null
166      * @param connectTimeout to set
167      * @param readTimeout to set
168      */
169     public HttpTransportImpl(String uri, int connectTimeout, int readTimeout) {
170         init(uri, connectTimeout, readTimeout, false);
171     }
172
173     /**
174      * Creates a new {@link HttpTransportImpl} and set ignore all SSL-Certificates..
175      *
176      * @param uri of the server, must not be null
177      * @param connectTimeout to set
178      * @param readTimeout to set
179      * @param exeptAllCerts (true = all will ignore)
180      */
181     public HttpTransportImpl(String uri, int connectTimeout, int readTimeout, boolean exeptAllCerts) {
182         init(uri, connectTimeout, readTimeout, exeptAllCerts);
183     }
184
185     private void init(String uri, int connectTimeout, int readTimeout, boolean exeptAllCerts) {
186         logger.debug("init HttpTransportImpl");
187         this.uri = fixURI(uri);
188         this.connectTimeout = connectTimeout;
189         this.readTimeout = readTimeout;
190         // Check SSL Certificate
191         if (exeptAllCerts) {
192             sslSocketFactory = generateSSLContextWhichAcceptAllSSLCertificats();
193         } else {
194             if (config != null) {
195                 cert = config.getCert();
196                 logger.debug("generate SSLcontext from config cert");
197                 if (StringUtils.isNotBlank(cert)) {
198                     sslSocketFactory = generateSSLContextFromPEMCertString(cert);
199                 } else {
200                     if (StringUtils.isNotBlank(config.getTrustCertPath())) {
201                         logger.debug("generate SSLcontext from config cert path");
202                         cert = readPEMCertificateStringFromFile(config.getTrustCertPath());
203                         if (StringUtils.isNotBlank(cert)) {
204                             sslSocketFactory = generateSSLContextFromPEMCertString(cert);
205                         }
206                     } else {
207                         logger.debug("generate SSLcontext from server");
208                         cert = getPEMCertificateFromServer(this.uri);
209                         sslSocketFactory = generateSSLContextFromPEMCertString(cert);
210                         if (sslSocketFactory != null) {
211                             config.setCert(cert);
212                         }
213                     }
214                 }
215             } else {
216                 logger.debug("generate SSLcontext from server");
217                 cert = getPEMCertificateFromServer(this.uri);
218                 sslSocketFactory = generateSSLContextFromPEMCertString(cert);
219             }
220         }
221     }
222
223     private String fixURI(String uri) {
224         String fixedURI = uri;
225         if (!fixedURI.startsWith("https://")) {
226             fixedURI = "https://" + fixedURI;
227         }
228         if (fixedURI.split(":").length != 3) {
229             fixedURI = fixedURI + ":8080";
230         }
231         return fixedURI;
232     }
233
234     private String fixRequest(String request) {
235         return request.replace(" ", "");
236     }
237
238     @Override
239     public String execute(String request) {
240         return execute(request, this.connectTimeout, this.readTimeout);
241     }
242
243     private short loginCounter = 0;
244
245     @Override
246     public String execute(String request, int connectTimeout, int readTimeout) {
247         // NOTE: We will only show exceptions in the debug level, because they will be handled in the checkConnection()
248         // method and this changes the bridge state. If a command was send it fails than and a sensorJob will be
249         // execute the next time, by TimeOutExceptions. By other exceptions the checkConnection() method handles it in
250         // max 1 second.
251         String response = null;
252         HttpsURLConnection connection = null;
253         try {
254             String correctedRequest = checkSessionToken(request);
255             connection = getConnection(correctedRequest, connectTimeout, readTimeout);
256             if (connection != null) {
257                 connection.connect();
258                 final int responseCode = connection.getResponseCode();
259                 if (responseCode != HttpURLConnection.HTTP_FORBIDDEN) {
260                     if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
261                         response = new String(connection.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
262                     } else {
263                         response = new String(connection.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
264                     }
265                     if (response != null) {
266                         if (!response.contains("Authentication failed")) {
267                             if (loginCounter > 0) {
268                                 connectionManager.checkConnection(responseCode);
269                             }
270                             loginCounter = 0;
271                         } else {
272                             connectionManager.checkConnection(ConnectionManager.AUTHENTIFICATION_PROBLEM);
273                             loginCounter++;
274                         }
275                     }
276
277                 }
278                 connection.disconnect();
279                 if (response == null && connectionManager != null
280                         && loginCounter <= MAY_A_NEW_SESSION_TOKEN_IS_NEEDED) {
281                     if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
282                         execute(addSessionToken(correctedRequest, connectionManager.getNewSessionToken()),
283                                 connectTimeout, readTimeout);
284                         loginCounter++;
285                     } else {
286                         connectionManager.checkConnection(responseCode);
287                         loginCounter++;
288                         return null;
289                     }
290                 }
291                 return response;
292             }
293         } catch (SocketTimeoutException e) {
294             informConnectionManager(ConnectionManager.SOCKET_TIMEOUT_EXCEPTION);
295         } catch (java.net.ConnectException e) {
296             informConnectionManager(ConnectionManager.CONNECTION_EXCEPTION);
297         } catch (MalformedURLException e) {
298             informConnectionManager(ConnectionManager.MALFORMED_URL_EXCEPTION);
299         } catch (java.net.UnknownHostException e) {
300             informConnectionManager(ConnectionManager.UNKNOWN_HOST_EXCEPTION);
301         } catch (SSLHandshakeException e) {
302             informConnectionManager(ConnectionManager.SSL_HANDSHAKE_EXCEPTION);
303         } catch (IOException e) {
304             logger.error("An IOException occurred: ", e);
305             informConnectionManager(ConnectionManager.GENERAL_EXCEPTION);
306         } finally {
307             if (connection != null) {
308                 connection.disconnect();
309             }
310         }
311         return null;
312     }
313
314     private boolean informConnectionManager(int code) {
315         if (connectionManager != null && loginCounter < MAY_A_NEW_SESSION_TOKEN_IS_NEEDED) {
316             connectionManager.checkConnection(code);
317             return true;
318         }
319         return false;
320     }
321
322     private String checkSessionToken(String request) {
323         if (checkNeededSessionToken(request)) {
324             if (connectionManager != null) {
325                 String sessionToken = connectionManager.getSessionToken();
326                 if (sessionToken == null) {
327                     return addSessionToken(request, connectionManager.getNewSessionToken());
328                 }
329                 return addSessionToken(request, sessionToken);
330             }
331         }
332         return request;
333     }
334
335     private boolean checkNeededSessionToken(String request) {
336         String functionName = StringUtils.substringAfterLast(StringUtils.substringBefore(request, "?"), "/");
337         return !DsAPIImpl.METHODS_MUST_NOT_BE_LOGGED_IN.contains(functionName);
338     }
339
340     private String addSessionToken(String request, String sessionToken) {
341         String correctedRequest = request;
342         if (!correctedRequest.contains(ParameterKeys.TOKEN)) {
343             if (correctedRequest.contains("?")) {
344                 correctedRequest = correctedRequest + "&" + ParameterKeys.TOKEN + "=" + sessionToken;
345             } else {
346                 correctedRequest = correctedRequest + "?" + ParameterKeys.TOKEN + "=" + sessionToken;
347             }
348         } else {
349             correctedRequest = StringUtils.replaceOnce(correctedRequest, StringUtils.substringBefore(
350                     StringUtils.substringAfter(correctedRequest, ParameterKeys.TOKEN + "="), "&"), sessionToken);
351
352         }
353         return correctedRequest;
354     }
355
356     private HttpsURLConnection getConnection(String request, int connectTimeout, int readTimeout) throws IOException {
357         String correctedRequest = request;
358         if (StringUtils.isNotBlank(correctedRequest)) {
359             correctedRequest = fixRequest(correctedRequest);
360             URL url = new URL(this.uri + correctedRequest);
361             HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
362             if (connection != null) {
363                 connection.setConnectTimeout(connectTimeout);
364                 connection.setReadTimeout(readTimeout);
365                 if (sslSocketFactory != null) {
366                     connection.setSSLSocketFactory(sslSocketFactory);
367                 }
368                 if (hostnameVerifier != null) {
369                     connection.setHostnameVerifier(hostnameVerifier);
370                 }
371             }
372             return connection;
373         }
374         return null;
375     }
376
377     @Override
378     public int checkConnection(String testRequest) {
379         try {
380             HttpsURLConnection connection = getConnection(testRequest, connectTimeout, readTimeout);
381             if (connection != null) {
382                 connection.connect();
383                 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
384                     if (new String(connection.getInputStream().readAllBytes(), StandardCharsets.UTF_8)
385                             .contains("Authentication failed")) {
386                         return ConnectionManager.AUTHENTIFICATION_PROBLEM;
387                     }
388                 }
389                 connection.disconnect();
390                 return connection.getResponseCode();
391             } else {
392                 return ConnectionManager.GENERAL_EXCEPTION;
393             }
394         } catch (SocketTimeoutException e) {
395             return ConnectionManager.SOCKET_TIMEOUT_EXCEPTION;
396         } catch (java.net.ConnectException e) {
397             return ConnectionManager.CONNECTION_EXCEPTION;
398         } catch (MalformedURLException e) {
399             return ConnectionManager.MALFORMED_URL_EXCEPTION;
400         } catch (java.net.UnknownHostException e) {
401             return ConnectionManager.UNKNOWN_HOST_EXCEPTION;
402         } catch (IOException e) {
403             return ConnectionManager.GENERAL_EXCEPTION;
404         }
405     }
406
407     @Override
408     public int getSensordataConnectionTimeout() {
409         return config != null ? config.getSensordataConnectionTimeout() : Config.DEFAULT_SENSORDATA_CONNECTION_TIMEOUT;
410     }
411
412     @Override
413     public int getSensordataReadTimeout() {
414         return config != null ? config.getSensordataReadTimeout() : Config.DEFAULT_SENSORDATA_READ_TIMEOUT;
415     }
416
417     private String readPEMCertificateStringFromFile(String path) {
418         if (StringUtils.isBlank(path)) {
419             logger.error("Path is empty.");
420         } else {
421             File dssCert = new File(path);
422             if (dssCert.exists()) {
423                 if (path.endsWith(".crt")) {
424                     try (InputStream certInputStream = new FileInputStream(dssCert)) {
425                         String cert = new String(certInputStream.readAllBytes(), StandardCharsets.UTF_8);
426                         if (cert.startsWith(BEGIN_CERT)) {
427                             return cert;
428                         } else {
429                             logger.error("File is not a PEM certificate file. PEM-Certificates starts with: {}",
430                                     BEGIN_CERT);
431                         }
432                     } catch (FileNotFoundException e) {
433                         logger.error("Can't find a certificate file at the path: {}\nPlease check the path!", path);
434                     } catch (IOException e) {
435                         logger.error("An IOException occurred: ", e);
436                     }
437                 } else {
438                     logger.error("File is not a certificate (.crt) file.");
439                 }
440             } else {
441                 logger.error("File not found");
442             }
443         }
444         return null;
445     }
446
447     @Override
448     public String writePEMCertFile(String path) {
449         String correctedPath = StringUtils.trimToEmpty(path);
450         File certFilePath;
451         if (StringUtils.isNotBlank(correctedPath)) {
452             certFilePath = new File(correctedPath);
453             boolean pathExists = certFilePath.exists();
454             if (!pathExists) {
455                 pathExists = certFilePath.mkdirs();
456             }
457             if (pathExists && !correctedPath.endsWith("/")) {
458                 correctedPath = correctedPath + "/";
459             }
460         }
461         InputStream certInputStream = IOUtils.toInputStream(cert);
462         X509Certificate trustedCert;
463         try {
464             trustedCert = (X509Certificate) CertificateFactory.getInstance("X.509")
465                     .generateCertificate(certInputStream);
466
467             certFilePath = new File(
468                     correctedPath + trustedCert.getSubjectDN().getName().split(",")[0].substring(2) + ".crt");
469             if (!certFilePath.exists()) {
470                 certFilePath.createNewFile();
471                 FileWriter writer = new FileWriter(certFilePath, true);
472                 writer.write(cert);
473                 writer.flush();
474                 writer.close();
475                 return certFilePath.getAbsolutePath();
476             } else {
477                 logger.error("File allready exists!");
478             }
479         } catch (IOException e) {
480             logger.error("An IOException occurred: ", e);
481         } catch (CertificateException e1) {
482             logger.error("A CertificateException occurred: ", e1);
483         }
484         return null;
485     }
486
487     private SSLSocketFactory generateSSLContextFromPEMCertString(String pemCert) {
488         if (StringUtils.isNotBlank(pemCert) && pemCert.startsWith(BEGIN_CERT)) {
489             try {
490                 InputStream certInputStream = IOUtils.toInputStream(pemCert);
491                 final X509Certificate trustedCert = (X509Certificate) CertificateFactory.getInstance("X.509")
492                         .generateCertificate(certInputStream);
493
494                 final TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
495
496                     @Override
497                     public java.security.cert.X509Certificate[] getAcceptedIssuers() {
498                         return null;
499                     }
500
501                     @Override
502                     public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
503                             throws CertificateException {
504                         if (!certs[0].equals(trustedCert)) {
505                             throw new CertificateException();
506                         }
507                     }
508
509                     @Override
510                     public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
511                             throws CertificateException {
512                         if (!certs[0].equals(trustedCert)) {
513                             throw new CertificateException();
514                         }
515                     }
516                 } };
517
518                 SSLContext sslContext = SSLContext.getInstance("SSL");
519                 sslContext.init(null, trustManager, new java.security.SecureRandom());
520                 return sslContext.getSocketFactory();
521             } catch (NoSuchAlgorithmException e) {
522                 logger.error("A NoSuchAlgorithmException occurred: ", e);
523             } catch (KeyManagementException e) {
524                 logger.error("A KeyManagementException occurred: ", e);
525             } catch (CertificateException e) {
526                 logger.error("A CertificateException occurred: ", e);
527             }
528         } else {
529             logger.error("Cert is empty");
530         }
531         return null;
532     }
533
534     private String getPEMCertificateFromServer(String host) {
535         HttpsURLConnection connection = null;
536         try {
537             URL url = new URL(host);
538
539             connection = (HttpsURLConnection) url.openConnection();
540             connection.setHostnameVerifier(hostnameVerifier);
541             connection.setSSLSocketFactory(generateSSLContextWhichAcceptAllSSLCertificats());
542             connection.connect();
543
544             java.security.cert.Certificate[] cert = connection.getServerCertificates();
545             connection.disconnect();
546
547             byte[] by = ((X509Certificate) cert[0]).getEncoded();
548             if (by.length != 0) {
549                 return BEGIN_CERT + Base64.getEncoder().encodeToString(by) + END_CERT;
550             }
551         } catch (MalformedURLException e) {
552             if (!informConnectionManager(ConnectionManager.MALFORMED_URL_EXCEPTION)) {
553                 logger.error("A MalformedURLException occurred: ", e);
554             }
555         } catch (IOException e) {
556             short code = ConnectionManager.GENERAL_EXCEPTION;
557             if (e instanceof java.net.ConnectException) {
558                 code = ConnectionManager.CONNECTION_EXCEPTION;
559             } else if (e instanceof java.net.UnknownHostException) {
560                 code = ConnectionManager.UNKNOWN_HOST_EXCEPTION;
561             }
562             if (!informConnectionManager(code) || code == -1) {
563                 logger.error("An IOException occurred: ", e);
564             }
565         } catch (CertificateEncodingException e) {
566             logger.error("A CertificateEncodingException occurred: ", e);
567         } finally {
568             if (connection != null) {
569                 connection.disconnect();
570             }
571         }
572         return null;
573     }
574
575     private SSLSocketFactory generateSSLContextWhichAcceptAllSSLCertificats() {
576         Security.addProvider(Security.getProvider("SunJCE"));
577         TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
578
579             @Override
580             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
581                 return null;
582             }
583
584             @Override
585             public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
586             }
587
588             @Override
589             public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
590             }
591         } };
592
593         try {
594             SSLContext sslContext = SSLContext.getInstance("SSL");
595
596             sslContext.init(null, trustAllCerts, new SecureRandom());
597
598             return sslContext.getSocketFactory();
599         } catch (KeyManagementException e) {
600             logger.error("A KeyManagementException occurred", e);
601         } catch (NoSuchAlgorithmException e) {
602             logger.error("A NoSuchAlgorithmException occurred", e);
603         }
604         return null;
605     }
606 }