Skip to content

Instantly share code, notes, and snippets.

@vletoux
Created April 6, 2015 14:49
Show Gist options
  • Save vletoux/b445f6fb3bf54263ce2b to your computer and use it in GitHub Desktop.
Save vletoux/b445f6fb3bf54263ce2b to your computer and use it in GitHub Desktop.
@@ -2696,10 +2696,11 @@ sc_pkcs15_get_object_guid(struct sc_pkcs15_card *p15card, const struct sc_pkcs15
struct sc_context *ctx = p15card->card->ctx;
struct sc_serial_number serialnr;
struct sc_pkcs15_id id;
unsigned char guid_bin[SC_PKCS15_MAX_ID_SIZE + SC_MAX_SERIALNR];
int rv;
+ int inputSize;
LOG_FUNC_CALLED(ctx);
if(!out || !out_size)
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
@@ -2746,20 +2747,27 @@ sc_pkcs15_get_object_guid(struct sc_pkcs15_card *p15card, const struct sc_pkcs15
}
memset(guid_bin, 0, sizeof(guid_bin));
memcpy(guid_bin, id.value, id.len);
memcpy(guid_bin + id.len, serialnr.value, serialnr.len);
-
- // If OpenSSL is available (SHA1), then rather use the hash of the data
- // - this also protects against data being too short
+ inputSize = id.len + serialnr.len;
+ // If OpenSSL is available (SHA1), then rather use the hash of the data
+ // - this also protects against data being too short
#ifdef ENABLE_OPENSSL
- SHA1(guid_bin, id.len + serialnr.len, guid_bin);
- id.len = SHA_DIGEST_LENGTH;
- serialnr.len = 0;
+ SHA1(guid_bin, inputSize, guid_bin);
+ inputSize = SHA_DIGEST_LENGTH;
+#else
+ if (inputSize < 16)
+ {
+ /* guid_bin has a size larger than 16 bytes
+ force the remaining bytes up to 16 bytes to be zero
+ so sc_pkcs15_serialize_guid won't fail because the size is less than 16*/
+ inputSize = 16;
+ }
#endif
- rv = sc_pkcs15_serialize_guid(guid_bin, id.len + serialnr.len, flags, (char *)out, *out_size);
+ rv = sc_pkcs15_serialize_guid(guid_bin, inputSize, flags, (char *)out, *out_size);
LOG_TEST_RET(ctx, rv, "Serialize GUID error");
*out_size = strlen((char *)out);
LOG_FUNC_RETURN(ctx, rv);
}
@dengert
Copy link

dengert commented Apr 6, 2015

define SC_PKCS15_MAX_ID_SIZE 255

define SC_MAX_SERIALNR 32

Without some combination of id and serial number, the resulting GUID could contain only the ID if the ID is long. if id_len/2 > out_size (if I did the math correctly.)
out_size is 40 bytes.
I believe his could result in guids that are the same for multiple cards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment