xca/lib/pki_x509req.cpp
chris f089fb1f4b more translation
pkcs12 export added
2002-07-17 23:52:49 +00:00

173 lines
3.9 KiB
C++

#include "pki_x509req.h"
pki_x509req::pki_x509req(pki_key *key, const string cn,
const string c, const string l,
const string st,const string o,
const string ou,const string email,
const string d, const string challenge)
:pki_base( d )
{
request = X509_REQ_new();
openssl_error();
if (key == NULL) {
pki_error( "key ist null");
return;
}
openssl_error();
X509_REQ_set_version(request, 0L);
openssl_error();
X509_REQ_set_pubkey(request, key->key);
openssl_error();
X509_NAME *subj = X509_REQ_get_subject_name(request);
if (cn != "")
X509_NAME_add_entry_by_NID(subj,NID_commonName, MBSTRING_ASC,
(unsigned char*)cn.c_str(),-1,-1,0);
if (c != "")
X509_NAME_add_entry_by_NID(subj,NID_countryName, MBSTRING_ASC,
(unsigned char*)c.c_str() , -1, -1, 0);
if (l != "")
X509_NAME_add_entry_by_NID(subj,NID_localityName, MBSTRING_ASC,
(unsigned char*)l.c_str() , -1, -1, 0);
if (st != "")
X509_NAME_add_entry_by_NID(subj,NID_stateOrProvinceName, MBSTRING_ASC,
(unsigned char*)st.c_str() , -1, -1, 0);
if (o != "")
X509_NAME_add_entry_by_NID(subj,NID_organizationName, MBSTRING_ASC,
(unsigned char*)o.c_str() , -1, -1, 0);
if (ou != "")
X509_NAME_add_entry_by_NID(subj,NID_organizationalUnitName, MBSTRING_ASC,
(unsigned char*)ou.c_str() , -1, -1, 0);
if (email != "")
X509_NAME_add_entry_by_NID(subj,NID_pkcs9_emailAddress, MBSTRING_ASC,
(unsigned char*)email.c_str() , -1, -1, 0);
const EVP_MD *digest = EVP_md5();
X509_REQ_sign(request,key->key ,digest);
openssl_error();
}
pki_x509req::pki_x509req() : pki_base()
{
request = X509_REQ_new();
openssl_error();
}
pki_x509req::~pki_x509req()
{
X509_REQ_free(request);
openssl_error();
}
pki_x509req::pki_x509req(const string fname)
{
FILE *fp = fopen(fname.c_str(),"r");
request = NULL;
if (fp != NULL) {
request = PEM_read_X509_REQ(fp, NULL, NULL, NULL);
if (!request) {
ign_openssl_error();
rewind(fp);
printf("Fallback to private key DER\n");
request = d2i_X509_REQ_fp(fp, NULL);
openssl_error();
}
int r = fname.rfind('.');
int l = fname.rfind('/');
desc = fname.substr(l,r);
if (desc == "") desc = fname;
openssl_error();
}
else pki_error("Error opening file");
fclose(fp);
}
bool pki_x509req::fromData(unsigned char *p, int size)
{
request = d2i_X509_REQ(NULL, &p, size);
if (openssl_error()) return false;
return true;
}
string pki_x509req::getDN(int nid)
{
char buf[200];
string s;
X509_NAME *subj = X509_REQ_get_subject_name(request);
X509_NAME_get_text_by_NID(subj, nid, buf, 200);
openssl_error();
s = buf;
return s;
}
unsigned char *pki_x509req::toData(int *size)
{
unsigned char *p, *p1;
*size = i2d_X509_REQ(request, NULL);
openssl_error();
p = (unsigned char*)OPENSSL_malloc(*size);
p1 = p;
i2d_X509_REQ(request, &p1);
openssl_error();
return p;
}
void pki_x509req::writeReq(const string fname, bool PEM)
{
FILE *fp = fopen(fname.c_str(),"w");
if (fp != NULL) {
if (request){
if (PEM)
PEM_write_X509_REQ(fp, request);
else
i2d_X509_REQ_fp(fp, request);
openssl_error();
}
}
else pki_error("Error opening file");
fclose(fp);
}
bool pki_x509req::compare(pki_base *refreq)
{
const EVP_MD *digest=EVP_md5();
unsigned char d1[EVP_MAX_MD_SIZE], d2[EVP_MAX_MD_SIZE];
unsigned int d1_len,d2_len;
X509_REQ_digest(request, digest, d1, &d1_len);
X509_REQ_digest(((pki_x509req *)refreq)->request, digest, d2, &d2_len);
openssl_error();
if ((d1_len == d2_len) &&
(d1_len >0) &&
(memcmp(d1,d2,d1_len) == 0) )return true;
return false;
}
int pki_x509req::verify()
{
EVP_PKEY *pkey = X509_REQ_get_pubkey(request);
bool x = (X509_REQ_verify(request,pkey) != 0);
EVP_PKEY_free(pkey);
openssl_error();
if (x) return pki_base::VERIFY_OK;
else return pki_base::VERIFY_ERROR;
}
pki_key *pki_x509req::getKey()
{
EVP_PKEY *pkey = X509_REQ_get_pubkey(request);
pki_key *key = new pki_key(pkey);
openssl_error();
return key;
}