2 * Copyright (c) 2010-2022 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.io.homekit.internal;
15 import java.math.BigInteger;
16 import java.security.InvalidAlgorithmParameterException;
17 import java.util.Base64;
18 import java.util.Collection;
19 import java.util.HashSet;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.storage.Storage;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import io.github.hapjava.server.HomekitAuthInfo;
27 import io.github.hapjava.server.impl.HomekitServer;
30 * Provides a mechanism to store authenticated HomeKit client details inside the
31 * StorageService, by implementing HomekitAuthInfo.
33 * @author Andy Lintner - Initial contribution
35 public class HomekitAuthInfoImpl implements HomekitAuthInfo {
36 private final Logger logger = LoggerFactory.getLogger(HomekitAuthInfoImpl.class);
37 public static final String STORAGE_KEY = "homekit";
38 private static final String STORAGE_MAC = "mac";
39 private static final String STORAGE_SALT = "salt";
40 private static final String STORAGE_PRIVATE_KEY = "privateKey";
41 private static final String STORAGE_USER_PREFIX = "user_";
43 private final Storage<String> storage;
45 private BigInteger salt;
46 private byte[] privateKey;
48 private String setupId;
49 private boolean blockUserDeletion;
51 public HomekitAuthInfoImpl(Storage<String> storage, String pin, String setupId, boolean blockUserDeletion)
52 throws InvalidAlgorithmParameterException {
53 this.storage = storage;
55 this.setupId = setupId;
56 this.blockUserDeletion = blockUserDeletion;
61 public void createUser(String username, byte[] publicKey) {
62 logger.trace("create user {}", username);
63 final String userKey = createUserKey(username);
64 final String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey);
65 storage.put(userKey, encodedPublicKey);
66 logger.trace("stored user key {} with value {}", userKey, encodedPublicKey);
70 public String getMac() {
74 public void setMac(String mac) {
79 public String getPin() {
83 public void setPin(String pin) {
88 public String getSetupId() {
92 public void setSetupId(String setupId) {
93 this.setupId = setupId;
97 public byte[] getPrivateKey() {
102 public BigInteger getSalt() {
107 public byte[] getUserPublicKey(String username) {
108 final String encodedKey = storage.get(createUserKey(username));
109 if (encodedKey != null) {
110 return Base64.getDecoder().decode(encodedKey);
117 public void removeUser(String username) {
118 logger.trace("remove user {}", username);
119 if (!this.blockUserDeletion) {
120 storage.remove(createUserKey(username));
122 logger.debug("deletion of the user was blocked by binding settings");
127 public boolean hasUser() {
128 Collection<String> keys = storage.getKeys();
129 return keys.stream().anyMatch(this::isUserKey);
132 public void clear() {
133 logger.trace("clear all users");
134 if (!this.blockUserDeletion) {
135 for (String key : new HashSet<>(storage.getKeys())) {
136 if (isUserKey(key)) {
141 logger.debug("deletion of users information was blocked by binding settings");
145 private String createUserKey(String username) {
146 return STORAGE_USER_PREFIX + username;
149 private boolean isUserKey(String key) {
150 return key.startsWith(STORAGE_USER_PREFIX);
153 private void initializeStorage() throws InvalidAlgorithmParameterException {
154 mac = storage.get(STORAGE_MAC);
155 final @Nullable Object saltConfig = storage.get(STORAGE_SALT);
156 final @Nullable Object privateKeyConfig = storage.get(STORAGE_PRIVATE_KEY);
159 "could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
160 storage.getClass().getName());
161 mac = HomekitServer.generateMac();
162 storage.put(STORAGE_MAC, mac);
164 if (saltConfig == null) {
165 salt = HomekitServer.generateSalt();
166 storage.put(STORAGE_SALT, salt.toString());
168 salt = new BigInteger(saltConfig.toString());
170 if (privateKeyConfig == null) {
171 privateKey = HomekitServer.generateKey();
172 storage.put(STORAGE_PRIVATE_KEY, Base64.getEncoder().encodeToString(privateKey));
174 privateKey = Base64.getDecoder().decode(privateKeyConfig.toString());