]> git.basschouten.com Git - openhab-addons.git/blob
7fd1613d6f446cc48fcdba94b5121d7e38c38115
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.homekit.internal;
14
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;
20
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.storage.Storage;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import io.github.hapjava.server.HomekitAuthInfo;
27 import io.github.hapjava.server.impl.HomekitServer;
28
29 /**
30  * Provides a mechanism to store authenticated HomeKit client details inside the
31  * ESH StorageService, by implementing HomekitAuthInfo.
32  *
33  * @author Andy Lintner - Initial contribution
34  */
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_";
42
43     private final Storage<String> storage;
44     private String mac;
45     private BigInteger salt;
46     private byte[] privateKey;
47     private final String pin;
48
49     public HomekitAuthInfoImpl(Storage<String> storage, String pin) throws InvalidAlgorithmParameterException {
50         this.storage = storage;
51         this.pin = pin;
52         initializeStorage();
53     }
54
55     @Override
56     public void createUser(String username, byte[] publicKey) {
57         logger.trace("Create user {}", username);
58         storage.put(createUserKey(username), Base64.getEncoder().encodeToString(publicKey));
59     }
60
61     @Override
62     public String getMac() {
63         return mac;
64     }
65
66     @Override
67     public String getPin() {
68         return pin;
69     }
70
71     @Override
72     public byte[] getPrivateKey() {
73         return privateKey;
74     }
75
76     @Override
77     public BigInteger getSalt() {
78         return salt;
79     }
80
81     @Override
82     public byte[] getUserPublicKey(String username) {
83         final String encodedKey = storage.get(createUserKey(username));
84         if (encodedKey != null) {
85             return Base64.getDecoder().decode(encodedKey);
86         } else {
87             return null;
88         }
89     }
90
91     @Override
92     public void removeUser(String username) {
93         logger.trace("Remove user {}", username);
94         storage.remove(createUserKey(username));
95     }
96
97     @Override
98     public boolean hasUser() {
99         Collection<String> keys = storage.getKeys();
100         return keys.stream().anyMatch(this::isUserKey);
101     }
102
103     public void clear() {
104         logger.trace("Clear all users");
105         for (String key : new HashSet<>(storage.getKeys())) {
106             if (isUserKey(key)) {
107                 storage.remove(key);
108             }
109         }
110     }
111
112     private String createUserKey(String username) {
113         return STORAGE_USER_PREFIX + username;
114     }
115
116     private boolean isUserKey(String key) {
117         return key.startsWith(STORAGE_USER_PREFIX);
118     }
119
120     private void initializeStorage() throws InvalidAlgorithmParameterException {
121         mac = storage.get(STORAGE_MAC);
122         final @Nullable Object saltConfig = storage.get(STORAGE_SALT);
123         final @Nullable Object privateKeyConfig = storage.get(STORAGE_PRIVATE_KEY);
124         if (mac == null) {
125             logger.warn(
126                     "Could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
127                     storage.getClass().getName());
128             mac = HomekitServer.generateMac();
129             storage.put(STORAGE_MAC, mac);
130         }
131         if (saltConfig == null) {
132             salt = HomekitServer.generateSalt();
133             storage.put(STORAGE_SALT, salt.toString());
134         } else {
135             salt = new BigInteger(saltConfig.toString());
136         }
137         if (privateKeyConfig == null) {
138             privateKey = HomekitServer.generateKey();
139             storage.put(STORAGE_PRIVATE_KEY, Base64.getEncoder().encodeToString(privateKey));
140         } else {
141             privateKey = Base64.getDecoder().decode(privateKeyConfig.toString());
142         }
143     }
144 }