Tree view for certificates

initial password (does still not work)
saving and restoring of settings
This commit is contained in:
chris 2002-07-11 01:42:03 +00:00
parent 99682311a2
commit 7ff69f4688
19 changed files with 380 additions and 176 deletions

View File

@ -16,14 +16,14 @@ MainWindow::MainWindow(QWidget *parent, const char *name)
if (!d.mkdir(baseDir))
cerr << "Couldnt create: " << baseDir.latin1() << "\n";
}
QString dbfile = baseDir + "/xca.db";
keys = new db_key(dbenv, dbfile.latin1(), "keydb", keyList);
reqs = new db_x509req(dbenv, dbfile.latin1(), "reqdb", reqList);
certs = new db_x509(dbenv, dbfile.latin1(), "certdb", certList);
dbfile = baseDir + "/xca.db";
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
loadSettings();
initPass();
keys = new db_key(dbenv, dbfile.latin1(), "keydb", keyList, passwd);
reqs = new db_x509req(dbenv, dbfile.latin1(), "reqdb", reqList);
certs = new db_x509(dbenv, dbfile.latin1(), "certdb", certList);
};
@ -36,8 +36,79 @@ MainWindow::~MainWindow()
delete(certs);
}
void MainWindow::loadSettings()
{
int x;
Dbc *cursor;
Db *data = new Db(dbenv, 0);
Dbt *k = new Dbt();
Dbt *d = new Dbt();
if ((x = data->open(dbfile.latin1(), "settings", DB_BTREE, DB_CREATE, 0600)))
data->err(x,"DB open");
if ((x = data->cursor(NULL, &cursor, 0)))
data->err(x,"DB new Cursor");
cerr <<"laden" <<endl;
while (!cursor->get(k, d, DB_NEXT)) {
cerr << "in Whileload: "<< endl;
if (x) data->err(x,"DB Error get");
else {
settings.insert((char *)k->get_data(), (char *)d->get_data());
}
}
cerr <<"pwhash " << settings["pwhash"] <<endl;
data->close(0);
delete(d);
delete(k);
delete(data);
}
// Static Password Callback functions
void MainWindow::saveSettings()
{
int x;
Dbc *cursor;
Db *data = new Db(dbenv, 0);
if ((x = data->open(dbfile.latin1(), "settings", DB_BTREE, DB_CREATE, 0600)))
data->err(x,"DB open");
if ((x = data->cursor(NULL, &cursor, 0)))
data->err(x,"DB new Cursor");
QAsciiDictIterator<char> it(settings);
for ( ; it.current(); ++it ) {
cerr << "in save: "<< it.current() << endl;
Dbt k( (void *)it.currentKey(), strlen(it.currentKey()) +1 );
Dbt d( (void *)it.current(), strlen(it.current()) +1 );
cerr << it.currentKey() << " -- " << it.current() <<endl;
int x = data->put(NULL, &k, &d, 0);
if (x) data->err(x,"DB Error put");
}
data->close(0);
delete(data);
}
void MainWindow::initPass()
{
if (!settings["pwhash"]) {
int keylen = passWrite(passwd, sizeof(passwd), 0, NULL);
if (keylen == 0) {
qFatal("Ohne Passwort laeuft hier gaaarnix :-)");
}
passwd[keylen]='\0';
settings.insert( "pwhash", passwd );
settings.insert( "meier", "aaaa");
settings.insert( "mueller", "bbbbb");
saveSettings();
}
else {
while (strncmp(passwd, settings["pwhash"], sizeof(passwd))) {
int keylen = passRead(passwd, sizeof(passwd), 0, NULL);
if (keylen == 0) {
qFatal("Ohne Passwort laeuft hier gaaarnix :-)");
}
passwd[keylen]='\0';
cerr << passwd << " - " << settings["pwhash"] << endl;
}
}
}
// Static Password Callback functions
int MainWindow::passRead(char *buf, int size, int rwflag, void *userdata)
{

View File

@ -19,7 +19,7 @@
#include <qdir.h>
#include <qlineedit.h>
#include <qcombobox.h>
#include <qlistbox.h>
#include <qlistview.h>
#include <qobjectlist.h>
#include <qobjcoll.h>
#include <qlabel.h>
@ -27,6 +27,7 @@
#include <qmessagebox.h>
#include <qcheckbox.h>
#include <qprogressdialog.h>
#include <qasciidict.h>
#include <qobject.h>
#ifndef MAINWINDOW_H
@ -43,11 +44,16 @@ class MainWindow: public MainWindow_UI
db_x509req *reqs;
db_key *keys;
DbEnv *dbenv;
char passwd[30];
public:
QString baseDir;
QAsciiDict<char> settings;
QString baseDir, dbfile;
static const int sizeList[];
MainWindow(QWidget *parent, const char *name);
~MainWindow();
void loadSettings();
void saveSettings();
void initPass();
void showDetailsKey(pki_key *key);
static int passRead(char *buf, int size, int rwflag, void *userdata);
static int passWrite(char *buf, int size, int rwflag, void *userdata);

View File

@ -11,7 +11,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>607</width>
<width>599</width>
<height>533</height>
</rect>
</property>
@ -237,7 +237,21 @@
</widget>
</widget>
<widget>
<class>QListBox</class>
<class>QListView</class>
<column>
<property>
<name>text</name>
<string>Schlüssel</string>
</property>
<property>
<name>clickable</name>
<bool>true</bool>
</property>
<property>
<name>resizeable</name>
<bool>true</bool>
</property>
</column>
<property stdset="1">
<name>name</name>
<cstring>keyList</cstring>
@ -245,9 +259,9 @@
<property stdset="1">
<name>geometry</name>
<rect>
<x>20</x>
<x>10</x>
<y>20</y>
<width>280</width>
<width>290</width>
<height>390</height>
</rect>
</property>
@ -412,7 +426,21 @@
</widget>
</widget>
<widget>
<class>QListBox</class>
<class>QListView</class>
<column>
<property>
<name>text</name>
<string>Anfragen</string>
</property>
<property>
<name>clickable</name>
<bool>true</bool>
</property>
<property>
<name>resizeable</name>
<bool>true</bool>
</property>
</column>
<property stdset="1">
<name>name</name>
<cstring>reqList</cstring>
@ -420,9 +448,9 @@
<property stdset="1">
<name>geometry</name>
<rect>
<x>20</x>
<x>10</x>
<y>20</y>
<width>280</width>
<width>290</width>
<height>390</height>
</rect>
</property>
@ -587,7 +615,21 @@
</widget>
</widget>
<widget>
<class>QListBox</class>
<class>QListView</class>
<column>
<property>
<name>text</name>
<string>Zertifikate</string>
</property>
<property>
<name>clickable</name>
<bool>true</bool>
</property>
<property>
<name>resizeable</name>
<bool>true</bool>
</property>
</column>
<property stdset="1">
<name>name</name>
<cstring>certList</cstring>
@ -595,9 +637,9 @@
<property stdset="1">
<name>geometry</name>
<rect>
<x>20</x>
<x>10</x>
<y>20</y>
<width>280</width>
<width>290</width>
<height>390</height>
</rect>
</property>
@ -771,12 +813,6 @@
<receiver>MainWindow_UI</receiver>
<slot>showDetailsKey()</slot>
</connection>
<connection>
<sender>keyList</sender>
<signal>doubleClicked(QListBoxItem*)</signal>
<receiver>MainWindow_UI</receiver>
<slot>showDetailsKey()</slot>
</connection>
<connection>
<sender>PushButton13</sender>
<signal>clicked()</signal>
@ -789,12 +825,6 @@
<receiver>MainWindow_UI</receiver>
<slot>showDetailsReq()</slot>
</connection>
<connection>
<sender>reqList</sender>
<signal>doubleClicked(QListBoxItem*)</signal>
<receiver>MainWindow_UI</receiver>
<slot>showDetailsReq()</slot>
</connection>
<connection>
<sender>PushButton26</sender>
<signal>clicked()</signal>
@ -861,12 +891,6 @@
<receiver>MainWindow_UI</receiver>
<slot>deleteKey()</slot>
</connection>
<connection>
<sender>certList</sender>
<signal>doubleClicked(QListBoxItem*)</signal>
<receiver>MainWindow_UI</receiver>
<slot>showDetailsCert()</slot>
</connection>
<slot access="public">deleteCert()</slot>
<slot access="public">deleteKey()</slot>
<slot access="public">deleteReq()</slot>

View File

@ -5,17 +5,28 @@ TARGET=xca-$(VERSION)
PREFIX=/usr/X11R6
GCC=g++
CFLAGS=-Wall -g
QTDIR=$(shell ./configure)
QTDIR=/usr/lib/qt
DEBQT=/usr/include/qt
export INC=-I$(QTDIR)/include -I$(DEBQT)
###########################
# LFS
QTDIR=/usr/lib/qt
export INC=-I$(QTDIR)/include
LPATH=-L$(QTDIR)/lib -Llib
LIBS=-lqt -lcrypto -ldb_cxx -lxcadb -lpki
MOC=$(QTDIR)/bin/moc
UIC=$(QTDIR)/bin/uic
###########################
# debian woody
#export INC=-I/usr/include/qt
#LPATH=-Llib
#LIBS=-lqt -lcrypto -ldb3_cxx -lxcadb -lpki
#MOC=/usr/bin/moc
#UIC=/usr/bin/uic
###################################
OBJS=NewKey_UI.o NewKey_UI_MOC.o \
KeyDetail_UI.o KeyDetail_UI_MOC.o \
ReqDetail_UI.o ReqDetail_UI_MOC.o \

View File

@ -1,7 +1,7 @@
#include "db_base.h"
db_base::db_base(DbEnv *dbe, string DBfile, string DB, QListBox *l)
db_base::db_base(DbEnv *dbe, string DBfile, string DB, QListView *l)
{
listView = l;
dbenv = dbe;
@ -10,6 +10,7 @@ db_base::db_base(DbEnv *dbe, string DBfile, string DB, QListBox *l)
int x;
if (( x = data->open(DBfile.c_str(), DB.c_str(), DB_BTREE, DB_CREATE, 0600)))
data->err(x,"DB open");
passwd = NULL;
}
@ -28,15 +29,19 @@ void db_base::loadContainer()
Dbt *d = new Dbt();
string desc;
pki_base *pki;
container.clear();
while (!cursor->get(k, d, DB_NEXT)) {
desc = (char *)k->get_data();
p = (unsigned char *)d->get_data();
int size = d->get_size();
pki = newPKI();
pki->fromData(p, size);
if (pki == NULL) continue;
cerr << desc.c_str() << endl;
pki->setDescription(desc);
container.append(pki);
if (pki->fromData(passwd, p, size)) {
pki->setDescription(desc);
container.append(pki);
}
else delete(pki);
}
delete (k);
delete (d);
@ -47,8 +52,11 @@ bool db_base::updateView()
{
listView->clear();
pki_base *pki;
for ( pki = container.first(); pki != 0; pki = container.next() ) {
listView->insertItem(pki->getDescription().c_str());
if (container.isEmpty()) return false;
QListIterator<pki_base> it(container);
for ( ; it.current(); ++it ) {
pki = it.current();
listView->insertItem(new QListViewItem(listView, pki->getDescription().c_str()));
}
return true;
}
@ -61,7 +69,7 @@ bool db_base::insertPKI(pki_base *pki)
int size=0;
char field[10];
unsigned char *p;
p = pki->toData(&size);
p = pki->toData(passwd, &size);
int cnt=0;
int x = DB_KEYEXIST;
while (x == DB_KEYEXIST) {
@ -93,15 +101,20 @@ bool db_base::deletePKI(pki_base *pki)
string desc = pki->getDescription();
Dbt k((void *)desc.c_str(), desc.length() + 1);
int x = data->del(NULL, &k, 0);
updateView();
if (x){
data->err(x,"DB Error del");
return false;
}
container.remove(pki);
remFromCont(pki);
updateView();
return true;
}
void db_base::remFromCont(pki_base *pki)
{
container.remove(pki);
}
bool db_base::updatePKI(pki_base *pki, string desc)
{
if (deletePKI(pki)){
@ -116,15 +129,20 @@ pki_base *db_base::getSelectedPKI(string desc)
{
if (desc == "" ) return NULL;
pki_base *pki;
for ( pki = container.first(); pki != 0; pki = container.next() )
QListIterator<pki_base> it(container);
for ( ; it.current(); ++it ) {
pki = it.current();
if (pki->getDescription() == desc) return pki;
}
}
pki_base *db_base::getSelectedPKI()
{
const char *tp;
string desc = "";
if ((tp = listView->currentText().latin1())) desc = tp;
QListViewItem *lvi;
if ((lvi = listView->selectedItem()) == NULL) return NULL;
if ((tp = lvi->text(0).latin1())) desc = tp;
cerr << "desc = '"<<desc <<"'\n";
return getSelectedPKI(desc);
}
@ -133,7 +151,10 @@ pki_base *db_base::getSelectedPKI()
pki_base *db_base::findPKI(pki_base *refpki)
{
pki_base *pki;
for ( pki = container.first(); pki != 0; pki = container.next() )
QListIterator<pki_base> it(container);
for ( ; it.current(); ++it ) {
pki = it.current();
if (refpki->compare(pki)) return pki;
}
return NULL;
}

View File

@ -1,5 +1,5 @@
#include <db_cxx.h>
#include <qlistbox.h>
#include <qlistview.h>
#include <qlist.h>
#include "pki_base.h"
@ -12,10 +12,11 @@ class db_base
protected:
Db *data;
DbEnv *dbenv;
QListBox *listView;
QListView *listView;
QList<pki_base> container;
char *passwd;
public:
db_base(DbEnv *dbe, string DBfile, string db, QListBox *l);
db_base(DbEnv *dbe, string DBfile, string db, QListView *l);
virtual ~db_base();
virtual pki_base *newPKI(){
cerr<<"VIRTUAL CALLED: newPKI\n"; return NULL;}
@ -27,6 +28,7 @@ class db_base
pki_base *getSelectedPKI();
pki_base *findPKI(pki_base *refpki);
virtual void loadContainer();
virtual void remFromCont(pki_base *pki);
};
#endif

View File

@ -1,9 +1,10 @@
#include "db_key.h"
db_key::db_key(DbEnv *dbe, string DBfile, string DB, QListBox *l)
db_key::db_key(DbEnv *dbe, string DBfile, string DB, QListView *l, char *pass)
:db_base(dbe, DBfile, DB, l)
{
passwd = pass;
loadContainer();
updateView();
}

View File

@ -10,7 +10,7 @@
class db_key: public db_base
{
public:
db_key(DbEnv *dbe, string DBfile, string DB, QListBox *l);
db_key(DbEnv *dbe, string DBfile, string DB, QListView *l, char * pass);
pki_base *newPKI();
QStringList getPrivateDesc();
};

View File

@ -1,7 +1,7 @@
#include "db_x509.h"
db_x509::db_x509(DbEnv *dbe, string DBfile, string DB, QListBox *l)
db_x509::db_x509(DbEnv *dbe, string DBfile, string DB, QListView *l)
:db_base(dbe, DBfile, DB, l)
{
loadContainer();
@ -14,55 +14,77 @@ pki_base *db_x509::newPKI(){
pki_x509 *db_x509::findsigner(pki_x509 *client)
{
unsigned char *p;
Dbc *cursor;
if (int x = data->cursor(NULL, &cursor, 0))
data->err(x,"DB new Cursor");
Dbt *k = new Dbt();
Dbt *d = new Dbt();
string desc;
pki_x509 *signer;
while (!cursor->get(k, d, DB_NEXT)) {
desc = (char *)k->get_data();
p = (unsigned char *)d->get_data();
int size = d->get_size();
signer = (pki_x509 *)newPKI();
signer->fromData(p, size);
cerr << "verifying " << signer->getDescription().c_str() << endl;
signer->setDescription(desc);
pki_x509 *signer;
if ((signer = client->getSigner()) != NULL) return signer;
QListIterator<pki_base> it(container);
for ( ; it.current(); ++it ) {
signer = (pki_x509 *)it.current();
if (client->verify(signer)) {
delete (k);
delete (d);
cerr << "SIGNER found" <<endl;
return signer;
}
delete(signer);
}
delete (k);
delete (d);
}
return NULL;
}
/*
bool db_x509::updateView()
{
listView->clear();
Dbc *cursor;
if (int x = data->cursor(NULL, &cursor, 0))
data->err(x,"DB new Cursor");
Dbt *key = new Dbt();
Dbt *data = new Dbt();
pki_x509 *client;
QString desc;
while (!cursor->get(key, data, DB_NEXT)) {
desc = (char *)key->get_data();
p = (unsigned char *)d->get_data();
int size = d->get_size();
client = (pki_x509 *)newPKI();
client->fromData(p, size);
signer = findsigner(client);
listView->insertItem(desc);
}
listView->clear();
listView->setRootIsDecorated(true);
pki_x509 *pki;
pki_x509 *signer;
QListViewItem *parentitem;
QListViewItem *current;
cerr <<"myupdate"<<endl;
if ( container.isEmpty() ) return false;
QList<pki_base> mycont = container;
for ( pki = (pki_x509 *)container.first(); pki != NULL; pki = (pki_x509 *)container.next() ) pki->delPointer();
int f=0;
while (! mycont.isEmpty() ) {
cerr << "-----------------------------------------------------------------Round "<< f++ <<endl;
QListIterator<pki_base> it(mycont);
for ( ; it.current(); ++it ) {
pki = (pki_x509 *)it.current();
parentitem = NULL;
signer = findsigner(pki);
cerr << "ARound "<< pki <<" - "<< signer << endl;
if ((signer != pki) && (signer != NULL)) // foreign signed
parentitem = (QListViewItem *)signer->getPointer();
if (signer == NULL); // do something for unknown signers...
if (((parentitem != NULL) || (signer == pki) || (signer == NULL)) && (pki->getPointer() == 0 )) {
// create the listview item
if (parentitem != NULL) {
current = new QListViewItem(parentitem, pki->getDescription().c_str());
cerr<< "Adding as client: "<<pki->getDescription().c_str()<<endl;
}
else {
current = new QListViewItem(listView, pki->getDescription().c_str());
cerr<< "Adding as parent: "<<pki->getDescription().c_str()<<endl;
}
pki->setPointer(current);
mycont.remove(pki);
it.toFirst();
listView->setOpen(current, true);
cerr << "CRound " <<endl;
}
}
}
return true;
}
*/
void db_x509::remFromCont(pki_base *pki)
{
container.remove(pki);
pki_x509 *pkiit;
QListIterator<pki_base> it(container);
for ( ; it.current(); ++it ) {
pkiit = (pki_x509 *)it.current();
if (pkiit->getSigner()==pki) {
pkiit->delSigner();
return;
}
}
return;
}

View File

@ -1,3 +1,4 @@
#include <qlistview.h>
#include <db_cxx.h>
#include "db_base.h"
#include "pki_x509.h"
@ -9,9 +10,11 @@
class db_x509: public db_base
{
public:
db_x509(DbEnv *dbe, string DBfile, string DB, QListBox *l);
db_x509(DbEnv *dbe, string DBfile, string DB, QListView *l);
pki_base *newPKI();
pki_x509 *findsigner(pki_x509 *client);
bool updateView();
void remFromCont(pki_base *pki);
};
#endif

View File

@ -1,7 +1,7 @@
#include "db_x509req.h"
db_x509req::db_x509req(DbEnv *dbe, string DBfile, string DB, QListBox *l)
db_x509req::db_x509req(DbEnv *dbe, string DBfile, string DB, QListView *l)
:db_base(dbe, DBfile, DB, l)
{
loadContainer();
@ -14,35 +14,18 @@ pki_base *db_x509req::newPKI(){
QStringList db_x509req::gethasPrivateDesc(db_base *keydb)
{
unsigned char *p;
Dbc *cursor;
if (int x = data->cursor(NULL, &cursor, 0))
data->err(x,"DB new Cursor");
Dbt *k = new Dbt();
Dbt *d = new Dbt();
string desc;
QStringList x;
pki_x509req *pki;
pki_key *key, *refkey;
pki_x509req *req = (pki_x509req *)newPKI();
//cerr << "before While loop\n";
while (!cursor->get(k, d, DB_NEXT)) {
//cerr << "in loop\n";
desc = (char *)k->get_data();
p = (unsigned char *)d->get_data();
int size = d->get_size();
req->fromData(p, size);
key = req->getKey();
QStringList x;
if ( container.isEmpty() ) return x;
for ( pki = (pki_x509req *)container.first(); pki != 0; pki = (pki_x509req *)container.next() ) {
key = pki->getKey();
if ((refkey = (pki_key *)keydb->findPKI(key))!= NULL) {
if (refkey->isPrivKey()){
x.append(desc.data());
cerr << desc <<endl;
x.append(pki->getDescription().c_str());
cerr << "found" <<endl;
}
delete(refkey);
}
delete(key);
}
delete(req);
delete (k);
delete (d);
return x;
}

View File

@ -10,7 +10,7 @@
class db_x509req: public db_base
{
public:
db_x509req(DbEnv *dbe, string DBfile, string DB, QListBox *l);
db_x509req(DbEnv *dbe, string DBfile, string DB, QListView *l);
pki_base *newPKI();
QStringList gethasPrivateDesc(db_base *keydb);
};

View File

@ -11,14 +11,15 @@ class pki_base
string desc;
string error;
bool openssl_error();
void *pointer;
public:
enum { VERIFY_ERROR, VERIFY_SELFSIGNED, VERIFY_TRUSTED,
VERIFY_UNTRUSTED, VERIFY_OK };
pki_base(const string d);
pki_base();
virtual void fromData(unsigned char *p, int size){
cerr << "VIRTUAL FUNCTION CALLED: fromData\n"; };
virtual unsigned char *toData(int *size){
virtual bool fromData(char *passwd, unsigned char *p, int size){
cerr << "VIRTUAL FUNCTION CALLED: fromData\n"; return false; };
virtual unsigned char *toData(char *passwd, int *size){
cerr << "VIRTUAL FUNCTION CALLED: toData\n";
return NULL;};
virtual bool compare(pki_base *ref){
@ -28,6 +29,9 @@ class pki_base
string getDescription();
void setDescription(const string d );
string getError();
void setPointer(void *ptr) { pointer = ptr; }
void delPointer() { pointer = NULL; }
void *getPointer() { return pointer; }
};
#endif

View File

@ -79,51 +79,86 @@ pki_key::pki_key(const string fname, pem_password_cb *cb, int type=EVP_PKEY_RSA)
}
void pki_key::fromData(unsigned char *p, int size )
bool pki_key::fromData(char *passwd, unsigned char *p, int size )
{
cerr << "KEY fromData\n";
unsigned char *sik;
sik = (unsigned char *)OPENSSL_malloc(size);
unsigned char *sik, *pdec, *pdec1, *sik1;
int outl, decsize;
RSA *rsakey;
memcpy(sik,p,size);
EVP_CIPHER_CTX ctx;
sik = (unsigned char *)OPENSSL_malloc(size);
if ( sik == NULL ) return false;
pdec = (unsigned char *)OPENSSL_malloc(size);
if (pdec == NULL ) {OPENSSL_free(sik); return false;}
pdec1=pdec;
sik1=sik;
EVP_CIPHER_CTX_init (&ctx);
EVP_DecryptInit( &ctx, EVP_des_ede3_cbc(),(unsigned char *)passwd, NULL);
EVP_DecryptUpdate( &ctx, pdec, &outl, p, size );
decsize = outl;
EVP_DecryptFinal( &ctx, pdec + decsize, &outl );
decsize += outl;
cerr << "Encr done: " << size << "--" << decsize << endl;
if (openssl_error()) return false;
memcpy(sik, pdec, decsize);
if (key->type == EVP_PKEY_RSA) {
rsakey = d2i_RSAPrivateKey(NULL, &p, size);
rsakey = d2i_RSAPrivateKey(NULL, &pdec, decsize);
if (openssl_error()) {
rsakey = d2i_RSA_PUBKEY(NULL, &sik, size);
rsakey = d2i_RSA_PUBKEY(NULL, &sik, decsize);
}
openssl_error();
if (openssl_error()) return false;
if (rsakey) EVP_PKEY_set1_RSA(key, rsakey);
}
OPENSSL_free(sik);
OPENSSL_free(sik1);
OPENSSL_free(pdec1);
return true;
}
unsigned char *pki_key::toData(int *size)
unsigned char *pki_key::toData(char *passwd, int *size)
{
cerr << "KEY toData " << getDescription()<< endl;
unsigned char *p = NULL , *p1;
unsigned char *p, *p1, *penc;
int outl, encsize=0;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (&ctx);
EVP_EncryptInit( &ctx, EVP_des_ede3_cbc(),(unsigned char *)passwd , NULL);
if (key->type == EVP_PKEY_RSA) {
if (isPubKey()) {
*size = i2d_RSA_PUBKEY(key->pkey.rsa, NULL);
cerr << "Sizeofpubkey: " << *size <<endl;
openssl_error();
p = (unsigned char *)OPENSSL_malloc(*size);
penc = (unsigned char *)OPENSSL_malloc(*size + EVP_MAX_KEY_LENGTH - 1);
p1 = p;
i2d_RSA_PUBKEY(key->pkey.rsa, &p1);
openssl_error();
EVP_EncryptUpdate( &ctx, penc, &outl, p, *size );
encsize = outl;
openssl_error();
}
else {
*size = i2d_RSAPrivateKey(key->pkey.rsa, NULL);
cerr << "Sizeofprivkey: " << *size <<endl;
openssl_error();
p = (unsigned char *)OPENSSL_malloc(*size);
penc = (unsigned char *)OPENSSL_malloc(*size + EVP_MAX_KEY_LENGTH - 1);
p1 = p;
i2d_RSAPrivateKey(key->pkey.rsa, &p1);
EVP_EncryptUpdate( &ctx, penc, &outl, p, *size );
encsize = outl;
openssl_error();
}
}
cerr << "KEY toData end ...\n";
return p;
EVP_EncryptFinal( &ctx, penc + encsize, &outl );
encsize += outl;
OPENSSL_free(p);
cerr << "KEY toData end ..."<< encsize << "--"<<*size <<endl;
*size = encsize;
return penc;
}

View File

@ -47,8 +47,8 @@ class pki_key: public pki_base
pki_key(const string d, int type=EVP_PKEY_RSA);
pki_key(EVP_PKEY *pkey);
void fromData(unsigned char *p, int size);
unsigned char *toData(int *size);
bool fromData(char *passwd, unsigned char *p, int size);
unsigned char *toData(char* passwd, int *size);
bool compare(pki_base *ref);
string length();
string modulus();

View File

@ -67,7 +67,7 @@ pki_x509::pki_x509(pki_key *key, const string cn,
}
openssl_error();
trust = true;
parent = NULL;
psigner = NULL;
}
@ -76,7 +76,7 @@ pki_x509::pki_x509() : pki_base()
{
cert = X509_new();
openssl_error();
parent= NULL;
psigner = NULL;
trust = false;
}
@ -102,14 +102,15 @@ pki_x509::pki_x509(const string fname)
else error = "Fehler beim Öffnen der Datei";
fclose(fp);
trust = false;
parent = NULL;
psigner = NULL;
}
void pki_x509::fromData(unsigned char *p, int size)
bool pki_x509::fromData(char *passwd, unsigned char *p, int size)
{
cert = d2i_X509(NULL, &p, size);
openssl_error();
if (openssl_error()) return false;
return true;
}
@ -155,7 +156,7 @@ string pki_x509::notAfter()
return time;
}
unsigned char *pki_x509::toData(int *size)
unsigned char *pki_x509::toData(char *passwd, int *size)
{
unsigned char *p, *p1;
*size = i2d_X509(cert, NULL);
@ -200,19 +201,38 @@ bool pki_x509::compare(pki_base *refreq)
bool pki_x509::verify(pki_x509 *signer)
{
if (parent == signer) return true;
if (parent != NULL) return false;
EVP_PKEY *pkey = X509_get_pubkey(signer->cert);
int i = X509_verify(cert,pkey);
EVP_PKEY_free(pkey);
openssl_error();
if (i>0) return true;
return false;
if (psigner == signer) return true;
if (psigner != NULL) return false;
if (getDNi(NID_commonName) != signer->getDNs(NID_commonName)) {
return false;
}
EVP_PKEY *pkey = X509_get_pubkey(signer->cert);
int i = X509_verify(cert,pkey);
openssl_error();
if (i>0) {
cerr << "psigner set for: " << getDescription().c_str() << endl;
psigner = signer;
return true;
}
return false;
}
pki_key *pki_x509::getKey()
{
EVP_PKEY *pkey = X509_get_pubkey(cert);
pki_key *key = new pki_key(pkey);
return key;
EVP_PKEY *pkey = X509_get_pubkey(cert);
pki_key *key = new pki_key(pkey);
return key;
}
pki_x509 *pki_x509::getSigner()
{
cerr << "getSigner..." << psigner << endl;
return (psigner);
}
void pki_x509::delSigner()
{
cerr << "delSigner..." << psigner << endl;
psigner=NULL;
}

View File

@ -9,9 +9,9 @@
class pki_x509 : public pki_base
{
pki_x509 *parent; // the signer
bool trust;
X509 *cert;
pki_x509 *psigner;
public:
pki_x509(pki_key *key, const string cn,
const string c, const string l,
@ -20,8 +20,8 @@ class pki_x509 : public pki_base
const string d, int days=365);
pki_x509();
pki_x509(const string fname);
virtual void fromData(unsigned char *p, int size);
virtual unsigned char *toData(int *size);
virtual bool fromData(char *passwd, unsigned char *p, int size);
virtual unsigned char *toData(char *passwd, int *size);
virtual bool compare(pki_base *refcert);
string getDNs(int nid);
string getDNi(int nid);
@ -30,6 +30,8 @@ class pki_x509 : public pki_base
pki_key *getKey();
string notAfter();
string notBefore();
pki_x509 *getSigner();
void pki_x509::delSigner();
};
#endif

View File

@ -74,10 +74,11 @@ pki_x509req::pki_x509req(const string fname)
}
void pki_x509req::fromData(unsigned char *p, int size)
bool pki_x509req::fromData(char *passwd, unsigned char *p, int size)
{
request = d2i_X509_REQ(NULL, &p, size);
openssl_error();
if (openssl_error()) return false;
return true;
}
@ -92,7 +93,7 @@ string pki_x509req::getDN(int nid)
}
unsigned char *pki_x509req::toData(int *size)
unsigned char *pki_x509req::toData(char *passwd, int *size)
{
unsigned char *p, *p1;
*size = i2d_X509_REQ(request, NULL);
@ -147,8 +148,6 @@ int pki_x509req::verify()
pki_key *pki_x509req::getKey()
{
EVP_PKEY *pkey = X509_REQ_get_pubkey(request);
pki_key *key = new pki_key("");
key->key=pkey;
key->onlyPubKey=true;
pki_key *key = new pki_key(pkey);
return key;
}

View File

@ -17,8 +17,8 @@ class pki_x509req : public pki_base
const string d);
pki_x509req();
pki_x509req(const string fname);
virtual void fromData(unsigned char *p, int size);
virtual unsigned char *toData(int *size);
virtual bool fromData(char *passwd, unsigned char *p, int size);
virtual unsigned char *toData(char *passwd, int *size);
virtual bool compare(pki_base *refreq);
string getDN(int nid);
void writeReq(const string fname, bool PEM);