summaryrefslogtreecommitdiffstats
path: root/test/cipherbytes_test.c
blob: 90a4c93c13814429e664495cd66facbda1fac5ae (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
/*
 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the OpenSSL licenses, (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.openssl.org/source/license.html
 * or in the file LICENSE in the source distribution.
 */

#include <string.h>
#include <stdio.h>

#include <openssl/opensslconf.h>
#include <openssl/err.h>
#include <openssl/e_os2.h>
#include <openssl/ssl.h>
#include <openssl/ssl3.h>
#include <openssl/tls1.h>

#include "e_os.h"

static int test_empty(SSL *s)
{
    STACK_OF(SSL_CIPHER) *sk = NULL, *scsv = NULL;
    const unsigned char bytes[] = {0x00};

    if (SSL_bytes_to_cipher_list(s, bytes, 0, 0, &sk, &scsv) ||
        sk != NULL || scsv != NULL)
        return 0;
    return 1;
}

static int test_unsupported(SSL *s)
{
    STACK_OF(SSL_CIPHER) *sk, *scsv;
    /* ECDH-RSA-AES256 (unsupported), ECDHE-ECDSA-AES128, <unassigned> */
    const unsigned char bytes[] = {0xc0, 0x0f, 0x00, 0x2f, 0x01, 0x00};

    if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 0, &sk, &scsv) ||
        sk == NULL || sk_SSL_CIPHER_num(sk) != 1 || scsv == NULL ||
        sk_SSL_CIPHER_num(scsv) != 0)
        return 0;
    if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
                "AES128-SHA") != 0)
        return 0;

    sk_SSL_CIPHER_free(sk);
    sk_SSL_CIPHER_free(scsv);
    return 1;
}

static int test_v2(SSL *s)
{
    STACK_OF(SSL_CIPHER) *sk, *scsv;
    /* ECDHE-ECDSA-AES256GCM, SSL2_RC4_1238_WITH_MD5,
     * ECDHE-ECDSA-CHACHA20-POLY1305 */
    const unsigned char bytes[] = {0x00, 0x00, 0x35, 0x01, 0x00, 0x80,
                                   0x00, 0x00, 0x33};

    if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 1, &sk, &scsv) ||
        sk == NULL || sk_SSL_CIPHER_num(sk) != 2 || scsv == NULL ||
        sk_SSL_CIPHER_num(scsv) != 0)
        return 0;
    if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
               "AES256-SHA") != 0 ||
        strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
               "DHE-RSA-AES128-SHA") != 0)
        return 0;
    sk_SSL_CIPHER_free(sk);
    sk_SSL_CIPHER_free(scsv);
    return 1;
}

static int test_v3(SSL *s)
{
    STACK_OF(SSL_CIPHER) *sk, *scsv;
    /* ECDHE-ECDSA-AES256GCM, ECDHE-ECDSA-CHACHAPOLY, DHE-RSA-AES256GCM,
     * EMPTY-RENEGOTIATION-INFO-SCSV, FALLBACK-SCSV */
    const unsigned char bytes[] = {0x00, 0x2f, 0x00, 0x33, 0x00, 0x9f, 0x00, 0xff,
                                   0x56, 0x00};

    if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 0, &sk, &scsv) ||
        sk == NULL || sk_SSL_CIPHER_num(sk) != 3 || scsv == NULL ||
        sk_SSL_CIPHER_num(scsv) != 2)
        return 0;
    if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
               "AES128-SHA") != 0 ||
        strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
               "DHE-RSA-AES128-SHA") != 0 ||
        strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 2)),
               "DHE-RSA-AES256-GCM-SHA384") != 0 ||
        strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 0)),
               "TLS_EMPTY_RENEGOTIATION_INFO_SCSV") != 0 ||
        strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 1)),
               "TLS_FALLBACK_SCSV") != 0)
        return 0;
    sk_SSL_CIPHER_free(sk);
    sk_SSL_CIPHER_free(scsv);
    return 1;
}

int main(int argc, char **argv)
{
    SSL_CTX *ctx;
    SSL *s;

    ctx = SSL_CTX_new(TLS_server_method());
    s = SSL_new(ctx);
    OPENSSL_assert(s != NULL);

    if (!test_empty(s))
        return 1;

    if (!test_unsupported(s))
        return 1;

    if (!test_v2(s))
        return 1;

    if (!test_v3(s))
        return 1;

    printf("PASS\n");
    SSL_free(s);
    SSL_CTX_free(ctx);
    return 0;
}