Example #1
1
int main(int argc, char *argv[]) {
    if(argc != 2) {
        printf("Usage: ./test_app <file_name>\n");
        return -1;
    }

    ENGINE *e = load_engine(ENGINE_MODULE, "test_engine");
    if(e == 0) {
        printf("Unable to load engine\n");
        return -1;
    }
    ENGINE_ctrl_cmd_string(e, "username", "user", 0);
    ENGINE_ctrl_cmd_string(e, "password", "password", 0);
    ENGINE_init(e);
    
    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);
    HMAC_Init_ex(&ctx, "test_key\0", 9, EVP_sha1(), e);

    FILE *f = fopen(argv[1], "r");
    char buf[BUF_SIZE];
    while(!feof(f)) {
        size_t ln = fread(buf, sizeof(char), BUF_SIZE, f);
        if(ln == 0) continue;
        HMAC_Update(&ctx, buf, ln);
    }
    fclose(f);
    
    unsigned int siglen;
    unsigned char md[20];
    HMAC_Final(&ctx, md, &siglen);
    ENGINE_finish(e);

    printf("HMAC-SHA1: ");
    for(size_t i = 0; i < siglen; i++) printf("%02x", md[i]);
    printf("\n");

    return 0;
}
Example #2
0
File: ssl.c Project: FPiriz/uperf
static protocol_t *
protocol_ssl_accept(protocol_t * p, void *options)
{
	protocol_t *newp;
	struct sockaddr_in remote;
	socklen_t addrlen;
	int ret;
	ssl_private_t *ssl_p = (ssl_private_t *) p->_protocol_p;
	ssl_private_t *new_ssl_p;
	struct sockaddr name;
	char hostname[128];
	flowop_options_t *flowop_options = (flowop_options_t *) options;
	BIO *sbio;

	newp = protocol_ssl_new();
	new_ssl_p = (ssl_private_t *) newp->_protocol_p;
	addrlen = (socklen_t) sizeof (remote);
	uperf_debug("ssl - ssl obj waiting for accept\n");
	newp->fd = accept(p->fd, (struct sockaddr *) &remote,
		&addrlen);
	if (newp->fd < 0) {
		uperf_log_msg(UPERF_LOG_ERROR, errno, "accept");
		return (NULL);
	}
	if (getnameinfo((const struct sockaddr *) & remote, addrlen,
			hostname, sizeof (hostname), NULL, 0, 0) == 0) {
		uperf_debug("ssl - Connection from %s:%d\n", hostname,
			SOCK_PORT(remote));
		strlcpy(newp->host, hostname, sizeof (newp->host));
		newp->port = SOCK_PORT(remote);
	}
	if (flowop_options) {
		if ((load_engine(flowop_options->engine)) == -1) {
			uperf_info(
"ssl - Engine %s does NOT exist! Using the default OpenSSL softtoken",
			flowop_options->engine);
		}
	}
	sbio = BIO_new_socket(newp->fd, BIO_NOCLOSE);
	if (!(new_ssl_p->ssl = SSL_new(ctx))) {
		uperf_log_msg(UPERF_LOG_ERROR, 0, "SSL_new error");
		return (NULL);
	}
	SSL_set_bio(new_ssl_p->ssl, sbio, sbio);

	ret = SSL_accept(new_ssl_p->ssl);
	if (my_ssl_error(new_ssl_p->ssl, ret) == 0) {
		return (newp);
	} else {
		return (0);
	}
}
void load_maxfiles(){
    fprintf(stdout,"Init maxfiles\n");
    max_files[K_fw_l0_conv] = CNN_FW_Conv_V0_DP_L0_0_init();
    max_files[K_fw_l0_maxpool] = CNN_FW_MaxPool_V0_DP_L0_0_init();
    max_files[K_fw_l1_conv] = CNN_FW_Conv_V0_DP_L1_0_init();
    max_files[K_fw_l1_maxpool] = CNN_FW_MaxPool_V0_DP_L1_0_init();
    max_files[K_fw_l2_mlp] = NULL;
    max_files[K_fw_l3_softmax] = CNN_FW_Softmax_V0_DP_L3_0_init();
    max_files[K_bp_l3_softmax] = CNN_BP_Softmax_V0_DP_L3_0_init();
    max_files[K_bp_l2_mlp] = NULL;
    max_files[K_bp_l1_maxpool] = CNN_BP_MaxPool_V0_DP_L1_0_init();
    max_files[K_bp_l1_conv] = CNN_BP_Conv_V0_DP_L1_0_init();
    max_files[K_bp_l0_maxpool] = CNN_BP_MaxPool_V0_DP_L0_0_init();
    max_files[K_bp_l0_conv] = CNN_BP_Conv_V0_DP_L0_0_init();
    for (int i=0;i<K_TOTAL;++i){
        max_engines[i] = NULL;
    }
    cur_engine = -1;

    int t = K_fw_l0_conv;
    load_engine(t);
    {
        fprintf(stdout,"Writing to LMem : train_set_x\n");
        max_actions_t* act;
        act = max_actions_init(max_files[t], "writeLMem");
        max_set_param_uint64t(act, "offset", train_set_x_offset);
        max_set_param_uint64t(act, "size", train_set_x_size);
        max_queue_input(act, "cpu_to_lmem_at_cpu", train_set_x, train_set_x_size);
        max_run(max_engines[t], act);
        max_actions_free(act);
    }
    {
        fprintf(stdout,"Writing to LMem : valid_set_x\n");
        max_actions_t* act;
        act = max_actions_init(max_files[t], "writeLMem");
        max_set_param_uint64t(act, "offset", valid_set_x_offset);
        max_set_param_uint64t(act, "size", valid_set_x_size);
        max_queue_input(act, "cpu_to_lmem_at_cpu", valid_set_x, valid_set_x_size);
        max_run(max_engines[t], act);
        max_actions_free(act);
    }
    {
        fprintf(stdout,"Writing to LMem : test_set_x\n");
        max_actions_t* act;
        act = max_actions_init(max_files[t], "writeLMem");
        max_set_param_uint64t(act, "offset", test_set_x_offset);
        max_set_param_uint64t(act, "size", test_set_x_size);
        max_queue_input(act, "cpu_to_lmem_at_cpu", test_set_x, test_set_x_size);
        max_run(max_engines[t], act);
        max_actions_free(act);
    }
}
static ENGINE_HANDLE_V1 *start_your_engines(const char *engine, const char* cfg, bool engine_init) {

    init_mock_server(handle);
    if (!load_engine(engine, &get_mock_server_api, logger_descriptor, &handle)) {
        fprintf(stderr, "Failed to load engine %s.\n", engine);
        return NULL;
    }

    if (engine_init) {
        if(!init_engine(handle, cfg, logger_descriptor)) {
            fprintf(stderr, "Failed to init engine %s with config %s.\n", engine, cfg);
            return NULL;
        }
    }

    mock_engine = default_mock_engine;
    handle_v1 = mock_engine.the_engine = (ENGINE_HANDLE_V1*)handle;
    handle = (ENGINE_HANDLE*)&mock_engine.me;
    handle_v1 = &mock_engine.me;

    // Reset all members that aren't set (to allow the users to write
    // testcases to verify that they initialize them..
    assert(mock_engine.me.interface.interface == mock_engine.the_engine->interface.interface);

    if (mock_engine.the_engine->get_stats_struct == NULL) {
        mock_engine.me.get_stats_struct = NULL;
    }
    if (mock_engine.the_engine->aggregate_stats == NULL) {
        mock_engine.me.aggregate_stats = NULL;
    }
    if (mock_engine.the_engine->unknown_command == NULL) {
        mock_engine.me.unknown_command = NULL;
    }
    if (mock_engine.the_engine->tap_notify == NULL) {
        mock_engine.me.tap_notify = NULL;
    }
    if (mock_engine.the_engine->get_tap_iterator == NULL) {
        mock_engine.me.get_tap_iterator = NULL;
    }
    if (mock_engine.the_engine->errinfo == NULL) {
        mock_engine.me.errinfo = NULL;
    }

    return &mock_engine.me;
}
Example #5
0
File: ssl.c Project: FPiriz/uperf
static int
protocol_ssl_connect(protocol_t * p, void *options)
{
	BIO *sbio;
	int status;

	ssl_private_t *ssl_p = (ssl_private_t *) p->_protocol_p;
	flowop_options_t *flowop_options = (flowop_options_t *) options;

	uperf_debug("ssl - Connecting to %s:%d\n", p->host, p->port);

	status = generic_connect(p, IPPROTO_TCP);
	if (status == UPERF_SUCCESS)
		set_tcp_options(p->fd, flowop_options);

	if (flowop_options && (strcmp(flowop_options->engine, ""))) {
		status = load_engine(flowop_options->engine);
		if (status == -1) {
			uperf_info(
"ssl - Engine %s does NOT exist! Using the default OpenSSL softtoken",
			flowop_options->engine);
		}
	}
	if ((ssl_p->ssl = SSL_new(ctx)) == NULL) {
		ulog_err("Error initializng SSL");
		return (-1);
	}
	sbio = BIO_new_socket(p->fd, BIO_NOCLOSE);
	SSL_set_bio(ssl_p->ssl, sbio, sbio);

	status = SSL_connect(ssl_p->ssl);
	if (status <= 0) {
		uperf_log_msg(UPERF_LOG_ERROR, 0, "ssl connect error");
		return (-1);
	}
	return (0);
}
Example #6
0
/**
 * Reads in a vehicle part from a JsonObject.
 */
void vpart_info::load( JsonObject &jo, const std::string &src )
{
    vpart_info def;

    if( jo.has_string( "copy-from" ) ) {
        const auto base = vpart_info_all.find( vpart_id( jo.get_string( "copy-from" ) ) );
        const auto ab = abstract_parts.find( vpart_id( jo.get_string( "copy-from" ) ) );
        if( base != vpart_info_all.end() ) {
            def = base->second;
            def.looks_like = base->second.id.str();
        } else if( ab != abstract_parts.end() ) {
            def = ab->second;
            if( def.looks_like.empty() ) {
                def.looks_like = ab->second.id.str();
            }
        } else {
            deferred.emplace_back( jo.str(), src );
            return;
        }
    }

    if( jo.has_string( "abstract" ) ) {
        def.id = vpart_id( jo.get_string( "abstract" ) );
    } else {
        def.id = vpart_id( jo.get_string( "id" ) );
    }

    assign( jo, "name", def.name_ );
    assign( jo, "item", def.item );
    assign( jo, "location", def.location );
    assign( jo, "durability", def.durability );
    assign( jo, "damage_modifier", def.dmg_mod );
    assign( jo, "energy_consumption", def.energy_consumption );
    assign( jo, "power", def.power );
    assign( jo, "epower", def.epower );
    assign( jo, "fuel_type", def.fuel_type );
    assign( jo, "default_ammo", def.default_ammo );
    assign( jo, "folded_volume", def.folded_volume );
    assign( jo, "size", def.size );
    assign( jo, "difficulty", def.difficulty );
    assign( jo, "bonus", def.bonus );
    assign( jo, "flags", def.flags );
    assign( jo, "description", def.description );

    if( jo.has_member( "requirements" ) ) {
        auto reqs = jo.get_object( "requirements" );

        parse_vp_reqs( reqs, def.id.str(), "install", def.install_reqs, def.install_skills,
                       def.install_moves );
        parse_vp_reqs( reqs, def.id.str(), "removal", def.removal_reqs, def.removal_skills,
                       def.removal_moves );
        parse_vp_reqs( reqs, def.id.str(), "repair",  def.repair_reqs,  def.repair_skills,
                       def.repair_moves );

        def.legacy = false;
    }

    if( jo.has_member( "symbol" ) ) {
        def.sym = jo.get_string( "symbol" )[ 0 ];
    }
    if( jo.has_member( "broken_symbol" ) ) {
        def.sym_broken = jo.get_string( "broken_symbol" )[ 0 ];
    }
    if( jo.has_member( "looks_like" ) ) {
        def.looks_like = jo.get_string( "looks_like" );
    }

    if( jo.has_member( "color" ) ) {
        def.color = color_from_string( jo.get_string( "color" ) );
    }
    if( jo.has_member( "broken_color" ) ) {
        def.color_broken = color_from_string( jo.get_string( "broken_color" ) );
    }

    if( jo.has_member( "breaks_into" ) ) {
        JsonIn &stream = *jo.get_raw( "breaks_into" );
        def.breaks_into_group = item_group::load_item_group( stream, "collection" );
    }

    auto qual = jo.get_array( "qualities" );
    if( !qual.empty() ) {
        def.qualities.clear();
        while( qual.has_more() ) {
            auto pair = qual.next_array();
            def.qualities[ quality_id( pair.get_string( 0 ) ) ] = pair.get_int( 1 );
        }
    }

    if( jo.has_member( "damage_reduction" ) ) {
        JsonObject dred = jo.get_object( "damage_reduction" );
        def.damage_reduction = load_damage_array( dred );
    } else {
        def.damage_reduction.fill( 0.0f );
    }

    if( def.has_flag( "ENGINE" ) ) {
        load_engine( def.engine_info, jo, def.fuel_type );
    }

    if( def.has_flag( "WHEEL" ) ) {
        load_wheel( def.wheel_info, jo );
    }

    if( jo.has_string( "abstract" ) ) {
        abstract_parts[def.id] = def;
    } else {
        vpart_info_all[def.id] = def;
    }
}
Example #7
0
int main(int argc,char *argv[])
{
	int r=ENGFAIL;
	int fd=0;
	int efd;
	int elen;
	int i;
	int m=1;
	char *name;
	void *ctx;
	DATA din;
	DATA dout;
	MESSAGE msg;

	if(geteuid()||setuid(0)||setgid(0)||getenv("LD_PRELOAD")||
		getenv("LD_LIBRARY_PATH"))goto out;
	if(ptrace(PTRACE_TRACEME,0,1,0))goto out;
	if(mlockall(MCL_CURRENT|MCL_FUTURE))goto out;
	if(prctl(PR_SET_DUMPABLE,0))goto out;

	signal(SIGALRM,killer);
	alarm(10);

	if(argc==2)fd=atoi(argv[1]);

#ifndef DEBUG
	for(i=0;i<3;i++)if(fd!=i)
	{
		close(i);
		open("/dev/null",i?O_WRONLY:O_RDONLY);
	}
#endif

	if((i=read(fd,&msg,sizeof(msg)))!=sizeof(msg))goto out;
	if(!msg.keyfile[0]||msg.passfile[0]!='/')goto out;
	if(msg.cfgfile[0]&&msg.cfgfile[0]!='/')goto out;
	if(msg.mode0&&!msg.cfgfile[0])goto out;
	if(msg.openscconf[0])putenv(msg.openscconf);
	i=-1;

	switch(msg.cmdres)
	{
	case GETPASS:
		if((r=load_engine(&ctx,&msg)))goto gerr1;
		suspend_engine(ctx,&engbits);

		name=strrchr(msg.passfile,'/')+1;

		if(msg.cfgfile[0])
		{
			for(r=NOCARD,i=0;r==NOCARD&&i<MAXFILES;i++)
				if(*msg.keyfile[i])r=validate(ctx,name,
					msg.cfgfile,msg.keyfile[i],0,
					msg.mode0,&m);
			if(r)goto gerr2;
			if(!m)
			{
				memset(msg.pass,0,sizeof(msg.pass));
				goto done;
			}
		}

		if((efd=open(msg.passfile,O_RDONLY|O_CLOEXEC))==-1)
			FAIL(FILEFAIL,gerr2);

		if(flock(efd,LOCK_EX))
		{
			close(efd);
			FAIL(FILEFAIL,gerr2);
		}

		elen=read(efd,&din,sizeof(din));

		flock(efd,LOCK_UN);
		close(efd);

		if(elen!=sizeof(din))FAIL(FILEFAIL,gerr2);

		if(i!=-1)r=decrypt(ctx,name,msg.keyfile[--i],&din,sizeof(din),
			&dout);
		else for(r=NOCARD,i=0;r==NOCARD&&i<MAXFILES;i++)
		    if(*msg.keyfile[i])
			r=decrypt(ctx,name,msg.keyfile[i],&din,sizeof(din),
				&dout);
		if(r)goto gerr2;

		sha256(dout.random,sizeof(dout)-sizeof(dout.dgst),din.dgst);
		if(memcmp(din.dgst,dout.dgst,sizeof(din.dgst)))
		{
			r=CRYPTOFAIL;
			goto gerr2;
		}

		memcpy(msg.pass,dout.data,sizeof(msg.pass));
done:		r=OK;

gerr2:		unload_engine(ctx);
gerr1:		memset(&msg,0,sizeof(msg));
		memcpy(msg.pass,dout.data,sizeof(msg.pass));
		msg.cmdres=r;
		msg.nopass=m?0:1;
		memclear(&din,0,sizeof(din));
		memclear(&dout,0,sizeof(dout));
		break;

	case SETPASS:
		if((r=load_engine(&ctx,&msg)))goto serr1;

		name=strrchr(msg.passfile,'/')+1;

		if(msg.cfgfile[0])
		{
			for(r=NOCARD,i=0;r==NOCARD&&i<MAXFILES;i++)
				if(*msg.keyfile[i])r=validate(ctx,name,
					msg.cfgfile,msg.keyfile[i],
					msg.precheck,msg.mode0,&m);
			if(r||!m||msg.precheck)goto serr2;
		}
		else if(msg.precheck)
		{
			for(r=NOCARD,i=0;r==NOCARD&&i<MAXFILES;i++)
				r=cardfingerprint(ctx,name,NULL);
			goto serr2;
		}

		if((efd=open("/dev/urandom",O_RDONLY|O_CLOEXEC))==-1)
			FAIL(FILEFAIL,serr2);
		elen=read(efd,din.random,sizeof(din.random));
		close(efd);
		if(elen!=sizeof(din.random))FAIL(FILEFAIL,serr2);

		memcpy(din.data,msg.pass,sizeof(msg.pass));
		sha256(din.random,sizeof(din)-sizeof(din.dgst),din.dgst);

		if(i!=-1)r=encrypt(ctx,name,msg.keyfile[--i],&din,sizeof(din),
			&dout);
		else for(r=NOCARD,i=0;r==NOCARD&&i<MAXFILES;i++)
		    if(*msg.keyfile[i])
			r=encrypt(ctx,name,msg.keyfile[i],&din,sizeof(din),
				&dout);
		if(r)goto serr2;

		if((efd=open(msg.passfile,O_WRONLY|O_CREAT|O_CLOEXEC,0600))
			==-1)FAIL(FILEFAIL,serr2);

		if(flock(efd,LOCK_EX)||ftruncate(efd,sizeof(dout))||
			write(efd,&dout,sizeof(dout))!=sizeof(dout)||
			fdatasync(efd))r=FILEFAIL;
		else r=OK;

		flock(efd,LOCK_UN);
		close(efd);

serr2:		unload_engine(ctx);
serr1:		memclear(&din,0,sizeof(din));
		memclear(&dout,0,sizeof(dout));
		memset(&msg,0,sizeof(msg));
		msg.cmdres=r;
		msg.nopass=m?0:1;
		break;

	default:goto out;
	}

	i=write(fd,&msg,sizeof(msg));
	r=OK;

out:	memclear(&msg,0,sizeof(msg));
	close(fd);
	return r;
}
Example #8
0
int MainWindow::init_database()
{
	int ret = 2;
	fprintf(stderr, "Opening database: %s\n", QString2filename(dbfile));
	keys = NULL; reqs = NULL; certs = NULL; temps = NULL; crls = NULL;

	certView->setRootIsDecorated(db_x509::treeview);

	try {
		ret = initPass();
		if (ret == 2)
			return ret;
		keys = new db_key(dbfile, this);
		reqs = new db_x509req(dbfile, this);
		certs = new db_x509(dbfile, this);
		temps = new db_temp(dbfile, this);
		crls = new db_crl(dbfile, this);
	}
	catch (errorEx &err) {
		Error(err);
		dbfile = "";
		return ret;
	}

	mandatory_dn = "";
	string_opt = QString("MASK:0x2002");
	ASN1_STRING_set_default_mask_asc((char*)CCHAR(string_opt));
	hashBox::resetDefault();
	pkcs11path = getDefaultPkcs11Lib();
	workingdir = QDir::currentPath();
	setOptFlags((QString()));
	try {
		pkcs11_lib p(pkcs11path);
	} catch (errorEx &e) {
		pkcs11path = QString();
	}

	connect( keys, SIGNAL(newKey(pki_key *)),
		certs, SLOT(newKey(pki_key *)) );
	connect( keys, SIGNAL(delKey(pki_key *)),
		certs, SLOT(delKey(pki_key *)) );
	connect( keys, SIGNAL(newKey(pki_key *)),
		reqs, SLOT(newKey(pki_key *)) );
	connect( keys, SIGNAL(delKey(pki_key *)),
		reqs, SLOT(delKey(pki_key *)) );

	connect( certs, SIGNAL(connNewX509(NewX509 *)), this,
		SLOT(connNewX509(NewX509 *)) );
	connect( reqs, SIGNAL(connNewX509(NewX509 *)), this,
		SLOT(connNewX509(NewX509 *)) );

	connect( reqs, SIGNAL(newCert(pki_x509req *)),
		certs, SLOT(newCert(pki_x509req *)) );
	connect( temps, SIGNAL(newCert(pki_temp *)),
		certs, SLOT(newCert(pki_temp *)) );
	connect( temps, SIGNAL(newReq(pki_temp *)),
		reqs, SLOT(newItem(pki_temp *)) );

	keyView->setIconSize(pki_evp::icon[0]->size());
	reqView->setIconSize(pki_x509req::icon[0]->size());
	certView->setIconSize(pki_x509::icon[0]->size());
	tempView->setIconSize(pki_temp::icon->size());
	crlView->setIconSize(pki_crl::icon->size());

	keyView->setModel(keys);
	reqView->setModel(reqs);
	certView->setModel(certs);
	tempView->setModel(temps);
	crlView->setModel(crls);

	try {
		db mydb(dbfile);
		char *p;
		if (!mydb.find(setting, "workingdir")) {
			if ((p = (char *)mydb.load(NULL))) {
				workingdir = p;
				free(p);
			}
		}
		mydb.first();
		if (!mydb.find(setting, "pkcs11path")) {
			if ((p = (char *)mydb.load(NULL))) {
				pkcs11path = p;
				free(p);
			}
		}
		mydb.first();
		if (!mydb.find(setting, "default_hash")) {
			if ((p = (char *)mydb.load(NULL))) {
				hashBox::setDefault(p);
				free(p);
			}
		}
		mydb.first();
		if (!mydb.find(setting, "mandatory_dn")) {
			if ((p = (char *)mydb.load(NULL))) {
				mandatory_dn = p;
				free(p);
			}
		}
		// what a stupid idea....
		mydb.first();
		if (!mydb.find(setting, "multiple_key_use")) {
			mydb.erase();
		}
		mydb.first();
		if (!mydb.find(setting, "string_opt")) {
			if ((p = (char *)mydb.load(NULL))) {
				string_opt = p;
				free(p);
			}
		}
		mydb.first();
		if (!mydb.find(setting, "suppress")) {
			if ((p = (char *)mydb.load(NULL))) {
				QString x = p;
				free(p);
				if (x == "1")
					pki_base::suppress_messages = 1;
			}
		}
		mydb.first();
		if (!mydb.find(setting, "optionflags")) {
			if ((p = (char *)mydb.load(NULL))) {
				setOptFlags((QString(p)));
				free(p);
			}
		}
		ASN1_STRING_set_default_mask_asc((char*)CCHAR(string_opt));
		mydb.first();
		if (!mydb.find(setting, "mw_geometry")) {
			db_header_t h;
			if ((p = (char *)mydb.load(&h))) {
				if (h.version == 1) {
					QByteArray ba;
					ba = QByteArray::fromRawData(p, h.len);
					int w, h, i;
					w = db::intFromData(ba);
					h = db::intFromData(ba);
					i = db::intFromData(ba);
					resize(w,h);
					if (i != -1)
						tabView->setCurrentIndex(i);
					}
				free(p);
			}
		}
	} catch (errorEx &err) {
		Error(err);
		return ret;
	}
	setWindowTitle(tr(XCA_TITLE));
	setItemEnabled(true);
	if (pki_evp::passwd.isNull())
		QMessageBox::information(this, XCA_TITLE,
			tr("Using or exporting private keys will not be possible without providing the correct password"));

	dbindex->setText(tr("Database") + ":" + dbfile);
	load_engine();
	return ret;
}
Example #9
0
int MainWindow::init_database()
{
	int ret = 2;
	qDebug("Opening database: %s", QString2filename(dbfile));
	keys = NULL; reqs = NULL; certs = NULL; temps = NULL; crls = NULL;

	Entropy::seed_rng();
	certView->setRootIsDecorated(db_x509::treeview);

	try {
		ret = initPass();
		if (ret == 2)
			return ret;
		keys = new db_key(dbfile, this);
		reqs = new db_x509req(dbfile, this);
		certs = new db_x509(dbfile, this);
		temps = new db_temp(dbfile, this);
		crls = new db_crl(dbfile, this);
		certs->updateAfterDbLoad();
	}
	catch (errorEx &err) {
		Error(err);
		dbfile = "";
		return ret;
	}

	searchEdit->setText("");
	searchEdit->show();
	statusBar()->addWidget(searchEdit, 1);
	mandatory_dn = "";
	explicit_dn = explicit_dn_default;

	string_opt = QString("MASK:0x2002");
	ASN1_STRING_set_default_mask_asc((char*)CCHAR(string_opt));
	hashBox::resetDefault();
	pkcs11path = QString();
	workingdir = QDir::currentPath();
	setOptFlags((QString()));

	connect( keys, SIGNAL(newKey(pki_key *)),
		certs, SLOT(newKey(pki_key *)) );
	connect( keys, SIGNAL(delKey(pki_key *)),
		certs, SLOT(delKey(pki_key *)) );
	connect( keys, SIGNAL(newKey(pki_key *)),
		reqs, SLOT(newKey(pki_key *)) );
	connect( keys, SIGNAL(delKey(pki_key *)),
		reqs, SLOT(delKey(pki_key *)) );

	connect( certs, SIGNAL(connNewX509(NewX509 *)), this,
		SLOT(connNewX509(NewX509 *)) );
	connect( reqs, SIGNAL(connNewX509(NewX509 *)), this,
		SLOT(connNewX509(NewX509 *)) );

	connect( reqs, SIGNAL(newCert(pki_x509req *)),
		certs, SLOT(newCert(pki_x509req *)) );
	connect( tempView, SIGNAL(newCert(pki_temp *)),
		certs, SLOT(newCert(pki_temp *)) );
	connect( tempView, SIGNAL(newReq(pki_temp *)),
		reqs, SLOT(newItem(pki_temp *)) );

	keyView->setIconSize(pki_evp::icon[0]->size());
	reqView->setIconSize(pki_x509req::icon[0]->size());
	certView->setIconSize(pki_x509::icon[0]->size());
	tempView->setIconSize(pki_temp::icon->size());
	crlView->setIconSize(pki_crl::icon->size());

	keyView->setModel(keys);
	reqView->setModel(reqs);
	certView->setModel(certs);
	tempView->setModel(temps);
	crlView->setModel(crls);
	try {
		db mydb(dbfile);

		while (mydb.find(setting, QString()) == 0) {
			QString key;
			db_header_t head;
			char *p = (char *)mydb.load(&head);
			if (!p) {
				if (mydb.next())
					break;
				continue;
			}
			key = head.name;

			if (key == "workingdir")
				workingdir = p;
			else if (key == "pkcs11path")
				pkcs11path = p;
			else if (key == "default_hash")
				hashBox::setDefault(p);
			else if (key == "mandatory_dn")
				mandatory_dn = p;
			else if (key == "explicit_dn")
				explicit_dn = p;
			/* what a stupid idea.... */
			else if (key == "multiple_key_use")
				mydb.erase();
			else if (key == "string_opt")
				string_opt = p;
			else if (key == "suppress")
				mydb.erase();
			else if (key == "optionflags1")
				setOptFlags((QString(p)));
			/* Different optionflags, since setOptFlags()
			 * does an abort() for unknown flags in
			 * older versions.   *Another stupid idea*
			 * This is for backward compatibility
			 */
			else if (key == "optionflags")
				setOptFlags_old((QString(p)));
			else if (key == "defaultkey")
				NewKey::setDefault((QString(p)));
			else if (key == "mw_geometry")
				set_geometry(p, &head);
			free(p);
			if (mydb.next())
				break;
		}
	} catch (errorEx &err) {
		Error(err);
		return ret;
	}
	ASN1_STRING_set_default_mask_asc((char*)CCHAR(string_opt));
	if (explicit_dn.isEmpty())
		explicit_dn = explicit_dn_default;
	setWindowTitle(tr(XCA_TITLE));
	setItemEnabled(true);
	if (pki_evp::passwd.isNull())
		XCA_INFO(tr("Using or exporting private keys will not be possible without providing the correct password"));

	dbindex->setText(tr("Database") + ": " + dbfile);
	load_engine();
	return ret;
}
Example #10
0
int main(int argc, char **argv)
{
    char *opt_input = NULL,
        *opt_output = NULL,
        *opt_key = NULL,
        *opt_engine = NULL,
        *opt_password = NULL;
    int long_optind = 0, ret = 1;
    int encrypt = 0, decrypt = 0, verbose = 0, quiet = 0;
    STACK_OF(X509) *crts = sk_X509_new_null();
    X509 *x509 = NULL;
    EVP_PKEY *key = NULL;
    BIO *in = NULL, *out = NULL, *err = NULL;
    ENGINE *engine = NULL;

    init_crypto();

    while (1) {
        char c = getopt_long(argc, argv, "deE:hi:k:o:p:qr:v",
                             options, &long_optind);
        if (c == -1)
            break;
        switch (c) {
            case 'd':
                decrypt = 1;
                break;
            case 'E':
                opt_engine = optarg;
                break;
            case 'e':
                encrypt = 1;
                break;
            case 'i':
                opt_input = optarg;
                break;
            case 'k':
                opt_key = optarg;
                break;
            case 'o':
                opt_output = optarg;
                break;
            case 'p':
                opt_password = optarg;
                break;
            case 'q':
                quiet = 1;
                break;
            case 'r':
                x509 = load_x509(NULL, optarg);
                if(x509) {
                    sk_X509_push(crts, x509);
                } else {
                    fprintf(stderr, "Error loading certificate '%s'\n", optarg);
                    goto end;
                }
                break;
            case 'v':
                verbose += 1;
                break;
            case 'h':
            default:
                print_usage_and_die(app_name, options, option_help);
        }
    }

    if(encrypt == 0 && decrypt == 0) {
        fprintf(stderr, "You must specify either --encrypt/-e or --decrypt/-d\n");
        goto end;
    }

    err = BIO_new_fp(stderr, BIO_NOCLOSE);
    if(err == NULL) {
        fprintf(stderr, "Error allocating error stream\n");
    }

    if(opt_engine) {
        engine = load_engine(err, opt_engine, verbose);
    }

    if(opt_key) {
        if((key = load_key(NULL, opt_key, engine)) == NULL) {
            fprintf(stderr, "Error loading key '%s'\n", opt_key);
            goto end;
        }
    }

    if(opt_input) {
        in = BIO_new_file(opt_input, "rb");
    } else {
        in = BIO_new_fp(stdin, BIO_NOCLOSE);
    }

    if(opt_output) {
        out = BIO_new_file(opt_output, "wb");
    } else {
        out = BIO_new_fp(stdout, BIO_NOCLOSE);
    }

    if(encrypt) {
        if(opt_password == NULL && sk_X509_num(crts) == 0) {
            fprintf(stderr, "You must specify at least one of --password/-p or --recipient/-r\n");
            goto end;
        }
        ret = encrypt_cms(in, out, err, opt_password, crts);
    } else if(decrypt) {
        if(opt_password == NULL && (opt_key == NULL || sk_X509_num(crts) == 0)) {
            fprintf(stderr, "You must specify either --password/-p or --recipient/-r and --key/-k\n");
            goto end;
        }
        ret = decrypt_cms(in, out, err, opt_password, x509, key);
    }

 end:
    return ret;
}
void my_process(int data_x_offset,const cateType* data_y,int mb_idx,real learning_rate){
    {
        int t = K_fw_l0_conv;
        load_engine(t);
        printf("Running on DFE: fw_l0_conv");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "ni", 1);
        max_set_param_uint64t(act, "no", NKERS[0]);
        max_queue_input(act, "b", layer0_b, layer0_b_size);
        max_queue_input(act, "w", layer0_w, layer0_w_size);
        max_set_param_uint64t(act, "x_offset", data_x_offset+mb_idx*layer0_x_size);
        max_set_param_uint64t(act, "z_offset", layer0_z2_offset);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_fw_l0_maxpool;
        load_engine(t);
        printf("Running on DFE: fw_l0_maxpool");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "no", NKERS[0]);
        max_set_param_uint64t(act, "z2_offset", layer0_z2_offset);
        max_set_param_uint64t(act, "sel_offset", layer0_sel_offset);
        max_set_param_uint64t(act, "z_offset", layer0_z_offset);
        max_set_param_uint64t(act, "a_offset", layer0_a_offset);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_fw_l1_conv;
        load_engine(t);
        printf("Running on DFE: fw_l1_conv");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "ni", NKERS[0]);
        max_set_param_uint64t(act, "no", NKERS[1]);
        max_queue_input(act, "b", layer1_b, layer1_b_size);
        max_queue_input(act, "w", layer1_w, layer1_w_size);
        max_set_param_uint64t(act, "x_offset", layer1_x_offset);
        max_set_param_uint64t(act, "z_offset", layer1_z2_offset);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_fw_l1_maxpool;
        load_engine(t);
        printf("Running on DFE: fw_l1_maxpool");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "no", NKERS[1]);
        max_set_param_uint64t(act, "z2_offset", layer1_z2_offset);
        max_set_param_uint64t(act, "sel_offset", layer1_sel_offset);
        max_set_param_uint64t(act, "z_offset", layer1_z_offset);
        max_set_param_uint64t(act, "a_offset", layer1_a_offset);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_fw_l3_softmax;
        load_engine(t);
        printf("Running on DFE: fw_l3_softmax");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "ni", NKERS[2]);
        max_set_param_uint64t(act, "x_offset", layer3_x_offset);
        max_queue_input(act, "w", layer3_w, layer3_w_size);
        max_queue_input(act, "b", layer3_b, layer3_b_size);
        max_set_param_uint64t(act, "softmax_offset", layer3_sm_offset);
        max_queue_output(act, "pred", layer3_pred, layer3_pred_size);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    //TODO: learning rate<0 exit
    {
        int t = K_bp_l3_softmax;
        load_engine(t);
        printf("Running on DFE: bp_l3_softmax");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "ni", NKERS[2]);
        max_set_param_uint64t(act, "x_offset", layer3_x_offset);
        max_queue_input(act, "w", layer3_w, layer3_w_size);
        max_set_param_uint64t(act, "softmax_offset", layer3_sm_offset);
        max_queue_input(act, "std", data_y+mb_idx*layer3_pred_size, layer3_pred_size);
        max_queue_output(act, "w_grad", layer3_w_grad, layer3_w_grad_size);
        max_queue_output(act, "b_grad", layer3_b_grad, layer3_b_grad_size);
        max_set_param_uint64t(act, "x_grad_offset", layer3_x_grad_offset);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_bp_l1_maxpool;
        load_engine(t);
        printf("Running on DFE: bp_l1_maxpool");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "no", NKERS[1]);
        max_set_param_uint64t(act, "a_grad_offset", layer1_a_grad_offset);
        max_set_param_uint64t(act, "z_offset", layer1_z_offset);
        max_set_param_uint64t(act, "sel_offset", layer1_sel_offset);
        max_set_param_uint64t(act, "z2_grad_offset", layer1_z2_grad_offset);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_bp_l1_conv;
        load_engine(t);
        printf("Running on DFE: bp_l1_conv");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "ni", NKERS[0]);
        max_set_param_uint64t(act, "no", NKERS[1]);
        max_set_param_uint64t(act, "z_grad_offset", layer1_z2_grad_offset);
        max_set_param_uint64t(act, "x_offset", layer1_x_offset);
        max_set_param_uint64t(act, "x_grad_offset", layer1_x_grad_offset);
        max_queue_input(act, "w", layer1_w, layer1_w_size);
        max_queue_output(act, "w_grad", layer1_w_grad, layer1_w_grad_size);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_bp_l0_maxpool;
        load_engine(t);
        printf("Running on DFE: bp_l0_maxpool");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "no", NKERS[0]);
        max_set_param_uint64t(act, "a_grad_offset", layer0_a_grad_offset);
        max_set_param_uint64t(act, "z_offset", layer0_z_offset);
        max_set_param_uint64t(act, "sel_offset", layer0_sel_offset);
        max_set_param_uint64t(act, "z2_grad_offset", layer0_z2_grad_offset);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
    {
        int t = K_bp_l0_conv;
        load_engine(t);
        printf("Running on DFE: bp_l0_conv");
        mark_timer(false,1);
        max_actions_t* act = max_actions_init(max_files[t], "default");
        max_set_param_uint64t(act, "ni", 1);
        max_set_param_uint64t(act, "no", NKERS[0]);
        max_set_param_uint64t(act, "z_grad_offset", layer0_z2_grad_offset);
        max_set_param_uint64t(act, "x_offset", data_x_offset+mb_idx*layer0_x_size);
        max_set_param_uint64t(act, "x_grad_offset", layer0_x_grad_offset);
        max_queue_input(act, "w", layer0_w, layer0_w_size);
        max_queue_output(act, "w_grad", layer0_w_grad, layer0_w_grad_size);
        max_run(max_engines[t], act);
        max_actions_free(act);
        mark_timer(true,1);
    }
}