]> git.basschouten.com Git - openhab-addons.git/blob
e825c190e376c716d4bef1af4c1da918c5fbfbf2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.vizio.internal.communication;
14
15 import java.net.MalformedURLException;
16 import java.security.cert.CertificateException;
17
18 import javax.net.ssl.X509ExtendedTrustManager;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.core.io.net.http.PEMTrustManager;
22 import org.openhab.core.io.net.http.TlsTrustManagerProvider;
23 import org.openhab.core.io.net.http.TrustAllTrustManager;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Provides a {@link PEMTrustManager} to allow secure connections to a Vizio TV that uses self signed
29  * certificates.
30  *
31  * @author Christoph Weitkamp - Initial Contribution
32  * @author Michael Lobstein - Adapted for Vizio binding
33  */
34 @NonNullByDefault
35 public class VizioTlsTrustManagerProvider implements TlsTrustManagerProvider {
36     private final String hostname;
37
38     private final Logger logger = LoggerFactory.getLogger(VizioTlsTrustManagerProvider.class);
39
40     public VizioTlsTrustManagerProvider(String hostname) {
41         this.hostname = hostname;
42     }
43
44     @Override
45     public String getHostName() {
46         return hostname;
47     }
48
49     @Override
50     public X509ExtendedTrustManager getTrustManager() {
51         try {
52             logger.trace("Use self-signed certificate downloaded from Vizio TV.");
53             return PEMTrustManager.getInstanceFromServer("https://" + getHostName());
54         } catch (CertificateException | MalformedURLException e) {
55             logger.debug("An unexpected exception occurred - returning a TrustAllTrustManager: {}", e.getMessage(), e);
56         }
57         return TrustAllTrustManager.getInstance();
58     }
59 }