@@ -24,10 +24,19 @@ # include #endif #ifdef HAVE_PTHREAD_H # include #endif +#ifdef HAVE_ZLIB_H +# ifdef HAVE_LIBZ +# include +# endif +#else +# ifdef HAVE_LIBZ +# undef HAVE_LIBZ +# endif +#endif #define CK_PTR * #define CK_DEFINE_FUNCTION(returnType, name) returnType name #define CK_DECLARE_FUNCTION(returnType, name) returnType name #define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name) @@ -1363,10 +1372,12 @@ ssize_t tlen, vlen; ssize_t read_ret; size_t offset_t = 0, offset_v = 0; unsigned char tag; size_t length; + uLongf tmpbuflen; + int uncompress_ret; CACKEY_DEBUG_PRINTF("Called."); read_ret = cackey_read_buffer(slot, tlen_buf, sizeof(tlen_buf), 1, offset_t); if (read_ret != sizeof(tlen_buf)) { @@ -1386,14 +1397,11 @@ vlen = (vlen_buf[1] << 8) | vlen_buf[0]; CACKEY_DEBUG_PRINTF("Tag Length = %i, Value Length = %i", tlen, vlen); - tlen -= 2; offset_t += 2; - - vlen -= 2; offset_v += 2; if (tlen > sizeof(tval_buf)) { CACKEY_DEBUG_PRINTF("Tag length is too large, returning in failure"); @@ -1437,12 +1445,10 @@ tlen--; } CACKEY_DEBUG_PRINTF("Tag: %s (%02x)", CACKEY_DEBUG_FUNC_TAG_TO_STR(tag), (unsigned int) tag); CACKEY_DEBUG_PRINTBUF("Value:", vval, length); - vval += length; - vlen -= length; curr_entity = NULL; switch (tag) { case GSCIS_TAG_CARDURL: curr_entity = malloc(sizeof(*curr_entity)); @@ -1469,16 +1475,34 @@ curr_entity->_next = NULL; break; case GSCIS_TAG_CERTIFICATE: curr_entity = malloc(sizeof(*curr_entity)); - tmpbuf = malloc(length); + tmpbuflen = length * 2; + tmpbuf = malloc(tmpbuflen); + +#ifdef HAVE_LIBZ + CACKEY_DEBUG_PRINTBUF("Decompressing:", vval, length); + uncompress_ret = uncompress(tmpbuf, &tmpbuflen, vval, length); + if (uncompress_ret != Z_OK) { + CACKEY_DEBUG_PRINTF("Failed to decompress, uncompress() returned %i -- resorting to direct copy", uncompress_ret); + + tmpbuflen = length; + memcpy(tmpbuf, vval, length); + } + + CACKEY_DEBUG_PRINTBUF("Decompressed to:", tmpbuf, tmpbuflen); +#else + CACKEY_DEBUG_PRINTF("Missing ZLIB Support, this certificate is likely useless..."); + + tmpbuflen = length; memcpy(tmpbuf, vval, length); +#endif curr_entity->tag = tag; - curr_entity->length = length; + curr_entity->length = tmpbuflen; curr_entity->value = tmpbuf; curr_entity->_next = NULL; break; case GSCIS_TAG_PKCS15: @@ -1488,10 +1512,13 @@ curr_entity->value_byte = vval[0]; curr_entity->_next = NULL; break; } + + vval += length; + vlen -= length; if (curr_entity != NULL) { if (root == NULL) { root = curr_entity; }