forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteCertificate.cpp
More file actions
96 lines (76 loc) · 2.99 KB
/
DeleteCertificate.cpp
File metadata and controls
96 lines (76 loc) · 2.99 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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Operations/DeleteCertificate.h>
#if MO_ENABLE_CERT_MGMT
#include <MicroOcpp/Model/Certificates/Certificate.h>
#include <MicroOcpp/Model/Certificates/CertificateService.h>
#include <MicroOcpp/Debug.h>
using MicroOcpp::Ocpp201::DeleteCertificate;
using MicroOcpp::JsonDoc;
DeleteCertificate::DeleteCertificate(CertificateService& certService) : MemoryManaged("v201.Operation.", "DeleteCertificate"), certService(certService) {
}
void DeleteCertificate::processReq(JsonObject payload) {
JsonObject certIdJson = payload["certificateHashData"];
if (!certIdJson.containsKey("hashAlgorithm") ||
!certIdJson.containsKey("issuerNameHash") ||
!certIdJson.containsKey("issuerKeyHash") ||
!certIdJson.containsKey("serialNumber")) {
errorCode = "FormationViolation";
return;
}
const char *hashAlgorithm = certIdJson["hashAlgorithm"] | "_Invalid";
if (!certIdJson["issuerNameHash"].is<const char*>() ||
!certIdJson["issuerKeyHash"].is<const char*>() ||
!certIdJson["serialNumber"].is<const char*>()) {
errorCode = "FormationViolation";
return;
}
CertificateHash cert;
if (!strcmp(hashAlgorithm, "SHA256")) {
cert.hashAlgorithm = HashAlgorithmType_SHA256;
} else if (!strcmp(hashAlgorithm, "SHA384")) {
cert.hashAlgorithm = HashAlgorithmType_SHA384;
} else if (!strcmp(hashAlgorithm, "SHA512")) {
cert.hashAlgorithm = HashAlgorithmType_SHA512;
} else {
errorCode = "FormationViolation";
return;
}
auto retIN = ocpp_cert_set_issuerNameHash(&cert, certIdJson["issuerNameHash"] | "_Invalid", cert.hashAlgorithm);
auto retIK = ocpp_cert_set_issuerKeyHash(&cert, certIdJson["issuerKeyHash"] | "_Invalid", cert.hashAlgorithm);
auto retSN = ocpp_cert_set_serialNumber(&cert, certIdJson["serialNumber"] | "_Invalid");
if (retIN < 0 || retIK < 0 || retSN < 0) {
errorCode = "FormationViolation";
return;
}
auto certStore = certService.getCertificateStore();
if (!certStore) {
errorCode = "NotSupported";
return;
}
auto status = certStore->deleteCertificate(cert);
switch (status) {
case DeleteCertificateStatus_Accepted:
this->status = "Accepted";
break;
case DeleteCertificateStatus_Failed:
this->status = "Failed";
break;
case DeleteCertificateStatus_NotFound:
this->status = "NotFound";
break;
default:
MO_DBG_ERR("internal error");
errorCode = "InternalError";
return;
}
//operation executed successfully
}
std::unique_ptr<JsonDoc> DeleteCertificate::createConf(){
auto doc = makeJsonDoc(getMemoryTag(), JSON_OBJECT_SIZE(1));
JsonObject payload = doc->to<JsonObject>();
payload["status"] = status;
return doc;
}
#endif //MO_ENABLE_CERT_MGMT