File: openssl_h_Macros.java

package info (click to toggle)
tomcat9 9.0.111-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,852 kB
  • sloc: java: 381,732; xml: 70,399; jsp: 4,682; sh: 1,336; perl: 324; makefile: 18; ansic: 14
file content (514 lines) | stat: -rw-r--r-- 19,772 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * 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.
 */

package org.apache.tomcat.util.openssl;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.MemorySegment;
import java.lang.invoke.MethodHandle;

import static org.apache.tomcat.util.openssl.openssl_h.*;

/**
 * Functional macros not handled by jextract.
 */
@SuppressWarnings("javadoc")
public class openssl_h_Macros {


    /**
     * Set maximum protocol version on the given context.
     * {@snippet lang = c : # define SSL_CTX_set_max_proto_version(sslCtx, version) \
     *    SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_MAX_PROTO_VERSION, version, NULL)
     * }
     *
     * @param sslCtx  the SSL context
     * @param version the maximum version
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set_max_proto_version(MemorySegment sslCtx, long version) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_set_max_proto_version";
                static final FunctionDescriptor DESC =
                        FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER, openssl_h.C_LONG);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, Long.valueOf(version));
                }
                return (long) mh$.invokeExact(sslCtx, version);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_MAX_PROTO_VERSION(), version, MemorySegment.NULL);
        }
    }


    /**
     * Set minimum protocol version on the given context.
     * {@snippet lang = c : # define SSL_CTX_set_min_proto_version(sslCtx, version) \
     *    SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_MIN_PROTO_VERSION, version, NULL)
     * }
     *
     * @param sslCtx  the SSL context
     * @param version the maximum version
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set_min_proto_version(MemorySegment sslCtx, long version) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_set_min_proto_version";
                static final FunctionDescriptor DESC =
                        FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER, openssl_h.C_LONG);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, Long.valueOf(version));
                }
                return (long) mh$.invokeExact(sslCtx, version);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_MIN_PROTO_VERSION(), version, MemorySegment.NULL);
        }
    }


    /**
     * Get the session cache size.
     * {@snippet lang = c : # define SSL_CTX_sess_get_cache_size(sslCtx) \
     *    SSL_CTX_ctrl(sslCtx, SSL_CTRL_GET_SESS_CACHE_SIZE, 0, NULL)
     * }
     *
     * @param sslCtx the SSL context
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_sess_get_cache_size(MemorySegment sslCtx) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_sess_get_cache_size";
                static final FunctionDescriptor DESC = FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx);
                }
                return (long) mh$.invokeExact(sslCtx);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_GET_SESS_CACHE_SIZE(), 0, MemorySegment.NULL);
        }
    }


    /**
     * Set the session cache size.
     * {@snippet lang = c : # define SSL_CTX_sess_set_cache_size(sslCtx, t) \
     *    SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_SESS_CACHE_SIZE, t, NULL)
     * }
     *
     * @param sslCtx    the SSL context
     * @param cacheSize the session cache size
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_sess_set_cache_size(MemorySegment sslCtx, long cacheSize) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_sess_set_cache_size";
                static final FunctionDescriptor DESC =
                        FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER, openssl_h.C_LONG);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, Long.valueOf(cacheSize));
                }
                return (long) mh$.invokeExact(sslCtx, cacheSize);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_SESS_CACHE_SIZE(), cacheSize, MemorySegment.NULL);
        }
    }


    /**
     * Get the session cache mode.
     * {@snippet lang = c : # define SSL_CTX_get_session_cache_mode(sslCtx) \
     *    SSL_CTX_ctrl(sslCtx, SSL_CTRL_GET_SESS_CACHE_MODE, 0, NULL)
     * }
     *
     * @param sslCtx the SSL context
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_get_session_cache_mode(MemorySegment sslCtx) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_get_session_cache_mode";
                static final FunctionDescriptor DESC = FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx);
                }
                return (long) mh$.invokeExact(sslCtx);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_GET_SESS_CACHE_MODE(), 0, MemorySegment.NULL);
        }
    }


    /**
     * Set the session cache mode.
     * {@snippet lang = c : # define SSL_CTX_set_session_cache_mode(sslCtx, m) \
     *    SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_SESS_CACHE_MODE, m, NULL)
     * }
     *
     * @param sslCtx    the SSL context
     * @param cacheMode the cache mode, SSL_SESS_CACHE_OFF to disable
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set_session_cache_mode(MemorySegment sslCtx, long cacheMode) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_set_session_cache_mode";
                static final FunctionDescriptor DESC =
                        FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER, openssl_h.C_LONG);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, Long.valueOf(cacheMode));
                }
                return (long) mh$.invokeExact(sslCtx, cacheMode);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_SESS_CACHE_MODE(), cacheMode, MemorySegment.NULL);
        }
    }


    /**
     * Set the certificate.
     * {@snippet lang = c : # define SSL_CTX_add0_chain_cert(sslCtx,x509) \
     *    SSL_CTX_ctrl(sslCtx, SSL_CTRL_CHAIN_CERT, 0, (char *)(x509))
     * }
     *
     * @param sslCtx the SSL context
     * @param x509   the certificate
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_add0_chain_cert(MemorySegment sslCtx, MemorySegment x509) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_add0_chain_cert";
                static final FunctionDescriptor DESC =
                        FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER, openssl_h.C_POINTER);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, x509);
                }
                return (long) mh$.invokeExact(sslCtx, x509);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_CHAIN_CERT(), 0, x509);
        }
    }


    /**
     * Set ticket keys.
     * {@snippet lang = c : # define SSL_CTX_set_tlsext_ticket_keys(ctx, keys, keylen) \
     *    SSL_CTX_ctrl((ctx),SSL_CTRL_SET_TLSEXT_TICKET_KEYS, (keylen), (keys))
     * }
     *
     * @param sslCtx    the SSL context
     * @param keys      the keys
     * @param keyLength the length
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set_tlsext_ticket_keys(MemorySegment sslCtx, MemorySegment keys, long keyLength) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_set_tlsext_ticket_keys";
                static final FunctionDescriptor DESC = FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER,
                        openssl_h.C_POINTER, openssl_h.C_LONG);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, keys, Long.valueOf(keyLength));
                }
                return (long) mh$.invokeExact(sslCtx, keys, keyLength);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_TLSEXT_TICKET_KEYS(), keyLength, keys);
        }
    }


    /**
     * Read the specified file.
     * {@snippet lang = c : # define BIO_read_filename(b,name) \
     *    (int)BIO_ctrl(b,BIO_C_SET_FILENAME,BIO_CLOSE|BIO_FP_READ,(char *)(name))
     * }
     *
     * @param bio  The BIO to read into
     * @param name the file name
     *
     * @return > 0 if successful
     */
    public static long BIO_read_filename(MemorySegment bio, MemorySegment name) {
        return BIO_ctrl(bio, BIO_C_SET_FILENAME(), BIO_CLOSE() | BIO_FP_READ(), name);
    }


    /**
     * Set tmp dh.
     * {@snippet lang = c : # define SSL_CTX_set_tmp_dh(sslCtx,dh) \
     *    SSL_CTX_ctrl(sslCtx,SSL_CTRL_SET_TMP_DH,0,(char *)(dh))
     * }
     *
     * @param sslCtx the SSL context
     * @param dh     the dh
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set_tmp_dh(MemorySegment sslCtx, MemorySegment dh) {
        if (openssl_h_Compatibility.BORINGSSL) {
            return 1;
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_TMP_DH(), 0, dh);
        }
    }


    /**
     * Set tmp ecdh.
     * {@snippet lang = c : # define SSL_CTX_set_tmp_ecdh(sslCtx,ecdh) \
     *    SSL_CTX_ctrl(sslCtx,SSL_CTRL_SET_TMP_ECDH,0,(char *)(ecdh))
     * }
     *
     * @param sslCtx the SSL context
     * @param ecdh   the ecdh
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set_tmp_ecdh(MemorySegment sslCtx, MemorySegment ecdh) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_set_tmp_ecdh";
                static final FunctionDescriptor DESC =
                        FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER, openssl_h.C_POINTER);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, ecdh);
                }
                return (long) mh$.invokeExact(sslCtx, ecdh);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_TMP_ECDH(), 0, ecdh);
        }
    }


    /**
     * Free memory.
     * {@snippet lang = c : # define OPENSSL_free(addr) CRYPTO_free(addr, OPENSSL_FILE, OPENSSL_LINE)
     * }
     *
     * @param segment The address to free
     */
    public static void OPENSSL_free(MemorySegment segment) {
        CRYPTO_free(segment, MemorySegment.NULL, 0);
    }


    /**
     * Reset BIO position.
     * {@snippet lang = c : # define BIO_reset(b) (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL)
     * }
     *
     * @param bio The BIO to reset
     *
     * @return > 0 if successful
     */
    public static long BIO_reset(MemorySegment bio) {
        return BIO_ctrl(bio, BIO_CTRL_RESET(), 0, MemorySegment.NULL);
    }


    /**
     * Set NIDs of groups in preference order.
     * {@snippet lang = c : # define SSL_CTX_set1_curves SSL_CTX_set1_groups
     *    # define SSL_CTX_set1_groups(ctx, glist, glistlen) \
     *       SSL_CTX_ctrl(ctx,SSL_CTRL_SET_GROUPS,glistlen,(int *)(glist))
     * }
     *
     * @param sslCtx     the SSL context
     * @param groupsList the groups list
     * @param listLength the list length
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set1_groups(MemorySegment sslCtx, MemorySegment groupsList, int listLength) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_set1_groups";
                static final FunctionDescriptor DESC = FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER,
                        openssl_h.C_POINTER, openssl_h.C_INT);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, groupsList, Integer.valueOf(listLength));
                }
                return (long) mh$.invokeExact(sslCtx, groupsList, listLength);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_GROUPS(), listLength, groupsList);
        }
    }


    /**
     * Set list of groups in preference order.
     * {@snippet lang = c :
     * # define SSL_set1_groups_list(s, str) \
     *          SSL_ctrl(s,SSL_CTRL_SET_GROUPS_LIST,0,(char *)(str))
     * }
     *
     * @param sslCtx     the SSL context
     * @param groupsList the groups list as a String
     *
     * @return > 0 if successful
     */
    public static long SSL_CTX_set1_groups_list(MemorySegment sslCtx, MemorySegment groupsList) {
        if (openssl_h_Compatibility.BORINGSSL) {
            class Holder {
                static final String NAME = "SSL_CTX_set1_groups_list";
                static final FunctionDescriptor DESC = FunctionDescriptor.of(openssl_h.C_LONG, openssl_h.C_POINTER,
                        openssl_h.C_POINTER);
                static final MethodHandle MH = Linker.nativeLinker().downcallHandle(openssl_h.findOrThrow(NAME), DESC);
            }
            var mh$ = Holder.MH;
            try {
                if (openssl_h.TRACE_DOWNCALLS) {
                    openssl_h.traceDowncall(Holder.NAME, sslCtx, groupsList);
                }
                return (long) mh$.invokeExact(sslCtx, groupsList);
            } catch (Throwable ex$) {
                throw new AssertionError("should not reach here", ex$);
            }
        } else {
            return SSL_CTX_ctrl(sslCtx, SSL_CTRL_SET_GROUPS_LIST(), 0, groupsList);
        }
    }


    /**
     * Pass a path from which certificates are loaded into the store.
     * {@snippet lang = c : # define X509_LOOKUP_add_dir(x,name,type) \
     *    X509_LOOKUP_ctrl((x),X509_L_ADD_DIR,(name),(long)(type),NULL)
     * }
     *
     * @param x509Lookup the X509 lookup
     * @param name       the path name
     * @param type       the type used
     *
     * @return > 0 if successful
     */
    public static long X509_LOOKUP_add_dir(MemorySegment x509Lookup, MemorySegment name, long type) {
        return X509_LOOKUP_ctrl(x509Lookup, X509_L_ADD_DIR(), name, X509_FILETYPE_PEM(), MemorySegment.NULL);
    }


    /**
     * Pass a file which will be loaded into the store.
     * {@snippet lang = c : # define X509_LOOKUP_load_file(x,name,type) \
     *    X509_LOOKUP_ctrl((x),X509_L_FILE_LOAD,(name),(long)(type),NULL)
     * }
     *
     * @param x509Lookup the X509 lookup
     * @param name       the path name
     * @param type       the type used
     *
     * @return > 0 if successful
     */
    public static long X509_LOOKUP_load_file(MemorySegment x509Lookup, MemorySegment name, long type) {
        return X509_LOOKUP_ctrl(x509Lookup, X509_L_FILE_LOAD(), name, X509_FILETYPE_PEM(), MemorySegment.NULL);
    }


    /**
     * Return the d2i_ECPKParameters symbol.
     *
     * @return the symbol
     */
    public static MemorySegment d2i_ECPKParameters$SYMBOL() {
        return openssl_h.findOrThrow("d2i_ECPKParameters");
    }

}