aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/internal/ccnx_ValidationFacadeV1.c
blob: d9f868957c5fdc66a7d47da870157d8d3d481f59 (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
/*
 * 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 <LongBow/runtime.h>

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

static void
_assertInvariants(const CCNxTlvDictionary *message)
{
    assertNotNull(message, "Parameter message must be non-null");
}

PARCBuffer *
ccnxValidationFacadeV1_GetKeyId(const CCNxTlvDictionary *message)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_GetBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYID);
}

CCNxLink *
ccnxValidationFacadeV1_GetKeyName(const CCNxTlvDictionary *message)
{
    _assertInvariants(message);
    CCNxLink *link = NULL;
    CCNxName *name = ccnxTlvDictionary_GetName(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_NAME);
    if (name) {
        PARCBuffer *keyid = ccnxTlvDictionary_GetBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_KEYID);
        PARCBuffer *hash = ccnxTlvDictionary_GetBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_OBJHASH);

        link = ccnxLink_Create(name, keyid, hash);
    }
    return link;
}

PARCBuffer *
ccnxValidationFacadeV1_GetPublicKey(const CCNxTlvDictionary *message)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_GetBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEY);
}

PARCBuffer *
ccnxValidationFacadeV1_GetCertificate(const CCNxTlvDictionary *message)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_GetBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CERT);
}

PARCBuffer *
ccnxValidationFacadeV1_GetPayload(const CCNxTlvDictionary *message)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_GetBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_PAYLOAD);
}

bool
ccnxValidationFacadeV1_HasCryptoSuite(const CCNxTlvDictionary *message)
{
    bool hasCryptoSuite = false;
    _assertInvariants(message);
    if (ccnxTlvDictionary_IsValueInteger(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CRYPTO_SUITE)) {
        hasCryptoSuite = true;
    }
    return hasCryptoSuite;
}

PARCCryptoSuite
ccnxValidationFacadeV1_GetCryptoSuite(const CCNxTlvDictionary *message)
{
    if (ccnxValidationFacadeV1_HasCryptoSuite(message)) {
        return (PARCCryptoSuite) ccnxTlvDictionary_GetInteger(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CRYPTO_SUITE);
    }

    trapUnexpectedState("Dictionary does not have a CryptoSuite set");
}

bool
ccnxValidationFacadeV1_HasSigningTime(const CCNxTlvDictionary *message)
{
    bool hasSigningTime = false;
    _assertInvariants(message);
    if (ccnxTlvDictionary_IsValueInteger(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_SIGNTIME)) {
        hasSigningTime = true;
    }
    return hasSigningTime;
}

uint64_t
ccnxValidationFacadeV1_GetSigningTime(const CCNxTlvDictionary *message)
{
    if (ccnxValidationFacadeV1_HasSigningTime(message)) {
        return ccnxTlvDictionary_GetInteger(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_SIGNTIME);
    }

    trapUnexpectedState("Dictionary does not have a CryptoSuite set");
}

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

bool
ccnxValidationFacadeV1_SetKeyId(CCNxTlvDictionary *message, const PARCBuffer *keyid)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_PutBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYID, keyid);
}

bool
ccnxValidationFacadeV1_SetKeyName(CCNxTlvDictionary *message, const CCNxLink *keyNameLink)
{
    _assertInvariants(message);
    const CCNxName *name = ccnxLink_GetName(keyNameLink);
    bool success = ccnxTlvDictionary_PutName(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_NAME, name);
    if (success) {
        PARCBuffer *keyid = ccnxLink_GetKeyID(keyNameLink);
        if (keyid) {
            success = ccnxTlvDictionary_PutBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_KEYID, keyid);
        }

        if (success) {
            PARCBuffer *hash = ccnxLink_GetContentObjectHash(keyNameLink);
            success = ccnxTlvDictionary_PutBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_OBJHASH, hash);
        }
    }
    return success;
}

bool
ccnxValidationFacadeV1_SetKeyLocator(CCNxTlvDictionary *message, CCNxKeyLocator *keyLocator)
{
    bool success = true;
    if (keyLocator) {
        if (ccnxKeyLocator_IsKey(keyLocator)) {
            PARCKey *key = ccnxKeyLocator_GetKey(keyLocator);
            PARCBuffer *keybuffer = parcKey_GetKey(key);
            success &= ccnxTlvDictionary_PutBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEY, keybuffer);
        } else if (ccnxKeyLocator_IsKeyLink(keyLocator)) {
            CCNxLink *link = ccnxKeyLocator_GetKeyLink(keyLocator);
            const CCNxName *name = ccnxLink_GetName(link);

            // Support KeyId and COH as part of the keyname, case 1012
            success &= ccnxTlvDictionary_PutName(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEYNAME_NAME, name);
        } else {
            trapUnrecoverableState("KeyLocator is not a known type");
        }
    }
    return success;
}

bool
ccnxValidationFacadeV1_SetPublicKey(CCNxTlvDictionary *message, const PARCBuffer *derEncodedKey)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_PutBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_KEY, derEncodedKey);
}

bool
ccnxValidationFacadeV1_SetCertificate(CCNxTlvDictionary *message, const PARCBuffer *derEncodedCertificate)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_PutBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CERT, derEncodedCertificate);
}

bool
ccnxValidationFacadeV1_SetCryptoSuite(CCNxTlvDictionary *message, PARCCryptoSuite suite)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_PutInteger(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_CRYPTO_SUITE, suite);
}

bool
ccnxValidationFacadeV1_SetSigningTime(CCNxTlvDictionary *message, uint64_t signingTime)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_PutInteger(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_SIGNTIME, signingTime);
}

bool
ccnxValidationFacadeV1_SetPayload(CCNxTlvDictionary *message, const PARCBuffer *validationPayload)
{
    _assertInvariants(message);
    return ccnxTlvDictionary_PutBuffer(message, CCNxCodecSchemaV1TlvDictionary_ValidationFastArray_PAYLOAD, validationPayload);
}