static BOOL check_buffer_resize(char **ptr_buf, size_t *buf_size, size_t check_size) { if (check_size > *buf_size) { *buf_size = check_size; if (*ptr_buf) { char *realloc_buf = CryptMemRealloc(*ptr_buf, *buf_size); if (!realloc_buf) return FALSE; *ptr_buf = realloc_buf; } else { *ptr_buf = CryptMemAlloc(*buf_size); if (!*ptr_buf) return FALSE; } } return TRUE; }
static void test_cryptAllocate(void) { LPVOID buf; buf = CryptMemAlloc(0); ok(buf != NULL, "CryptMemAlloc failed: %08x\n", GetLastError()); CryptMemFree(buf); /* CryptMemRealloc(NULL, 0) fails pre-Vista */ buf = CryptMemAlloc(0); buf = CryptMemRealloc(buf, 1); ok(buf != NULL, "CryptMemRealloc failed: %08x\n", GetLastError()); CryptMemFree(buf); }
static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info, PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError) { BOOL ret = FALSE; TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID), debugstr_wn(value->start, value->end - value->start)); if (!info->rgRDN) info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN)); else info->rgRDN = CryptMemRealloc(info->rgRDN, (info->cRDN + 1) * sizeof(CERT_RDN)); if (info->rgRDN) { /* FIXME: support multiple RDN attrs */ info->rgRDN[info->cRDN].rgRDNAttr = CryptMemAlloc(sizeof(CERT_RDN_ATTR)); if (info->rgRDN[info->cRDN].rgRDNAttr) { static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING, CERT_RDN_BMP_STRING, 0 }; const DWORD *types; info->rgRDN[info->cRDN].cRDNAttr = 1; info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId = (LPSTR)keyOID->pszOID; info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType = CERT_RDN_ENCODED_BLOB; if (keyOID->ExtraInfo.cbData) types = (const DWORD *)keyOID->ExtraInfo.pbData; else types = defaultTypes; /* Remove surrounding quotes */ if (value->start[0] == '"') { value->start++; value->end--; } ret = CRYPT_EncodeValue(dwCertEncodingType, value, &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError); } else SetLastError(ERROR_OUTOFMEMORY); info->cRDN++; } else SetLastError(ERROR_OUTOFMEMORY); return ret; }
static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper, struct X500TokenW *key) { DWORD len = key->end - key->start; if (len > keeper->keyLen) { if (keeper->keyName == keeper->buf) keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR)); else keeper->keyName = CryptMemRealloc(keeper->keyName, len * sizeof(WCHAR)); keeper->keyLen = len; } memcpy(keeper->keyName, key->start, (key->end - key->start) * sizeof(WCHAR)); keeper->keyName[len] = '\0'; TRACE("Keyname is %s\n", debugstr_w(keeper->keyName)); }
static BOOL add_line_to_buffer(struct DynamicBuffer *buffer, LPCSTR line) { BOOL ret; if (buffer->used + strlen(line) + 1 > buffer->allocated) { if (!buffer->allocated) { buffer->data = CryptMemAlloc(INITIAL_CERT_BUFFER); if (buffer->data) { buffer->data[0] = 0; buffer->allocated = INITIAL_CERT_BUFFER; } } else { DWORD new_size = max(buffer->allocated * 2, buffer->used + strlen(line) + 1); buffer->data = CryptMemRealloc(buffer->data, new_size); if (buffer->data) buffer->allocated = new_size; } } if (buffer->data) { strcpy((char *)buffer->data + strlen((char *)buffer->data), line); /* Not strlen + 1, otherwise we'd count the NULL for every line's * addition (but we overwrite the previous NULL character.) Not an * overrun, we allocate strlen + 1 bytes above. */ buffer->used += strlen(line); ret = TRUE; } else ret = FALSE; return ret; }
static BOOL CRYPT_SerializeStoreElement(const void *context, const BYTE *encodedContext, DWORD cbEncodedContext, DWORD contextPropID, PCWINE_CONTEXT_INTERFACE contextInterface, DWORD dwFlags, BOOL omitHashes, BYTE *pbElement, DWORD *pcbElement) { BOOL ret; TRACE("(%p, %p, %08x, %d, %p, %p)\n", context, contextInterface, dwFlags, omitHashes, pbElement, pcbElement); if (context) { DWORD bytesNeeded = sizeof(WINE_CERT_PROP_HEADER) + cbEncodedContext; DWORD prop = 0; ret = TRUE; do { prop = contextInterface->enumProps(context, prop); if (prop && (!omitHashes || !IS_CERT_HASH_PROP_ID(prop))) { DWORD propSize = 0; ret = contextInterface->getProp(context, prop, NULL, &propSize); if (ret) bytesNeeded += sizeof(WINE_CERT_PROP_HEADER) + propSize; } } while (ret && prop != 0); if (!pbElement) { *pcbElement = bytesNeeded; ret = TRUE; } else if (*pcbElement < bytesNeeded) { *pcbElement = bytesNeeded; SetLastError(ERROR_MORE_DATA); ret = FALSE; } else { PWINE_CERT_PROP_HEADER hdr; DWORD bufSize = 0; LPBYTE buf = NULL; prop = 0; do { prop = contextInterface->enumProps(context, prop); if (prop && (!omitHashes || !IS_CERT_HASH_PROP_ID(prop))) { DWORD propSize = 0; ret = contextInterface->getProp(context, prop, NULL, &propSize); if (ret) { if (bufSize < propSize) { if (buf) buf = CryptMemRealloc(buf, propSize); else buf = CryptMemAlloc(propSize); bufSize = propSize; } if (buf) { ret = contextInterface->getProp(context, prop, buf, &propSize); if (ret) { hdr = (PWINE_CERT_PROP_HEADER)pbElement; hdr->propID = prop; hdr->unknown = 1; hdr->cb = propSize; pbElement += sizeof(WINE_CERT_PROP_HEADER); if (propSize) { memcpy(pbElement, buf, propSize); pbElement += propSize; } } } else ret = FALSE; } } } while (ret && prop != 0); CryptMemFree(buf); hdr = (PWINE_CERT_PROP_HEADER)pbElement; hdr->propID = contextPropID; hdr->unknown = 1; hdr->cb = cbEncodedContext; memcpy(pbElement + sizeof(WINE_CERT_PROP_HEADER), encodedContext, cbEncodedContext); } } else ret = FALSE; return ret; }