Skip to content

Commit 241ce99

Browse files
committed
Split out PEM utils, return append length for some ASN.1 utils
1 parent 344095e commit 241ce99

File tree

5 files changed

+125
-59
lines changed

5 files changed

+125
-59
lines changed

src/utility/ASN1Utils.cpp

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void ASN1UtilsClass::appendIssuerOrSubject(const String& countryName,
165165
}
166166
}
167167

168-
void ASN1UtilsClass::appendPublicKey(const byte publicKey[], byte out[])
168+
int ASN1UtilsClass::appendPublicKey(const byte publicKey[], byte out[])
169169
{
170170
int subjectPublicKeyDataLength = 2 + 9 + 10 + 4 + 64;
171171

@@ -205,9 +205,11 @@ void ASN1UtilsClass::appendPublicKey(const byte publicKey[], byte out[])
205205
*out++ = 0x04;
206206

207207
memcpy(out, publicKey, 64);
208+
209+
return (2 + subjectPublicKeyDataLength);
208210
}
209211

210-
void ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
212+
int ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
211213
{
212214
// signature algorithm
213215
*out++ = ASN1_SEQUENCE;
@@ -273,9 +275,11 @@ void ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
273275
}
274276
memcpy(out, s, sLength);
275277
out += rLength;
278+
279+
return (21 + rLength + sLength);
276280
}
277281

278-
void ASN1UtilsClass::appendSerialNumber(const byte serialNumber[], int length, byte out[])
282+
int ASN1UtilsClass::appendSerialNumber(const byte serialNumber[], int length, byte out[])
279283
{
280284
while (*serialNumber == 0 && length) {
281285
serialNumber++;
@@ -295,6 +299,8 @@ void ASN1UtilsClass::appendSerialNumber(const byte serialNumber[], int length, b
295299
}
296300

297301
memcpy(out, serialNumber, length);
302+
303+
return (2 + length);
298304
}
299305

300306
int ASN1UtilsClass::appendName(const String& name, int type, byte out[])
@@ -320,7 +326,7 @@ int ASN1UtilsClass::appendName(const String& name, int type, byte out[])
320326
return (nameLength + 11);
321327
}
322328

323-
void ASN1UtilsClass::appendSequenceHeader(int length, byte out[])
329+
int ASN1UtilsClass::appendSequenceHeader(int length, byte out[])
324330
{
325331
*out++ = ASN1_SEQUENCE;
326332
if (length > 255) {
@@ -330,56 +336,14 @@ void ASN1UtilsClass::appendSequenceHeader(int length, byte out[])
330336
*out++ = 0x81;
331337
}
332338
*out++ = (length) & 0xff;
333-
}
334-
335-
336-
String ASN1UtilsClass::base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix)
337-
{
338-
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
339-
340-
int b;
341-
String out;
342-
343-
int reserveLength = 4 * ((length + 2) / 3) + ((length / 3 * 4) / 76) + strlen(prefix) + strlen(suffix);
344-
out.reserve(reserveLength);
345339

346-
if (prefix) {
347-
out += prefix;
348-
}
349-
350-
for (unsigned int i = 0; i < length; i += 3) {
351-
if (i > 0 && (i / 3 * 4) % 76 == 0) {
352-
out += '\n';
353-
}
354-
355-
b = (in[i] & 0xFC) >> 2;
356-
out += CODES[b];
357-
358-
b = (in[i] & 0x03) << 4;
359-
if (i + 1 < length) {
360-
b |= (in[i + 1] & 0xF0) >> 4;
361-
out += CODES[b];
362-
b = (in[i + 1] & 0x0F) << 2;
363-
if (i + 2 < length) {
364-
b |= (in[i + 2] & 0xC0) >> 6;
365-
out += CODES[b];
366-
b = in[i + 2] & 0x3F;
367-
out += CODES[b];
368-
} else {
369-
out += CODES[b];
370-
out += '=';
371-
}
372-
} else {
373-
out += CODES[b];
374-
out += "==";
375-
}
376-
}
377-
378-
if (suffix) {
379-
out += suffix;
340+
if (length > 255) {
341+
return 4;
342+
} else if (length > 127) {
343+
return 3;
344+
} else {
345+
return 2;
380346
}
381-
382-
return out;
383347
}
384348

385349
int ASN1UtilsClass::appendDate(int year, int month, int day, int hour, int minute, int second, byte out[])

src/utility/ASN1Utils.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,19 @@ class ASN1UtilsClass {
5959
const String& commonName,
6060
byte out[]);
6161

62-
void appendPublicKey(const byte publicKey[], byte out[]);
62+
int appendPublicKey(const byte publicKey[], byte out[]);
6363

64-
void appendSignature(const byte signature[], byte out[]);
64+
int appendSignature(const byte signature[], byte out[]);
6565

66-
void appendSerialNumber(const byte serialNumber[], int length, byte out[]);
66+
int appendSerialNumber(const byte serialNumber[], int length, byte out[]);
6767

6868
int appendName(const String& name, int type, byte out[]);
6969

70-
void appendSequenceHeader(int length, byte out[]);
70+
int appendSequenceHeader(int length, byte out[]);
7171

7272
int appendDate(int year, int month, int day, int hour, int minute, int second, byte out[]);
7373

7474
int appendEcdsaWithSHA256(byte out[]);
75-
76-
String base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix);
7775
};
7876

7977
extern ASN1UtilsClass ASN1Utils;

src/utility/ECCX08CSR.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ArduinoECCX08.h"
2121

2222
#include "ASN1Utils.h"
23+
#include "PEMUtils.h"
2324

2425
#include "ECCX08CSR.h"
2526

@@ -139,7 +140,7 @@ String ECCX08CSRClass::end()
139140
ASN1Utils.appendSignature(signature, out);
140141
out += signatureLen;
141142

142-
return ASN1Utils.base64Encode(csr, csrLen + csrHeaderLen, "-----BEGIN CERTIFICATE REQUEST-----\n", "\n-----END CERTIFICATE REQUEST-----\n");
143+
return PEMUtils.base64Encode(csr, csrLen + csrHeaderLen, "-----BEGIN CERTIFICATE REQUEST-----\n", "\n-----END CERTIFICATE REQUEST-----\n");
143144
}
144145

145146
void ECCX08CSRClass::setCountryName(const char *countryName)

src/utility/PEMUtils.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
This file is part of the ArduinoECCX08 library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "PEMUtils.h"
21+
22+
String PEMUtilsClass::base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix)
23+
{
24+
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
25+
26+
int b;
27+
String out;
28+
29+
int reserveLength = 4 * ((length + 2) / 3) + ((length / 3 * 4) / 76) + strlen(prefix) + strlen(suffix);
30+
out.reserve(reserveLength);
31+
32+
if (prefix) {
33+
out += prefix;
34+
}
35+
36+
for (unsigned int i = 0; i < length; i += 3) {
37+
if (i > 0 && (i / 3 * 4) % 76 == 0) {
38+
out += '\n';
39+
}
40+
41+
b = (in[i] & 0xFC) >> 2;
42+
out += CODES[b];
43+
44+
b = (in[i] & 0x03) << 4;
45+
if (i + 1 < length) {
46+
b |= (in[i + 1] & 0xF0) >> 4;
47+
out += CODES[b];
48+
b = (in[i + 1] & 0x0F) << 2;
49+
if (i + 2 < length) {
50+
b |= (in[i + 2] & 0xC0) >> 6;
51+
out += CODES[b];
52+
b = in[i + 2] & 0x3F;
53+
out += CODES[b];
54+
} else {
55+
out += CODES[b];
56+
out += '=';
57+
}
58+
} else {
59+
out += CODES[b];
60+
out += "==";
61+
}
62+
}
63+
64+
if (suffix) {
65+
out += suffix;
66+
}
67+
68+
return out;
69+
}
70+
71+
PEMUtilsClass PEMUtils;

src/utility/PEMUtils.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
This file is part of the ArduinoECCX08 library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _PEM_UTILS_H_
21+
#define _PEM_UTILS_H_
22+
23+
#include <Arduino.h>
24+
25+
class PEMUtilsClass {
26+
public:
27+
String base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix);
28+
};
29+
30+
extern PEMUtilsClass PEMUtils;
31+
32+
#endif

0 commit comments

Comments
 (0)