static int setStringParam(char **param, const char *val) { char *tmp = NULL; int res = KSI_UNKNOWN_ERROR; tmp = KSI_calloc(strlen(val) + 1, 1); if (tmp == NULL) { res = KSI_INVALID_ARGUMENT; goto cleanup; } memcpy(tmp, val, strlen(val) + 1); if (*param != NULL) { KSI_free(*param); } *param = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_free(tmp); return res; }
int KSI_CTX_setDefaultPubFileCertConstraints(KSI_CTX *ctx, const KSI_CertConstraint *arr) { int res = KSI_UNKNOWN_ERROR; KSI_CertConstraint *tmp = NULL; size_t count = 0; size_t i; KSI_ERR_clearErrors(ctx); if (ctx == NULL || arr == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } /* Count the input. */ while(arr[count++].oid != NULL); /* Allocate buffer with extra space for the trailing {NULL, NULL}. */ tmp = KSI_calloc(count, sizeof(KSI_CertConstraint)); if (tmp == NULL) { KSI_pushError(ctx, res = KSI_OUT_OF_MEMORY, NULL); goto cleanup; } /* Copy the values. */ for (i = 0; arr[i].oid != NULL; i++) { res = KSI_strdup(arr[i].oid, &tmp[i].oid); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } if (arr[i].val == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, "Expected OID value may not be NULL"); goto cleanup; } res = KSI_strdup(arr[i].val, &tmp[i].val); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } } /* Add terminator for the array. */ tmp[i].oid = NULL; tmp[i].val = NULL; /* Free the existing constraints. */ freeCertConstraintsArray(ctx->certConstraints); ctx->certConstraints = tmp; tmp = NULL; res = KSI_OK; cleanup: freeCertConstraintsArray(tmp); return res; }
static size_t receiveDataFromLibCurl(void *ptr, size_t size, size_t nmemb, void *stream) { size_t bytesCount = 0; unsigned char *tmp_buffer = NULL; CurlNetHandleCtx *nc = (CurlNetHandleCtx *) stream; bytesCount = nc->len + size * nmemb; if (bytesCount > UINT_MAX) { goto cleanup; } tmp_buffer = KSI_calloc(bytesCount, 1); if (tmp_buffer == NULL) goto cleanup; memcpy(tmp_buffer, nc->raw, nc->len); memcpy(tmp_buffer + nc->len, ptr, size * nmemb); KSI_free(nc->raw); nc->raw = tmp_buffer; nc->len = bytesCount; tmp_buffer = NULL; KSI_LOG_debug(nc->ctx, "0x%x: Received %llu bytes (%llu so far)", nc, (unsigned long long) bytesCount, nc->len); bytesCount = size * nmemb; cleanup: KSI_free(tmp_buffer); return bytesCount; }
KSI_HashAlgorithm KSI_getHashAlgorithmByName(const char *name) { size_t i; KSI_HashAlgorithm algo_id = KSI_HASHALG_INVALID; int alias_id; char *alias = NULL; char *upperName = NULL; if (name == NULL || !*name || strchr(name, ',') != NULL) goto cleanup; upperName = KSI_calloc(strlen(name) + 1, 1); if (upperName == NULL) goto cleanup; /* Create upper-case name */ for (i = 0; i < (int) strlen(name); i++) { if (name[i] == '_') { upperName[i] = '-'; } else { upperName[i] = (char) toupper(name[i]); } } upperName[i] = '\0'; for (i = 0; i < KSI_NUMBER_OF_KNOWN_HASHALGS; i++) { /* Skip all records without a name. */ if (KSI_hashAlgorithmInfo[i].name == NULL) continue; /* Do we have a bingo? */ if (!strcmp(upperName, KSI_hashAlgorithmInfo[i].name)) { algo_id = i; goto cleanup; } alias_id = 0; /* Loop until a null pointer or empty string. */ while ((alias = KSI_hashAlgorithmInfo[i].aliases[alias_id++]) && *alias) { if (!strcmp(upperName, alias)) { algo_id = i; goto cleanup; } } } cleanup: KSI_free(upperName); KSI_nofree(alias); return algo_id; }
int KSI_CTX_setPublicationCertEmail(KSI_CTX *ctx, const char *email) { int res = KSI_UNKNOWN_ERROR; char *tmp = NULL; KSI_CertConstraint arr[] = { { KSI_CERT_EMAIL, NULL }, { NULL, NULL } }; if (ctx == NULL || email == NULL) { res = KSI_INVALID_ARGUMENT; goto cleanup; } /* The value is only read - cast is safe. */ arr[0].val = (char *) email; res = KSI_CTX_setDefaultPubFileCertConstraints(ctx, arr); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } /* Keep the value for compatibility. */ if (email != NULL && email[0] != '\0') { size_t len = strlen(email); tmp = KSI_calloc(len + 1, 1); if (tmp == NULL) { res = KSI_OUT_OF_MEMORY; goto cleanup; } memcpy(tmp, email, len + 1); } KSI_free(ctx->publicationCertEmail_DEPRECATED); ctx->publicationCertEmail_DEPRECATED = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_free(tmp); return res; }
int KSI_PublicationsFile_fromFile(KSI_CTX *ctx, const char *fileName, KSI_PublicationsFile **pubFile) { int res; KSI_PublicationsFile *tmp = NULL; unsigned char *raw = NULL; size_t raw_len = 0; long raw_size = 0; FILE *f = NULL; KSI_ERR_clearErrors(ctx); if (ctx == NULL || fileName == NULL || pubFile == 0) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } f = fopen(fileName, "rb"); if (f == NULL) { KSI_pushError(ctx, res = KSI_IO_ERROR, "Unable to open publications file."); goto cleanup; } res = fseek(f, 0, SEEK_END); if (res != 0) { KSI_pushError(ctx, res = KSI_IO_ERROR, NULL); goto cleanup; } raw_size = ftell(f); if (raw_size < 0) { KSI_pushError(ctx, res = KSI_IO_ERROR, NULL); goto cleanup; } if (raw_size > UINT_MAX) { KSI_pushError(ctx, res = KSI_INVALID_FORMAT, "Publications file exceeds max size."); goto cleanup; } res = fseek(f, 0, SEEK_SET); if (res != 0) { KSI_pushError(ctx, res = KSI_IO_ERROR, NULL); goto cleanup; } raw = KSI_calloc((unsigned)raw_size, 1); if (raw == NULL) { KSI_pushError(ctx, res = KSI_OUT_OF_MEMORY, NULL); goto cleanup; } raw_len = fread(raw, 1, (unsigned)raw_size, f); if (raw_len != (unsigned)raw_size) { KSI_pushError(ctx, res = KSI_IO_ERROR, NULL); goto cleanup; } res = KSI_PublicationsFile_parse(ctx, raw, (unsigned)raw_len, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } *pubFile = tmp; tmp = NULL; res = KSI_OK; cleanup: if (f != NULL) fclose(f); KSI_free(raw); KSI_PublicationsFile_free(tmp); return res; }
int KSI_PublicationData_toBase32(const KSI_PublicationData *pubData, char **pubStr) { int res; const unsigned char *imprint = NULL; size_t imprint_len = 0; KSI_uint64_t publication_identifier = 0; unsigned char *binPub = NULL; size_t binPub_length; int i; unsigned long tmp_ulong; char *tmp = NULL; if (pubData == NULL) { res = KSI_INVALID_ARGUMENT; goto cleanup; } KSI_ERR_clearErrors(pubData->ctx); if (pubStr == NULL) { KSI_pushError(pubData->ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } res = KSI_DataHash_getImprint(pubData->imprint, &imprint, &imprint_len); if (res != KSI_OK) { KSI_pushError(pubData->ctx, res, NULL); goto cleanup; } binPub_length = 8 + imprint_len + 4; binPub = KSI_calloc(binPub_length, 1); if (binPub == NULL) { KSI_pushError(pubData->ctx, res = KSI_OUT_OF_MEMORY, NULL); goto cleanup; } publication_identifier = KSI_Integer_getUInt64(pubData->time); for (i = 7; i >= 0; --i) { binPub[i] = (unsigned char) (publication_identifier & 0xff); publication_identifier >>= 8; } memcpy(binPub + 8, imprint, imprint_len); tmp_ulong = KSI_crc32(binPub, binPub_length - 4, 0); for (i = 3; i >= 0; --i) { binPub[binPub_length - 4 + i] = (unsigned char) (tmp_ulong & 0xff); tmp_ulong >>= 8; } res = KSI_base32Encode(binPub, binPub_length, 6, &tmp); if (res != KSI_OK) { KSI_pushError(pubData->ctx, res, NULL); goto cleanup; } *pubStr = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_free(binPub); KSI_free(tmp); return res; }