Search code examples
c++csslgnutls

SNI in GNUTLS, when to ask?


So, I'm attempting to implement SNI in my GNUTLS server.

My code:

if(sniCallback != NULL) {
    size_t ds = 256;
    char data[ds];
    unsigned int type = (unsigned int)GNUTLS_NAME_DNS;
    int snir = gnutls_server_name_get(sessiond, &data[0], &ds, &type, (unsigned int)0);
    data[ds - 1] = 0; // the last one must always be \0
    if(snir >= 0) {
        jstring js = (*this)->NewStringUTF(this, data);
        jlong snicert = (*this)->CallLongMethod(this, sniCallback, (*this)->GetMethodID(this, (*this)->GetObjectClass(this, sniCallback), "getSNICert", "(Ljava/lang/String;)J"), js);
        if(snicert > 0L) {
            struct cert* oc = (struct cert*)snicert;
            gnutls_credentials_set(sessiond, GNUTLS_CRD_CERTIFICATE, oc->cert);
        }
    } else {
        printf("%i\n", snir);
    }
}

(Don't mind the JNI)

I get the -56 error codes, which is GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE, which the documentation translates to no SNI was sent (yet). How do I know WHEN I have the SNI information? AKA When do I get the Client Hello? I'm getting this directly from Chrome, so I know there is SNI. This is all NIO by the way.


Solution

  • For those on the same path as me, the answer is:

    gnutls_handshake_set_post_client_hello_function