2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.loxone.internal.security;
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;
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;
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;
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;
56 import com.google.gson.JsonParseException;
59 * A token-based authentication algorithm with AES-256 encryption and decryption.
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.
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
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.
74 * @author Pawel Pieczul - initial contribution
77 class LxWsSecurityToken extends LxWsSecurity {
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.
82 * @author Pawel Pieczul - initial contribution
85 private class LxResponseKeySalt {
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.
95 * @author Pawel Pieczul - initial contribution
98 private class LxResponseToken {
101 Boolean unsecurePass;
102 @SuppressWarnings("unused")
104 @SuppressWarnings("unused")
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;
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
124 // AES encryption random initialization vector length
125 private static final int IV_LENGTH_BYTES = 16;
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/";
136 private static final String SETTINGS_TOKEN = "authToken";
137 private static final String SETTINGS_PASSWORD = "password";
139 private SecretKey aesKey;
140 private Cipher aesEncryptCipher;
141 private Cipher aesDecryptCipher;
142 private SecureRandom secureRandom;
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;
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());
159 * Create a token-based authentication instance.
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
167 LxWsSecurityToken(int debugId, LxServerHandlerApi thingHandler, LxWebSocket socket, String user, String password) {
168 super(debugId, thingHandler, socket, user, password);
173 logger.debug("[{}] Starting token-based authentication.", debugId);
177 if ((token == null || token.isEmpty()) && (password == null || password.isEmpty())) {
178 return setError(LxErrorCode.USER_UNAUTHORIZED, "Enter password to acquire token.");
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);
184 return setError(LxErrorCode.COMMUNICATION_ERROR, "Get public key failed - null response.");
186 // RSA cipher to encrypt our AES-256 key using Miniserver's public key
187 Cipher rsaCipher = getRsaCipher(resp.getValueAsString());
188 if (rsaCipher == null) {
191 // Generate session key
192 byte[] sessionKey = generateSessionKey(rsaCipher);
193 if (sessionKey == null) {
197 resp = socket.sendCmdWithResp(CMD_KEY_EXCHANGE + Base64.getEncoder().encodeToString(sessionKey), true, false);
198 if (!checkResponse(resp)) {
199 return setError(null, "Key exchange failed.");
201 logger.debug("[{}] Keys exchanged.", debugId);
202 encryptionReady = true;
204 if (token == null || token.isEmpty()) {
205 if (!acquireToken()) {
208 logger.debug("[{}] Authenticated - acquired new token.", debugId);
213 logger.debug("[{}] Authenticated - used stored token.", debugId);
220 public String encrypt(String command) {
221 if (!encryptionReady) {
225 if (salt != null && newSaltNeeded()) {
226 String prevSalt = salt;
227 salt = generateSalt();
228 str = "nextSalt/" + prevSalt + "/" + salt + "/" + command + "\0";
231 salt = generateSalt();
233 str = "salt/" + salt + "/" + command + "\0";
236 logger.debug("[{}] Command for encryption: {}", debugId, str);
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());
249 public String decryptControl(String control) {
250 String string = control;
251 if (!encryptionReady || !string.startsWith(CMD_ENCRYPT_CMD)) {
254 string = string.substring(CMD_ENCRYPT_CMD.length());
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/[^/]*/[^/]*/", "");
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());
272 public void cancel() {
274 tokenRefreshLock.lock();
276 if (tokenRefreshTimer != null) {
277 logger.debug("[{}] Cancelling token refresh.", debugId);
278 tokenRefreshTimer.cancel(true);
281 tokenRefreshLock.unlock();
285 private boolean initialize() {
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).");
293 // generate a random key for the session
294 KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
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.");
320 private Cipher getRsaCipher(String key) {
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);
332 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException e) {
333 setError(LxErrorCode.INTERNAL_ERROR, "Exception enabling RSA cipher: " + e.getMessage());
338 private byte[] generateSessionKey(Cipher rsaCipher) {
339 String key = HexUtils.bytesToHex(aesKey.getEncoded()) + ":" + HexUtils.bytesToHex(initVector);
341 byte[] sessionKey = rsaCipher.doFinal(key.getBytes());
342 logger.debug("[{}] Generated session key: {}", debugId, HexUtils.bytesToHex(sessionKey));
344 } catch (IllegalBlockSizeException | BadPaddingException e) {
345 setError(LxErrorCode.INTERNAL_ERROR, "Exception encrypting session key: " + e.getMessage());
350 private String hashCredentials(LxResponseKeySalt keySalt, boolean sha256) {
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());
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.");
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());
374 if ("SHA256".equals(keySalt.hashAlg)) {
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);
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.");
392 LxResponseToken tokenResponse = parseTokenResponse(resp);
393 if (tokenResponse == null) {
396 token = tokenResponse.token;
398 return setError(LxErrorCode.INTERNAL_ERROR, "Received null token.");
400 } catch (JsonParseException e) {
401 return setError(LxErrorCode.INTERNAL_ERROR, "Error parsing token response: " + e.getMessage());
405 logger.debug("[{}] Token acquired.", debugId);
409 private boolean useToken() {
410 String hash = hashToken();
414 LxResponse resp = socket.sendCmdWithResp(CMD_AUTH_WITH_TOKEN + hash + "/" + user, true, true);
415 if (!checkResponse(resp)) {
416 if (reason == LxErrorCode.USER_UNAUTHORIZED) {
419 return setError(null, "Enter password to generate a new token.");
421 return setError(null, "Token-based authentication failed.");
423 parseTokenResponse(resp);
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.");
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);
438 setError(null, "Error hashing token.");
441 } catch (ClassCastException | IllegalStateException e) {
442 setError(LxErrorCode.INTERNAL_ERROR, "Error parsing Miniserver key.");
447 private void persistToken() {
448 Map<String, String> properties = new HashMap<>();
449 properties.put(SETTINGS_TOKEN, token);
451 properties.put(SETTINGS_PASSWORD, null);
453 thingHandler.setSettings(properties);
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.");
462 Boolean unsecurePass = tokenResponse.unsecurePass;
463 if (unsecurePass != null && unsecurePass) {
464 logger.warn("[{}] Unsecure user password on Miniserver.", debugId);
466 long secondsToExpiry;
467 Integer validUntil = tokenResponse.validUntil;
468 if (validUntil == null) {
469 secondsToExpiry = TOKEN_REFRESH_DEFAULT_SECONDS;
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()) {
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));
487 if (secondsToExpiry <= 0) {
488 logger.warn("[{}] Time to token expiry is negative or zero: {}", debugId, secondsToExpiry);
489 secondsToExpiry = TOKEN_REFRESH_DEFAULT_SECONDS;
491 int correction = TOKEN_REFRESH_SECONDS_BEFORE_EXPIRY;
492 while (secondsToExpiry - correction < 0) {
495 secondsToExpiry -= correction;
498 scheduleTokenRefresh(secondsToExpiry);
499 return tokenResponse;
502 private void refreshToken() {
503 tokenRefreshLock.lock();
505 tokenRefreshTimer = null;
506 String hash = hashToken();
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);
515 logger.debug("[{}] Token refresh failed, retrying (retry={}).", debugId, tokenRefreshRetryCount);
516 if (tokenRefreshRetryCount-- > 0) {
517 scheduleTokenRefresh(TOKEN_REFRESH_RETRY_DELAY_SECONDS);
519 logger.warn("[{}] All token refresh attempts failed.", debugId);
522 tokenRefreshLock.unlock();
526 private void scheduleTokenRefresh(long delay) {
527 logger.debug("[{}] Setting token refresh in {} days.", debugId, TimeUnit.SECONDS.toDays(delay));
528 tokenRefreshLock.lock();
530 tokenRefreshTimer = SCHEDULER.schedule(this::refreshToken, delay, TimeUnit.SECONDS);
532 tokenRefreshLock.unlock();
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();
543 logger.debug("[{}] Generated salt: {}", debugId, salt);
547 private boolean newSaltNeeded() {
548 return (++saltUseCount > SALT_MAX_USE_COUNT || timeElapsedInSeconds() - saltTimeStamp > SALT_MAX_AGE_SECONDS);
551 private long timeElapsedInSeconds() {
552 return TimeUnit.NANOSECONDS.toSeconds(System.nanoTime());