@@ -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+
103117int 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+
264300int 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+
349437ASN1UtilsClass ASN1Utils;
0 commit comments