summaryrefslogtreecommitdiffstats
path: root/java/org/direct_bt/SMPLinkKey.java
blob: 88832b4fee4866490ce4832978526e401cef382e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.direct_bt;

import org.jau.util.BasicTypes;

/**
 * Local SMP Link Key, used for platform agnostic persistence,
 * mapping to platform specific link keys format.
 * <p>
 * Notable: No endian wise conversion shall occur on this data,
 *          since the encryption values are interpreted as a byte stream.
 * </p>
 * <p>
 * Byte layout must be synchronized with native direct_bt::SMPLinkKeyInfo
 * </p>
 * @since 2.4.0
 */
public class SMPLinkKey {
    /**
     * Link Key Types compatible with Mgmt's MgmtLinkKeyType and hence MgmtLinkKeyInfo
     */
    static public enum KeyType {
        /** Combination key */
        COMBI((byte)0x00),
        /** Local Unit key */
        LOCAL_UNIT((byte)0x01),
        /** Remote Unit key */
        REMOTE_UNIT((byte)0x02),
        /** Debug Combination key */
        DBG_COMBI((byte)0x03),
        /** Unauthenticated Combination key from P-192 */
        UNAUTH_COMBI_P192((byte)0x04),
        /** Authenticated Combination key from P-192 */
        AUTH_COMBI_P192((byte)0x05),
        /** Changed Combination key */
        CHANGED_COMBI((byte)0x06),
        /** Unauthenticated Combination key from P-256 */
        UNAUTH_COMBI_P256((byte)0x07),
        /** Authenticated Combination key from P-256 */
        AUTH_COMBI_P256((byte)0x08),
        /** Denoting no or invalid link key type */
        NONE((byte)0xff);

        public final byte value;

        /**
         * Maps the specified name to a constant of {@link KeyType}.
         * <p>
         * Implementation simply returns {@link #valueOf(String)}.
         * This maps the constant names itself to their respective constant.
         * </p>
         * @param name the string name to be mapped to a constant of this enum type.
         * @return the corresponding constant of this enum type.
         * @throws IllegalArgumentException if the specified name can't be mapped to a constant of this enum type
         *                                  as described above.
         */
        public static KeyType get(final String name) throws IllegalArgumentException {
            return valueOf(name);
        }

        /**
         * Maps the specified integer value to a constant of {@link KeyType}.
         * @param value the integer value to be mapped to a constant of this enum type.
         * @return the corresponding constant of this enum type, using {@link #NONE} if not supported.
         */
        public static KeyType get(final byte value) {
            switch(value) {
                case (byte) 0x00: return COMBI;
                case (byte) 0x01: return LOCAL_UNIT;
                case (byte) 0x02: return REMOTE_UNIT;
                case (byte) 0x03: return DBG_COMBI;
                case (byte) 0x04: return UNAUTH_COMBI_P192;
                case (byte) 0x05: return AUTH_COMBI_P192;
                case (byte) 0x06: return CHANGED_COMBI;
                case (byte) 0x07: return UNAUTH_COMBI_P256;
                case (byte) 0x08: return AUTH_COMBI_P256;
                default: return NONE;
            }
        }

        KeyType(final byte v) {
            value = v;
        }
    }

    /** Responder (ll slave) flag. */
    public boolean responder;

    /** {@link KeyType} value. */
    public KeyType type;

    /** Link key. */
    public byte key[/*16*/];

    /** Pin length */
    public byte pin_length;

    /**
     * Size of the byte stream representation in bytes
     * @see #put(byte[], int)
     */
    public static final int byte_size = 1+1+16+1;

    /** Construct instance via given source byte array */
    public SMPLinkKey(final byte source[], final int pos) {
        key = new byte[16];
        get(source, pos);
    }

    /** Construct emoty unset instance. */
    public SMPLinkKey() {
        responder = false;
        type = KeyType.NONE;
        key = new byte[16];
        pin_length = 0;
    }

    /**
     * Method transfers all bytes representing a SMPLinkKeyInfo from the given
     * source array at the given position into this instance.
     * <p>
     * Implementation is consistent with {@link #put(byte[], int)}.
     * </p>
     * @param source the source array
     * @param pos starting position in the source array
     * @see #put(byte[], int)
     */
    public void get(final byte[] source, int pos) {
        if( byte_size > ( source.length - pos ) ) {
            throw new IllegalArgumentException("Stream ( "+source.length+" - "+pos+" ) < "+byte_size+" bytes");
        }
        responder = (byte)0 != source[pos++];
        type = KeyType.get(source[pos++]);
        System.arraycopy(source, pos, key, 0, 16); pos+=16;
        pin_length = source[pos++];
    }

    /**
     * Method transfers all bytes representing this instance into the given
     * destination array at the given position.
     * <p>
     * Implementation is consistent with {@link #SMPLinkKeyInfo(byte[], int)}.
     * </p>
     * @param sink the destination array
     * @param pos starting position in the destination array
     * @see #SMPLinkKeyInfo(byte[], int)
     * @see #get(byte[], int)
     */
    public final void put(final byte[] sink, int pos) {
        if( byte_size > ( sink.length - pos ) ) {
            throw new IllegalArgumentException("Stream ( "+sink.length+" - "+pos+" ) < "+byte_size+" bytes");
        }
        sink[pos++] = responder ? (byte)1 : (byte)0;
        sink[pos++] = type.value;
        System.arraycopy(key,  0, sink, pos, 16); pos+=16;
        sink[pos++] = pin_length;
    }

    public final boolean isValid() { return KeyType.NONE != type; }

    public final boolean isResponder() { return responder; }

    @Override
    public String toString() { // hex-fmt aligned with btmon
        return "LK[res "+responder+", type "+type.toString()+
               ", key "+BasicTypes.bytesHexString(key, 0, -1, true /* lsbFirst */)+
               ", plen "+pin_length+
               "]";
    }

};