2 * Copyright (c) 2010-2021 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;
50 public HomekitAuthInfoImpl(Storage<String> storage, String pin, String setupId)
51 throws InvalidAlgorithmParameterException {
52 this.storage = storage;
54 this.setupId = setupId;
59 public void createUser(String username, byte[] publicKey) {
60 logger.trace("Create user {}", username);
61 storage.put(createUserKey(username), Base64.getEncoder().encodeToString(publicKey));
65 public String getMac() {
69 public void setMac(String mac) {
74 public String getPin() {
78 public void setPin(String pin) {
83 public String getSetupId() {
87 public void setSetupId(String setupId) {
88 this.setupId = setupId;
92 public byte[] getPrivateKey() {
97 public BigInteger getSalt() {
102 public byte[] getUserPublicKey(String username) {
103 final String encodedKey = storage.get(createUserKey(username));
104 if (encodedKey != null) {
105 return Base64.getDecoder().decode(encodedKey);
112 public void removeUser(String username) {
113 logger.trace("Remove user {}", username);
114 storage.remove(createUserKey(username));
118 public boolean hasUser() {
119 Collection<String> keys = storage.getKeys();
120 return keys.stream().anyMatch(this::isUserKey);
123 public void clear() {
124 logger.trace("Clear all users");
125 for (String key : new HashSet<>(storage.getKeys())) {
126 if (isUserKey(key)) {
132 private String createUserKey(String username) {
133 return STORAGE_USER_PREFIX + username;
136 private boolean isUserKey(String key) {
137 return key.startsWith(STORAGE_USER_PREFIX);
140 private void initializeStorage() throws InvalidAlgorithmParameterException {
141 mac = storage.get(STORAGE_MAC);
142 final @Nullable Object saltConfig = storage.get(STORAGE_SALT);
143 final @Nullable Object privateKeyConfig = storage.get(STORAGE_PRIVATE_KEY);
146 "Could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
147 storage.getClass().getName());
148 mac = HomekitServer.generateMac();
149 storage.put(STORAGE_MAC, mac);
151 if (saltConfig == null) {
152 salt = HomekitServer.generateSalt();
153 storage.put(STORAGE_SALT, salt.toString());
155 salt = new BigInteger(saltConfig.toString());
157 if (privateKeyConfig == null) {
158 privateKey = HomekitServer.generateKey();
159 storage.put(STORAGE_PRIVATE_KEY, Base64.getEncoder().encodeToString(privateKey));
161 privateKey = Base64.getDecoder().decode(privateKeyConfig.toString());