| /* |
| * |
| * Copyright (c) 2013-2017 Nest Labs, Inc. |
| * All rights reserved. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| import com.nestlabs.weave.security.WeaveKeyExportClient; |
| import com.nestlabs.weave.security.WeaveKeyExportSupport; |
| import com.nestlabs.weave.security.WeaveKeyId; |
| |
| public class WeaveKeyExportClientTest extends SimpleTest { |
| |
| public static void main(String[] args) |
| { |
| WeaveKeyExportClientTest testObj = new WeaveKeyExportClientTest(); |
| |
| int res = testObj.runTests(new String[] { |
| "testKeyExport_ResponderId", |
| "testKeyExport_NoResponderId", |
| "testKeyExport_SHA1DeviceCert" |
| }); |
| |
| System.exit(res); |
| } |
| |
| public void testKeyExport_ResponderId() throws Exception |
| { |
| final WeaveKeyExportClient keyExportClient = new WeaveKeyExportClient(); |
| |
| // Enable the key export client object to trust Nest development devices, since the test uses a development |
| // device certificate. |
| keyExportClient.setAllowNestDevelopmentDevices(true); |
| |
| // Call the key export client object to create a key export request. |
| byte[] keyExportReq = keyExportClient.generateKeyExportRequest(exportedKeyId, deviceId, accessToken); |
| |
| // Call a test support function to simulate the device's behavior when it processes a key export request. |
| Object[] simOutput = WeaveKeyExportSupport.simulateDeviceKeyExport(deviceCertSHA256, devicePrivKey, accessTokenCert, keyExportReq); |
| String respType = (String)simOutput[0]; |
| byte[] keyExportResp = (byte[])simOutput[1]; |
| |
| // Verify that the output from the simulated device was a key export response message. |
| if (!respType.equals("KeyExportResponse")) { |
| throw new Exception(String.format("Unexpected response type from simulateDeviceKeyExport(): %s", respType)); |
| } |
| |
| // Call the key export client object to process the response from the simulated device. |
| byte[] exportedKey = keyExportClient.processKeyExportResponse(deviceId, keyExportResp); |
| |
| // Verify that the exported key value matches the value returned by the simulateDeviceKeyExport() method. |
| assertEqual(exportedKey, expectedCRKValue, "exportedKey"); |
| } |
| |
| public void testKeyExport_NoResponderId() throws Exception |
| { |
| final WeaveKeyExportClient keyExportClient = new WeaveKeyExportClient(); |
| |
| // Enable the key export client object to trust Nest development devices, since the test uses a development |
| // device certificate. |
| keyExportClient.setAllowNestDevelopmentDevices(true); |
| |
| // Call the key export client object to create a key export request. |
| byte[] keyExportReq = keyExportClient.generateKeyExportRequest(exportedKeyId, 0, accessToken); |
| |
| // Call a test support function to simulate the device's behavior when it processes a key export request. |
| Object[] simOutput = WeaveKeyExportSupport.simulateDeviceKeyExport(deviceCertSHA256, devicePrivKey, accessTokenCert, keyExportReq); |
| String respType = (String)simOutput[0]; |
| byte[] keyExportResp = (byte[])simOutput[1]; |
| |
| // Verify that the output from the simulated device was a key export response message. |
| if (!respType.equals("KeyExportResponse")) { |
| throw new Exception(String.format("Unexpected response type from simulateDeviceKeyExport(): %s", respType)); |
| } |
| |
| // Call the key export client object to process the response from the simulated device. |
| byte[] exportedKey = keyExportClient.processKeyExportResponse(0, keyExportResp); |
| |
| // Verify that the exported key value matches the value returned by the simulateDeviceKeyExport() method. |
| assertEqual(exportedKey, expectedCRKValue, "exportedKey"); |
| } |
| |
| public void testKeyExport_SHA1DeviceCert() throws Exception |
| { |
| final WeaveKeyExportClient keyExportClient = new WeaveKeyExportClient(); |
| |
| // Enable the key export client object to trust Nest development devices, since the test uses a development |
| // device certificate. |
| keyExportClient.setAllowNestDevelopmentDevices(true); |
| |
| // Enable the client to trust devices that sign their responses using SHA1 certificates. |
| keyExportClient.setAllowSHA1DeviceCertificates(true); |
| |
| // Call the key export client object to create a key export request. |
| byte[] keyExportReq = keyExportClient.generateKeyExportRequest(exportedKeyId, deviceId, accessToken); |
| |
| // Call a test support function to simulate the device's behavior when it processes a key export request. |
| Object[] simOutput = WeaveKeyExportSupport.simulateDeviceKeyExport(deviceCertSHA1, devicePrivKey, accessTokenCert, keyExportReq); |
| String respType = (String)simOutput[0]; |
| byte[] keyExportResp = (byte[])simOutput[1]; |
| |
| // Verify that the output from the simulated device was a key export response message. |
| if (!respType.equals("KeyExportResponse")) { |
| throw new Exception(String.format("Unexpected response type from simulateDeviceKeyExport(): %s", respType)); |
| } |
| |
| // Call the key export client object to process the response from the simulated device. |
| byte[] exportedKey = keyExportClient.processKeyExportResponse(deviceId, keyExportResp); |
| |
| // Verify that the exported key value matches the value returned by the simulateDeviceKeyExport() method. |
| assertEqual(exportedKey, expectedCRKValue, "exportedKey"); |
| } |
| |
| // WeaveKeyExportSupport.simulateDeviceKeyExport() method supports the simulated exporting of a single key, the CRK. |
| private static final int exportedKeyId = WeaveKeyId.ClientRootKey; |
| |
| // The WeaveKeyExportSupport.simulateDeviceKeyExport() method returns a fixed CRK value derived from a |
| // hard-coded fabric secret of [ 0x01, 0x02, ... 0x23 ]. This is the derived CRK value. |
| private static final byte[] expectedCRKValue = toByteArray(new int[] |
| { |
| 0x4b, 0xbb, 0x9d, 0x3b, 0x63, 0x11, 0xbf, 0x23, 0x24, 0x01, 0xd1, 0xe2, 0x12, 0xb8, 0x18, 0x07, |
| 0x98, 0xc2, 0x8d, 0x9c, 0xec, 0x3e, 0xfe, 0x04, 0xb9, 0xf4, 0xa0, 0x67, 0x8b, 0x28, 0xc7, 0xa5, |
| }); |
| |
| private static final byte[] accessToken = javax.xml.bind.DatatypeConverter.parseBase64Binary( |
| "1QAABAAJADUBMAEITi8yS0HXOtskAgQ3AyyBEERVTU1ZLUFDQ09VTlQtSUQYJgTLqPobJgVLNU9C" + |
| "NwYsgRBEVU1NWS1BQ0NPVU5ULUlEGCQHAiYIJQBaIzAKOQQr2dtaYu+6sVMqD5ljt4owxYpBKaUZ" + |
| "TksL837axemzNfB1GG1JXYbERCUHQbTTqe/utCrWCl2d4DWDKQEYNYIpASQCBRg1hCkBNgIEAgQB" + |
| "GBg1gTACCEI8lV9GHlLbGDWAMAIIQjyVX0YeUtsYNQwwAR0AimGGYj0XstLP0m05PeQlaeCR6gVq" + |
| "dc7dReuDzzACHHS0K6RtFGW3t3GaWq9k0ohgbrOxoDHKkm/K8kMYGDUCJgElAFojMAIcuvzjT4a/" + |
| "fDgScCv5oxC/T5vz7zAPpURNQjpnajADOQQr2dtaYu+6sVMqD5ljt4owxYpBKaUZTksL837axemz" + |
| "NfB1GG1JXYbERCUHQbTTqe/utCrWCl2d4BgY"); |
| |
| private static final byte[] accessTokenCert = toByteArray(new int[] |
| { |
| 0xd5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30, 0x01, 0x08, 0x4e, 0x2f, 0x32, 0x4b, 0x41, 0xd7, |
| 0x3a, 0xdb, 0x24, 0x02, 0x04, 0x37, 0x03, 0x2c, 0x81, 0x10, 0x44, 0x55, 0x4d, 0x4d, 0x59, 0x2d, |
| 0x41, 0x43, 0x43, 0x4f, 0x55, 0x4e, 0x54, 0x2d, 0x49, 0x44, 0x18, 0x26, 0x04, 0xcb, 0xa8, 0xfa, |
| 0x1b, 0x26, 0x05, 0x4b, 0x35, 0x4f, 0x42, 0x37, 0x06, 0x2c, 0x81, 0x10, 0x44, 0x55, 0x4d, 0x4d, |
| 0x59, 0x2d, 0x41, 0x43, 0x43, 0x4f, 0x55, 0x4e, 0x54, 0x2d, 0x49, 0x44, 0x18, 0x24, 0x07, 0x02, |
| 0x26, 0x08, 0x25, 0x00, 0x5a, 0x23, 0x30, 0x0a, 0x39, 0x04, 0x2b, 0xd9, 0xdb, 0x5a, 0x62, 0xef, |
| 0xba, 0xb1, 0x53, 0x2a, 0x0f, 0x99, 0x63, 0xb7, 0x8a, 0x30, 0xc5, 0x8a, 0x41, 0x29, 0xa5, 0x19, |
| 0x4e, 0x4b, 0x0b, 0xf3, 0x7e, 0xda, 0xc5, 0xe9, 0xb3, 0x35, 0xf0, 0x75, 0x18, 0x6d, 0x49, 0x5d, |
| 0x86, 0xc4, 0x44, 0x25, 0x07, 0x41, 0xb4, 0xd3, 0xa9, 0xef, 0xee, 0xb4, 0x2a, 0xd6, 0x0a, 0x5d, |
| 0x9d, 0xe0, 0x35, 0x83, 0x29, 0x01, 0x18, 0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, |
| 0x84, 0x29, 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18, 0x18, 0x35, 0x81, 0x30, 0x02, 0x08, |
| 0x42, 0x3c, 0x95, 0x5f, 0x46, 0x1e, 0x52, 0xdb, 0x18, 0x35, 0x80, 0x30, 0x02, 0x08, 0x42, 0x3c, |
| 0x95, 0x5f, 0x46, 0x1e, 0x52, 0xdb, 0x18, 0x35, 0x0c, 0x30, 0x01, 0x1d, 0x00, 0x8a, 0x61, 0x86, |
| 0x62, 0x3d, 0x17, 0xb2, 0xd2, 0xcf, 0xd2, 0x6d, 0x39, 0x3d, 0xe4, 0x25, 0x69, 0xe0, 0x91, 0xea, |
| 0x05, 0x6a, 0x75, 0xce, 0xdd, 0x45, 0xeb, 0x83, 0xcf, 0x30, 0x02, 0x1c, 0x74, 0xb4, 0x2b, 0xa4, |
| 0x6d, 0x14, 0x65, 0xb7, 0xb7, 0x71, 0x9a, 0x5a, 0xaf, 0x64, 0xd2, 0x88, 0x60, 0x6e, 0xb3, 0xb1, |
| 0xa0, 0x31, 0xca, 0x92, 0x6f, 0xca, 0xf2, 0x43, 0x18, 0x18, |
| }); |
| |
| private static final long deviceId = 0x18B4300000000001L; |
| |
| private static final byte[] deviceCertSHA256 = toByteArray(new int[] |
| { |
| 0xd5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30, 0x01, 0x08, 0x48, 0xaa, 0x6f, 0x02, 0xcf, 0xbd, |
| 0x2e, 0xcc, 0x24, 0x02, 0x05, 0x37, 0x03, 0x27, 0x13, 0x02, 0x00, 0x00, 0xee, 0xee, 0x30, 0xb4, |
| 0x18, 0x18, 0x26, 0x04, 0x00, 0x18, 0x73, 0x1a, 0x26, 0x05, 0x7f, 0x12, 0x9a, 0x2d, 0x37, 0x06, |
| 0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xb4, 0x18, 0x18, 0x24, 0x07, 0x02, 0x26, 0x08, |
| 0x25, 0x00, 0x5a, 0x23, 0x30, 0x0a, 0x39, 0x04, 0xef, 0x67, 0x9d, 0x53, 0x0c, 0x99, 0xff, 0x9d, |
| 0x72, 0x42, 0xb1, 0xf9, 0xb6, 0x60, 0x20, 0x8e, 0x25, 0x9f, 0x35, 0x72, 0xf0, 0xa3, 0xe7, 0x83, |
| 0xe6, 0x56, 0x14, 0x93, 0xf9, 0x68, 0x45, 0x65, 0x8b, 0x24, 0x31, 0x5e, 0x87, 0x8c, 0x64, 0x35, |
| 0x25, 0x87, 0x19, 0x03, 0x99, 0xcd, 0x45, 0xa1, 0x24, 0xfa, 0x76, 0x0b, 0x12, 0x9e, 0x39, 0x7e, |
| 0x35, 0x83, 0x29, 0x01, 0x18, 0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, |
| 0x01, 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18, 0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x4e, 0xff, |
| 0x47, 0x51, 0xe4, 0xc6, 0x63, 0x9b, 0x18, 0x35, 0x80, 0x30, 0x02, 0x08, 0x44, 0xe3, 0x40, 0x38, |
| 0xa9, 0xd4, 0xb5, 0xa7, 0x18, 0x35, 0x0c, 0x30, 0x01, 0x18, 0x20, 0xb7, 0x5a, 0x29, 0xde, 0xc1, |
| 0x72, 0xa9, 0xcb, 0x01, 0xe1, 0x61, 0x49, 0x1a, 0x04, 0x2e, 0xc5, 0x01, 0x39, 0x35, 0xbb, 0x0c, |
| 0x05, 0x18, 0x30, 0x02, 0x19, 0x00, 0xa2, 0x96, 0x60, 0xc1, 0xb9, 0xb8, 0xe0, 0xfa, 0x4d, 0xfa, |
| 0x66, 0x29, 0xb1, 0x10, 0xaa, 0x55, 0x45, 0x8e, 0xb4, 0x5d, 0xf6, 0xdf, 0xb8, 0x96, 0x18, 0x18, |
| }); |
| |
| private static final byte[] deviceCertSHA1 = toByteArray(new int[] |
| { |
| 0xd5, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x30, 0x01, 0x08, 0x10, 0x48, 0xca, 0xf2, 0xed, 0x4f, |
| 0x9b, 0x30, 0x24, 0x02, 0x04, 0x57, 0x03, 0x00, 0x27, 0x13, 0x02, 0x00, 0x00, 0xee, 0xee, 0x30, |
| 0xb4, 0x18, 0x18, 0x26, 0x04, 0x04, 0x23, 0x73, 0x1a, 0x26, 0x05, 0x04, 0xcc, 0x98, 0x2d, 0x57, |
| 0x06, 0x00, 0x27, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x30, 0xb4, 0x18, 0x18, 0x24, 0x07, 0x02, |
| 0x24, 0x08, 0x25, 0x30, 0x0a, 0x39, 0x04, 0xef, 0x67, 0x9d, 0x53, 0x0c, 0x99, 0xff, 0x9d, 0x72, |
| 0x42, 0xb1, 0xf9, 0xb6, 0x60, 0x20, 0x8e, 0x25, 0x9f, 0x35, 0x72, 0xf0, 0xa3, 0xe7, 0x83, 0xe6, |
| 0x56, 0x14, 0x93, 0xf9, 0x68, 0x45, 0x65, 0x8b, 0x24, 0x31, 0x5e, 0x87, 0x8c, 0x64, 0x35, 0x25, |
| 0x87, 0x19, 0x03, 0x99, 0xcd, 0x45, 0xa1, 0x24, 0xfa, 0x76, 0x0b, 0x12, 0x9e, 0x39, 0x7e, 0x35, |
| 0x83, 0x29, 0x01, 0x18, 0x35, 0x82, 0x29, 0x01, 0x24, 0x02, 0x05, 0x18, 0x35, 0x84, 0x29, 0x01, |
| 0x36, 0x02, 0x04, 0x02, 0x04, 0x01, 0x18, 0x18, 0x35, 0x81, 0x30, 0x02, 0x08, 0x4e, 0xff, 0x47, |
| 0x51, 0xe4, 0xc6, 0x63, 0x9b, 0x18, 0x35, 0x80, 0x30, 0x02, 0x08, 0x44, 0xe3, 0x40, 0x38, 0xa9, |
| 0xd4, 0xb5, 0xa7, 0x18, 0x35, 0x0c, 0x30, 0x01, 0x18, 0x5d, 0xb7, 0x52, 0xb0, 0x95, 0x13, 0x11, |
| 0x71, 0xf1, 0x5b, 0x64, 0x03, 0x80, 0x8c, 0x18, 0xbe, 0xa1, 0x20, 0xf1, 0x86, 0xba, 0x45, 0x6c, |
| 0x14, 0x30, 0x02, 0x19, 0x00, 0xc5, 0x0d, 0xcf, 0x26, 0x02, 0x80, 0x11, 0x8c, 0x51, 0x3a, 0xbd, |
| 0x95, 0x95, 0x76, 0x94, 0x77, 0xc9, 0x46, 0xff, 0xed, 0xc0, 0xa0, 0x3d, 0xbd, 0x18, 0x18, |
| }); |
| |
| private static final byte[] devicePrivKey = toByteArray(new int[] |
| { |
| 0xd5, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x24, 0x01, 0x25, 0x30, 0x02, 0x1c, 0xa9, 0x87, 0xdb, |
| 0x66, 0xc9, 0x7b, 0x69, 0x8b, 0xa2, 0x68, 0x22, 0x98, 0xb2, 0xed, 0xef, 0x47, 0x60, 0xb9, 0x6f, |
| 0x7c, 0x33, 0xa1, 0x2c, 0x29, 0xa8, 0xd5, 0xb8, 0xee, 0x30, 0x03, 0x39, 0x04, 0xef, 0x67, 0x9d, |
| 0x53, 0x0c, 0x99, 0xff, 0x9d, 0x72, 0x42, 0xb1, 0xf9, 0xb6, 0x60, 0x20, 0x8e, 0x25, 0x9f, 0x35, |
| 0x72, 0xf0, 0xa3, 0xe7, 0x83, 0xe6, 0x56, 0x14, 0x93, 0xf9, 0x68, 0x45, 0x65, 0x8b, 0x24, 0x31, |
| 0x5e, 0x87, 0x8c, 0x64, 0x35, 0x25, 0x87, 0x19, 0x03, 0x99, 0xcd, 0x45, 0xa1, 0x24, 0xfa, 0x76, |
| 0x0b, 0x12, 0x9e, 0x39, 0x7e, 0x18, |
| }); |
| } |