1/*
2 *  sc-31-peerinfo.c
3 *
4 *  Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
5 *
6 */
7
8
9#include <Security/SecBase.h>
10#include <Security/SecItem.h>
11#include <Security/SecKey.h>
12#include <Security/SecKeyPriv.h>
13
14#include <SecureObjectSync/SOSCircle.h>
15#include <SecureObjectSync/SOSPeerInfo.h>
16#include <SecureObjectSync/SOSInternal.h>
17#include <SecureObjectSync/SOSUserKeygen.h>
18
19#include <utilities/SecCFWrappers.h>
20
21#include <CoreFoundation/CoreFoundation.h>
22
23#include <stdlib.h>
24#include <unistd.h>
25
26#include "SOSCircle_regressions.h"
27
28#include "SOSRegressionUtilities.h"
29
30#if TARGET_OS_IPHONE
31#include <MobileGestalt.h>
32#endif
33
34static unsigned long kTestCount = 2;
35static unsigned long kTestFuzzerCount = 20000;
36
37static void tests(void)
38{
39    SecKeyRef signingKey = NULL;
40    SOSFullPeerInfoRef fpi = SOSCreateFullPeerInfoFromName(CFSTR("Test Peer"), &signingKey, NULL);
41    SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(fpi);
42    unsigned long count;
43
44    CFRetainSafe(pi);
45
46    ok(NULL != pi, "info creation");
47
48    uint8_t buffer[4096];
49
50    const uint8_t *buffer_p = SOSPeerInfoEncodeToDER(pi, NULL, buffer, buffer + sizeof(buffer));
51
52    ok(buffer_p != NULL, "encode");
53
54    size_t length = (buffer + sizeof(buffer)) - buffer_p;
55
56    uint8_t buffer2[length];
57    if(buffer_p == NULL) goto errOut;
58
59        for (count = 0; count < kTestFuzzerCount; count++) {
60            memcpy(buffer2, buffer_p, length);
61
62            const uint8_t *startp = buffer2;
63
64            buffer2[arc4random_uniform((u_int32_t)length)] = arc4random() & 0xff;
65
66            SOSPeerInfoRef pi2 = SOSPeerInfoCreateFromDER(NULL, NULL, &startp, buffer2 + length);
67            CFReleaseNull(pi2);
68            ok(1, "fuzz");
69        }
70
71errOut:
72    CFReleaseNull(signingKey);
73    CFReleaseNull(pi);
74}
75
76int sc_31_peerinfo(int argc, char *const *argv)
77{
78    plan_tests((int)(kTestCount + kTestFuzzerCount));
79
80    tests();
81
82	return 0;
83}
84