Search in sources :

Example 1 with ResourceScope

use of jdk.incubator.foreign.ResourceScope in project tomcat by apache.

the class OpenSSLContext method openSSLCallbackAlpnSelectProto.

// int SSL_callback_alpn_select_proto(SSL* ssl, const unsigned char **out, unsigned char *outlen,
// const unsigned char *in, unsigned int inlen, void *arg)
public static int openSSLCallbackAlpnSelectProto(MemoryAddress ssl, MemoryAddress out, MemoryAddress outlen, MemoryAddress in, int inlen, MemoryAddress arg) {
    ContextState state = getState(arg);
    if (state == null) {
        log.warn(sm.getString("context.noSSL", Long.valueOf(arg.toRawLongValue())));
        return SSL_TLSEXT_ERR_NOACK();
    }
    // However, the Java 17 API forces use of a scope later on, so create one for everything
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        byte[] advertisedBytes = in.asSegment(inlen, scope).toByteArray();
        for (byte[] negotiableProtocolBytes : state.negotiableProtocols) {
            for (int i = 0; i <= advertisedBytes.length - negotiableProtocolBytes.length; i++) {
                if (advertisedBytes[i] == negotiableProtocolBytes[0]) {
                    for (int j = 0; j < negotiableProtocolBytes.length; j++) {
                        if (advertisedBytes[i + j] == negotiableProtocolBytes[j]) {
                            if (j == negotiableProtocolBytes.length - 1) {
                                MemorySegment outSegment = out.asSegment(CLinker.C_POINTER.byteSize(), scope);
                                MemorySegment outlenSegment = outlen.asSegment(CLinker.C_CHAR.byteSize(), scope);
                                // Match
                                MemoryAccess.setAddress(outSegment, in.addOffset(i));
                                MemoryAccess.setByte(outlenSegment, (byte) negotiableProtocolBytes.length);
                                return SSL_TLSEXT_ERR_OK();
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        return SSL_TLSEXT_ERR_NOACK();
    }
}
Also used : ResourceScope(jdk.incubator.foreign.ResourceScope) AbstractEndpoint(org.apache.tomcat.util.net.AbstractEndpoint) MemorySegment(jdk.incubator.foreign.MemorySegment)

Aggregations

MemorySegment (jdk.incubator.foreign.MemorySegment)1 ResourceScope (jdk.incubator.foreign.ResourceScope)1 AbstractEndpoint (org.apache.tomcat.util.net.AbstractEndpoint)1