Skip to content

Commit a16ce8c

Browse files
guillep2ksandeepmistry
authored andcommitted
Fixes to various integer encoding
1 parent d9a708e commit a16ce8c

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/utility/ASN1Utils.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ int ASN1UtilsClass::signatureLength(const byte signature[])
7979
int rLength = 32;
8080
int sLength = 32;
8181

82-
while (*r == 0x00 && rLength) {
82+
while (*r == 0x00 && rLength > 1) {
8383
r++;
8484
rLength--;
8585
}
@@ -88,7 +88,7 @@ int ASN1UtilsClass::signatureLength(const byte signature[])
8888
rLength++;
8989
}
9090

91-
while (*s == 0x00 && sLength) {
91+
while (*s == 0x00 && sLength > 1) {
9292
s++;
9393
sLength--;
9494
}
@@ -102,12 +102,12 @@ int ASN1UtilsClass::signatureLength(const byte signature[])
102102

103103
int ASN1UtilsClass::serialNumberLength(const byte serialNumber[], int length)
104104
{
105-
while (*serialNumber == 0 && length) {
105+
while (*serialNumber == 0 && length > 1) {
106106
serialNumber++;
107107
length--;
108108
}
109109

110-
if (length && *serialNumber & 0x80) {
110+
if (*serialNumber & 0x80) {
111111
length++;
112112
}
113113

@@ -233,12 +233,12 @@ int ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
233233
int rLength = 32;
234234
int sLength = 32;
235235

236-
while (*r == 0 && rLength) {
236+
while (*r == 0 && rLength > 1) {
237237
r++;
238238
rLength--;
239239
}
240240

241-
while (*s == 0 && sLength) {
241+
while (*s == 0 && sLength > 1) {
242242
s++;
243243
sLength--;
244244
}
@@ -260,45 +260,57 @@ int ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
260260

261261
*out++ = ASN1_INTEGER;
262262
*out++ = rLength;
263-
if ((*r & 0x80) && rLength) {
263+
if (*r & 0x80) {
264264
*out++ = 0;
265265
rLength--;
266266
}
267267
memcpy(out, r, rLength);
268268
out += rLength;
269269

270+
if (*r & 0x80) {
271+
rLength++;
272+
}
273+
270274
*out++ = ASN1_INTEGER;
271275
*out++ = sLength;
272-
if ((*s & 0x80) && sLength) {
276+
if (*s & 0x80) {
273277
*out++ = 0;
274278
sLength--;
275279
}
276280
memcpy(out, s, sLength);
277-
out += rLength;
278-
281+
out += sLength;
282+
283+
if (*s & 0x80) {
284+
sLength++;
285+
}
286+
279287
return (21 + rLength + sLength);
280288
}
281289

282290
int ASN1UtilsClass::appendSerialNumber(const byte serialNumber[], int length, byte out[])
283291
{
284-
while (*serialNumber == 0 && length) {
292+
while (*serialNumber == 0 && length > 1) {
285293
serialNumber++;
286294
length--;
287295
}
288296

289-
if (length && *serialNumber & 0x80) {
297+
if (*serialNumber & 0x80) {
290298
length++;
291299
}
292300

293301
*out++ = ASN1_INTEGER;
294302
*out++ = length;
295303

296-
if (length && *serialNumber & 0x80) {
304+
if (*serialNumber & 0x80) {
297305
*out++ = 0x00;
298306
length--;
299307
}
300308

301309
memcpy(out, serialNumber, length);
310+
311+
if (*serialNumber & 0x80) {
312+
length++;
313+
}
302314

303315
return (2 + length);
304316
}

0 commit comments

Comments
 (0)