/**
 * Read private keys and fingerprints into the userstate.
 */
void OtrConnection::loadConfig() {
	keysFile = plugin->homeDir() + "/otr.keys";
	fingerprintFile = plugin->homeDir() + "/otr.fingerprints";

	otrl_privkey_read(userstate, keysFile.toStdString().c_str());
	otrl_privkey_read_fingerprints(userstate, fingerprintFile.toStdString().c_str(), NULL, NULL);
}
Exemplo n.º 2
0
void ReadPrivkeyFiles()
{
	DEBUGOUT_T("READ privkey");
	lib_cs_lock();
	otrl_privkey_read(otr_user_state, _T2A(g_private_key_filename));
	otrl_privkey_read_fingerprints(otr_user_state, _T2A(g_fingerprint_store_filename), set_context_contact, 0);
	otrl_instag_read(otr_user_state, _T2A(g_instag_filename));
}
Exemplo n.º 3
0
void OtrPrivateKeyService::readPrivateKeys()
{
    if (!UserStateService)
        return;

    OtrlUserState userState = UserStateService->userState();
    otrl_privkey_read(userState, PathService->privateKeysStoreFilePath().toUtf8().data());
}
OtrInternal::OtrInternal(psiotr::OtrCallback* callback,
                         psiotr::OtrPolicy& policy)
    : m_userstate(),
      m_uiOps(),
      m_callback(callback),
      m_otrPolicy(policy),
      is_generating(false)
{
    QDir profileDir(callback->dataDir());

    m_keysFile        = profileDir.filePath(OTR_KEYS_FILE);
    m_instagsFile     = profileDir.filePath(OTR_INSTAGS_FILE);
    m_fingerprintFile = profileDir.filePath(OTR_FINGERPRINTS_FILE);

    OTRL_INIT;
    m_userstate                 = otrl_userstate_create();
    m_uiOps.policy              = (*OtrInternal::cb_policy);
    m_uiOps.create_privkey      = (*OtrInternal::cb_create_privkey);
    m_uiOps.is_logged_in        = (*OtrInternal::cb_is_logged_in);
    m_uiOps.inject_message      = (*OtrInternal::cb_inject_message);
    m_uiOps.update_context_list = (*OtrInternal::cb_update_context_list);
    m_uiOps.new_fingerprint     = (*OtrInternal::cb_new_fingerprint);
    m_uiOps.write_fingerprints  = (*OtrInternal::cb_write_fingerprints);
    m_uiOps.gone_secure         = (*OtrInternal::cb_gone_secure);
    m_uiOps.gone_insecure       = (*OtrInternal::cb_gone_insecure);
    m_uiOps.still_secure        = (*OtrInternal::cb_still_secure);

#if (OTRL_VERSION_MAJOR > 3 || (OTRL_VERSION_MAJOR == 3 && OTRL_VERSION_MINOR >= 2))
    m_uiOps.max_message_size    = NULL;
    m_uiOps.account_name        = (*OtrInternal::cb_account_name);
    m_uiOps.account_name_free   = (*OtrInternal::cb_account_name_free);
#endif

#if (OTRL_VERSION_MAJOR >= 4)
    m_uiOps.handle_msg_event    = (*OtrInternal::cb_handle_msg_event);
    m_uiOps.handle_smp_event    = (*OtrInternal::cb_handle_smp_event);
    m_uiOps.create_instag       = (*OtrInternal::cb_create_instag);
#else
    m_uiOps.log_message         = (*OtrInternal::cb_log_message);

    m_uiOps.notify              = (*OtrInternal::cb_notify);
    m_uiOps.display_otr_message = (*OtrInternal::cb_display_otr_message);

    m_uiOps.protocol_name       = (*OtrInternal::cb_protocol_name);
    m_uiOps.protocol_name_free  = (*OtrInternal::cb_protocol_name_free);
#endif

    otrl_privkey_read(m_userstate, QFile::encodeName(m_keysFile).constData());
    otrl_privkey_read_fingerprints(m_userstate,
                                   QFile::encodeName(m_fingerprintFile).constData(),
                                   NULL, NULL);
#if (OTRL_VERSION_MAJOR >= 4)
    otrl_instag_read(m_userstate, QFile::encodeName(m_instagsFile).constData());
#endif
}
Exemplo n.º 5
0
static void* read_keys_background(void* data) {
  VALUE* argv = data;
  VALUE self = argv[0], file = argv[1];
  gcry_error_t err;
  OtrlUserState us;
  int fd;
  FILE* fp;

  Data_Get_Struct(self, struct s_OtrlUserState, us);

  if (rb_obj_is_instance_of(file, rb_cFile)) {
    fd = FIX2INT(rb_funcall(file, rb_intern("fileno"), 0));
    fp = fdopen(dup(fd), "rb");
    err = otrl_privkey_read_FILEp(us, fp);
    fclose(fp);
  } else if (rb_obj_is_instance_of(file, rb_cString)) {
    err = otrl_privkey_read(us, RSTRING_PTR(file));
  }

  return err;
}
Exemplo n.º 6
0
OtrInternal::OtrInternal(qutim_sdk_0_2::PluginSystemInterface *plugin,
                         qutimotr::OtrPolicy& policy, OtrlUserState userstate)
    : m_userstate(),
      m_uiOps(),
      m_plugin(plugin),
      m_keysFile(plugin->getProfilePath() + QDir::separator () + OTR_KEYS_FILE),
      m_fingerprintFile(plugin->getProfilePath() + QDir::separator () + OTR_FINGERPRINTS_FILE),
      m_otrPolicy(policy),
      m_mutex()
{
    //m_userstate = otrl_userstate_create();
    m_userstate = userstate;
    m_uiOps.policy = (OtrInternal::cb_policy);
    m_uiOps.create_privkey = (OtrInternal::cb_create_privkey);
    m_uiOps.is_logged_in = (OtrInternal::cb_is_logged_in);
    m_uiOps.inject_message = (OtrInternal::cb_inject_message);
    m_uiOps.notify = (OtrInternal::cb_notify);
    m_uiOps.display_otr_message = (OtrInternal::cb_display_otr_message);
    m_uiOps.update_context_list = (OtrInternal::cb_update_context_list);
    m_uiOps.protocol_name = (OtrInternal::cb_protocol_name);
    m_uiOps.protocol_name_free = (OtrInternal::cb_protocol_name_free);
    m_uiOps.new_fingerprint = (OtrInternal::cb_new_fingerprint);
    m_uiOps.write_fingerprints = (OtrInternal::cb_write_fingerprints);
    m_uiOps.gone_secure = (OtrInternal::cb_gone_secure);
    m_uiOps.gone_insecure = (OtrInternal::cb_gone_insecure);
    m_uiOps.still_secure = (OtrInternal::cb_still_secure);
    m_uiOps.log_message = (OtrInternal::cb_log_message);
    m_uiOps.max_message_size = (OtrInternal::cb_max_message_size);

#if not (OTRL_VERSION_MAJOR==3 && OTRL_VERSION_MINOR==0)
    m_uiOps.account_name = NULL;
    m_uiOps.account_name_free = NULL;
#endif

    otrl_privkey_read(m_userstate, m_keysFile.toLocal8Bit().data());
    otrl_privkey_read_fingerprints(m_userstate,
                                   m_fingerprintFile.toLocal8Bit().data(),
                                   NULL, NULL);
}
Exemplo n.º 7
0
Arquivo: tests.c Projeto: Ri0n/libotr
int main(int argc, char **argv)
{
    OTRL_INIT;
    us = otrl_userstate_create();

    otrl_privkey_read(us, "/home/iang/.gaim/otr.private_key");
    otrl_instag_read(us, "inst.txt");

    test(1,0);
    test(2,0);
    test(3,0);
    test(1,1);
    test(2,1);
    test_unreadable();
    test_crash1();
    test_refresh(3);
    test_refresh(2);
    test_refresh(1);

    otrl_userstate_free(us);

    return 0;
}
Exemplo n.º 8
0
// static
void OTR_Wrapper::init()
{
    otrwui_trace("OTR_Wrapper::init()");
    if (! gOTR)
    {
        gOTR = new OTR_Wrapper;
        if (!gOTR)
        {
            llwarns << "$PLOTR$ Can't new otr_wrapper." << llendl;
        }
        else
        {
            llinfos << "$PLOTR$ newed otr_wrapper." << llendl;
            gOTR->uistate.policy = &otrwui_policy;
            gOTR->uistate.create_privkey = &otrwui_create_privkey;
            gOTR->uistate.is_logged_in = &otrwui_is_logged_in;
            gOTR->uistate.inject_message = &otrwui_inject_message;
            gOTR->uistate.notify = &otrwui_notify;
            gOTR->uistate.display_otr_message = &otrwui_display_otr_message;
            gOTR->uistate.update_context_list = &otrwui_update_context_list;
            gOTR->uistate.protocol_name = &otrwui_protocol_name;
            gOTR->uistate.protocol_name_free = &otrwui_protocol_name_free;
            gOTR->uistate.new_fingerprint = &otrwui_new_fingerprint;
            gOTR->uistate.write_fingerprints = &otrwui_write_fingerprints;
            gOTR->uistate.gone_secure = &otrwui_gone_secure;
            gOTR->uistate.gone_insecure = &otrwui_gone_insecure;
            gOTR->uistate.still_secure = &otrwui_still_secure;
            gOTR->uistate.log_message = &otrwui_log_message;
            gOTR->uistate.max_message_size = &otrwui_max_message_size;
            gOTR->uistate.account_name = &otrwui_account_name;
            gOTR->uistate.account_name_free = &otrwui_account_name_free;
            OTRL_INIT;
            gOTR->userstate = otrl_userstate_create();
            std::string privpath =
                gDirUtilp->getExpandedFilename(
                    LL_PATH_PER_SL_ACCOUNT, OTR_PRIVATE_KEYS_FILE);
            otrl_privkey_read(gOTR->userstate,              privpath.c_str());
            std::string pubpath =
                gDirUtilp->getExpandedFilename(
                    LL_PATH_PER_SL_ACCOUNT, OTR_PUBLIC_KEYS_FILE);
            otrl_privkey_read_fingerprints(gOTR->userstate, pubpath.c_str(), NULL, NULL);
#if 0 // this will gen a key, if the user doesn't have one, at init() time
            if (gOTR && gOTR->userstate)
            {
                OtrlPrivKey *r = gOTR->userstate->privkey_root;
                OtrlPrivKey *k = gOTR->userstate->privkey_root;
                while (k && (k != r))
                {
                    if (0 == strcmp(gOTR->get_protocolid(), k->protocol))
                    {
                        return;
                    }
                }
                char my_uuid[UUID_STR_SIZE];
                gAgent.getID().toString(&(my_uuid[0]));
                otrl_privkey_generate(gOTR->userstate, privpath.c_str(),
                                      my_uuid, gOTR->get_protocolid());
            }
#endif
        }
    }
}
Exemplo n.º 9
0
void
otr_keygen(ProfAccount *account)
{
    if (data_loaded) {
        cons_show("OTR key already generated.");
        return;
    }

    if (jid) {
        free(jid);
    }
    jid = strdup(account->jid);
    log_info("Generating OTR key for %s", jid);

    gchar *data_home = xdg_get_data_home();
    GString *basedir = g_string_new(data_home);
    free(data_home);

    gchar *account_dir = str_replace(jid, "@", "_at_");
    g_string_append(basedir, "/profanity/otr/");
    g_string_append(basedir, account_dir);
    g_string_append(basedir, "/");
    free(account_dir);

    if (!mkdir_recursive(basedir->str)) {
        log_error("Could not create %s for account %s.", basedir->str, jid);
        cons_show_error("Could not create %s for account %s.", basedir->str, jid);
        g_string_free(basedir, TRUE);
        return;
    }

    gcry_error_t err = 0;

    GString *keysfilename = g_string_new(basedir->str);
    g_string_append(keysfilename, "keys.txt");
    log_debug("Generating private key file %s for %s", keysfilename->str, jid);
    cons_show("Generating private key, this may take some time.");
    cons_show("Moving the mouse randomly around the screen may speed up the process!");
    ui_update();
    err = otrl_privkey_generate(user_state, keysfilename->str, account->jid, "xmpp");
    if (!err == GPG_ERR_NO_ERROR) {
        g_string_free(basedir, TRUE);
        g_string_free(keysfilename, TRUE);
        log_error("Failed to generate private key");
        cons_show_error("Failed to generate private key");
        return;
    }
    log_info("Private key generated");
    cons_show("");
    cons_show("Private key generation complete.");

    GString *fpsfilename = g_string_new(basedir->str);
    g_string_append(fpsfilename, "fingerprints.txt");
    log_debug("Generating fingerprints file %s for %s", fpsfilename->str, jid);
    err = otrl_privkey_write_fingerprints(user_state, fpsfilename->str);
    if (!err == GPG_ERR_NO_ERROR) {
        g_string_free(basedir, TRUE);
        g_string_free(keysfilename, TRUE);
        log_error("Failed to create fingerprints file");
        cons_show_error("Failed to create fingerprints file");
        return;
    }
    log_info("Fingerprints file created");

    err = otrl_privkey_read(user_state, keysfilename->str);
    if (!err == GPG_ERR_NO_ERROR) {
        g_string_free(basedir, TRUE);
        g_string_free(keysfilename, TRUE);
        log_error("Failed to load private key");
        data_loaded = FALSE;
        return;
    }

    err = otrl_privkey_read_fingerprints(user_state, fpsfilename->str, NULL, NULL);
    if (!err == GPG_ERR_NO_ERROR) {
        g_string_free(basedir, TRUE);
        g_string_free(keysfilename, TRUE);
        log_error("Failed to load fingerprints");
        data_loaded = FALSE;
        return;
    }

    data_loaded = TRUE;

    g_string_free(basedir, TRUE);
    g_string_free(keysfilename, TRUE);
    g_string_free(fpsfilename, TRUE);
    return;
}
Exemplo n.º 10
0
void
otr_on_connect(ProfAccount *account)
{
    if (jid) {
        free(jid);
    }
    jid = strdup(account->jid);
    log_info("Loading OTR key for %s", jid);

    gchar *data_home = xdg_get_data_home();
    GString *basedir = g_string_new(data_home);
    free(data_home);

    gchar *account_dir = str_replace(jid, "@", "_at_");
    g_string_append(basedir, "/profanity/otr/");
    g_string_append(basedir, account_dir);
    g_string_append(basedir, "/");
    free(account_dir);

    if (!mkdir_recursive(basedir->str)) {
        log_error("Could not create %s for account %s.", basedir->str, jid);
        cons_show_error("Could not create %s for account %s.", basedir->str, jid);
        g_string_free(basedir, TRUE);
        return;
    }

    user_state = otrl_userstate_create();

    gcry_error_t err = 0;

    GString *keysfilename = g_string_new(basedir->str);
    g_string_append(keysfilename, "keys.txt");
    if (!g_file_test(keysfilename->str, G_FILE_TEST_IS_REGULAR)) {
        log_info("No OTR private key file found %s", keysfilename->str);
        data_loaded = FALSE;
    } else {
        log_info("Loading OTR private key %s", keysfilename->str);
        err = otrl_privkey_read(user_state, keysfilename->str);
        if (!err == GPG_ERR_NO_ERROR) {
            log_warning("Failed to read OTR private key file: %s", keysfilename->str);
            cons_show_error("Failed to read OTR private key file: %s", keysfilename->str);
            g_string_free(basedir, TRUE);
            g_string_free(keysfilename, TRUE);
            return;
        }

        OtrlPrivKey* privkey = otrl_privkey_find(user_state, jid, "xmpp");
        if (!privkey) {
            log_warning("No OTR private key found for account \"%s\", protocol \"xmpp\" in file: %s", jid, keysfilename->str);
            cons_show_error("No OTR private key found for account \"%s\", protocol \"xmpp\" in file: %s", jid, keysfilename->str);
            g_string_free(basedir, TRUE);
            g_string_free(keysfilename, TRUE);
            return;
        }
        log_info("Loaded OTR private key");
        data_loaded = TRUE;
    }

    GString *fpsfilename = g_string_new(basedir->str);
    g_string_append(fpsfilename, "fingerprints.txt");
    if (!g_file_test(fpsfilename->str, G_FILE_TEST_IS_REGULAR)) {
        log_info("No OTR fingerprints file found %s", fpsfilename->str);
        data_loaded = FALSE;
    } else {
        log_info("Loading OTR fingerprints %s", fpsfilename->str);
        err = otrl_privkey_read_fingerprints(user_state, fpsfilename->str, NULL, NULL);
        if (!err == GPG_ERR_NO_ERROR) {
            log_error("Failed to load OTR fingerprints file: %s", fpsfilename->str);
            g_string_free(basedir, TRUE);
            g_string_free(keysfilename, TRUE);
            g_string_free(fpsfilename, TRUE);
            return;
        } else {
            log_info("Loaded OTR fingerprints");
            data_loaded = TRUE;
        }
    }

    if (data_loaded) {
        cons_show("Loaded OTR private key for %s", jid);
    }

    g_string_free(basedir, TRUE);
    g_string_free(keysfilename, TRUE);
    g_string_free(fpsfilename, TRUE);
    return;
}
/**
 * Read private keys from a file into the userstate.
 */
void OtrConnection::readPrivateKeys(char* keyFile) {
	otrl_privkey_read(userstate, keyFile);
}
Exemplo n.º 12
0
static void read_privkey(char *file) {
  if (otrl_privkey_read(us, file)) {
    fprintf(stderr, "Error reading private key");
  }
}
Exemplo n.º 13
0
int main(){
	OTRL_INIT;
	OtrlUserState userstate;
	char *newmessage = NULL;
	char *nlmsg=NULL;
	OtrlMessageAppOps ui_ops;
	int sockfd, clientfd, recvbytes, recv_ret;
	socklen_t sin_size;
	char buf[1024], pwdbuf[1024];
	char *dbuf;
	struct sockaddr_in remote_addr, my_addr;
	fd_set fds;
	int one = 1;
	
	/* initialize ui callbacks */
	memset(&ui_ops, 0, sizeof(OtrlMessageAppOps));
	ui_ops.inject_message= inject_msg;
	ui_ops.gone_secure=gone_secure;
	printf("\033[31mStarting driver...\033[0m\n");

	/* initialize socket connection */
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	my_addr.sin_family=AF_INET;
	my_addr.sin_port= htons(3333);
	my_addr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(my_addr.sin_zero),8);

	/* Allow rebinding to address */
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));

	if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
		perror("bind");
		exit(1);
	}
	if (listen(sockfd, 1) == -1) {
		perror("listen");
		exit(1);
	}
	sin_size = sizeof(struct sockaddr_in);
	if ((clientfd = accept(sockfd, (struct sockaddr *)&remote_addr, \
					&sin_size)) == -1) {
		perror("accept");
	}
	printf("\033[31mConnected to Bob\033[0m\n");

	/* create userstate, and read the private key */
	userstate = otrl_userstate_create();
	getcwd(pwdbuf, 1024);
	strcat(pwdbuf, "/private_key");
	otrl_privkey_read(userstate, pwdbuf);


	/* Clients send messages asynchronously*/
	while(1){
		FD_ZERO(&fds);
		FD_SET(0, &fds);
		FD_SET(clientfd, &fds);
		select(clientfd+1, &fds, NULL, NULL, NULL);
		if(FD_ISSET(0, &fds)){
			/* send message */
			fgets(buf, 1023, stdin);
			buf[strlen(buf)-1]= '\0';
			printf("\033[31mTo OTR:%d:\033[0m%s\n\033[0m",strlen(buf), buf);
			dbuf = strdup(buf);
			otrl_message_sending(userstate, &ui_ops, NULL, "*****@*****.**",
					"msn", "*****@*****.**", &dbuf, NULL, &newmessage,
					OTRL_FRAGMENT_SEND_SKIP, NULL, NULL, NULL, NULL);
			free(dbuf);
			nlmsg = (char*)malloc(strlen(newmessage)+2);			
			memcpy(nlmsg, newmessage, strlen(newmessage));
			nlmsg[strlen(newmessage)]='\n';
			nlmsg[strlen(newmessage)+1]='\0';
			send(clientfd, nlmsg, strlen(nlmsg), 0);
			printf("\033[31mTo network:%d:\033[35m%s\033[0m\n\033[0m", strlen(newmessage), newmessage);
		}else{
			/*receive message */
			recvbytes=recv(clientfd, buf, MAXDATASIZE, 0);
			if(recvbytes==0) break;
			recvbytes--;
			buf[recvbytes] = '\0';
			if(buf[recvbytes-1]=='\r'){
				 recvbytes--;
				 buf[recvbytes]='\0';
			}
			printf("\033[31mFrom network:%d:\033[35m %s\033[0m\n",recvbytes, buf);
			recv_ret=otrl_message_receiving(userstate, &ui_ops, &clientfd, "*****@*****.**",
					"msn", "*****@*****.**", buf, &newmessage, NULL,
					NULL, NULL, NULL, NULL);
			if(recv_ret==0)
				printf("\033[31mFrom OTR:%d:\033[0m %s\n", newmessage ? strlen(newmessage) : 0, newmessage);
		}
	}
	return 0;
}