-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScpResponseFactory.cpp
More file actions
156 lines (138 loc) · 5.6 KB
/
Copy pathScpResponseFactory.cpp
File metadata and controls
156 lines (138 loc) · 5.6 KB
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
/*
secure-control-protocol
This is the source file for the ScpResponseFactory class.
SPDX-License-Identifier: GPL-3.0-or-later
Copyright (C) 2018 Benjamin Schilling
*/
#include "ScpResponseFactory.h"
ScpResponseFactory::ScpResponseFactory()
{
}
// ====== Control Repsonse ======
String ScpResponseFactory::createResponseControl(String deviceID, String action, String result)
{
String response = "{ \"type\" : \"control\",";
response += "\"action\" : \"" + action + "\",";
response += "\"deviceId\" : \"" + deviceID + "\",";
response += "\"result\" : \"" + result + "\"";
response += "}";
return response;
}
// ====== Measure Repsonse ======
String ScpResponseFactory::createResponseMeasure(String deviceID, String action, double value, String result)
{
String response = "{ \"type\" : \"measure\",";
response += "\"action\" : \"" + action + "\",";
response += "\"deviceId\" : \"" + deviceID + "\",";
response += "\"value\" : \"" + String(value) + "\",";
response += "\"result\" : \"" + result + "\"";
response += "}";
return response;
}
// ====== Security Repsonses ======
String ScpResponseFactory::createResponseSecurityFetchNVCN(String deviceID, String nvcnString)
{
String response = "{ \"type\" : \"security-fetch-nvcn\",";
response += "\"deviceId\" :\"" + deviceID + "\" ,";
response += "\"nvcn\" :\"" + nvcnString + "\"";
response += "}";
return response;
}
String ScpResponseFactory::createResponseSecurityPwChange(String deviceID, String numberOfPasswordChanges, String status)
{
String response = "{\"type\":\"security-pw-change\",";
response += "\"deviceId\":\"" + deviceID + "\",";
response += "\"currentPasswordNumber\":\"" + numberOfPasswordChanges + "\",";
response += "\"result\":\"" + status + "\"";
response += "}";
return response;
}
String ScpResponseFactory::createResponseSecurityRename(String deviceID, String deviceName, String status)
{
String response = "{\"type\":\"security-rename\",";
response += "\"deviceId\":\"" + deviceID + "\",";
response += "\"deviceName\":\"" + deviceName + "\",";
response += "\"result\":\"" + status + "\"";
response += "}";
return response;
}
String ScpResponseFactory::createResponseSecurityWifiConfig(String deviceID, String result)
{
String response = "{ \"type\" : \"security-wifi-config\",";
response += "\"deviceId\" :\"" + deviceID + "\" ,";
response += "\"result\" :\"" + result + "\"";
response += "}";
return response;
}
String ScpResponseFactory::createResponseSecurityResetToDefault(String deviceID, String result)
{
String response = "{ \"type\" : \"security-reset-to-default\",";
response += "\"deviceId\" :\"" + deviceID + "\" ,";
response += "\"result\" :\"" + result + "\"";
response += "}";
return response;
}
String ScpResponseFactory::createResponseSecurityRestart(String deviceID, String result)
{
String response = "{ \"type\" : \"security-restart\",";
response += "\"deviceId\" :\"" + deviceID + "\" ,";
response += "\"result\" :\"" + result + "\"";
response += "}";
return response;
}
// ====== Discover Response ======
String ScpResponseFactory::createResponseDiscoverHello(String deviceID, String deviceType, String deviceName, String controlActions, String measureActions, String currentPasswordNumber)
{
String controlActionsString = controlActions;
controlActionsString.replace(",", "");
String measureActionsString = measureActions;
measureActionsString.replace(",", "");
String stringForHMAC = "discover-response" + deviceID + deviceType + deviceName + controlActionsString + measureActionsString + currentPasswordNumber + "\0";
scpDebug.println(scpDebug.base, "ScpResponseFactory::createResponseDiscoverHello - String for HMAC:" + stringForHMAC);
uint8_t key[KEY_LENGTH];
memset(key, 0, KEY_LENGTH * sizeof(uint8_t));
String pw = scpPassword.readPassword();
//Get bytes of password string + 1, otherwise the last character is omitted
pw.getBytes(key, KEY_LENGTH + 1);
scpDebug.println(scpDebug.base, "ScpResponseFactory::createResponseDiscoverHello - PW for HMAC:" + pw);
String hmac = scpCrypto.generateHMAC(stringForHMAC, key, KEY_LENGTH);
String response = "{";
response += "\"type\":\"discover-response\",";
response += "\"deviceId\":\"" + deviceID + "\",";
response += "\"deviceType\":\"" + deviceType + "\",";
response += "\"deviceName\":\"" + deviceName + "\",";
if (controlActions != "")
{
response += "\"controlActions\": [";
response += controlActions;
response += "],";
}
if (measureActions != "")
{
response += "\"measureActions\": [";
response += measureActions;
response += "],";
}
response += "\"currentPasswordNumber\":\"" + currentPasswordNumber + "\",";
response += "\"hmac\":\"" + hmac + "\"";
response += "}";
return response;
}
// ====== HMAC SHA512 Response ======
String ScpResponseFactory::createHmacResponse(String plainTextResponse)
{
bigBase64.encode(plainTextResponse);
char *encodedPayload = bigBase64.result();
String payloadToSend = String(encodedPayload);
uint8_t key[KEY_LENGTH];
memset(key, 0, KEY_LENGTH * sizeof(uint8_t));
String pw = scpPassword.readPassword();
//Get bytes of password string + 1, otherwise the last character is omitted
pw.getBytes(key, KEY_LENGTH + 1);
String hmac = scpCrypto.generateHMAC(payloadToSend, key, KEY_LENGTH);
String response = "{";
response += "\"response\":\"" + payloadToSend + "\",";
response += "\"hmac\":\"" + hmac + "\"";
response += "}";
return response;
}