Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
    tcp_ctx *tcp;
    tls_ctx *ctx;
    char    *host, *port;
    
    if (argc != 3) {
      printf("\nusage: tlscmd <host> <port>\n");
      return 0;
    }
    
    host = argv[1];
    port = argv[2];
    
    printf ("  [ connecting to %s:%i...\n", host, atoi(port));
    tcp = tcp_new_ctx(AF_INET, host, port);
    if (tcp!=NULL) {
      printf ("  [ host resolved to %s.\n", tcp_addr2ip(tcp));
      ctx = tls_new_ctx();
      if (ctx!=NULL) {
        printf ("  [ TLS client initialized.\n");
        tls_client(tcp, ctx);
        tls_free_ctx(ctx);
      }
      tcp_free_ctx(tcp);
    }
    return 0;
}
Ejemplo n.º 2
0
VMINT vm_tls_new_ctx(vm_tls_version_enum ver, vm_socket_type_enum sock_type, VMINT apn, vm_tls_side_enum side, callback_t cb)
{
    kal_int32 ret;
    vm_tls_context_t * ctx_p = NULL;
    VMUINT acct_id = 0;
    kal_uint8 val;
    
    MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_S, 1, __LINE__);

    
    ctx_p = vm_tls_malloc_ctx();

    if (NULL == ctx_p)
    {
        MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E1, 1, __LINE__);
        return VM_TLS_RET_BASE -2;
    }

    ret = tls_new_ctx((tls_version_enum)ver,
        (tls_side_enum)side,
        MOD_MMI,
        STR_MRE_TITLE);

    if (ret < 0)
    {
        vm_tls_free_ctx(ctx_p);
        MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E2, 1, ret);
        return ret;
    }

    ctx_p->tls_ctx = ret;
    ctx_p->cb = cb;
    ctx_p->soc_id = 0;
    ctx_p->p_hdl = vm_pmng_get_current_handle();


    if (VM_E_SOC_SUCCESS != vm_get_encoded_dtacct_id(apn, &acct_id))
    {
        MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E3, 1, __LINE__);
        return VM_TLS_RET_BASE -3;
    }

    
    ctx_p->soc_id = soc_create(SOC_PF_INET, sock_type, 0, MOD_MMI, acct_id);
    if (0 > ctx_p->soc_id)
    {
        // TODO: print ctx_p->soc_id
        ctx_p->soc_id = 0;
        MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E4, 1, __LINE__);
        return VM_TLS_RET_BASE -3;
    }
    
    val = KAL_TRUE;
    ret = soc_setsockopt(ctx_p->soc_id, SOC_NBIO, &val, sizeof(val));
    if (ret < 0)
    {
        MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E5, 1, __LINE__);
        return VM_TLS_RET_BASE -5;
    }

    val = SOC_READ | SOC_WRITE | SOC_CONNECT | SOC_CLOSE;
    ret = soc_setsockopt(ctx_p->soc_id, SOC_ASYNC, &val, sizeof(val));
    if (ret < 0)
    {
        MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E6, 1, __LINE__);
        return VM_TLS_RET_BASE -6;
    }
    
    MMI_TRACE(TRACE_GROUP_8, TRC_MRE_DLS_LOG, 
        ctx_p->tls_ctx,
        ctx_p->soc_id,
        ctx_p->p_hdl,
        0,
        0,
        ctx_p->cb,
        1, __LINE__);
    
    if (0 == g_ref_count++)
    {
        mmi_frm_set_protocol_event_handler(MSG_ID_APP_SOC_NOTIFY_IND, (PsIntFuncPtr)vm_tls_notify_cb, MMI_TRUE);
        mmi_frm_set_protocol_event_handler(MSG_ID_APP_TLS_NOTIFY_IND, (PsIntFuncPtr)vm_tls_notify_ind, MMI_TRUE);
        mmi_frm_set_protocol_event_handler(MSG_ID_APP_TLS_ALERT_IND, (PsIntFuncPtr)vm_tls_alert_ind, MMI_TRUE);
        mmi_frm_set_protocol_event_handler(MSG_ID_APP_TLS_INVALID_CERT_IND, (PsIntFuncPtr)vm_tls_invalid_cert_ind, MMI_TRUE);
        mmi_frm_set_protocol_event_handler(MSG_ID_APP_TLS_CLIENT_AUTH_IND, (PsIntFuncPtr)vm_tls_client_auth_ind, MMI_TRUE);
        MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E7, 1, __LINE__);
    }

    MMI_TRACE(TRACE_GROUP_8, TRC_MRE_SSL_E, 1, ctx_p->res_id);
    return ctx_p->res_id;
    
    
}