qcrypto_tls_session_new(QCryptoTLSCreds *creds,

                        const char *hostname,

                        const char *aclname,

                        QCryptoTLSCredsEndpoint endpoint,

                        Error **errp)

{

    QCryptoTLSSession *session;

    int ret;



    session = g_new0(QCryptoTLSSession, 1);

    trace_qcrypto_tls_session_new(

        session, creds, hostname ? hostname : "<none>",

        aclname ? aclname : "<none>", endpoint);



    if (hostname) {

        session->hostname = g_strdup(hostname);

    }

    if (aclname) {

        session->aclname = g_strdup(aclname);

    }

    session->creds = creds;

    object_ref(OBJECT(creds));



    if (creds->endpoint != endpoint) {

        error_setg(errp, "Credentials endpoint doesn't match session");

        goto error;

    }



    if (endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

        ret = gnutls_init(&session->handle, GNUTLS_SERVER);

    } else {

        ret = gnutls_init(&session->handle, GNUTLS_CLIENT);

    }

    if (ret < 0) {

        error_setg(errp, "Cannot initialize TLS session: %s",

                   gnutls_strerror(ret));

        goto error;

    }



    if (object_dynamic_cast(OBJECT(creds),

                            TYPE_QCRYPTO_TLS_CREDS_ANON)) {

        QCryptoTLSCredsAnon *acreds = QCRYPTO_TLS_CREDS_ANON(creds);



        ret = gnutls_priority_set_direct(session->handle,

                                         "NORMAL:+ANON-DH", NULL);

        if (ret < 0) {

            error_setg(errp, "Unable to set TLS session priority: %s",

                       gnutls_strerror(ret));

            goto error;

        }

        if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

            ret = gnutls_credentials_set(session->handle,

                                         GNUTLS_CRD_ANON,

                                         acreds->data.server);

        } else {

            ret = gnutls_credentials_set(session->handle,

                                         GNUTLS_CRD_ANON,

                                         acreds->data.client);

        }

        if (ret < 0) {

            error_setg(errp, "Cannot set session credentials: %s",

                       gnutls_strerror(ret));

            goto error;

        }

    } else if (object_dynamic_cast(OBJECT(creds),

                                   TYPE_QCRYPTO_TLS_CREDS_X509)) {

        QCryptoTLSCredsX509 *tcreds = QCRYPTO_TLS_CREDS_X509(creds);



        ret = gnutls_set_default_priority(session->handle);

        if (ret < 0) {

            error_setg(errp, "Cannot set default TLS session priority: %s",

                       gnutls_strerror(ret));

            goto error;

        }

        ret = gnutls_credentials_set(session->handle,

                                     GNUTLS_CRD_CERTIFICATE,

                                     tcreds->data);

        if (ret < 0) {

            error_setg(errp, "Cannot set session credentials: %s",

                       gnutls_strerror(ret));

            goto error;

        }



        if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

            /* This requests, but does not enforce a client cert.

             * The cert checking code later does enforcement */

            gnutls_certificate_server_set_request(session->handle,

                                                  GNUTLS_CERT_REQUEST);

        }

    } else {

        error_setg(errp, "Unsupported TLS credentials type %s",

                   object_get_typename(OBJECT(creds)));

        goto error;

    }



    gnutls_transport_set_ptr(session->handle, session);

    gnutls_transport_set_push_function(session->handle,

                                       qcrypto_tls_session_push);

    gnutls_transport_set_pull_function(session->handle,

                                       qcrypto_tls_session_pull);



    return session;



 error:

    qcrypto_tls_session_free(session);

    return NULL;

}

qcrypto_tls_session_new(QCryptoTLSCreds *creds,

                        const char *hostname,

                        const char *aclname,

                        QCryptoTLSCredsEndpoint endpoint,

                        Error **errp)

{

    QCryptoTLSSession *session;

    int ret;



    session = g_new0(QCryptoTLSSession, 1);

    trace_qcrypto_tls_session_new(

        session, creds, hostname ? hostname : "<none>",

        aclname ? aclname : "<none>", endpoint);



    if (hostname) {

        session->hostname = g_strdup(hostname);

    }

    if (aclname) {

        session->aclname = g_strdup(aclname);

    }

    session->creds = creds;

    object_ref(OBJECT(creds));



    if (creds->endpoint != endpoint) {

        error_setg(errp, "Credentials endpoint doesn't match session");

        goto error;

    }



    if (endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

        ret = gnutls_init(&session->handle, GNUTLS_SERVER);

    } else {

        ret = gnutls_init(&session->handle, GNUTLS_CLIENT);

    }

    if (ret < 0) {

        error_setg(errp, "Cannot initialize TLS session: %s",

                   gnutls_strerror(ret));

        goto error;

    }



    if (object_dynamic_cast(OBJECT(creds),

                            TYPE_QCRYPTO_TLS_CREDS_ANON)) {

        QCryptoTLSCredsAnon *acreds = QCRYPTO_TLS_CREDS_ANON(creds);



        ret = gnutls_priority_set_direct(session->handle,

                                         "NORMAL:+ANON-DH", NULL);

        if (ret < 0) {

            error_setg(errp, "Unable to set TLS session priority: %s",

                       gnutls_strerror(ret));

            goto error;

        }

        if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

            ret = gnutls_credentials_set(session->handle,

                                         GNUTLS_CRD_ANON,

                                         acreds->data.server);

        } else {

            ret = gnutls_credentials_set(session->handle,

                                         GNUTLS_CRD_ANON,

                                         acreds->data.client);

        }

        if (ret < 0) {

            error_setg(errp, "Cannot set session credentials: %s",

                       gnutls_strerror(ret));

            goto error;

        }

    } else if (object_dynamic_cast(OBJECT(creds),

                                   TYPE_QCRYPTO_TLS_CREDS_X509)) {

        QCryptoTLSCredsX509 *tcreds = QCRYPTO_TLS_CREDS_X509(creds);



        ret = gnutls_set_default_priority(session->handle);

        if (ret < 0) {

            error_setg(errp, "Cannot set default TLS session priority: %s",

                       gnutls_strerror(ret));

            goto error;

        }

        ret = gnutls_credentials_set(session->handle,

                                     GNUTLS_CRD_CERTIFICATE,

                                     tcreds->data);

        if (ret < 0) {

            error_setg(errp, "Cannot set session credentials: %s",

                       gnutls_strerror(ret));

            goto error;

        }



        if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

            



            gnutls_certificate_server_set_request(session->handle,

                                                  GNUTLS_CERT_REQUEST);

        }

    } else {

        error_setg(errp, "Unsupported TLS credentials type %s",

                   object_get_typename(OBJECT(creds)));

        goto error;

    }



    gnutls_transport_set_ptr(session->handle, session);

    gnutls_transport_set_push_function(session->handle,

                                       qcrypto_tls_session_push);

    gnutls_transport_set_pull_function(session->handle,

                                       qcrypto_tls_session_pull);



    return session;



 error:

    qcrypto_tls_session_free(session);

    return NULL;

}

FUNC_0(QCryptoTLSCreds *VAR_0,

                        const char *VAR_1,

                        const char *VAR_2,

                        QCryptoTLSCredsEndpoint VAR_3,

                        Error **VAR_4)

{

    QCryptoTLSSession *session;

    int VAR_5;



    session = g_new0(QCryptoTLSSession, 1);

    trace_qcrypto_tls_session_new(

        session, VAR_0, VAR_1 ? VAR_1 : "<none>",

        VAR_2 ? VAR_2 : "<none>", VAR_3);



    if (VAR_1) {

        session->VAR_1 = g_strdup(VAR_1);

    }

    if (VAR_2) {

        session->VAR_2 = g_strdup(VAR_2);

    }

    session->VAR_0 = VAR_0;

    object_ref(OBJECT(VAR_0));



    if (VAR_0->VAR_3 != VAR_3) {

        error_setg(VAR_4, "Credentials VAR_3 doesn't match session");

        goto error;

    }



    if (VAR_3 == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

        VAR_5 = gnutls_init(&session->handle, GNUTLS_SERVER);

    } else {

        VAR_5 = gnutls_init(&session->handle, GNUTLS_CLIENT);

    }

    if (VAR_5 < 0) {

        error_setg(VAR_4, "Cannot initialize TLS session: %s",

                   gnutls_strerror(VAR_5));

        goto error;

    }



    if (object_dynamic_cast(OBJECT(VAR_0),

                            TYPE_QCRYPTO_TLS_CREDS_ANON)) {

        QCryptoTLSCredsAnon *acreds = QCRYPTO_TLS_CREDS_ANON(VAR_0);



        VAR_5 = gnutls_priority_set_direct(session->handle,

                                         "NORMAL:+ANON-DH", NULL);

        if (VAR_5 < 0) {

            error_setg(VAR_4, "Unable to set TLS session priority: %s",

                       gnutls_strerror(VAR_5));

            goto error;

        }

        if (VAR_0->VAR_3 == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

            VAR_5 = gnutls_credentials_set(session->handle,

                                         GNUTLS_CRD_ANON,

                                         acreds->data.server);

        } else {

            VAR_5 = gnutls_credentials_set(session->handle,

                                         GNUTLS_CRD_ANON,

                                         acreds->data.client);

        }

        if (VAR_5 < 0) {

            error_setg(VAR_4, "Cannot set session credentials: %s",

                       gnutls_strerror(VAR_5));

            goto error;

        }

    } else if (object_dynamic_cast(OBJECT(VAR_0),

                                   TYPE_QCRYPTO_TLS_CREDS_X509)) {

        QCryptoTLSCredsX509 *tcreds = QCRYPTO_TLS_CREDS_X509(VAR_0);



        VAR_5 = gnutls_set_default_priority(session->handle);

        if (VAR_5 < 0) {

            error_setg(VAR_4, "Cannot set default TLS session priority: %s",

                       gnutls_strerror(VAR_5));

            goto error;

        }

        VAR_5 = gnutls_credentials_set(session->handle,

                                     GNUTLS_CRD_CERTIFICATE,

                                     tcreds->data);

        if (VAR_5 < 0) {

            error_setg(VAR_4, "Cannot set session credentials: %s",

                       gnutls_strerror(VAR_5));

            goto error;

        }



        if (VAR_0->VAR_3 == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {

            



            gnutls_certificate_server_set_request(session->handle,

                                                  GNUTLS_CERT_REQUEST);

        }

    } else {

        error_setg(VAR_4, "Unsupported TLS credentials type %s",

                   object_get_typename(OBJECT(VAR_0)));

        goto error;

    }



    gnutls_transport_set_ptr(session->handle, session);

    gnutls_transport_set_push_function(session->handle,

                                       qcrypto_tls_session_push);

    gnutls_transport_set_pull_function(session->handle,

                                       qcrypto_tls_session_pull);



    return session;



 error:

    qcrypto_tls_session_free(session);

    return NULL;

}

FUNC_0(QCryptoTLSCreds *VAR_0,
const char *VAR_1,
const char *VAR_2,
QCryptoTLSCredsEndpoint VAR_3,
Error **VAR_4)
{ QCryptoTLSSession *session; int VAR_5; session = g_new0(QCryptoTLSSession, 1); trace_qcrypto_tls_session_new(
session, VAR_0, VAR_1 ? VAR_1 : "<none>",
VAR_2 ? VAR_2 : "<none>", VAR_3); if (VAR_1) { session->VAR_1 = g_strdup(VAR_1); } if (VAR_2) { session->VAR_2 = g_strdup(VAR_2); } session->VAR_0 = VAR_0; object_ref(OBJECT(VAR_0)); if (VAR_0->VAR_3 != VAR_3) { error_setg(VAR_4, "Credentials VAR_3 doesn't match session"); goto error; } if (VAR_3 == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { VAR_5 = gnutls_init(&session->handle, GNUTLS_SERVER); } else { VAR_5 = gnutls_init(&session->handle, GNUTLS_CLIENT); } if (VAR_5 < 0) { error_setg(VAR_4, "Cannot initialize TLS session: %s",
gnutls_strerror(VAR_5)); goto error; } if (object_dynamic_cast(OBJECT(VAR_0),
TYPE_QCRYPTO_TLS_CREDS_ANON)) { QCryptoTLSCredsAnon *acreds = QCRYPTO_TLS_CREDS_ANON(VAR_0); VAR_5 = gnutls_priority_set_direct(session->handle,
"NORMAL:+ANON-DH", NULL); if (VAR_5 < 0) { error_setg(VAR_4, "Unable to set TLS session priority: %s",
gnutls_strerror(VAR_5)); goto error; } if (VAR_0->VAR_3 == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { VAR_5 = gnutls_credentials_set(session->handle,
GNUTLS_CRD_ANON,
acreds->data.server); } else { VAR_5 = gnutls_credentials_set(session->handle,
GNUTLS_CRD_ANON,
acreds->data.client); } if (VAR_5 < 0) { error_setg(VAR_4, "Cannot set session credentials: %s",
gnutls_strerror(VAR_5)); goto error; } } else if (object_dynamic_cast(OBJECT(VAR_0), TYPE_QCRYPTO_TLS_CREDS_X509)) { QCryptoTLSCredsX509 *tcreds = QCRYPTO_TLS_CREDS_X509(VAR_0); VAR_5 = gnutls_set_default_priority(session->handle); if (VAR_5 < 0) { error_setg(VAR_4, "Cannot set default TLS session priority: %s",
gnutls_strerror(VAR_5)); goto error; } VAR_5 = gnutls_credentials_set(session->handle,
GNUTLS_CRD_CERTIFICATE,
tcreds->data); if (VAR_5 < 0) { error_setg(VAR_4, "Cannot set session credentials: %s",
gnutls_strerror(VAR_5)); goto error; } if (VAR_0->VAR_3 == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { gnutls_certificate_server_set_request(session->handle,
GNUTLS_CERT_REQUEST); } } else { error_setg(VAR_4, "Unsupported TLS credentials type %s",
object_get_typename(OBJECT(VAR_0))); goto error; } gnutls_transport_set_ptr(session->handle, session); gnutls_transport_set_push_function(session->handle,
qcrypto_tls_session_push); gnutls_transport_set_pull_function(session->handle,
qcrypto_tls_session_pull); return session; error:
qcrypto_tls_session_free(session); return NULL; }