summaryrefslogtreecommitdiff
path: root/sysutils/tlsdate/files/patch-src_tlsdate-helper.c
blob: 7cebac427cc14aba40a244c515b8dbb168ee86f4 (plain) (blame)
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
Obtained from:	https://chromium-review.googlesource.com/c/chromiumos/third_party/tlsdate/+/549533

--- src/tlsdate-helper.c.orig	2015-05-28 18:49:40 UTC
+++ src/tlsdate-helper.c
@@ -370,11 +370,29 @@ xfree (void *ptr)
   free(ptr);
 }
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+size_t
+SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
+{
+  size_t ret = min(outlen, sizeof(uint32_t));
+  // Per https://www.openssl.org/docs/man1.1.0/ssl/SSL_get_server_random.html
+  // If outlen is 0, return the maximum number of bytes that would be copied.
+  if (!outlen)
+    return sizeof(uint32_t);
+  memcpy(out, ssl->s3->server_random, ret);
+  return ret;
+}
+#endif
+
 void
 openssl_time_callback (const SSL* ssl, int where, int ret)
 {
   if (where == SSL_CB_CONNECT_LOOP &&
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
       (ssl->state == SSL3_ST_CR_SRVR_HELLO_A || ssl->state == SSL3_ST_CR_SRVR_HELLO_B))
+#else
+      (SSL_get_state(ssl) == TLS_ST_CR_SRVR_HELLO))
+#endif
   {
     // XXX TODO: If we want to trust the remote system for time,
     // can we just read that time out of the remote system and if the
@@ -387,7 +405,7 @@ openssl_time_callback (const SSL* ssl, i
     uint32_t max_reasonable_time = MAX_REASONABLE_TIME;
     uint32_t server_time;
     verb("V: freezing time for x509 verification");
-    memcpy(&server_time, ssl->s3->server_random, sizeof(uint32_t));
+    SSL_get_server_random(ssl, (unsigned char *)&server_time, sizeof (uint32_t));
     if (compiled_time < ntohl(server_time)
         &&
         ntohl(server_time) < max_reasonable_time)
@@ -395,7 +413,7 @@ openssl_time_callback (const SSL* ssl, i
       verb("V: remote peer provided: %d, preferred over compile time: %d",
             ntohl(server_time), compiled_time);
       verb("V: freezing time with X509_VERIFY_PARAM_set_time");
-      X509_VERIFY_PARAM_set_time(ssl->ctx->cert_store->param,
+      X509_VERIFY_PARAM_set_time(SSL_get0_param((SSL *)ssl),
                                  (time_t) ntohl(server_time) + 86400);
     } else {
       die("V: the remote server is a false ticker! server: %d compile: %d",
@@ -404,6 +422,12 @@ openssl_time_callback (const SSL* ssl, i
   }
 }
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#define EVP_PKEY_BN_bits(p, k) BN_num_bits((p)->pkey.k)
+#else
+#define EVP_PKEY_BN_bits(p, k) EVP_PKEY_bits(p)
+#endif
+
 uint32_t
 get_certificate_keybits (EVP_PKEY *public_key)
 {
@@ -411,39 +435,39 @@ get_certificate_keybits (EVP_PKEY *publi
     In theory, we could use check_bitlen_dsa() and check_bitlen_rsa()
    */
   uint32_t key_bits;
-  switch (public_key->type)
+  switch (EVP_PKEY_id(public_key))
   {
     case EVP_PKEY_RSA:
       verb("V: key type: EVP_PKEY_RSA");
-      key_bits = BN_num_bits(public_key->pkey.rsa->n);
+      key_bits = EVP_PKEY_BN_bits(public_key, rsa->n);
       break;
     case EVP_PKEY_RSA2:
       verb("V: key type: EVP_PKEY_RSA2");
-      key_bits = BN_num_bits(public_key->pkey.rsa->n);
+      key_bits = EVP_PKEY_BN_bits(public_key, rsa->n);
       break;
     case EVP_PKEY_DSA:
       verb("V: key type: EVP_PKEY_DSA");
-      key_bits = BN_num_bits(public_key->pkey.dsa->p);
+      key_bits = EVP_PKEY_BN_bits(public_key, dsa->p);
       break;
     case EVP_PKEY_DSA1:
       verb("V: key type: EVP_PKEY_DSA1");
-      key_bits = BN_num_bits(public_key->pkey.dsa->p);
+      key_bits = EVP_PKEY_BN_bits(public_key, dsa->p);
       break;
     case EVP_PKEY_DSA2:
       verb("V: key type: EVP_PKEY_DSA2");
-      key_bits = BN_num_bits(public_key->pkey.dsa->p);
+      key_bits = EVP_PKEY_BN_bits(public_key, dsa->p);
       break;
     case EVP_PKEY_DSA3:
       verb("V: key type: EVP_PKEY_DSA3");
-      key_bits = BN_num_bits(public_key->pkey.dsa->p);
+      key_bits = EVP_PKEY_BN_bits(public_key, dsa->p);
       break;
     case EVP_PKEY_DSA4:
       verb("V: key type: EVP_PKEY_DSA4");
-      key_bits = BN_num_bits(public_key->pkey.dsa->p);
+      key_bits = EVP_PKEY_BN_bits(public_key, dsa->p);
       break;
     case EVP_PKEY_DH:
       verb("V: key type: EVP_PKEY_DH");
-      key_bits = BN_num_bits(public_key->pkey.dh->pub_key);
+      key_bits = EVP_PKEY_BN_bits(public_key, dh->pub_key);
       break;
     case EVP_PKEY_EC:
       verb("V: key type: EVP_PKEY_EC");
@@ -681,7 +705,9 @@ check_san (SSL *ssl, const char *hostnam
 
         int j;
         void *extvalstr;
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
         const unsigned char *tmp;
+#endif
 
         STACK_OF(CONF_VALUE) *val;
         CONF_VALUE *nval;
@@ -695,6 +721,7 @@ check_san (SSL *ssl, const char *hostnam
           break;
         }
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
         tmp = ext->value->data;
         if (method->it)
         {
@@ -703,7 +730,9 @@ check_san (SSL *ssl, const char *hostnam
         } else {
           extvalstr = method->d2i(NULL, &tmp, ext->value->length);
         }
-
+#else
+	extvalstr = X509V3_EXT_d2i(ext);
+#endif
         if (!extvalstr)
         {
           break;
@@ -886,11 +915,11 @@ check_key_length (SSL *ssl)
   }
 
   key_bits = get_certificate_keybits (public_key);
-  if (MIN_PUB_KEY_LEN >= key_bits && public_key->type != EVP_PKEY_EC)
+  if (MIN_PUB_KEY_LEN >= key_bits && EVP_PKEY_id(public_key) != EVP_PKEY_EC)
   {
     die ("Unsafe public key size: %d bits", key_bits);
   } else {
-     if (public_key->type == EVP_PKEY_EC)
+     if (EVP_PKEY_id(public_key) == EVP_PKEY_EC)
        if(key_bits >= MIN_ECC_PUB_KEY_LEN
           && key_bits <= MAX_ECC_PUB_KEY_LEN)
        {
@@ -1129,20 +1158,34 @@ run_ssl (uint32_t *time_map, int time_is
   SSL_library_init();
 
   ctx = NULL;
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
   if (0 == strcmp("sslv23", protocol))
   {
     verb ("V: using SSLv23_client_method()");
     ctx = SSL_CTX_new(SSLv23_client_method());
+#ifndef OPENSSL_NO_SSL3_METHOD
   } else if (0 == strcmp("sslv3", protocol))
   {
     verb ("V: using SSLv3_client_method()");
     ctx = SSL_CTX_new(SSLv3_client_method());
+#endif
+#ifndef OPENSSL_NO_TLS1_METHOD
   } else if (0 == strcmp("tlsv1", protocol))
   {
     verb ("V: using TLSv1_client_method()");
     ctx = SSL_CTX_new(TLSv1_client_method());
+#endif
   } else
     die("Unsupported protocol `%s'", protocol);
+#else /* OPENSSL_VERSION_NUMBER < 0x10100000L */
+  /*
+   * Use general-purpose version-flexible SSL/TLS method. The actual protocol
+   * version used will be negotiated to the highest version mutually supported
+   * by the client and the server.
+   */
+  verb ("V: using TLS_client_method()\n");
+  ctx = SSL_CTX_new(TLS_client_method());
+#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
 
   if (ctx == NULL)
     die("OpenSSL failed to support protocol `%s'", protocol);
@@ -1204,7 +1247,7 @@ run_ssl (uint32_t *time_map, int time_is
 
   // from /usr/include/openssl/ssl3.h
   //  ssl->s3->server_random is an unsigned char of 32 bits
-  memcpy(&result_time, ssl->s3->server_random, sizeof (uint32_t));
+  SSL_get_server_random(ssl, (unsigned char *)&result_time, sizeof (uint32_t));
   verb("V: In TLS response, T=%lu", (unsigned long)ntohl(result_time));
 
   if (http) {