Skip to content

Commit 344095e

Browse files
committed
More ASN.1 utils
1 parent 778344b commit 344095e

File tree

3 files changed

+129
-33
lines changed

3 files changed

+129
-33
lines changed

src/utility/ASN1Utils.cpp

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ int ASN1UtilsClass::versionLength()
2424
return 3;
2525
}
2626

27-
int ASN1UtilsClass::subjectLength(const String& countryName,
28-
const String& stateProvinceName,
29-
const String& localityName,
30-
const String& organizationName,
31-
const String& organizationalUnitName,
32-
const String& commonName)
27+
int ASN1UtilsClass::issuerOrSubjectLength(const String& countryName,
28+
const String& stateProvinceName,
29+
const String& localityName,
30+
const String& organizationName,
31+
const String& organizationalUnitName,
32+
const String& commonName)
3333
{
3434
int length = 0;
3535
int countryNameLength = countryName.length();
@@ -100,6 +100,20 @@ int ASN1UtilsClass::signatureLength(const byte signature[])
100100
return (21 + rLength + sLength);
101101
}
102102

103+
int ASN1UtilsClass::serialNumberLength(const byte serialNumber[], int length)
104+
{
105+
while (*serialNumber == 0 && length) {
106+
serialNumber++;
107+
length--;
108+
}
109+
110+
if (length && *serialNumber & 0x80) {
111+
length++;
112+
}
113+
114+
return (2 + length);
115+
}
116+
103117
int ASN1UtilsClass::sequenceHeaderLength(int length)
104118
{
105119
if (length > 255) {
@@ -118,13 +132,13 @@ void ASN1UtilsClass::appendVersion(int version, byte out[])
118132
out[2] = version;
119133
}
120134

121-
void ASN1UtilsClass::appendSubject(const String& countryName,
122-
const String& stateProvinceName,
123-
const String& localityName,
124-
const String& organizationName,
125-
const String& organizationalUnitName,
126-
const String& commonName,
127-
byte out[])
135+
void ASN1UtilsClass::appendIssuerOrSubject(const String& countryName,
136+
const String& stateProvinceName,
137+
const String& localityName,
138+
const String& organizationName,
139+
const String& organizationalUnitName,
140+
const String& commonName,
141+
byte out[])
128142
{
129143
if (countryName.length() > 0) {
130144
out += appendName(countryName, 0x06, out);
@@ -261,6 +275,28 @@ void ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
261275
out += rLength;
262276
}
263277

278+
void ASN1UtilsClass::appendSerialNumber(const byte serialNumber[], int length, byte out[])
279+
{
280+
while (*serialNumber == 0 && length) {
281+
serialNumber++;
282+
length--;
283+
}
284+
285+
if (length && *serialNumber & 0x80) {
286+
length++;
287+
}
288+
289+
*out++ = ASN1_INTEGER;
290+
*out++ = length;
291+
292+
if (length && *serialNumber & 0x80) {
293+
*out++ = 0x00;
294+
length--;
295+
}
296+
297+
memcpy(out, serialNumber, length);
298+
}
299+
264300
int ASN1UtilsClass::appendName(const String& name, int type, byte out[])
265301
{
266302
int nameLength = name.length();
@@ -346,4 +382,56 @@ String ASN1UtilsClass::base64Encode(const byte in[], unsigned int length, const
346382
return out;
347383
}
348384

385+
int ASN1UtilsClass::appendDate(int year, int month, int day, int hour, int minute, int second, byte out[])
386+
{
387+
bool useGeneralizedTime = (year > 2049);
388+
389+
if (useGeneralizedTime) {
390+
*out++ = 0x18;
391+
*out++ = 0x0f;
392+
*out++ = '0' + (year / 1000);
393+
*out++ = '0' + ((year % 1000) / 100);
394+
*out++ = '0' + ((year % 100) / 10);
395+
*out++ = '0' + (year % 10);
396+
} else {
397+
year -= 2000;
398+
399+
*out++ = 0x17;
400+
*out++ = 0x0d;
401+
*out++ = '0' + (year / 10);
402+
*out++ = '0' + (year % 10);
403+
}
404+
*out++ = '0' + (month / 10);
405+
*out++ = '0' + (month % 10);
406+
*out++ = '0' + (day / 10);
407+
*out++ = '0' + (day % 10);
408+
*out++ = '0' + (hour / 10);
409+
*out++ = '0' + (hour % 10);
410+
*out++ = '0' + (minute / 10);
411+
*out++ = '0' + (minute % 10);
412+
*out++ = '0' + (second / 10);
413+
*out++ = '0' + (second % 10);
414+
*out++ = 0x5a; // UTC
415+
416+
return (useGeneralizedTime ? 17 : 15);
417+
}
418+
419+
int ASN1UtilsClass::appendEcdsaWithSHA256(byte out[])
420+
{
421+
*out++ = ASN1_SEQUENCE;
422+
*out++ = 0x0A;
423+
*out++ = ASN1_OBJECT_IDENTIFIER;
424+
*out++ = 0x08;
425+
*out++ = 0x2A;
426+
*out++ = 0x86;
427+
*out++ = 0x48;
428+
*out++ = 0xCE;
429+
*out++ = 0x3D;
430+
*out++ = 0x04;
431+
*out++ = 0x03;
432+
*out++ = 0x02;
433+
434+
return 12;
435+
}
436+
349437
ASN1UtilsClass ASN1Utils;

src/utility/ASN1Utils.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ASN1UtilsClass {
3434
public:
3535
int versionLength();
3636

37-
int subjectLength(const String& countryName,
37+
int issuerOrSubjectLength(const String& countryName,
3838
const String& stateProvinceName,
3939
const String& localityName,
4040
const String& organizationName,
@@ -45,26 +45,34 @@ class ASN1UtilsClass {
4545

4646
int signatureLength(const byte signature[]);
4747

48+
int serialNumberLength(const byte serialNumber[], int length);
49+
4850
int sequenceHeaderLength(int length);
4951

5052
void appendVersion(int version, byte out[]);
5153

52-
void appendSubject(const String& countryName,
53-
const String& stateProvinceName,
54-
const String& localityName,
55-
const String& organizationName,
56-
const String& organizationalUnitName,
57-
const String& commonName,
58-
byte out[]);
54+
void appendIssuerOrSubject(const String& countryName,
55+
const String& stateProvinceName,
56+
const String& localityName,
57+
const String& organizationName,
58+
const String& organizationalUnitName,
59+
const String& commonName,
60+
byte out[]);
5961

6062
void appendPublicKey(const byte publicKey[], byte out[]);
6163

6264
void appendSignature(const byte signature[], byte out[]);
6365

66+
void appendSerialNumber(const byte serialNumber[], int length, byte out[]);
67+
6468
int appendName(const String& name, int type, byte out[]);
6569

6670
void appendSequenceHeader(int length, byte out[]);
6771

72+
int appendDate(int year, int month, int day, int hour, int minute, int second, byte out[]);
73+
74+
int appendEcdsaWithSHA256(byte out[]);
75+
6876
String base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix);
6977
};
7078

src/utility/ECCX08CSR.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ int ECCX08CSRClass::begin(int slot, bool newPrivateKey)
5151
String ECCX08CSRClass::end()
5252
{
5353
int versionLen = ASN1Utils.versionLength();
54-
int subjectLen = ASN1Utils.subjectLength(_countryName,
55-
_stateProvinceName,
56-
_localityName,
57-
_organizationName,
58-
_organizationalUnitName,
59-
_commonName);
54+
int subjectLen = ASN1Utils.issuerOrSubjectLength(_countryName,
55+
_stateProvinceName,
56+
_localityName,
57+
_organizationName,
58+
_organizationalUnitName,
59+
_commonName);
6060
int subjectHeaderLen = ASN1Utils.sequenceHeaderLength(subjectLen);
6161
int publicKeyLen = ASN1Utils.publicKeyLength();
6262

@@ -76,12 +76,12 @@ String ECCX08CSRClass::end()
7676
// subject
7777
ASN1Utils.appendSequenceHeader(subjectLen, out);
7878
out += subjectHeaderLen;
79-
ASN1Utils.appendSubject(_countryName,
80-
_stateProvinceName,
81-
_localityName,
82-
_organizationName,
83-
_organizationalUnitName,
84-
_commonName, out);
79+
ASN1Utils.appendIssuerOrSubject(_countryName,
80+
_stateProvinceName,
81+
_localityName,
82+
_organizationName,
83+
_organizationalUnitName,
84+
_commonName, out);
8585
out += subjectLen;
8686

8787
// public key

0 commit comments

Comments
 (0)