aboutsummaryrefslogtreecommitdiffstats
path: root/src/scepclient/scep.c
diff options
context:
space:
mode:
authorMartin Willi <martin@revosec.ch>2013-02-13 12:06:09 +0100
committerMartin Willi <martin@revosec.ch>2013-02-14 16:56:32 +0100
commit46ae05dd174403d406fcfee2328a2b3b6f4c0eff (patch)
tree346c251a3a10bae493f73a46dd31acd666fa9680 /src/scepclient/scep.c
parent96a2d2077b51eee9d2174b1cc3dc2a5616637c86 (diff)
downloadstrongswan-46ae05dd174403d406fcfee2328a2b3b6f4c0eff.tar.bz2
strongswan-46ae05dd174403d406fcfee2328a2b3b6f4c0eff.tar.xz
Remove leading zeros in SCEP certificate serialNumbers
Diffstat (limited to 'src/scepclient/scep.c')
-rw-r--r--src/scepclient/scep.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/scepclient/scep.c b/src/scepclient/scep.c
index 974eb9862..3fdcd6c28 100644
--- a/src/scepclient/scep.c
+++ b/src/scepclient/scep.c
@@ -151,8 +151,7 @@ void scep_generate_transaction_id(public_key_t *key, chunk_t *transID,
chunk_t digest = chunk_alloca(HASH_SIZE_MD5);
chunk_t keyEncoding = chunk_empty, keyInfo;
hasher_t *hasher;
- bool msb_set;
- u_char *pos;
+ int zeros = 0, msb_set = 0;
key->get_encoding(key, PUBKEY_ASN1_DER, &keyEncoding);
@@ -168,20 +167,27 @@ void scep_generate_transaction_id(public_key_t *key, chunk_t *transID,
DESTROY_IF(hasher);
free(keyInfo.ptr);
- /* is the most significant bit of the digest set? */
- msb_set = (*digest.ptr & 0x80) == 0x80;
-
- /* allocate space for the serialNumber */
- serialNumber->len = msb_set + digest.len;
- serialNumber->ptr = malloc(serialNumber->len);
-
- /* the serial number as the two's complement of the digest */
- pos = serialNumber->ptr;
+ /* the serialNumber should be valid ASN1 integer content:
+ * remove leading zeros, add one if MSB is set (two's complement) */
+ while (zeros < digest.len)
+ {
+ if (digest.ptr[zeros])
+ {
+ if (digest.ptr[zeros] & 0x80)
+ {
+ msb_set = 1;
+ }
+ break;
+ }
+ zeros++;
+ }
+ *serialNumber = chunk_alloc(digest.len - zeros + msb_set);
if (msb_set)
{
- *pos++ = 0x00;
+ serialNumber->ptr[0] = 0x00;
}
- memcpy(pos, digest.ptr, digest.len);
+ memcpy(serialNumber->ptr + msb_set, digest.ptr + zeros,
+ digest.len - zeros);
/* the transaction id is the serial number in hex format */
*transID = chunk_to_hex(digest, NULL, TRUE);