]> git.basschouten.com Git - openhab-addons.git/blob
96ebf926b647cde2e6acd637f31c18b1d4ffc206
[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.loxone.internal.security;
14
15 import java.net.URLEncoder;
16 import java.nio.charset.StandardCharsets;
17 import java.security.InvalidAlgorithmParameterException;
18 import java.security.InvalidKeyException;
19 import java.security.InvalidParameterException;
20 import java.security.KeyFactory;
21 import java.security.MessageDigest;
22 import java.security.NoSuchAlgorithmException;
23 import java.security.PublicKey;
24 import java.security.SecureRandom;
25 import java.security.spec.InvalidKeySpecException;
26 import java.security.spec.X509EncodedKeySpec;
27 import java.text.SimpleDateFormat;
28 import java.util.Base64;
29 import java.util.Calendar;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.concurrent.ScheduledExecutorService;
33 import java.util.concurrent.ScheduledFuture;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.locks.Lock;
36 import java.util.concurrent.locks.ReentrantLock;
37
38 import javax.crypto.BadPaddingException;
39 import javax.crypto.Cipher;
40 import javax.crypto.IllegalBlockSizeException;
41 import javax.crypto.KeyGenerator;
42 import javax.crypto.NoSuchPaddingException;
43 import javax.crypto.SecretKey;
44 import javax.crypto.spec.IvParameterSpec;
45
46 import org.openhab.binding.loxone.internal.LxServerHandlerApi;
47 import org.openhab.binding.loxone.internal.LxWebSocket;
48 import org.openhab.binding.loxone.internal.types.LxErrorCode;
49 import org.openhab.binding.loxone.internal.types.LxResponse;
50 import org.openhab.core.common.ThreadPoolManager;
51 import org.openhab.core.id.InstanceUUID;
52 import org.openhab.core.util.HexUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.google.gson.JsonParseException;
57
58 /**
59  * A token-based authentication algorithm with AES-256 encryption and decryption.
60  *
61  * The encryption algorithm uses public Miniserver key to RSA-encrypt own AES-256 key and initialization vector into a
62  * session key. The encrypted session key is sent to the Miniserver. From this point on encryption (and decryption) of
63  * the communication is possible and all further commands sent to the Miniserver are encrypted. The encryption makes use
64  * of an additional salt value injected into the commands and updated frequently.
65  *
66  * To get the token, a hash key and salt values that are specific to the user are received from the Miniserver. These
67  * values are used to compute a hash over user name and password using Miniserver's salt and key values (combined SHA1
68  * and HMAC-SHA1 algorithm). This hash is sent to the Miniserver in an encrypted message to authorize the user and
69  * obtain a token.
70  *
71  * Once a token is obtained, it can be used in all future authorizations instead of hashed user name and password.
72  * When a token expires, it is refreshed.
73  *
74  * @author Pawel Pieczul - initial contribution
75  *
76  */
77 class LxWsSecurityToken extends LxWsSecurity {
78     /**
79      * A sub-response value structure that is received as a response to get key-salt request command sent to the
80      * Miniserver during authentication procedure.
81      *
82      * @author Pawel Pieczul - initial contribution
83      *
84      */
85     private class LxResponseKeySalt {
86         String key;
87         String salt;
88         String hashAlg;
89     }
90
91     /**
92      * A sub-response value structure that is received as a response to token request or token update command sent to
93      * the Miniserver during authentication procedure.
94      *
95      * @author Pawel Pieczul - initial contribution
96      *
97      */
98     private class LxResponseToken {
99         String token;
100         Integer validUntil;
101         Boolean unsecurePass;
102         @SuppressWarnings("unused")
103         String key;
104         @SuppressWarnings("unused")
105         Integer tokenRights;
106     }
107
108     // length of salt used for encrypting commands
109     private static final int SALT_BYTES = 16;
110     // after salt aged or reached max use count, a new salt will be generated
111     private static final int SALT_MAX_AGE_SECONDS = 60 * 60;
112     private static final int SALT_MAX_USE_COUNT = 30;
113
114     // defined by Loxone API, value 4 gives longest token expiration time
115     private static final int TOKEN_PERMISSION = 4; // 2=web, 4=app
116     // number of attempts for token refresh and delay between them
117     private static final int TOKEN_REFRESH_RETRY_COUNT = 5;
118     private static final int TOKEN_REFRESH_RETRY_DELAY_SECONDS = 10;
119     // token will be refreshed 1 day before its expiration date
120     private static final int TOKEN_REFRESH_SECONDS_BEFORE_EXPIRY = 24 * 60 * 60; // 1 day
121     // if can't determine token expiration date, it will be refreshed after 2 days
122     private static final int TOKEN_REFRESH_DEFAULT_SECONDS = 2 * 24 * 60 * 60; // 2 days
123
124     // AES encryption random initialization vector length
125     private static final int IV_LENGTH_BYTES = 16;
126
127     private static final String CMD_GET_KEY_AND_SALT = "jdev/sys/getkey2/";
128     private static final String CMD_GET_PUBLIC_KEY = "jdev/sys/getPublicKey";
129     private static final String CMD_KEY_EXCHANGE = "jdev/sys/keyexchange/";
130     private static final String CMD_REQUEST_TOKEN = "jdev/sys/gettoken/";
131     private static final String CMD_GET_KEY = "jdev/sys/getkey";
132     private static final String CMD_AUTH_WITH_TOKEN = "authwithtoken/";
133     private static final String CMD_REFRESH_TOKEN = "jdev/sys/refreshtoken/";
134     private static final String CMD_ENCRYPT_CMD = "jdev/sys/enc/";
135
136     private static final String SETTINGS_TOKEN = "authToken";
137     private static final String SETTINGS_PASSWORD = "password";
138
139     private SecretKey aesKey;
140     private Cipher aesEncryptCipher;
141     private Cipher aesDecryptCipher;
142     private SecureRandom secureRandom;
143     private String salt;
144     private int saltUseCount;
145     private long saltTimeStamp;
146     private boolean encryptionReady = false;
147     private String token;
148     private int tokenRefreshRetryCount;
149     private ScheduledFuture<?> tokenRefreshTimer;
150     private final Lock tokenRefreshLock = new ReentrantLock();
151     private boolean sha256 = false;
152
153     private final byte[] initVector = new byte[IV_LENGTH_BYTES];
154     private final Logger logger = LoggerFactory.getLogger(LxWsSecurityToken.class);
155     private static final ScheduledExecutorService SCHEDULER = ThreadPoolManager
156             .getScheduledPool(LxWsSecurityToken.class.getName());
157
158     /**
159      * Create a token-based authentication instance.
160      *
161      * @param debugId instance of the client used for debugging purposes only
162      * @param thingHandler API to the thing handler
163      * @param socket websocket to perform communication with Miniserver
164      * @param user user to authenticate
165      * @param password password to authenticate
166      */
167     LxWsSecurityToken(int debugId, LxServerHandlerApi thingHandler, LxWebSocket socket, String user, String password) {
168         super(debugId, thingHandler, socket, user, password);
169     }
170
171     @Override
172     boolean execute() {
173         logger.debug("[{}] Starting token-based authentication.", debugId);
174         if (!initialize()) {
175             return false;
176         }
177         if ((token == null || token.isEmpty()) && (password == null || password.isEmpty())) {
178             return setError(LxErrorCode.USER_UNAUTHORIZED, "Enter password to acquire token.");
179         }
180         // Get Miniserver's public key - must be over http, not websocket
181         String msg = socket.httpGet(CMD_GET_PUBLIC_KEY);
182         LxResponse resp = socket.getResponse(msg);
183         if (resp == null) {
184             return setError(LxErrorCode.COMMUNICATION_ERROR, "Get public key failed - null response.");
185         }
186         // RSA cipher to encrypt our AES-256 key using Miniserver's public key
187         Cipher rsaCipher = getRsaCipher(resp.getValueAsString());
188         if (rsaCipher == null) {
189             return false;
190         }
191         // Generate session key
192         byte[] sessionKey = generateSessionKey(rsaCipher);
193         if (sessionKey == null) {
194             return false;
195         }
196         // Exchange keys
197         resp = socket.sendCmdWithResp(CMD_KEY_EXCHANGE + Base64.getEncoder().encodeToString(sessionKey), true, false);
198         if (!checkResponse(resp)) {
199             return setError(null, "Key exchange failed.");
200         }
201         logger.debug("[{}] Keys exchanged.", debugId);
202         encryptionReady = true;
203
204         if (token == null || token.isEmpty()) {
205             if (!acquireToken()) {
206                 return false;
207             }
208             logger.debug("[{}] Authenticated - acquired new token.", debugId);
209         } else {
210             if (!useToken()) {
211                 return false;
212             }
213             logger.debug("[{}] Authenticated - used stored token.", debugId);
214         }
215
216         return true;
217     }
218
219     @Override
220     public String encrypt(String command) {
221         if (!encryptionReady) {
222             return command;
223         }
224         String str;
225         if (salt != null && newSaltNeeded()) {
226             String prevSalt = salt;
227             salt = generateSalt();
228             str = "nextSalt/" + prevSalt + "/" + salt + "/" + command + "\0";
229         } else {
230             if (salt == null) {
231                 salt = generateSalt();
232             }
233             str = "salt/" + salt + "/" + command + "\0";
234         }
235
236         logger.debug("[{}] Command for encryption: {}", debugId, str);
237         try {
238             String encrypted = Base64.getEncoder()
239                     .encodeToString(aesEncryptCipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
240             encrypted = URLEncoder.encode(encrypted, StandardCharsets.UTF_8);
241             return CMD_ENCRYPT_CMD + encrypted;
242         } catch (IllegalBlockSizeException | BadPaddingException e) {
243             logger.warn("[{}] Command encryption failed: {}", debugId, e.getMessage());
244             return command;
245         }
246     }
247
248     @Override
249     public String decryptControl(String control) {
250         String string = control;
251         if (!encryptionReady || !string.startsWith(CMD_ENCRYPT_CMD)) {
252             return string;
253         }
254         string = string.substring(CMD_ENCRYPT_CMD.length());
255         try {
256             byte[] bytes = Base64.getDecoder().decode(string);
257             bytes = aesDecryptCipher.doFinal(bytes);
258             string = new String(bytes, StandardCharsets.UTF_8);
259             string = string.replaceAll("\0+.*$", "");
260             string = string.replaceFirst("^salt/[^/]*/", "");
261             string = string.replaceFirst("^nextSalt/[^/]*/[^/]*/", "");
262             return string;
263         } catch (IllegalArgumentException e) {
264             logger.debug("[{}] Failed to decode base64 string: {}", debugId, string);
265         } catch (IllegalBlockSizeException | BadPaddingException e) {
266             logger.warn("[{}] Command decryption failed: {}", debugId, e.getMessage());
267         }
268         return string;
269     }
270
271     @Override
272     public void cancel() {
273         super.cancel();
274         tokenRefreshLock.lock();
275         try {
276             if (tokenRefreshTimer != null) {
277                 logger.debug("[{}] Cancelling token refresh.", debugId);
278                 tokenRefreshTimer.cancel(true);
279             }
280         } finally {
281             tokenRefreshLock.unlock();
282         }
283     }
284
285     private boolean initialize() {
286         try {
287             encryptionReady = false;
288             tokenRefreshRetryCount = TOKEN_REFRESH_RETRY_COUNT;
289             if (Cipher.getMaxAllowedKeyLength("AES") < 256) {
290                 return setError(LxErrorCode.INTERNAL_ERROR,
291                         "Enable Java cryptography unlimited strength (see binding doc).");
292             }
293             // generate a random key for the session
294             KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
295             aesKeyGen.init(256);
296             aesKey = aesKeyGen.generateKey();
297             // generate an initialization vector
298             secureRandom = new SecureRandom();
299             secureRandom.nextBytes(initVector);
300             IvParameterSpec ivSpec = new IvParameterSpec(initVector);
301             // initialize aes cipher for command encryption
302             aesEncryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
303             aesEncryptCipher.init(Cipher.ENCRYPT_MODE, aesKey, ivSpec);
304             // initialize aes cipher for response decryption
305             aesDecryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
306             aesDecryptCipher.init(Cipher.DECRYPT_MODE, aesKey, ivSpec);
307             // get token value from configuration storage
308             token = thingHandler.getSetting(SETTINGS_TOKEN);
309             logger.debug("[{}] Retrieved token value: {}", debugId, token);
310         } catch (InvalidParameterException e) {
311             return setError(LxErrorCode.INTERNAL_ERROR, "Invalid parameter: " + e.getMessage());
312         } catch (NoSuchAlgorithmException e) {
313             return setError(LxErrorCode.INTERNAL_ERROR, "AES not supported on platform.");
314         } catch (InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
315             return setError(LxErrorCode.INTERNAL_ERROR, "AES cipher initialization failed.");
316         }
317         return true;
318     }
319
320     private Cipher getRsaCipher(String key) {
321         try {
322             KeyFactory keyFactory = KeyFactory.getInstance("RSA");
323             String keyString = key.replace("-----BEGIN CERTIFICATE-----", "").replace("-----END CERTIFICATE-----", "");
324             byte[] keyData = Base64.getDecoder().decode(keyString);
325             X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyData);
326             PublicKey publicKey = keyFactory.generatePublic(keySpec);
327             logger.debug("[{}] Miniserver public key: {}", debugId, publicKey);
328             Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
329             cipher.init(Cipher.PUBLIC_KEY, publicKey);
330             logger.debug("[{}] Initialized RSA public key cipher", debugId);
331             return cipher;
332         } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException e) {
333             setError(LxErrorCode.INTERNAL_ERROR, "Exception enabling RSA cipher: " + e.getMessage());
334             return null;
335         }
336     }
337
338     private byte[] generateSessionKey(Cipher rsaCipher) {
339         String key = HexUtils.bytesToHex(aesKey.getEncoded()) + ":" + HexUtils.bytesToHex(initVector);
340         try {
341             byte[] sessionKey = rsaCipher.doFinal(key.getBytes());
342             logger.debug("[{}] Generated session key: {}", debugId, HexUtils.bytesToHex(sessionKey));
343             return sessionKey;
344         } catch (IllegalBlockSizeException | BadPaddingException e) {
345             setError(LxErrorCode.INTERNAL_ERROR, "Exception encrypting session key: " + e.getMessage());
346             return null;
347         }
348     }
349
350     private String hashCredentials(LxResponseKeySalt keySalt, boolean sha256) {
351         try {
352             MessageDigest msgDigest = MessageDigest.getInstance(sha256 ? "SHA-256" : "SHA-1");
353             String pwdHashStr = password + ":" + keySalt.salt;
354             byte[] rawData = msgDigest.digest(pwdHashStr.getBytes(StandardCharsets.UTF_8));
355             String pwdHash = HexUtils.bytesToHex(rawData).toUpperCase();
356             logger.debug("[{}] PWDHASH: {}", debugId, pwdHash);
357             return hashString(user + ":" + pwdHash, keySalt.key, sha256);
358         } catch (NoSuchAlgorithmException e) {
359             logger.debug("[{}] Error hashing token credentials: {}", debugId, e.getMessage());
360             return null;
361         }
362     }
363
364     private boolean acquireToken() {
365         // Get Miniserver hash key and salt - this command should be encrypted
366         LxResponse resp = socket.sendCmdWithResp(CMD_GET_KEY_AND_SALT + user, true, true);
367         if (!checkResponse(resp)) {
368             return setError(null, "Hash key/salt get failed.");
369         }
370         LxResponseKeySalt keySalt = resp.getValueAs(thingHandler.getGson(), LxResponseKeySalt.class);
371         if (keySalt == null) {
372             return setError(null, "Error parsing hash key/salt json: " + resp.getValueAsString());
373         }
374         if ("SHA256".equals(keySalt.hashAlg)) {
375             sha256 = true;
376         }
377         logger.debug("[{}] Hash key: {}, salt: {}", debugId, keySalt.key, keySalt.salt);
378         // Hash user name, password, key and salt
379         String hash = hashCredentials(keySalt, sha256);
380         if (hash == null) {
381             return false;
382         }
383         // Request token
384         String uuid = InstanceUUID.get();
385         resp = socket.sendCmdWithResp(CMD_REQUEST_TOKEN + hash + "/" + user + "/" + TOKEN_PERMISSION + "/"
386                 + (uuid != null ? uuid : "098802e1-02b4-603c-ffffeee000d80cfd") + "/openHAB", true, true);
387         if (!checkResponse(resp)) {
388             return setError(null, "Request token failed.");
389         }
390
391         try {
392             LxResponseToken tokenResponse = parseTokenResponse(resp);
393             if (tokenResponse == null) {
394                 return false;
395             }
396             token = tokenResponse.token;
397             if (token == null) {
398                 return setError(LxErrorCode.INTERNAL_ERROR, "Received null token.");
399             }
400         } catch (JsonParseException e) {
401             return setError(LxErrorCode.INTERNAL_ERROR, "Error parsing token response: " + e.getMessage());
402         }
403
404         persistToken();
405         logger.debug("[{}] Token acquired.", debugId);
406         return true;
407     }
408
409     private boolean useToken() {
410         String hash = hashToken();
411         if (hash == null) {
412             return false;
413         }
414         LxResponse resp = socket.sendCmdWithResp(CMD_AUTH_WITH_TOKEN + hash + "/" + user, true, true);
415         if (!checkResponse(resp)) {
416             if (reason == LxErrorCode.USER_UNAUTHORIZED) {
417                 token = null;
418                 persistToken();
419                 return setError(null, "Enter password to generate a new token.");
420             }
421             return setError(null, "Token-based authentication failed.");
422         }
423         parseTokenResponse(resp);
424         return true;
425     }
426
427     private String hashToken() {
428         LxResponse resp = socket.sendCmdWithResp(CMD_GET_KEY, true, true);
429         if (!checkResponse(resp)) {
430             setError(null, "Get key command failed.");
431             return null;
432         }
433         try {
434             String hashKey = resp.getValueAsString();
435             // here is a difference to the API spec, which says the string to hash is "user:token", but this is "token"
436             String hash = hashString(token, hashKey, sha256);
437             if (hash == null) {
438                 setError(null, "Error hashing token.");
439             }
440             return hash;
441         } catch (ClassCastException | IllegalStateException e) {
442             setError(LxErrorCode.INTERNAL_ERROR, "Error parsing Miniserver key.");
443             return null;
444         }
445     }
446
447     private void persistToken() {
448         Map<String, String> properties = new HashMap<>();
449         properties.put(SETTINGS_TOKEN, token);
450         if (token != null) {
451             properties.put(SETTINGS_PASSWORD, null);
452         }
453         thingHandler.setSettings(properties);
454     }
455
456     private LxResponseToken parseTokenResponse(LxResponse response) {
457         LxResponseToken tokenResponse = response.getValueAs(thingHandler.getGson(), LxResponseToken.class);
458         if (tokenResponse == null) {
459             setError(LxErrorCode.INTERNAL_ERROR, "Error parsing token response.");
460             return null;
461         }
462         Boolean unsecurePass = tokenResponse.unsecurePass;
463         if (unsecurePass != null && unsecurePass) {
464             logger.warn("[{}] Unsecure user password on Miniserver.", debugId);
465         }
466         long secondsToExpiry;
467         Integer validUntil = tokenResponse.validUntil;
468         if (validUntil == null) {
469             secondsToExpiry = TOKEN_REFRESH_DEFAULT_SECONDS;
470         } else {
471             // validUntil is the end of token life-span in seconds from 2009/01/01
472             Calendar loxoneCalendar = Calendar.getInstance();
473             loxoneCalendar.clear();
474             loxoneCalendar.set(2009, Calendar.JANUARY, 1);
475             loxoneCalendar.add(Calendar.SECOND, validUntil);
476             Calendar ohCalendar = Calendar.getInstance();
477             secondsToExpiry = (loxoneCalendar.getTimeInMillis() - ohCalendar.getTimeInMillis()) / 1000;
478             if (logger.isDebugEnabled()) {
479                 try {
480                     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
481                     logger.debug("[{}] Token will expire on: {}.", debugId, format.format(loxoneCalendar.getTime()));
482                 } catch (IllegalArgumentException e) {
483                     logger.debug("[{}] Token will expire in {} days.", debugId,
484                             TimeUnit.SECONDS.toDays(secondsToExpiry));
485                 }
486             }
487             if (secondsToExpiry <= 0) {
488                 logger.warn("[{}] Time to token expiry is negative or zero: {}", debugId, secondsToExpiry);
489                 secondsToExpiry = TOKEN_REFRESH_DEFAULT_SECONDS;
490             } else {
491                 int correction = TOKEN_REFRESH_SECONDS_BEFORE_EXPIRY;
492                 while (secondsToExpiry - correction < 0) {
493                     correction /= 2;
494                 }
495                 secondsToExpiry -= correction;
496             }
497         }
498         scheduleTokenRefresh(secondsToExpiry);
499         return tokenResponse;
500     }
501
502     private void refreshToken() {
503         tokenRefreshLock.lock();
504         try {
505             tokenRefreshTimer = null;
506             String hash = hashToken();
507             if (hash != null) {
508                 LxResponse resp = socket.sendCmdWithResp(CMD_REFRESH_TOKEN + hash + "/" + user, true, true);
509                 if (checkResponse(resp)) {
510                     logger.debug("[{}] Successful token refresh.", debugId);
511                     parseTokenResponse(resp);
512                     return;
513                 }
514             }
515             logger.debug("[{}] Token refresh failed, retrying (retry={}).", debugId, tokenRefreshRetryCount);
516             if (tokenRefreshRetryCount-- > 0) {
517                 scheduleTokenRefresh(TOKEN_REFRESH_RETRY_DELAY_SECONDS);
518             } else {
519                 logger.warn("[{}] All token refresh attempts failed.", debugId);
520             }
521         } finally {
522             tokenRefreshLock.unlock();
523         }
524     }
525
526     private void scheduleTokenRefresh(long delay) {
527         logger.debug("[{}] Setting token refresh in {} days.", debugId, TimeUnit.SECONDS.toDays(delay));
528         tokenRefreshLock.lock();
529         try {
530             tokenRefreshTimer = SCHEDULER.schedule(this::refreshToken, delay, TimeUnit.SECONDS);
531         } finally {
532             tokenRefreshLock.unlock();
533         }
534     }
535
536     private String generateSalt() {
537         byte[] bytes = new byte[SALT_BYTES];
538         secureRandom.nextBytes(bytes);
539         String salt = HexUtils.bytesToHex(bytes);
540         salt = URLEncoder.encode(salt, StandardCharsets.UTF_8);
541         saltTimeStamp = timeElapsedInSeconds();
542         saltUseCount = 0;
543         logger.debug("[{}] Generated salt: {}", debugId, salt);
544         return salt;
545     }
546
547     private boolean newSaltNeeded() {
548         return (++saltUseCount > SALT_MAX_USE_COUNT || timeElapsedInSeconds() - saltTimeStamp > SALT_MAX_AGE_SECONDS);
549     }
550
551     private long timeElapsedInSeconds() {
552         return TimeUnit.NANOSECONDS.toSeconds(System.nanoTime());
553     }
554 }