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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* 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.
*/
/**
* @header parc_InMemoryVerifier.c
* <#Abstract#>
*
* <#Discussion#>
*
* Example:
* @code
* <#example#>
* @endcode
*/
#include <config.h>
#include <stdio.h>
#include <LongBow/runtime.h>
#include <parc/security/parc_InMemoryVerifier.h>
#include <parc/security/parc_CryptoHasher.h>
#include <parc/security/parc_CryptoCache.h>
#include <parc/algol/parc_Memory.h>
#include <openssl/x509v3.h>
#include <openssl/ecdsa.h>
struct parc_inmemory_verifier {
PARCCryptoHasher *hasher_sha256;
PARCCryptoHasher *hasher_sha512;
PARCCryptoCache *key_cache;
};
static bool
_parcInMemoryVerifier_Destructor(PARCInMemoryVerifier **verifierPtr)
{
PARCInMemoryVerifier *verifier = *verifierPtr;
parcCryptoHasher_Release(&(verifier->hasher_sha256));
parcCryptoHasher_Release(&(verifier->hasher_sha512));
parcCryptoCache_Destroy(&(verifier->key_cache));
return true;
}
parcObject_ImplementAcquire(parcInMemoryVerifier, PARCInMemoryVerifier);
parcObject_ImplementRelease(parcInMemoryVerifier, PARCInMemoryVerifier);
parcObject_Override(PARCInMemoryVerifier, PARCObject,
.destructor = (PARCObjectDestructor *) _parcInMemoryVerifier_Destructor);
PARCInMemoryVerifier *
parcInMemoryVerifier_Create()
{
PARCInMemoryVerifier *verifier = parcObject_CreateInstance(PARCInMemoryVerifier);
if (verifier != NULL) {
// right now only support sha-256. need to figure out how to make this flexible
verifier->hasher_sha256 = parcCryptoHasher_Create(PARCCryptoHashType_SHA256);
verifier->hasher_sha512 = parcCryptoHasher_Create(PARCCryptoHashType_SHA512);
verifier->key_cache = parcCryptoCache_Create();
}
return verifier;
}
// ======================================
static PARCCryptoHasher *
_parcInMemoryVerifier_GetCryptoHasher(void *interfaceContext, PARCKeyId *keyid, PARCCryptoHashType hashType)
{
PARCInMemoryVerifier *verifier = (PARCInMemoryVerifier *) interfaceContext;
const PARCKey *key = parcCryptoCache_GetKey(verifier->key_cache, keyid);
if (key == NULL) {
return false;
}
assertFalse(parcKey_GetSigningAlgorithm(key) == PARCSigningAlgorithm_HMAC, "HMAC not supported yet");
switch (hashType) {
case PARCCryptoHashType_SHA256:
return verifier->hasher_sha256;
case PARCCryptoHashType_SHA512:
return verifier->hasher_sha512;
default:
trapUnexpectedState("unsupported hash type: %d", hashType);
}
}
static bool
_parcInMemoryVerifier_AllowedCryptoSuite(void *interfaceContext, PARCKeyId *keyid, PARCCryptoSuite suite)
{
PARCInMemoryVerifier *verifier = (PARCInMemoryVerifier *) interfaceContext;
const PARCKey *key = parcCryptoCache_GetKey(verifier->key_cache, keyid);
if (key == NULL) {
return false;
}
switch (parcKey_GetSigningAlgorithm(key)) {
case PARCSigningAlgorithm_RSA:
switch (suite) {
case PARCCryptoSuite_RSA_SHA256:
return true;
case PARCCryptoSuite_RSA_SHA512:
return true;
default:
return false;
}
break;
case PARCSigningAlgorithm_ECDSA:
switch (suite) {
case PARCCryptoSuite_ECDSA_SHA256:
return true;
default:
return false;
}
break;
case PARCSigningAlgorithm_DSA:
switch (suite) {
default:
return false;
}
break;
case PARCSigningAlgorithm_HMAC:
switch (suite) {
case PARCCryptoSuite_HMAC_SHA256:
return true;
default:
return false;
}
break;
default:
trapUnexpectedState("Unknown signing algorithm: %s",
parcSigningAlgorithm_ToString(parcKey_GetSigningAlgorithm(key)));
return false;
}
return false;
}
static bool _parcInMemoryVerifier_RSAKey_Verify(PARCInMemoryVerifier *verifier, PARCCryptoHash *localHash,
PARCSignature *signatureToVerify, PARCBuffer *derEncodedKey);
static bool _parcInMemoryVerifier_ECDSAKey_Verify(PARCInMemoryVerifier *verifier, PARCCryptoHash *localHash,
PARCSignature *signatureToVerify, PARCBuffer *derEncodedKey);
/**
* The signature verifies if:
* 0) we know the key for keyid
* 1) the signing algorithm of the key corresponding to keyid is same as CCNxSignature
* 2) The hash of the locallyComputedHash is the same type as the content object's ciphersuite
* 3) the signature verifies
*
* Example:
* @code
* <#example#>
* @endcode
*/
static bool
_parcInMemoryVerifier_VerifyDigest(void *interfaceContext, PARCKeyId *keyid, PARCCryptoHash *locallyComputedHash,
PARCCryptoSuite suite, PARCSignature *objectSignature)
{
PARCInMemoryVerifier *verifier = (PARCInMemoryVerifier *) interfaceContext;
const PARCKey *key = parcCryptoCache_GetKey(verifier->key_cache, keyid);
if (key == NULL) {
return false;
}
assertTrue(_parcInMemoryVerifier_AllowedCryptoSuite(interfaceContext, keyid, suite), "Invalid crypto suite for keyid");
if (parcKey_GetSigningAlgorithm(key) != parcSignature_GetSigningAlgorithm(objectSignature)) {
fprintf(stdout, "Signatured failed, signing algorithms do not match: key %s sig %s\n",
parcSigningAlgorithm_ToString(parcKey_GetSigningAlgorithm(key)),
parcSigningAlgorithm_ToString(parcSignature_GetSigningAlgorithm(objectSignature)));
return false;
}
if (parcCryptoHash_GetDigestType(locallyComputedHash) != parcCryptoSuite_GetCryptoHash(suite)) {
fprintf(stdout, "Signatured failed, digest algorithms do not match: digest %s suite %s\n",
parcCryptoHashType_ToString(parcCryptoHash_GetDigestType(locallyComputedHash)),
parcCryptoHashType_ToString(parcCryptoSuite_GetCryptoHash(suite)));
return false;
}
switch (parcSignature_GetSigningAlgorithm(objectSignature)) {
case PARCSigningAlgorithm_RSA:
return _parcInMemoryVerifier_RSAKey_Verify(verifier, locallyComputedHash, objectSignature, parcKey_GetKey(key));
case PARCSigningAlgorithm_ECDSA:
return _parcInMemoryVerifier_ECDSAKey_Verify(verifier, locallyComputedHash, objectSignature, parcKey_GetKey(key));
case PARCSigningAlgorithm_DSA:
trapNotImplemented("DSA not supported");
break;
case PARCSigningAlgorithm_HMAC:
trapNotImplemented("HMAC not supported");
break;
default:
trapUnexpectedState("Unknown signing algorithm: %d", parcSignature_GetSigningAlgorithm(objectSignature));
}
return false;
}
static void
_parcInMemoryVerifier_AddKey(void *interfaceContext, PARCKey *key)
{
assertNotNull(interfaceContext, "interfaceContext must be non-null");
assertNotNull(key, "key must be non-null");
PARCInMemoryVerifier *verifier = (PARCInMemoryVerifier *) interfaceContext;
bool success = parcCryptoCache_AddKey(verifier->key_cache, key);
assertTrue(success, "could not add key, it must be a duplicate");
}
static void
_parcInMemoryVerifier_RemoveKeyId(void *interfaceContext, PARCKeyId *keyid)
{
assertNotNull(interfaceContext, "interfaceContent must be non-null");
assertNotNull(keyid, "key must be non-null");
PARCInMemoryVerifier *verifier = (PARCInMemoryVerifier *) interfaceContext;
parcCryptoCache_RemoveKey(verifier->key_cache, keyid);
}
// ==============================================================
// Openssl specific parts
#ifdef __APPLE__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
/**
* Return if the signature and key verify with the local hash.
*
* PRECONDITION:
* - You know the signature and key are RSA.
*
* Example:
* @code
* <#example#>
* @endcode
*/
static bool
_parcInMemoryVerifier_RSAKey_Verify(PARCInMemoryVerifier *verifier, PARCCryptoHash *localHash,
PARCSignature *signatureToVerify, PARCBuffer *derEncodedKey)
{
const uint8_t *der_bytes = parcByteArray_Array(parcBuffer_Array(derEncodedKey));
long der_length = parcBuffer_Remaining(derEncodedKey);
EVP_PKEY *unwrapped_key = d2i_PUBKEY(NULL, &der_bytes, der_length);
if (unwrapped_key != NULL) {
int success = 0;
RSA *rsa = EVP_PKEY_get1_RSA(unwrapped_key);
if (rsa != NULL) {
int openssl_digest_type;
switch (parcCryptoHash_GetDigestType(localHash)) {
case PARCCryptoHashType_SHA256:
openssl_digest_type = NID_sha256;
break;
case PARCCryptoHashType_SHA512:
openssl_digest_type = NID_sha512;
break;
default:
trapUnexpectedState("Unknown digest type: %s",
parcCryptoHashType_ToString(parcCryptoHash_GetDigestType(localHash)));
}
PARCBuffer *sigbits = parcSignature_GetSignature(signatureToVerify);
PARCByteArray *bytearray = parcBuffer_Array(sigbits);
unsigned signatureLength = (unsigned) parcBuffer_Remaining(sigbits);
uint8_t *sigbuffer = parcByteArray_Array(bytearray);
size_t signatureOffset = parcBuffer_ArrayOffset(sigbits);
success = RSA_verify(openssl_digest_type,
(unsigned char *) parcByteArray_Array(parcBuffer_Array(parcCryptoHash_GetDigest(localHash))),
(unsigned) parcBuffer_Remaining(parcCryptoHash_GetDigest(localHash)),
sigbuffer + signatureOffset,
signatureLength,
rsa);
RSA_free(rsa);
}
EVP_PKEY_free(unwrapped_key);
if (success == 1) {
return true;
}
}
return false;
}
/**
* Return if the signature and key verify with the local hash.
*
* PRECONDITION:
* - You know the signature and key are ECDSA.
*
* Example:
* @code
* <#example#>
* @endcode
*/
static bool
_parcInMemoryVerifier_ECDSAKey_Verify(PARCInMemoryVerifier *verifier, PARCCryptoHash *localHash,
PARCSignature *signatureToVerify, PARCBuffer *derEncodedKey)
{
const uint8_t *der_bytes = parcByteArray_Array(parcBuffer_Array(derEncodedKey));
long der_length = parcBuffer_Remaining(derEncodedKey);
EVP_PKEY *unwrapped_key = d2i_PUBKEY(NULL, &der_bytes, der_length);
if (unwrapped_key != NULL) {
int success = 0;
EC_KEY *ec_key = EVP_PKEY_get1_EC_KEY(unwrapped_key);
if (ec_key != NULL) {
int openssl_digest_type;
switch (parcCryptoHash_GetDigestType(localHash)) {
case PARCCryptoHashType_SHA256:
openssl_digest_type = NID_sha256;
break;
case PARCCryptoHashType_SHA512:
openssl_digest_type = NID_sha512;
break;
default:
trapUnexpectedState("Unknown digest type: %s",
parcCryptoHashType_ToString(parcCryptoHash_GetDigestType(localHash)));
}
PARCBuffer *sigbits = parcSignature_GetSignature(signatureToVerify);
PARCByteArray *bytearray = parcBuffer_Array(sigbits);
unsigned signatureLength = (unsigned) parcBuffer_Remaining(sigbits);
uint8_t *sigbuffer = parcByteArray_Array(bytearray);
size_t signatureOffset = parcBuffer_ArrayOffset(sigbits);
success = ECDSA_verify(openssl_digest_type,
(unsigned char *) parcByteArray_Array(parcBuffer_Array(parcCryptoHash_GetDigest(localHash))),
(unsigned) parcBuffer_Remaining(parcCryptoHash_GetDigest(localHash)),
sigbuffer + signatureOffset,
signatureLength,
ec_key);
EC_KEY_free(ec_key);
}
EVP_PKEY_free(unwrapped_key);
if (success == 1) {
return true;
}
}
return false;
}
PARCVerifierInterface *PARCInMemoryVerifierAsVerifier = &(PARCVerifierInterface) {
.GetCryptoHasher = _parcInMemoryVerifier_GetCryptoHasher,
.VerifyDigest = _parcInMemoryVerifier_VerifyDigest,
.AddKey = _parcInMemoryVerifier_AddKey,
.RemoveKeyId = _parcInMemoryVerifier_RemoveKeyId,
.AllowedCryptoSuite = _parcInMemoryVerifier_AllowedCryptoSuite,
};
#ifdef __APPLE__
#pragma clang diagnostic pop
#endif
|