aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/internal/ccnx_ContentObjectFacadeV1.c
blob: 1ca0c5af5559b438f4c23f3a857d4ae5291f43b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 */
#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include <LongBow/runtime.h>

#include <parc/security/parc_SigningAlgorithm.h>

#include <ccnx/common/internal/ccnx_ContentObjectFacadeV1.h>
#include <ccnx/common/internal/ccnx_ValidationFacadeV1.h>
#include <ccnx/common/internal/ccnx_ChunkingFacadeV1.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_TlvDictionary.h>

#include <ccnx/common/codec/ccnxCodec_TlvEncoder.h>

static void
_assertInvariants(const CCNxTlvDictionary *contentObjectDictionary)
{
    assertNotNull(contentObjectDictionary, "Dictionary is null");
    assertTrue(ccnxTlvDictionary_IsContentObject(contentObjectDictionary), "Dictionary is not a content object");
    assertTrue(ccnxTlvDictionary_GetSchemaVersion(contentObjectDictionary) == CCNxTlvDictionary_SchemaVersion_V1,
               "Dictionary is wrong schema version, got %d expected %d",
               ccnxTlvDictionary_GetSchemaVersion(contentObjectDictionary), CCNxTlvDictionary_SchemaVersion_V1);
}

// =========================
// Creation

static CCNxTlvDictionary *
_ccnxContentObjectFacadeV1_CreateWithNameAndPayload(const CCNxName *name,              // required
                                                    const CCNxPayloadType payloadType, // required
                                                    const PARCBuffer *payload)         // may be null
{
    assertNotNull(name, "Parameter name must be non-null");

    CCNxTlvDictionary *dictionary = ccnxCodecSchemaV1TlvDictionary_CreateContentObject();

    if (dictionary) {
        ccnxTlvDictionary_PutName(dictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_NAME, name);

        if (payloadType != CCNxPayloadType_DATA) {
            ccnxTlvDictionary_PutInteger(dictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOADTYPE, payloadType);
        }

        if (payload) {
            ccnxTlvDictionary_PutBuffer(dictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD, payload);
        }
    } else {
        trapOutOfMemory("Could not allocate ContentObject");
    }

    return dictionary;
}

static CCNxTlvDictionary *
_ccnxContentObjectFacadeV1_CreateWithPayload(const CCNxPayloadType payloadType, // required
                                             const PARCBuffer *payload)         // may be null
{
    CCNxTlvDictionary *dictionary = ccnxCodecSchemaV1TlvDictionary_CreateContentObject();

    if (dictionary) {
        if (payloadType != CCNxPayloadType_DATA) {
            ccnxTlvDictionary_PutInteger(dictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOADTYPE, payloadType);
        }

        if (payload) {
            ccnxTlvDictionary_PutBuffer(dictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD, payload);
        }
    } else {
        trapOutOfMemory("Could not allocate ContentObject");
    }

    return dictionary;
}

// =========================
// Getters

static CCNxName *
_ccnxContentObjectFacadeV1_GetName(const CCNxTlvDictionary *contentObjectDictionary)
{
    _assertInvariants(contentObjectDictionary);
    return ccnxTlvDictionary_GetName(contentObjectDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_NAME);
}

static bool
_ccnxContentObjectFacadeV1_HasExpiryTime(const CCNxTlvDictionary *packetDictionary)
{
    if (ccnxTlvDictionary_IsValueInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_EXPIRY_TIME)) {
        return true;
    }
    return false;
}

static uint64_t
_ccnxContentObjectFacadeV1_GetExpiryTime(const CCNxTlvDictionary *packetDictionary)
{
    if (ccnxTlvDictionary_IsValueInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_EXPIRY_TIME)) {
        return ccnxTlvDictionary_GetInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_EXPIRY_TIME);
    }
    trapUnexpectedState("The dictionary does not contain an Expiry Time");
}

static bool
_ccnxContentObjectFacadeV1_HasPathLabel(const CCNxTlvDictionary *packetDictionary)
{
    if (ccnxTlvDictionary_IsValueInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_PathLabel)) {
        return true;
    }
    return false;
}


static uint64_t
_ccnxContentObjectFacadeV1_GetPathLabel(const CCNxTlvDictionary *packetDictionary)
{
    if (ccnxTlvDictionary_IsValueInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_PathLabel)) {
        return ccnxTlvDictionary_GetInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_PathLabel);
    }
    trapUnexpectedState("The dictionary does not contain a Path Label");
}

static PARCBuffer *
_ccnxContentObjectFacadeV1_GetPayload(const CCNxTlvDictionary *contentObjectDictionary)
{
    _assertInvariants(contentObjectDictionary);
    return ccnxTlvDictionary_GetBuffer(contentObjectDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD);
}

static CCNxPayloadType
_ccnxContentObjectFacadeV1_GetPayloadType(const CCNxTlvDictionary *contentObjectDictionary)
{
    CCNxPayloadType result = CCNxPayloadType_DATA;

    _assertInvariants(contentObjectDictionary);
    if (ccnxTlvDictionary_IsValueInteger(contentObjectDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOADTYPE)) {
        result = (CCNxPayloadType) ccnxTlvDictionary_GetInteger(contentObjectDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOADTYPE);
    }

    return result;
}

// =========================
// Setters

static bool
_ccnxContentObjectFacadeV1_SetSignature(CCNxTlvDictionary *contentObject, const PARCBuffer *keyId,
                                        const PARCSignature *signature, const CCNxKeyLocator *keyLocator)
{
    bool result = false;

    CCNxTlvDictionary *contentObjectDictionary = (CCNxTlvDictionary *) contentObject;

    if (parcSignature_GetSigningAlgorithm(signature) == PARCSigningAlgorithm_RSA
        && parcSignature_GetHashType(signature) == PARCCryptoHashType_SHA256) {
        ccnxValidationRsaSha256_Set(contentObjectDictionary, keyId, keyLocator);
    } else if (parcSignature_GetSigningAlgorithm(signature) == PARCSigningAlgorithm_HMAC
               && parcSignature_GetHashType(signature) == PARCCryptoHashType_SHA256) {
        ccnxValidationHmacSha256_Set(contentObjectDictionary, keyId);
    } else {
        trapNotImplemented("Have not implemented the signature parameters");
    }

    PARCBuffer *sigbits = parcSignature_GetSignature(signature);

    result = ccnxValidationFacadeV1_SetPayload(contentObjectDictionary, sigbits);

    return result;
}

static PARCBuffer *
_ccnxContentObjectFacadeV1_GetKeyId(const CCNxTlvDictionary *contentObject)
{
    return ccnxValidationFacadeV1_GetKeyId(contentObject);
}

static bool
_ccnxContentObjectFacadeV1_SetExpiryTime(CCNxTlvDictionary *contentObjectDictionary, uint64_t expiryTime)
{
    bool success = ccnxTlvDictionary_PutInteger(contentObjectDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_EXPIRY_TIME, expiryTime);
    trapUnexpectedStateIf(!success, "Could not set integer in dictionary");
    return success;
}

static bool
_ccnxContentObjectFacadeV1_SetPathLabel(CCNxTlvDictionary *contentObjectDictionary, uint64_t pathLabel)
{
    bool success = ccnxTlvDictionary_PutInteger(contentObjectDictionary, CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_PathLabel, pathLabel);
    trapUnexpectedStateIf(!success, "Could not set integer in dictionary (path label)");
    return success;
}

static bool
_ccnxContentObjectFacadeV1_SetPayload(CCNxTlvDictionary *contentObjectDictionary, CCNxPayloadType payloadType, const PARCBuffer *payload)
{
    bool result = false;

    if (payload != NULL) {
        PARCBuffer *originalPayload = _ccnxContentObjectFacadeV1_GetPayload(contentObjectDictionary);

        result = ccnxTlvDictionary_PutBuffer(contentObjectDictionary,
                                             CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD, payload);

        if (result) {
            if (_ccnxContentObjectFacadeV1_GetPayloadType(contentObjectDictionary) != payloadType) {
                ccnxTlvDictionary_PutInteger(contentObjectDictionary,
                                             CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOADTYPE, payloadType);
            }

            if (originalPayload != NULL) {
                parcBuffer_Release(&originalPayload);
            }
        }
    }

    return result;
}

// =========================
// Miscellaneous functions

static bool
_ccnxContentObjectFacadeV1_Equals(const CCNxTlvDictionary *objectA, const CCNxTlvDictionary *objectB)
{
    return ccnxTlvDictionary_Equals(objectA, objectB);
}

static char *
_ccnxContentObjectFacadeV1_ToString(const CCNxTlvDictionary *contentObjectDictionary)
{
    trapNotImplemented("_ccnxContentObjectFacadeV1_ToString(): not yet implemented");
}

static void
_ccnxContentObjectFacadeV1_Display(const CCNxTlvDictionary *contentObjectDictionary, size_t indentation)
{
    _assertInvariants(contentObjectDictionary);
    ccnxTlvDictionary_Display(contentObjectDictionary, (unsigned) indentation);
}

/**
 * `CCNxContentObjectFacadeV1_Implementation` is the structure containing the pointers to the
 * V1 schema ContentObject implementation.
 */
CCNxContentObjectInterface CCNxContentObjectFacadeV1_Implementation = {
    .description              = "CCNxContentObjectFacadeV1_Implementation",

    .createWithNameAndPayload = &_ccnxContentObjectFacadeV1_CreateWithNameAndPayload,
    .createWithPayload        = &_ccnxContentObjectFacadeV1_CreateWithPayload,

    .setSignature             = &_ccnxContentObjectFacadeV1_SetSignature,
    .getKeyId                 = &_ccnxContentObjectFacadeV1_GetKeyId,

    .getName                  = &_ccnxContentObjectFacadeV1_GetName,
    .getPayload               = &_ccnxContentObjectFacadeV1_GetPayload,
    .setPayload               = &_ccnxContentObjectFacadeV1_SetPayload,
    .getPayloadType           = &_ccnxContentObjectFacadeV1_GetPayloadType,

    .getFinalChunkNumber      = &ccnxChunkingFacadeV1_GetEndChunkNumber,
    .setFinalChunkNumber      = &ccnxChunkingFacadeV1_SetEndChunkNumber,
    .hasFinalChunkNumber      = &ccnxChunkingFacadeV1_HasEndChunkNumber,

    .getExpiryTime            = &_ccnxContentObjectFacadeV1_GetExpiryTime,
    .setExpiryTime            = &_ccnxContentObjectFacadeV1_SetExpiryTime,
    .hasExpiryTime            = &_ccnxContentObjectFacadeV1_HasExpiryTime,

    .getPathLabel             = &_ccnxContentObjectFacadeV1_GetPathLabel,
    .setPathLabel             = &_ccnxContentObjectFacadeV1_SetPathLabel,
    .hasPathLabel             = &_ccnxContentObjectFacadeV1_HasPathLabel,

    .toString                 = &_ccnxContentObjectFacadeV1_ToString,
    .display                  = &_ccnxContentObjectFacadeV1_Display,
    .equals                   = &_ccnxContentObjectFacadeV1_Equals,

    .assertValid              = &_assertInvariants,
};