int KSI_OctetString_toTlv(KSI_CTX *ctx, KSI_OctetString *o, unsigned tag, int isNonCritical, int isForward, KSI_TLV **tlv) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tmp = NULL; KSI_ERR_clearErrors(ctx); if (ctx == NULL || o == NULL || tlv == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } res = KSI_TLV_new(ctx, tag, isNonCritical, isForward, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_setRawValue(tmp, o->data, o->data_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } *tlv = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_TLV_free(tmp); return res; }
int KSI_TlvTemplate_serializeObject(KSI_CTX *ctx, const void *obj, unsigned tag, int isNc, int isFwd, const KSI_TlvTemplate *tmpl, unsigned char **raw, unsigned *raw_len) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tlv = NULL; unsigned char *tmp = NULL; unsigned tmp_len = 0; KSI_ERR_clearErrors(ctx); if (ctx == NULL || obj == NULL || tmpl == NULL || raw == NULL || raw_len == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } /* Create TLV for the PDU object. */ res = KSI_TLV_new(ctx, KSI_TLV_PAYLOAD_TLV, tag, isFwd, isNc, &tlv); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } /* Evaluate the TLV. */ res = KSI_TlvTemplate_construct(ctx, tlv, obj, tmpl); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } KSI_LOG_logTlv(ctx, KSI_LOG_DEBUG, "Serializing object", tlv); /* Serialize the TLV. */ res = KSI_TLV_serialize(tlv, &tmp, &tmp_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } *raw = tmp; tmp = NULL; *raw_len = tmp_len; res = KSI_OK; cleanup: KSI_free(tmp); KSI_TLV_free(tlv); return res; }
int KSI_Integer_toTlv(KSI_CTX *ctx, KSI_Integer *o, unsigned tag, int isNonCritical, int isForward, KSI_TLV **tlv) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tmp = NULL; unsigned char raw[8]; unsigned len = 0; KSI_uint64_t val; KSI_ERR_clearErrors(ctx); if (ctx == NULL || o == NULL || tlv == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } val = o->value; res = KSI_TLV_new(ctx, tag, isNonCritical, isForward, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } /* Encode the integer value. */ while (val != 0) { raw[7 - len++] = val & 0xff; val >>= 8; } /* If the length is greater than 0 (val > 0), add the raw value. */ if (len > 0) { res = KSI_TLV_setRawValue(tmp, raw + 8 - len, len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } } *tlv = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_TLV_free(tmp); return res; }
int KSI_PKISignature_toTlv(KSI_CTX *ctx, KSI_PKISignature *sig, unsigned tag, int isNonCritical, int isForward, KSI_TLV **tlv) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tmp = NULL; unsigned char *raw = NULL; unsigned raw_len = 0; KSI_ERR_clearErrors(ctx); if (ctx == NULL || sig == NULL || tlv == NULL){ res = KSI_INVALID_ARGUMENT; goto cleanup; } res = KSI_TLV_new(ctx, KSI_TLV_PAYLOAD_RAW, tag, isNonCritical, isForward, &tmp); if (res != KSI_OK){ KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_PKISignature_serialize(sig, &raw, &raw_len); if (res != KSI_OK){ KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_setRawValue(tmp, raw, raw_len); if (res != KSI_OK){ KSI_pushError(ctx, res, NULL); goto cleanup; } *tlv = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_nofree(raw); KSI_TLV_free(tmp); return res; }
int KSI_DataHash_toTlv(KSI_CTX *ctx, KSI_DataHash *hsh, unsigned tag, int isNonCritical, int isForward, KSI_TLV **tlv) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tmp = NULL; const unsigned char *raw = NULL; size_t raw_len = 0; KSI_ERR_clearErrors(ctx); if (ctx == NULL || hsh == NULL || tlv == NULL) { res = KSI_INVALID_ARGUMENT; goto cleanup; } res = KSI_TLV_new(ctx, tag, isNonCritical, isForward, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_DataHash_getImprint(hsh, &raw, &raw_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_setRawValue(tmp, raw, raw_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } *tlv = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_nofree(raw); KSI_TLV_free(tmp); return res; }
int KSI_TlvTemplate_deepCopy(KSI_CTX *ctx, const void *from, const KSI_TlvTemplate *baseTemplate, void *to) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tmpTlv = NULL; KSI_ERR_clearErrors(ctx); if (ctx == NULL || from == NULL || baseTemplate == NULL || to == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } /* Create a dummy TLV */ res = KSI_TLV_new(ctx, KSI_TLV_PAYLOAD_TLV, 0x0, 0, 0, &tmpTlv); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } /* Create a TLV object */ res = KSI_TlvTemplate_construct(ctx, tmpTlv, from, baseTemplate); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } /* Evaluate the cloned object */ res = KSI_TlvTemplate_extract(ctx, to, tmpTlv, baseTemplate); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_OK; cleanup: KSI_TLV_free(tmpTlv); return res; }
int KSI_Utf8String_toTlv(KSI_CTX *ctx, KSI_Utf8String *o, unsigned tag, int isNonCritical, int isForward, KSI_TLV **tlv) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tmp = NULL; KSI_ERR_clearErrors(ctx); if (ctx == NULL || o == NULL || tlv == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } res = KSI_TLV_new(ctx, tag, isNonCritical, isForward, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } if (o->len > 0xffff){ KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, "UTF8 string too long for TLV conversion."); goto cleanup; } res = KSI_TLV_setRawValue(tmp, o->value, (unsigned)o->len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } *tlv = tmp; tmp = NULL; res = KSI_OK; cleanup: KSI_TLV_free(tmp); return res; }
int KSI_PublicationsFile_serialize(KSI_CTX *ctx, KSI_PublicationsFile *pubFile, char **raw, size_t *raw_len) { int res; const unsigned char *buf = NULL; size_t buf_len = 0; KSI_TLV *tlv = NULL; unsigned char *tmp = NULL; size_t tmp_len = 0; size_t sig_len = 0; KSI_ERR_clearErrors(ctx); if (ctx == NULL || pubFile == 0 || raw == NULL || raw_len == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } /** * Create TLV 0x700 that contains nested list of publication file TLVs. * Calculate signed data length assuming that signature TLV is always the last. */ res = KSI_TLV_new(ctx, 0x700, 0, 0, &tlv); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TlvTemplate_construct(ctx, tlv, pubFile, KSI_TLV_TEMPLATE(KSI_PublicationsFile)); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = publicationsFileTLV_getSignatureTLVLength(tlv, &sig_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_getRawValue(tlv, &buf, &buf_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } /** * Append raw value to publication file header. Copy raw publication file into * internal and external buffer. */ tmp_len = buf_len + sizeof(PUB_FILE_HEADER_ID) - 1; tmp = (unsigned char *) KSI_malloc(tmp_len); if (tmp == NULL) { KSI_pushError(ctx, res = KSI_OUT_OF_MEMORY, NULL); goto cleanup; } if (KSI_strncpy((char *)tmp, PUB_FILE_HEADER_ID, tmp_len) == NULL) { KSI_pushError(ctx, res = KSI_UNKNOWN_ERROR, NULL); goto cleanup; } memcpy(tmp + sizeof(PUB_FILE_HEADER_ID) - 1, buf, buf_len); if (pubFile->raw != NULL) KSI_free(pubFile->raw); pubFile->raw = tmp; pubFile->raw_len = tmp_len; pubFile->signedDataLength = tmp_len - sig_len; tmp = NULL; tmp = (unsigned char *) KSI_malloc(pubFile->raw_len); if (tmp == NULL) { KSI_pushError(ctx, res = KSI_OUT_OF_MEMORY, NULL); goto cleanup; } memcpy(tmp, pubFile->raw, pubFile->raw_len); *raw = (char *)tmp; *raw_len = pubFile->raw_len; tmp = NULL; res = KSI_OK; cleanup: KSI_TLV_free(tlv); KSI_free(tmp); return res; }
KSI_END_TLV_TEMPLATE static int replaceCalendarChain(KSI_Signature *sig, KSI_CalendarHashChain *calendarHashChain) { int res; KSI_DataHash *aggrOutputHash = NULL; KSI_TLV *oldCalChainTlv = NULL; KSI_TLV *newCalChainTlv = NULL; KSI_LIST(KSI_TLV) *nestedList = NULL; size_t i; if (sig == NULL || calendarHashChain == NULL) { res = KSI_INVALID_ARGUMENT; goto cleanup; } KSI_ERR_clearErrors(sig->ctx); res = KSI_TLV_getNestedList(sig->baseTlv, &nestedList); if (res != KSI_OK) { KSI_pushError(sig->ctx, res, NULL); goto cleanup; } if (sig->calendarChain != NULL) { for (i = 0; i < KSI_TLVList_length(nestedList); i++) { res = KSI_TLVList_elementAt(nestedList,i, &oldCalChainTlv); if (res != KSI_OK) { KSI_pushError(sig->ctx, res, NULL); goto cleanup; } if (oldCalChainTlv == NULL) { KSI_pushError(sig->ctx, res = KSI_INVALID_SIGNATURE, "Signature TLV element missing."); goto cleanup; } if (KSI_TLV_getTag(oldCalChainTlv) == 0x0802) break; } } res = KSI_TLV_new(sig->ctx, 0x0802, 0, 0, &newCalChainTlv); if (res != KSI_OK) { KSI_pushError(sig->ctx, res, NULL); goto cleanup; } res = KSI_TlvTemplate_construct(sig->ctx, newCalChainTlv, calendarHashChain, KSI_TLV_TEMPLATE(KSI_CalendarHashChain)); if (res != KSI_OK) { KSI_pushError(sig->ctx, res, NULL); goto cleanup; } res = (sig->calendarChain == NULL) ? /* In case there is no calendar hash chain attached, append a new one. */ KSI_TLV_appendNestedTlv(sig->baseTlv, newCalChainTlv) : /* Otherwise replace the calendar hash chain. */ KSI_TLV_replaceNestedTlv(sig->baseTlv, oldCalChainTlv, newCalChainTlv); if (res != KSI_OK) { KSI_pushError(sig->ctx, res, NULL); goto cleanup; } newCalChainTlv = NULL; /* The memory was freed within KSI_TLV_replaceNestedTlv. */ oldCalChainTlv = NULL; KSI_CalendarHashChain_free(sig->calendarChain); sig->calendarChain = calendarHashChain; res = KSI_OK; cleanup: KSI_nofree(nestedList); KSI_DataHash_free(aggrOutputHash); KSI_TLV_free(newCalChainTlv); return res; }
static int construct(KSI_CTX *ctx, KSI_TLV *tlv, const void *payload, const KSI_TlvTemplate *tmpl, struct tlv_track_s *tr, size_t tr_len, const size_t tr_size) { int res = KSI_UNKNOWN_ERROR; KSI_TLV *tmp = NULL; void *payloadp = NULL; int isNonCritical = 0; int isForward = 0; size_t template_len = 0; bool templateHit[MAX_TEMPLATE_SIZE]; bool groupHit[2] = {false, false}; bool oneOf[2] = {false, false}; size_t i; char buf[1000]; KSI_ERR_clearErrors(ctx); if (ctx == NULL || tlv == NULL || payload == NULL || tmpl == NULL || tr == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } /* Calculate the template length. */ template_len = getTemplateLength(tmpl); if (template_len == 0) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, "A template may not be empty."); goto cleanup; } if (template_len > MAX_TEMPLATE_SIZE) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, "Template too big."); goto cleanup; } memset(templateHit, 0, sizeof(templateHit)); for (i = 0; i < template_len; i++) { if ((tmpl[i].flags & KSI_TLV_TMPL_FLG_NO_SERIALIZE) != 0) continue; payloadp = NULL; res = tmpl[i].getValue(payload, &payloadp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } if (payloadp != NULL) { /* Register for tracking. */ if (tr_len < tr_size) { tr[tr_len].tag = tmpl[i].tag; tr[tr_len].desc = tmpl[i].descr; } templateHit[i] = true; if ((tmpl[i].flags & KSI_TLV_TMPL_FLG_LEAST_ONE_G0) != 0) groupHit[0] = true; if ((tmpl[i].flags & KSI_TLV_TMPL_FLG_LEAST_ONE_G1) != 0) groupHit[1] = true; if (FLAGSET(tmpl[i], KSI_TLV_TMPL_FLG_MOST_ONE_G0)) { if (oneOf[0]) { char errm[1000]; KSI_snprintf(errm, sizeof(errm), "Mutually exclusive elements present within group 0 (%s).", track_str(tr, tr_len, tr_size, buf, sizeof(buf))); KSI_pushError(ctx, res = KSI_INVALID_FORMAT, errm); } oneOf[0] = true; } if (FLAGSET(tmpl[i], KSI_TLV_TMPL_FLG_MOST_ONE_G1)) { if (oneOf[1]) { char errm[1000]; KSI_snprintf(errm, sizeof(errm), "Mutually exclusive elements present within group 1 (%s).", track_str(tr, tr_len, tr_size, buf, sizeof(buf))); KSI_pushError(ctx, res = KSI_INVALID_FORMAT, errm); } oneOf[1] = true; } isNonCritical = (tmpl[i].flags & KSI_TLV_TMPL_FLG_NONCRITICAL) != 0; isForward = (tmpl[i].flags & KSI_TLV_TMPL_FLG_FORWARD) != 0; switch (tmpl[i].type) { case KSI_TLV_TEMPLATE_OBJECT: if (tmpl[i].toTlv == NULL) { KSI_pushError(ctx, res = KSI_UNKNOWN_ERROR, "Invalid template: toTlv not set."); goto cleanup; } if (tmpl[i].listLength != NULL) { int j; for (j = 0; j < tmpl[i].listLength(payloadp); j++) { void *listElement = NULL; res = tmpl[i].listElementAt(payloadp, j, &listElement); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = tmpl[i].toTlv(ctx, listElement, tmpl[i].tag, isNonCritical, isForward != 0, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_appendNestedTlv(tlv, tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } tmp = NULL; } } else { res = tmpl[i].toTlv(ctx, payloadp, tmpl[i].tag, isNonCritical, isForward, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_appendNestedTlv(tlv, tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } tmp = NULL; } break; case KSI_TLV_TEMPLATE_COMPOSITE: if (tmpl[i].listLength != NULL) { int j; for (j = 0; j < tmpl[i].listLength(payloadp); j++) { void *listElement = NULL; res = KSI_TLV_new(ctx, KSI_TLV_PAYLOAD_TLV, tmpl[i].tag, isNonCritical, isForward, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = tmpl[i].listElementAt(payloadp, j, &listElement); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = construct(ctx, tmp, listElement, tmpl[i].subTemplate, tr, tr_len + 1, tr_size); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_appendNestedTlv(tlv, tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } tmp = NULL; } } else { res = KSI_TLV_new(ctx, KSI_TLV_PAYLOAD_TLV, tmpl[i].tag, isNonCritical, isForward, &tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = construct(ctx, tmp, payloadp, tmpl[i].subTemplate, tr, tr_len + 1, tr_size); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_appendNestedTlv(tlv, tmp); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } tmp = NULL; } break; default: KSI_LOG_error(ctx, "Unimplemented template type: %d", tmpl[i].type); KSI_pushError(ctx, res = KSI_UNKNOWN_ERROR, "Unimplemented template type."); goto cleanup; } } } /* Check that every mandatory component was present. */ for (i = 0; i < template_len; i++) { char errm[1000]; if ((tmpl[i].flags & KSI_TLV_TMPL_FLG_MANDATORY) != 0 && !templateHit[i]) { KSI_snprintf(errm, sizeof(errm), "Mandatory element missing: %s->[0x%02x]%s", track_str(tr, tr_len, tr_size, buf, sizeof(buf)), tmpl[i].tag, tmpl[i].descr == NULL ? "" : tmpl[i].descr); KSI_LOG_debug(ctx, "%s", errm); KSI_pushError(ctx, res = KSI_INVALID_FORMAT, errm); goto cleanup; } if (((tmpl[i].flags & KSI_TLV_TMPL_FLG_LEAST_ONE_G0) != 0 && !groupHit[0]) || ((tmpl[i].flags & KSI_TLV_TMPL_FLG_LEAST_ONE_G1) != 0 && !groupHit[1])) { KSI_snprintf(errm, sizeof(errm), "Mandatory group missing: %s->[0x%02x]%s", track_str(tr, tr_len, tr_size, buf, sizeof(buf)), tmpl[i].tag, tmpl[i].descr == NULL ? "" : tmpl[i].descr); KSI_LOG_debug(ctx, "%s", errm); KSI_pushError(ctx, res = KSI_INVALID_FORMAT, errm); goto cleanup; } } res = KSI_OK; cleanup: KSI_nofree(payloadp); KSI_TLV_free(tmp); return res; }