File: us1864.c

package info (click to toggle)
libest 3.2.0%2Bds-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,792 kB
  • sloc: ansic: 60,980; java: 12,082; sh: 4,956; python: 4,474; xml: 385; makefile: 290
file content (322 lines) | stat: -rw-r--r-- 11,718 bytes parent folder | download
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
/*------------------------------------------------------------------
 * us1864.c - Unit Tests for User Story 1864 - Enable Token Auth mode in server
 *
 * March, 2015
 *
 * Copyright (c) 2015, 2016 by cisco Systems, Inc.
 * All rights reserved.
 *------------------------------------------------------------------
 */
#include <stdio.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <est.h>
#include <curl/curl.h>
#include "curl_utils.h"
#include "test_utils.h"
#include "st_server.h"
#include <openssl/ssl.h>
#include <openssl/evp.h>

#ifdef HAVE_CUNIT
#include "CUnit/Basic.h"
#include "CUnit/Automated.h"
#endif
#include <errno.h>

static unsigned char *cacerts = NULL;
static int cacerts_len = 0;

#define US1864_TCP_PORT     29001

/*
 * The following CSR was generated using the following openssl command and then
 * using cat on the rsa.req file:
 *
 * openssl req -newkey rsa:2048 -keyout rsakey.pem -keyform PEM -out rsa.req -outform PEM
 */
#define US1864_PKCS10_RSA2048   "MIICvTCCAaUCAQAweDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAk5DMQwwCgYDVQQH\nDANSVFAxEjAQBgNVBAoMCVJTQWNlcnRjbzEMMAoGA1UECwwDcnNhMRAwDgYDVQQD\nDAdyc2EgZG9lMRowGAYJKoZIhvcNAQkBFgtyc2FAZG9lLmNvbTCCASIwDQYJKoZI\nhvcNAQEBBQADggEPADCCAQoCggEBAN6pCTBrK7T029Bganq0QHXHyNL8opvxc7JY\nXaQz39R3J9BoBE72XZ0QXsBtUEYGNhHOLaISASNzs2ZKWpvMHJWmPYNt39OCi48Y\nFOgLDbAn83mAOKSfcMLbibCcsh4HOlhaaFrWskRTAsew16MUOzFu6vBkw/AhI82J\nKPYws0dYOxuWFIgE1HL+m/gplbzq7FrBIdrqkNL+ddgyXoDd5NuLMJGDAK7vB1Ww\n9/Baw/6Ai9V5psye1v8fWDr6HW2gg9XnVtMwB4pCg1rl1lSYstumTGYbM6cxJywe\nLuMnDjj1ZwDsZ1wIXaBAXZaxEIS/rXOX0HnZMTefxY/gpFk1Kv0CAwEAAaAAMA0G\nCSqGSIb3DQEBBQUAA4IBAQB6rIwNjE8l8jFKR1hQ/qeSvee/bAQa58RufZ4USKuK\nlsih7UCf8bkQvgljnhscQuczIbnJzeqEPqSdnomFW6CvMc/ah+QfX87FGYxJgpwF\nutnUifjDiZhrWgf/jNNbtHrkecw/Zex4sZ/HC127jtE3cyEkDsrA1oBxYRCq93tC\nW2q9PLVmLlyjcZcS1KHVD2nya79kfS0YGMocsw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vHKmFqcxA7g\ngbEwXkAP5+VaMD92DghcNjXOqGKclZdmGj2oREqZwzvTDRo4zP1yen5vgL/Yz7SA\nxze8wPg2WhlV9+qvkVCpHN3EUIfO+rBgi2reo/vF7xq5CAU4UtQ1h4gHax67Yww8\nJmypyGGa0ad0Z8ruiclI/QtluADUxy1YM0Up2FC0s7j72xzrRpEl1fPlOi/bFaZp\nsr4zllOpwnRdxvffXO7gXtXVIr4IHVHNWj6kmDzyk0ovat2Ms5aGUcMDN6Jm8KIB\nNBVH5FgkBVQOPSngkwnEOj0RsaKSxT5EfmOxm9pCrAE3rNdVOgO4t8wZ6DQUqye/\nBUdmgXtWoGsKIg8oR5HAWBER8yw/qdiRlBGgN/PKZdpmYI2TEfZvp/nXwG7QLjGx\nsj5TWeRKNgghUCu3uF+1s0R+gqgY1S9GgiDSifL7+h+bXJ4ncyRGq+XPnrfMiRkB\neSyv3kyIxtZfAB6TjkUbtVfo2KrfqNxu4lbJYE2b6hs1L6t7YPhjubz9aES7wES7\nk+ZZPZn/k/GsqUpsWHnEFEvi5C5WPrnpvVN6rKh0fB+AukGrS+9EK4KNZWfV/yf8\nXN5qWyOtgd4oLUUsgFDJsqNh6A1mlmx6CnY=\n"

#define US1864_ENROLL_URL_BA    "https://127.0.0.1:29001/.well-known/est/simpleenroll"
#define US1864_PKCS10_CT        "Content-Type: application/pkcs10"
#define US1864_UIDPWD_GOOD      "estuser:estpwd"
#ifndef WIN32
#define US1864_CACERTS          "CA/estCA/cacert.crt"
#define US1864_CACERT           "CA/estCA/cacert.crt"
#define US1864_TRUSTED_CERT     "CA/trustedcerts.crt"
#define US1864_SERVER_CERT      "CA/estCA/private/estservercertandkey.pem"
#define US1864_SERVER_KEY       "CA/estCA/private/estservercertandkey.pem"
#define US1864_CLIENT_CERT      "CA/estCA/private/estservercertandkey.pem"
#define US1864_CLIENT_KEY       "CA/estCA/private/estservercertandkey.pem"
#else
#define US1864_CACERTS          "CA\\estCA\\cacert.crt"
#define US1864_CACERT           "CA\\estCA\\cacert.crt"
#define US1864_TRUSTED_CERT     "CA\\trustedcerts.crt"
#define US1864_SERVER_CERT      "CA\\estCA\\private\\estservercertandkey.pem"
#define US1864_SERVER_KEY       "CA\\estCA\\private\\estservercertandkey.pem"
#define US1864_CLIENT_CERT      "CA\\estCA\\private\\estservercertandkey.pem"
#define US1864_CLIENT_KEY       "CA\\estCA\\private\\estservercertandkey.pem"
#endif

/*
 * curl_data_cb is passed to Curl and will be called from Curl whenever data
 * has been received, or if this function has been specified to retrieve the
 * http headers.  In this test it's used to retrieve the http headers and
 * look for the "bearer" token Authorization challenge.
 */
static int bearer_found = 0;
static size_t curl_data_cb (void *ptr, size_t size, size_t nmemb,
                            void *userdata)
{
    void * rc;

    if (bearer_found == 0) {

        /*
         * WARNING: strstr can be dangerous because it assumes null terminated
         * strings.  In this case the http headers came from EST server and we
         * know they are null terminated
         */
        rc = strstr(ptr, "WWW-Authenticate: Bearer");
        if (rc) {
            bearer_found = 1;
        }
    }

    return size * nmemb;
}

static void us1864_clean (void)
{
}

static int us1864_start_server (int manual_enroll, int nid)
{
    int rv;

    rv = st_start(US1864_TCP_PORT,
                  US1864_SERVER_CERT,
                  US1864_SERVER_KEY,
                  "US1864 test realm",
                  US1864_CACERTS,
                  US1864_TRUSTED_CERT,
                  "CA/estExampleCA.cnf",
                  manual_enroll,
                  0,
                  nid);
    return rv;
}

/*
 * This routine is called when CUnit initializes this test
 * suite.  This can be used to allocate data or open any
 * resources required for all the test cases.
 */
static int us1864_init_suite (void)
{
    int rv;

    est_init_logger(EST_LOG_LVL_INFO, NULL);

    /*
     * Read in the CA certificates
     */
    cacerts_len = read_binary_file(US1864_CACERTS, &cacerts);
    if (cacerts_len <= 0) {
        return 1;
    }

    us1864_clean();

    /*
     * Start an instance of the EST server with
     * automatic enrollment enabled.
     */
    rv = us1864_start_server(0, 0);

    return rv;
}

/*
 * This routine is called when CUnit uninitializes this test
 * suite.  This can be used to deallocate data or close any
 * resources that were used for the test cases.
 */
static int us1864_destroy_suite (void)
{
    st_stop();
    free(cacerts);
    return 0;
}

/*
 * est_server_set_auth_mode() - unit test
 *
 * First, Test the parameters of est_server_set_auth_mode()
 */
static void us1864_test1 (void)
{
    unsigned char *cacerts = NULL;
    int cacerts_len = 0;
    BIO *certin, *keyin;
    X509 *x;
    EVP_PKEY * priv_key;
    int rv;
    EST_CTX *ctx;
    EST_ERROR est_rv;

    LOG_FUNC_NM
    ;

    /*
     * Read in the CA certificates
     */
    cacerts_len = read_binary_file(US1864_CACERT, &cacerts);
    CU_ASSERT(cacerts_len > 0);

    /*
     * Read the server cert
     */
    certin = BIO_new(BIO_s_file());
    rv = BIO_read_filename(certin, US1864_SERVER_CERT);
    CU_ASSERT(rv > 0);
    x = PEM_read_bio_X509(certin, NULL, NULL, NULL);
    CU_ASSERT(x != NULL);
    BIO_free(certin);

    /*
     * Read the server key
     */
    keyin = BIO_new(BIO_s_file());
    rv = BIO_read_filename(keyin, US1864_SERVER_KEY);
    CU_ASSERT(rv > 0);
    priv_key = PEM_read_bio_PrivateKey(keyin, NULL, NULL, NULL);
    CU_ASSERT(priv_key != NULL);
    BIO_free(keyin);

    /*
     * init EST in server mode
     */
    est_init_logger(EST_LOG_LVL_INFO, NULL);
    ctx = est_server_init(cacerts, cacerts_len, cacerts, cacerts_len,
        EST_CERT_FORMAT_PEM, "testrealm", x, priv_key);

    CU_ASSERT(ctx != NULL);

    est_rv = est_server_set_auth_mode(ctx, AUTH_NONE);
    CU_ASSERT(est_rv == EST_ERR_BAD_MODE);
    est_rv = est_server_set_auth_mode(ctx, AUTH_BASIC);
    CU_ASSERT(est_rv == EST_ERR_NONE);
    est_rv = est_server_set_auth_mode(ctx, AUTH_DIGEST);
    CU_ASSERT(est_rv == EST_ERR_NONE);
    est_rv = est_server_set_auth_mode(ctx, AUTH_TOKEN);
    CU_ASSERT(est_rv == EST_ERR_NONE);
    est_rv = est_server_set_auth_mode(ctx, 0xffffffff);
    CU_ASSERT(est_rv == EST_ERR_BAD_MODE);

    /*
     * Make sure we don't allow DIGEST mode when in FIPS mode
     */
    if (!EVP_default_properties_enable_fips(NULL, 1)) {
        printf("FIPS mode not supported, skipping test to prevent digest auth when in FIPS mode");
    } else {
        est_rv = est_server_set_auth_mode(ctx, AUTH_DIGEST);
        CU_ASSERT(est_rv == EST_ERR_BAD_MODE);
        EVP_default_properties_enable_fips(NULL, 0)
    }

    X509_free(x);
    EVP_PKEY_free(priv_key);

    est_destroy(ctx);
}

/*
 * Simple enroll - Token Auth Mode
 *
 * The goal of this test is to verify that the EST server responds with the
 * correct auth challenge when it's configured for Token Auth mode.  It will
 * first perform a sanity check by performing an HTTP Basic request while the
 * server is still in its default mode of HTTP Basic auth.  The test will then
 * configure the server for Token Auth mode and issue a request that does not
 * contain any auth header.  This should force the server to respond with a
 * token auth challenge header.
 */
static void us1864_test2 (void)
{
    long rv;

    LOG_FUNC_NM
    ;

    rv = curl_http_post(US1864_ENROLL_URL_BA, US1864_PKCS10_CT,
    US1864_PKCS10_RSA2048,
    US1864_UIDPWD_GOOD, US1864_CACERTS, CURLAUTH_BASIC, NULL, NULL, NULL);
    /*
     * Since we specify BASIC and the server is still in BASIC
     * we expect the server to respond with 200
     */
    CU_ASSERT(rv == 200);

    /*
     * Switch the server over to Token mode.
     *
     * NOTE: I see this being done in numerous places, and it's probably safe
     * in a test setting, but it is dangerous to change this on the fly in an
     * operational setting.  Also note, no return code for any of these
     * set/enable functions.
     */
    st_enable_http_token_auth();

    bearer_found = 0;

    rv = curl_http_post_cert_write(US1864_ENROLL_URL_BA,
    US1864_PKCS10_CT,
    US1864_PKCS10_RSA2048,
    US1864_CLIENT_CERT,
    US1864_CLIENT_KEY,
    US1864_CACERTS, curl_data_cb, curl_data_cb);

    /*
     * Since we changed auth modes on the server we expect this to now
     * fail.  We're not capturing the actual auth challenge we
     * we expect the server to respond with 200
     */
    CU_ASSERT(rv == 401);
    CU_ASSERT(bearer_found == 1);

}

/* The main() function for setting up and running the tests.
 * Returns a CUE_SUCCESS on successful running, another
 * CUnit error code on failure.
 */
int us1864_add_suite (void)
{
#ifdef HAVE_CUNIT
    CU_pSuite pSuite = NULL;

    /* add a suite to the registry */
    pSuite = CU_add_suite("us1864_cfg_tok_auth",
            us1864_init_suite,
            us1864_destroy_suite);
    if (NULL == pSuite) {
        CU_cleanup_registry();
        return CU_get_error();
    }

    /* add the tests to the suite */
    if ((NULL == CU_add_test(pSuite, "Check parms", us1864_test1)) ||
        (NULL == CU_add_test(pSuite, "Attempt enroll - BASIC pass", us1864_test2))
        )
    {
       CU_cleanup_registry();
       return CU_get_error();
    }

    return CUE_SUCCESS;
 #endif
 }