forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCertificate.cpp
More file actions
167 lines (123 loc) · 4.24 KB
/
Certificate.cpp
File metadata and controls
167 lines (123 loc) · 4.24 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
157
158
159
160
161
162
163
164
165
166
167
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Model/Certificates/Certificate.h>
#if MO_ENABLE_CERT_MGMT
#include <string.h>
#include <stdio.h>
bool ocpp_cert_equals(const ocpp_cert_hash *h1, const ocpp_cert_hash *h2) {
return h1->hashAlgorithm == h2->hashAlgorithm &&
h1->serialNumberLen == h2->serialNumberLen && !memcmp(h1->serialNumber, h2->serialNumber, h1->serialNumberLen) &&
!memcmp(h1->issuerNameHash, h2->issuerNameHash, HashAlgorithmSize(h1->hashAlgorithm)) &&
!memcmp(h1->issuerKeyHash, h2->issuerKeyHash, HashAlgorithmSize(h1->hashAlgorithm));
}
int ocpp_cert_bytes_to_hex(char *dst, size_t dst_size, const unsigned char *src, size_t src_len) {
if (!dst || !dst_size || !src) {
return -1;
}
dst[0] = '\0';
size_t hexLen = 2 * src_len; // hex-encoding needs two characters per byte
if (dst_size < hexLen + 1) { // buf will hold hex-encoding + terminating null
return -1;
}
for (size_t i = 0; i < src_len; i++) {
snprintf(dst, 3, "%02X", src[i]);
dst += 2;
}
return (int)hexLen;
}
int ocpp_cert_print_issuerNameHash(const ocpp_cert_hash *src, char *buf, size_t size) {
return ocpp_cert_bytes_to_hex(buf, size, src->issuerNameHash, HashAlgorithmSize(src->hashAlgorithm));
}
int ocpp_cert_print_issuerKeyHash(const ocpp_cert_hash *src, char *buf, size_t size) {
return ocpp_cert_bytes_to_hex(buf, size, src->issuerKeyHash, HashAlgorithmSize(src->hashAlgorithm));
}
int ocpp_cert_print_serialNumber(const ocpp_cert_hash *src, char *buf, size_t size) {
if (!buf || !size) {
return -1;
}
buf[0] = '\0';
if (!src->serialNumberLen) {
return 0;
}
int hexLen = snprintf(buf, size, "%X", src->serialNumber[0]);
if (hexLen < 0 || (size_t)hexLen >= size) {
return -1;
}
if (src->serialNumberLen > 1) {
auto ret = ocpp_cert_bytes_to_hex(buf + (size_t)hexLen, size - (size_t)hexLen, src->serialNumber + 1, src->serialNumberLen - 1);
if (ret < 0) {
return -1;
}
hexLen += ret;
}
return hexLen;
}
int ocpp_cert_hex_to_bytes(unsigned char *dst, size_t dst_size, const char *hex_src) {
if (!dst || !dst_size || !hex_src) {
return -1;
}
dst[0] = '\0';
size_t hex_len = strlen(hex_src);
size_t write_len = (hex_len + 1) / 2;
if (dst_size < write_len) {
return -1;
}
for (size_t i = 0; i < write_len; i++) {
char octet [2];
if (i == 0 && hex_len % 2) {
octet[0] = '0';
octet[1] = hex_src[2*i];
} else {
octet[0] = hex_src[2*i];
octet[1] = hex_src[2*i + 1];
}
unsigned char val = 0;
for (size_t j = 0; j < 2; j++) {
char c = octet[j];
if (c >= '0' && c <= '9') {
val += c - '0';
} else if (c >= 'A' && c <= 'F') {
val += (c - 'A') + 0xA;
} else if (c >= 'a' && c <= 'f') {
val += (c - 'a') + 0xA;
} else {
return -1;
}
if (j == 0) {
val *= 0x10;
}
}
dst[i] = val;
}
return (int)write_len;
}
int ocpp_cert_set_issuerNameHash(ocpp_cert_hash *dst, const char *hex_src, HashAlgorithmType hash_algorithm) {
auto ret = ocpp_cert_hex_to_bytes(dst->issuerNameHash, sizeof(dst->issuerNameHash), hex_src);
if (ret < 0) {
return ret;
}
if (ret != HashAlgorithmSize(hash_algorithm)) {
return -1;
}
return ret;
}
int ocpp_cert_set_issuerKeyHash(ocpp_cert_hash *dst, const char *hex_src, HashAlgorithmType hash_algorithm) {
auto ret = ocpp_cert_hex_to_bytes(dst->issuerKeyHash, sizeof(dst->issuerNameHash), hex_src);
if (ret < 0) {
return ret;
}
if (ret != HashAlgorithmSize(hash_algorithm)) {
return -1;
}
return ret;
}
int ocpp_cert_set_serialNumber(ocpp_cert_hash *dst, const char *hex_src) {
auto ret = ocpp_cert_hex_to_bytes(dst->serialNumber, sizeof(dst->serialNumber), hex_src);
if (ret < 0) {
return ret;
}
dst->serialNumberLen = (size_t)ret;
return ret;
}
#endif //MO_ENABLE_CERT_MGMT