1212 */
1313package org .openhab .binding .hue .internal .connection ;
1414
15- import java .io .ByteArrayInputStream ;
1615import java .io .IOException ;
1716import java .io .InputStream ;
1817import java .net .MalformedURLException ;
19- import java .net .URL ;
20- import java .nio .charset .StandardCharsets ;
2118import java .security .KeyStore ;
19+ import java .security .KeyStoreException ;
20+ import java .security .NoSuchAlgorithmException ;
2221import java .security .cert .Certificate ;
2322import java .security .cert .CertificateException ;
2423import java .security .cert .CertificateFactory ;
4544@ NonNullByDefault
4645public class HueTlsTrustManagerProvider implements TlsTrustManagerProvider {
4746
48- private static final String PEM_CACERT_FILENAME = "huebridge_cacert.pem" ;
47+ private static final String PEM_CACERT_FILENAME = "/ huebridge_cacert.pem" ;
4948 private final String hostname ;
5049 private final boolean useSelfSignedCertificate ;
5150
@@ -108,29 +107,34 @@ public X509ExtendedTrustManager getTrustManager() {
108107
109108 /**
110109 * Creates a {@link X509ExtendedTrustManager} instance by reading one or more PEM certificates from the given
111- * file . The returned trust manager will trust all certificates that are signed by any of the certificates in
110+ * resource . The returned trust manager will trust all certificates that are signed by any of the certificates in
112111 * the PEM file, including certificates with intermediates. This is useful if you have private CA Certificate(s)
113112 * stored in a file.
114113 *
115- * @param fileName name of the PEM file located in the resources folder
114+ * @param resourceName name of the PEM resource located in the resources folder
116115 * @return a {@link X509ExtendedTrustManager} instance
117116 * @throws CertificateException
118117 */
119- private X509ExtendedTrustManager getInstanceFromResource (String fileName ) throws CertificateException {
120- String certificatesString = readPEMCertificatesStringFromResource (fileName );
121- if (certificatesString == null ) {
122- throw new CertificateException ("Certificate resource '" + fileName + "' not found or not accessible." );
118+ private X509ExtendedTrustManager getInstanceFromResource (String resourceName ) throws CertificateException {
119+ CertificateFactory certificateFactory = CertificateFactory .getInstance ("X.509" );
120+
121+ // load all certificates from the PEM file
122+ Collection <? extends Certificate > certificates ;
123+
124+ try (InputStream inputStream = HueTlsTrustManagerProvider .class .getResourceAsStream (resourceName )) {
125+ if (inputStream == null ) {
126+ throw new CertificateException ("Certificate resource not found: " + resourceName );
127+ }
128+ certificates = certificateFactory .generateCertificates (inputStream );
129+ } catch (IOException e ) {
130+ throw new CertificateException ("Certificate resource cannot be read: " + resourceName , e );
131+ }
132+
133+ if (certificates .isEmpty ()) {
134+ throw new CertificateException ("No certificates found in " + resourceName );
123135 }
136+
124137 try {
125- CertificateFactory certificateFactory = CertificateFactory .getInstance ("X.509" );
126- // load all certificates from the PEM file
127- Collection <? extends Certificate > certificates ;
128- try (InputStream input = new ByteArrayInputStream (certificatesString .getBytes (StandardCharsets .UTF_8 ))) {
129- certificates = certificateFactory .generateCertificates (input );
130- }
131- if (certificates .isEmpty ()) {
132- throw new CertificateException ("No certificates found in " + fileName );
133- }
134138 // build a key store containing all the certificates
135139 KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
136140 keyStore .load (null , null );
@@ -148,28 +152,8 @@ private X509ExtendedTrustManager getInstanceFromResource(String fileName) throws
148152 }
149153 }
150154 throw new CertificateException ("No X509ExtendedTrustManager available." );
151- } catch (Exception e ) {
155+ } catch (KeyStoreException | NoSuchAlgorithmException | IOException e ) {
152156 throw new CertificateException ("Failed to load certificates: " + e .getMessage (), e );
153157 }
154158 }
155-
156- /**
157- * Reads the content of a PEM file from the resources folder and returns it as a string. It may contain multiple
158- * certificates, e.g. a certificate chain with intermediate certificates. If the file is not found or cannot be
159- * read, null is returned.
160- *
161- * @param fileName name of the PEM file located in the resources folder
162- * @return the content of the PEM file as a string, or null if the file is not found or cannot be read
163- */
164- private @ Nullable String readPEMCertificatesStringFromResource (String fileName ) {
165- URL resource = HueTlsTrustManagerProvider .class .getClassLoader ().getResource (fileName );
166- if (resource != null ) {
167- try (InputStream certInputStream = resource .openStream ()) {
168- return new String (certInputStream .readAllBytes (), StandardCharsets .UTF_8 );
169- } catch (IOException e ) {
170- logger .error ("An unexpected exception occurred: " , e );
171- }
172- }
173- return null ;
174- }
175159}
0 commit comments