Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
SMPSignatureResolvingKeyInfo.java
Go to the documentation of this file.
1 /**
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  * Copyright (c) 2020 ZAFENA AB
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 package org.direct_bt;
27 
28 /**
29  * SMP Signature Resolving Key Info, used for platform agnostic persistence.
30  * <p>
31  * Notable: No endian wise conversion shall occur on this data,
32  * since the encryption values are interpreted as a byte stream.
33  * </p>
34  * <p>
35  * Byte layout must be synchronized with native direct_bt::SMPSignatureResolvingKey
36  * </p>
37  * @since 2.2.0
38  */
40  /**
41  * {@link SMPSignatureResolvingKeyInfo} Property Bits
42  */
43  static public enum PropertyType {
44  /** No specific property */
45  NONE((byte)0),
46  /** Responder Key (LL slave). Absence indicates Initiator Key (LL master). */
47  RESPONDER((byte)0x01),
48  /** Authentication used. */
49  AUTH((byte)0x02);
50 
51  public final byte value;
52 
53  /**
54  * Maps the specified name to a constant of {@link PropertyType}.
55  * <p>
56  * Implementation simply returns {@link #valueOf(String)}.
57  * This maps the constant names itself to their respective constant.
58  * </p>
59  * @param name the string name to be mapped to a constant of this enum type.
60  * @return the corresponding constant of this enum type.
61  * @throws IllegalArgumentException if the specified name can't be mapped to a constant of this enum type
62  * as described above.
63  */
64  public static PropertyType get(final String name) throws IllegalArgumentException {
65  return valueOf(name);
66  }
67 
68  /**
69  * Maps the specified integer value to a constant of {@link PropertyType}.
70  * @param value the integer value to be mapped to a constant of this enum type.
71  * @return the corresponding constant of this enum type, using {@link #NONE} if not supported.
72  */
73  public static PropertyType get(final byte value) {
74  switch(value) {
75  case (byte) 0x01: return RESPONDER;
76  case (byte) 0x02: return AUTH;
77  default: return NONE;
78  }
79  }
80 
81  PropertyType(final byte v) {
82  value = v;
83  }
84  }
85 
86  /**
87  * {@link SMPSignatureResolvingKeyInfo} {@link PropertyType} Bit Mask
88  */
89  static public class Properties {
90  /** The {@link PropertyType} bit mask */
91  public byte mask;
92 
93  public Properties(final byte v) {
94  mask = v;
95  }
96 
97  public boolean isEmpty() { return 0 == mask; }
98  public boolean isSet(final PropertyType bit) { return 0 != ( mask & bit.value ); }
99  public void set(final PropertyType bit) { mask = (byte) ( mask | bit.value ); }
100 
101  @Override
102  public String toString() {
103  int count = 0;
104  final StringBuilder out = new StringBuilder();
105  if( isSet(PropertyType.RESPONDER) ) {
106  out.append(PropertyType.RESPONDER.name()); count++;
107  }
108  if( isSet(PropertyType.AUTH) ) {
109  if( 0 < count ) { out.append(", "); }
110  out.append(PropertyType.AUTH.name()); count++;
111  }
112  return "["+out.toString()+"]";
113  }
114  }
115 
116  /** {@link Properties} bit mask. 1 octet or 8 bits. */
118 
119  /** Connection Signature Resolving Key (CSRK) */
120  public byte csrk[/*16*/];
121 
122  /**
123  * Size of the byte stream representation in bytes
124  * @see #getStream(byte[], int)
125  */
126  public static final int byte_size = 1+16;
127 
128  /** Construct instance via given source byte array */
129  public SMPSignatureResolvingKeyInfo(final byte source[], final int pos) {
130  if( byte_size > ( source.length - pos ) ) {
131  throw new IllegalArgumentException("Stream ( "+source.length+" - "+pos+" ) < "+byte_size+" bytes");
132  }
133  csrk = new byte[16];
134  putStream(source, pos);
135  }
136 
137  /** Construct emoty unset instance. */
139  properties = new Properties((byte)0);
140  csrk = new byte[16];
141  }
142 
143  /**
144  * Method transfers all bytes representing a SMPLongTermKeyInfo from the given
145  * source array at the given position into this instance.
146  * <p>
147  * Implementation is consistent with {@link #getStream(byte[], int)}.
148  * </p>
149  * @param source the source array
150  * @param pos starting position in the source array
151  * @see #getStream(byte[], int)
152  */
153  public void putStream(final byte[] source, int pos) {
154  if( byte_size > ( source.length - pos ) ) {
155  throw new IllegalArgumentException("Stream ( "+source.length+" - "+pos+" ) < "+byte_size+" bytes");
156  }
157  properties = new Properties(source[pos++]);
158  System.arraycopy(source, pos, csrk, 0, 16); pos+=16;
159  }
160 
161  /**
162  * Method transfers all bytes representing this instance into the given
163  * destination array at the given position.
164  * <p>
165  * Implementation is consistent with {@link #SMPLongTermKeyInfo(byte[], int)}.
166  * </p>
167  * @param sink the destination array
168  * @param pos starting position in the destination array
169  * @see #SMPLongTermKeyInfo(byte[], int)
170  * @see #putStream(byte[], int)
171  */
172  public final void getStream(final byte[] sink, int pos) {
173  if( byte_size > ( sink.length - pos ) ) {
174  throw new IllegalArgumentException("Stream ( "+sink.length+" - "+pos+" ) < "+byte_size+" bytes");
175  }
176  sink[pos++] = properties.mask;
177  System.arraycopy(csrk, 0, sink, pos, 16); pos+=16;
178  }
179 
180  public final boolean isResponder() { return properties.isSet(PropertyType.RESPONDER); }
181 
182  @Override
183  public String toString() { // hex-fmt aligned with btmon
184  return "CSRK[props "+properties.toString()+
185  ", csrk "+BTUtils.bytesHexString(csrk, 0, -1, true /* lsbFirst */)+
186  "]";
187  }
188 
189 };
org.direct_bt.SMPSignatureResolvingKeyInfo.putStream
void putStream(final byte[] source, int pos)
Method transfers all bytes representing a SMPLongTermKeyInfo from the given source array at the given...
Definition: SMPSignatureResolvingKeyInfo.java:153
org.direct_bt.SMPSignatureResolvingKeyInfo.Properties.isEmpty
boolean isEmpty()
Definition: SMPSignatureResolvingKeyInfo.java:97
org.direct_bt.SMPSignatureResolvingKeyInfo.Properties.Properties
Properties(final byte v)
Definition: SMPSignatureResolvingKeyInfo.java:93
org.direct_bt.SMPSignatureResolvingKeyInfo.properties
Properties properties
Properties bit mask.
Definition: SMPSignatureResolvingKeyInfo.java:117
org.direct_bt.SMPSignatureResolvingKeyInfo.toString
String toString()
Definition: SMPSignatureResolvingKeyInfo.java:183
org.direct_bt.SMPSignatureResolvingKeyInfo.getStream
final void getStream(final byte[] sink, int pos)
Method transfers all bytes representing this instance into the given destination array at the given p...
Definition: SMPSignatureResolvingKeyInfo.java:172
org.direct_bt.SMPSignatureResolvingKeyInfo.PropertyType
SMPSignatureResolvingKeyInfo Property Bits
Definition: SMPSignatureResolvingKeyInfo.java:43
org.direct_bt.SMPSignatureResolvingKeyInfo.Properties
SMPSignatureResolvingKeyInfo PropertyType Bit Mask
Definition: SMPSignatureResolvingKeyInfo.java:89
org.direct_bt.SMPSignatureResolvingKeyInfo.SMPSignatureResolvingKeyInfo
SMPSignatureResolvingKeyInfo(final byte source[], final int pos)
Construct instance via given source byte array.
Definition: SMPSignatureResolvingKeyInfo.java:129
org.direct_bt.SMPSignatureResolvingKeyInfo.PropertyType.AUTH
AUTH
Authentication used.
Definition: SMPSignatureResolvingKeyInfo.java:49
org.direct_bt.SMPSignatureResolvingKeyInfo.byte_size
static final int byte_size
Size of the byte stream representation in bytes.
Definition: SMPSignatureResolvingKeyInfo.java:126
org.direct_bt.SMPSignatureResolvingKeyInfo.PropertyType.RESPONDER
RESPONDER
Responder Key (LL slave).
Definition: SMPSignatureResolvingKeyInfo.java:47
org.direct_bt.SMPSignatureResolvingKeyInfo.SMPSignatureResolvingKeyInfo
SMPSignatureResolvingKeyInfo()
Construct emoty unset instance.
Definition: SMPSignatureResolvingKeyInfo.java:138
org.direct_bt.BTUtils
Definition: BTUtils.java:29
org.direct_bt.SMPSignatureResolvingKeyInfo.PropertyType.NONE
NONE
No specific property.
Definition: SMPSignatureResolvingKeyInfo.java:45
org.direct_bt.SMPSignatureResolvingKeyInfo.PropertyType.PropertyType
PropertyType(final byte v)
Definition: SMPSignatureResolvingKeyInfo.java:81
org.direct_bt.SMPSignatureResolvingKeyInfo.csrk
byte csrk[]
Connection Signature Resolving Key (CSRK)
Definition: SMPSignatureResolvingKeyInfo.java:120
org.direct_bt.SMPSignatureResolvingKeyInfo.Properties.mask
byte mask
The PropertyType bit mask.
Definition: SMPSignatureResolvingKeyInfo.java:91
org.direct_bt.SMPSignatureResolvingKeyInfo.PropertyType.value
final byte value
Definition: SMPSignatureResolvingKeyInfo.java:51
org.direct_bt.SMPSignatureResolvingKeyInfo
SMP Signature Resolving Key Info, used for platform agnostic persistence.
Definition: SMPSignatureResolvingKeyInfo.java:39
org.direct_bt.BTUtils.bytesHexString
static String bytesHexString(final byte[] bytes, final int offset, final int length, final boolean lsbFirst)
Produce a lower-case hexadecimal string representation of the given byte values.
Definition: BTUtils.java:123
org.direct_bt.SMPSignatureResolvingKeyInfo.isResponder
final boolean isResponder()
Definition: SMPSignatureResolvingKeyInfo.java:180
org.direct_bt.SMPSignatureResolvingKeyInfo.Properties.toString
String toString()
Definition: SMPSignatureResolvingKeyInfo.java:102
org.direct_bt.SMPSignatureResolvingKeyInfo.Properties.isSet
boolean isSet(final PropertyType bit)
Definition: SMPSignatureResolvingKeyInfo.java:98