aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_ValidationEncoder.c
blob: 4e9ecd13c75e04d76b62b75b203aecdbb0352158 (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
/*
 * 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 <sys/time.h>
#include <ccnx/common/internal/ccnx_TlvDictionary.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_TlvDictionary.h>

#include <ccnx/common/codec/ccnxCodec_TlvUtilities.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_Types.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_ValidationEncoder.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_LinkCodec.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_CryptoSuite.h>

static ssize_t
_encodeKeyId(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;
    PARCBuffer *keyid = ccnxTlvDictionary_GetBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYID);
    if (keyid) {
        length = ccnxCodecTlvEncoder_AppendBuffer(encoder, CCNxCodecSchemaV1Types_ValidationAlg_KeyId, keyid);
    }
    return length;
}

static ssize_t
_encodePublicKey(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;
    PARCBuffer *key = ccnxTlvDictionary_GetBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEY);
    if (key) {
        length = ccnxCodecTlvEncoder_AppendBuffer(encoder, CCNxCodecSchemaV1Types_ValidationAlg_PublicKey, key);
    }
    return length;
}

static ssize_t
_encodeCertificate(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;
    PARCBuffer *cert = ccnxTlvDictionary_GetBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CERT);
    if (cert) {
        length = ccnxCodecTlvEncoder_AppendBuffer(encoder, CCNxCodecSchemaV1Types_ValidationAlg_Cert, cert);
    }
    return length;
}

/**
 * If there is a CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_NAME entry in the dictionary, look up the
 * optional keyid and hash restrictions, create a CCNxLink, then encode the Link.
 */
static ssize_t
_encodeKeyName(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;
    CCNxName *keyname = ccnxTlvDictionary_GetName(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_NAME);
    if (keyname) {
        PARCBuffer *keyid = ccnxTlvDictionary_GetBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_KEYID);
        PARCBuffer *hash = ccnxTlvDictionary_GetBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_OBJHASH);
        CCNxLink *link = ccnxLink_Create(keyname, keyid, hash);

        size_t startPosition = ccnxCodecTlvEncoder_Position(encoder);
        ccnxCodecTlvEncoder_AppendContainer(encoder, CCNxCodecSchemaV1Types_ValidationAlg_KeyName, 0);
        ssize_t innerLength = ccnxCodecSchemaV1LinkCodec_Encode(encoder, link);

        if (innerLength == 0) {
            // backup and erase the container
            ccnxCodecTlvEncoder_SetPosition(encoder, startPosition);
        } else if (innerLength > 0) {
            ccnxCodecTlvEncoder_SetContainerLength(encoder, startPosition, innerLength);
            ssize_t endPosition = ccnxCodecTlvEncoder_Position(encoder);
            length = endPosition - startPosition;
        } else {
            // an error signal
            length = innerLength;
        }

        ccnxLink_Release(&link);
    }
    return length;
}

/**
 * If a time is not provided, use the current time
 */
static ssize_t
_encodeSignatureTime(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;

    bool haveSignTime = false;
    uint64_t signTime = 0;
    if (ccnxTlvDictionary_IsValueInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_SIGNTIME)) {
        haveSignTime = true;
        signTime = ccnxTlvDictionary_GetInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_SIGNTIME);
    } else {
        // if we have a signer and with a signature algorithm, create the signing time
        PARCSigner *signer = ccnxCodecTlvEncoder_GetSigner(encoder);
        if (signer) {
            PARCSigningAlgorithm alg = parcSigner_GetSigningAlgorithm(signer);
            if (alg != PARCSigningAlgorithm_NULL && alg != PARCSigningAlgorithm_UNKNOWN) {
                // We will generate a signature, so generate a signing time

                struct timeval tv;
                gettimeofday(&tv, NULL);

                // convert to milli-seconds
                signTime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
                haveSignTime = true;
            }
        }
    }

    if (haveSignTime) {
        length = ccnxCodecTlvEncoder_AppendUint64(encoder, CCNxCodecSchemaV1Types_ValidationAlg_SigTime, signTime);
    }

    return length;
}

static ssize_t
_encodeAlgParameters(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;
    ssize_t result;

    result = _encodeKeyId(encoder, packetDictionary);
    if (result < 0) {
        return result;
    }
    length += result;

    result = _encodePublicKey(encoder, packetDictionary);
    if (result < 0) {
        return result;
    }
    length += result;

    result = _encodeCertificate(encoder, packetDictionary);
    if (result < 0) {
        return result;
    }
    length += result;

    result = _encodeKeyName(encoder, packetDictionary);
    if (result < 0) {
        return result;
    }
    length += result;

    result = _encodeSignatureTime(encoder, packetDictionary);
    if (result < 0) {
        return result;
    }
    length += result;

    return length;
}

ssize_t
ccnxCodecSchemaV1ValidationEncoder_EncodeAlg(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;
    bool haveCryptoSuite = false;
    CCNxCodecSchemaV1TlvDictionary_CryptoSuite suite;

    if (ccnxTlvDictionary_IsValueInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CRYPTO_SUITE)) {
        // try from dictionary

        PARCCryptoSuite parcSuite = (PARCCryptoSuite) ccnxTlvDictionary_GetInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CRYPTO_SUITE);

        haveCryptoSuite = ccnxCodecSchemaV1CryptoSuite_ParcToTlv(parcSuite, &suite);
    } else if (ccnxTlvDictionary_IsContentObject(packetDictionary)) {
        // deduce from the signer

        PARCSigner *signer = ccnxCodecTlvEncoder_GetSigner(encoder);
        if (signer != NULL) {
            PARCCryptoHashType hashType = parcSigner_GetCryptoHashType(signer);
            PARCSigningAlgorithm signAlg = parcSigner_GetSigningAlgorithm(signer);

            if (ccnxCodecSchemaV1CryptoSuite_SignAndHashToTlv(signAlg, hashType, &suite)) {
                haveCryptoSuite = true;
            }
        }
    }

    if (haveCryptoSuite) {
        // write the TL container then encode any enclosed TLVs
        ssize_t startPosition = ccnxCodecTlvEncoder_Position(encoder);

        ccnxCodecTlvEncoder_AppendContainer(encoder, suite, 0);
        ssize_t innerLength = _encodeAlgParameters(encoder, packetDictionary);

        // 0 inner length is acceptable
        if (innerLength >= 0) {
            ccnxCodecTlvEncoder_SetContainerLength(encoder, startPosition, innerLength);
            ssize_t endPosition = ccnxCodecTlvEncoder_Position(encoder);
            length = endPosition - startPosition;
        } else {
            // an error signal
            length = innerLength;
        }
    }
    return length;
}

ssize_t
ccnxCodecSchemaV1ValidationEncoder_EncodePayload(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = 0;

    if (!ccnxTlvDictionary_IsValueBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_PAYLOAD)) {
        // try to compute a signature

        // If signer is NULL, then no signature is genearted
        PARCSigner *signer = ccnxCodecTlvEncoder_GetSigner(encoder);
        if (signer != NULL) {
            // user did not give us one, so fill it in
            PARCSignature *signature = ccnxCodecTlvEncoder_ComputeSignature(encoder);
            PARCBuffer *sigbits = parcSignature_GetSignature(signature);

            // this creates its own reference to sigbits
            ccnxTlvDictionary_PutBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_PAYLOAD, sigbits);

            // this will release our hold on sigbitsElastic and sigbits.
            parcSignature_Release(&signature);
        }
    }

    PARCBuffer *sigbits = ccnxTlvDictionary_GetBuffer(packetDictionary, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_PAYLOAD);
    if (sigbits) {
        size_t remaining = parcBuffer_Remaining(sigbits);
        uint8_t *overlay = parcBuffer_Overlay(sigbits, 0);
        length = ccnxCodecTlvEncoder_AppendRawArray(encoder, remaining, overlay);
    }

    return length;
}