@@ -24,12 +24,12 @@ int ASN1UtilsClass::versionLength()
24
24
return 3 ;
25
25
}
26
26
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)
33
33
{
34
34
int length = 0 ;
35
35
int countryNameLength = countryName.length ();
@@ -100,6 +100,20 @@ int ASN1UtilsClass::signatureLength(const byte signature[])
100
100
return (21 + rLength + sLength );
101
101
}
102
102
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
+
103
117
int ASN1UtilsClass::sequenceHeaderLength (int length)
104
118
{
105
119
if (length > 255 ) {
@@ -118,13 +132,13 @@ void ASN1UtilsClass::appendVersion(int version, byte out[])
118
132
out[2 ] = version;
119
133
}
120
134
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[])
128
142
{
129
143
if (countryName.length () > 0 ) {
130
144
out += appendName (countryName, 0x06 , out);
@@ -261,6 +275,28 @@ void ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
261
275
out += rLength;
262
276
}
263
277
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
+
264
300
int ASN1UtilsClass::appendName (const String& name, int type, byte out[])
265
301
{
266
302
int nameLength = name.length ();
@@ -346,4 +382,56 @@ String ASN1UtilsClass::base64Encode(const byte in[], unsigned int length, const
346
382
return out;
347
383
}
348
384
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
+
349
437
ASN1UtilsClass ASN1Utils;
0 commit comments