summaryrefslogtreecommitdiff
path: root/test/testssl.c
blob: e7f090e1bd645c2d12a4144872fd87793bc82a95 (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
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
/*
 * testssl: Simple APR SSL sockets test.
 */

#include "apr.h"
#include "apr_general.h"
#include "apr_pools.h"
#include "apr_errno.h"
#include "apr_getopt.h"
#include "apr_time.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"

#include "apr_ssl.h"
#include "apr_network_io.h"

#if APR_HAVE_STDIO_H
#include <stdio.h>
#endif
#if APR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>     /* for atexit(), malloc() */
#include <string.h>

struct sslTestCase {
    char *host;
    int port;
    const char *request;
    int result;
} tests[] = {
    { "svn.apache.org", 443, "GET / HTTP/1.0\n\n", 1 },
    { NULL }
};

static apr_ssl_socket_t *createSocket(apr_ssl_factory_t *asf, 
                                      apr_pollset_t *pollset,
                                      apr_pool_t *pool, int blocking)
{
    apr_ssl_socket_t *sock;
    apr_status_t rv;
    printf("::Creating SSL socket\n");
    rv = apr_ssl_socket_create(&sock, AF_INET, SOCK_STREAM, 0, asf, NULL);
    if (rv != APR_SUCCESS) {
        printf("\tFailed to create socket\n");
        return NULL;
    }
    rv = apr_pollset_add_ssl_socket(pollset, sock);
    if (rv != APR_SUCCESS) {
        printf("\tFailed to add to pollset\n");
        return NULL;
    }
    printf("\tOK\n");
    return sock;
}

static apr_status_t connectSocket(apr_ssl_socket_t *sock,
                                  const char *host, int port,
                                  apr_pool_t *pool)
{
    apr_status_t rv;
    apr_sockaddr_t *remoteSA;

    printf("::Connecting socket\n");
    rv = apr_sockaddr_info_get(&remoteSA, host, APR_UNSPEC, port, 0, pool);
    if (rv != APR_SUCCESS) {
        printf("\tFailed to get address for '%s', port %d\n", host, port);
        return rv;
    }
    rv = apr_ssl_socket_connect(sock, remoteSA);
    if (rv != APR_SUCCESS) {
        printf("\tFailed to connect to '%s' port %d\n", host, port);
        return rv;
    }
    printf("\tOK\n");
    return rv;
}

static apr_status_t socketRead(apr_ssl_socket_t *sock,
                               apr_pollset_t *pollset,
                               char *buf, apr_size_t *len)
{
    int lrv;
    const apr_pollfd_t *descs = NULL;
    apr_status_t rv;

    printf("::Reading from socket\n");
    rv = apr_ssl_socket_set_poll_events(sock, APR_POLLIN);
    if (rv != APR_SUCCESS) {
        printf("\tUnable to change socket poll events!\n");
        return rv;
    }

    rv = apr_pollset_poll(pollset, 30 * APR_USEC_PER_SEC, &lrv, &descs);
    if (APR_STATUS_IS_TIMEUP(rv)) {
        printf("\tTime up!\n");
        return rv;
    }

    if (lrv != 1) {
        printf("\tIncorrect return count, %d\n", lrv);
        return rv;
    }
    if (descs[0].client_data != sock) {
        printf("\tWrong socket returned?!\n");
        return rv;
    }
    if ((descs[0].rtnevents & APR_POLLIN) == 0) {
        printf("\tSocket wasn't ready? huh? req [%08x] vs rtn [%08x]\n",
               descs[0].reqevents, descs[0].rtnevents);
        return rv;
    }
    rv = apr_ssl_socket_recv(sock, buf, len);
    if (rv == APR_SUCCESS)
        printf("\tOK, read %d bytes\n", *len);
    else
        printf("\tFailed\n");
    return rv;
}

static apr_status_t socketWrite(apr_ssl_socket_t *sock,
                                apr_pollset_t *pollset,
                                const char *buf, apr_size_t *len)
{
    int lrv;
    const apr_pollfd_t *descs = NULL;
    apr_status_t rv;

    printf("::Writing to socket\n");
    rv = apr_ssl_socket_set_poll_events(sock, APR_POLLOUT);
    if (rv != APR_SUCCESS) {
        printf("\tUnable to change socket poll events!\n");
        return rv;
    }

    rv = apr_pollset_poll(pollset, 30 * APR_USEC_PER_SEC, &lrv, &descs);
    if (APR_STATUS_IS_TIMEUP(rv)) {
        printf("\tTime up!\n");
        return rv;
    }
    if (lrv != 1) {
        printf("\tIncorrect return count, %d\n", lrv);
        return rv;
    }
    if (descs[0].client_data != sock) {
        printf("\tWrong socket returned?!\n");
        return rv;
    }
    if ((descs[0].rtnevents & APR_POLLOUT) == 0) {
        printf("\tSocket wasn't ready? huh?\n");
        return rv;
    }
    rv = apr_ssl_socket_send(sock, buf, len);
    if (rv == APR_SUCCESS)
        printf("\tOK, wrote %d bytes\n", *len);
    else
        printf("\tFailed\n");
    return rv;
}

apr_status_t socketClose(apr_ssl_socket_t *sock, apr_pollset_t *pollset)
{
    apr_status_t rv;
    printf("::Closing socket\n");
    rv = apr_pollset_remove_ssl_socket(sock);
    if (rv != APR_SUCCESS)
        printf("\tUnable to remove socket from pollset?\n");
    rv = apr_ssl_socket_close(sock);
    if (rv != APR_SUCCESS)
        printf("\tFailed to close SSL socket\n");
    else
        printf("\tOK\n");
    return rv;
}


int main(int argc, const char * const * argv)
{
    apr_pool_t *pool;
    apr_ssl_factory_t *asf = NULL;
    apr_status_t rv;
    apr_pollset_t *pollset;

    (void) apr_initialize();
    apr_pool_create(&pool, NULL);
    atexit(apr_terminate);

    printf("SSL Library: %s\n", apr_ssl_library_name());

    if (apr_pollset_create(&pollset, 1, pool, 0) != APR_SUCCESS) {
        printf("Failed to create pollset!\n");
        exit(1);
    }

    if (apr_ssl_factory_create(&asf, NULL, NULL, NULL, 
                               APR_SSL_FACTORY_CLIENT, pool) != APR_SUCCESS) {
        fprintf(stderr, "Unable to create client factory\n");
    } else {
        int i;
        for(i = 0; tests[i].host; i++) {
            apr_ssl_socket_t *sslSock = createSocket(asf, pollset, pool, 0);
            if (!sslSock)
                continue;

            rv = connectSocket(sslSock, tests[i].host, tests[i].port, pool);
            if (rv == APR_SUCCESS) {
                apr_size_t len = strlen(tests[i].request);
                rv = socketWrite(sslSock, pollset, tests[i].request, &len);
                if (rv == APR_SUCCESS) {
                    char buffer[4096];
                    len = 4096;
                    rv = socketRead(sslSock, pollset, buffer, &len);
                }
            }
            socketClose(sslSock, pollset);
        }
    }

    apr_pollset_destroy(pollset);
    apr_pool_destroy(pool);

    return 0;
}