Fix all fopen() calls to always use "wb" or "rb"

fix PKI item autodetection
Fix compiler warning because of a changed OpenSSL 0.9.8/1.0.1
  "PEM_load_bio()" API change
This commit is contained in:
Christian Hohnstaedt 2015-05-20 11:15:36 +02:00
parent 68cf3615df
commit 85d2a26aa9
15 changed files with 46 additions and 28 deletions

View File

@ -97,7 +97,7 @@ int db_base::handleBadEntry(unsigned char *p, db_header_t *head)
size_t l;
db_header_t h;
FILE *fp = fopen(QString2filename(s), "wb");
FILE *fp = fopen_write(s);
if (!fp) {
throw errorEx(tr("Error opening file: '%1': %2").
arg(s).arg(strerror(errno)), class_name);
@ -637,7 +637,7 @@ void db_base::store(QModelIndexList indexes)
mainwin->setPath(s.mid(0, s.lastIndexOf(QRegExp("[/\\\\]")) ));
try {
QString pem = pem2QString(indexes);
FILE *fp = fopen(QString2filename(s), "wb");
FILE *fp = fopen_write(s);
if (!fp) {
throw errorEx(tr("Error opening file: '%1': %2").
arg(s).arg(strerror(errno)), class_name);

View File

@ -8,6 +8,7 @@
#ifndef __FUNC_H
#define __FUNC_H
#include <stdio.h>
#include <openssl/asn1.h>
#include <QtGui/QPixmap>
#include <QtCore/QByteArray>
@ -42,7 +43,6 @@ void inc_progress_bar(int, int, void *p);
extern bool translate_dn;
extern QMap<int, QString> dn_translations;
void dn_translations_setup();
#define openssl_error(x) _openssl_error(QString(x), C_FILE, __LINE__)
#define ign_openssl_error(x) _ign_openssl_error(QString(x), C_FILE, __LINE__)
void _openssl_error(const QString txt, const char *file, int line);
@ -57,4 +57,15 @@ BIO *BIO_QBA_mem_buf(QByteArray &a);
#define D2I_VOID(a) ((void *(*)(void *, unsigned char **, long))(a))
#define QString2filename(str) filename2bytearray(str).constData()
static inline FILE *fopen_read(QString s)
{
return fopen(QString2filename(s), "rb");
}
static inline FILE *fopen_write(QString s)
{
return fopen(QString2filename(s), "wb");
}
#endif

View File

@ -26,7 +26,7 @@ static void readOIDs(QString fname)
int line = 0;
QStringList sl;
fp = fopen(QString2filename(fname), "r");
fp = fopen_read(fname);
if (fp == NULL)
return;
@ -92,7 +92,7 @@ NIDlist readNIDlist(QString fname)
int line = 0, nid;
NIDlist nl;
nl.clear();
fp = fopen(QString2filename(fname), "r");
fp = fopen_read(fname);
if (fp == NULL) return nl;
while (fgets(buff, 127, fp)) {
line++;

View File

@ -54,7 +54,7 @@ QString pki_crl::getMsg(msg_type msg)
void pki_crl::fload(const QString fname)
{
FILE *fp = fopen(QString2filename(fname), "r");
FILE *fp = fopen_read(fname);
X509_CRL *_crl;
if (fp != NULL) {
_crl = PEM_read_X509_CRL(fp, NULL, NULL, NULL);
@ -198,7 +198,7 @@ void pki_crl::writeDefault(const QString fname)
void pki_crl::writeCrl(const QString fname, bool pem)
{
FILE *fp = fopen(QString2filename(fname), "w");
FILE *fp = fopen_write(fname);
if (fp != NULL) {
if (crl){
if (pem)

View File

@ -240,7 +240,7 @@ void pki_evp::fload(const QString fname)
pass_info p(XCA_TITLE, tr("Please enter the password to decrypt the private key from file:\n%1").
arg(compressFilename(fname)));
pem_password_cb *cb = PwDialog::pwCallback;
FILE *fp = fopen(QString2filename(fname), "r");
FILE *fp = fopen_read(fname);
EVP_PKEY *pkey;
pki_ign_openssl_error();
@ -561,7 +561,7 @@ void pki_evp::writePKCS8(const QString fname, const EVP_CIPHER *enc,
{
EVP_PKEY *pkey;
pass_info p(XCA_TITLE, tr("Please enter the password protecting the PKCS#8 key '%1'").arg(getIntName()));
FILE *fp = fopen(QString2filename(fname), "w");
FILE *fp = fopen_write(fname);
if (fp != NULL) {
if (key) {
pkey = decryptKey();
@ -600,7 +600,7 @@ void pki_evp::writeKey(const QString fname, const EVP_CIPHER *enc,
writePublic(fname, pem);
return;
}
FILE *fp = fopen(QString2filename(fname), "w");
FILE *fp = fopen_write(fname);
if (!fp) {
fopen_error(fname);
return;

View File

@ -300,7 +300,7 @@ bool pki_key::compare(pki_base *ref)
void pki_key::writePublic(const QString fname, bool pem)
{
FILE *fp = fopen(QString2filename(fname), "w");
FILE *fp = fopen_write(fname);
if (fp == NULL) {
fopen_error(fname);
return;

View File

@ -98,7 +98,7 @@ void pki_multi::fload(const QString fname)
FILE * fp;
BIO *bio = NULL;
fp = fopen(QString2filename(fname), "r");
fp = fopen_read(fname);
if (!fp) {
fopen_error(fname);
return;
@ -151,6 +151,8 @@ void pki_multi::fromPEM_BIO(BIO *bio, QString name)
item = NULL;
}
}
if (multi.size() == 0)
throw errorEx(tr("No known PEM encoded items found"));
}
void pki_multi::probeAnything(const QString fname)

View File

@ -36,7 +36,7 @@ pki_pkcs12::pki_pkcs12(const QString fname)
class_name="pki_pkcs12";
certstack = sk_X509_new_null();
pass_info p(XCA_TITLE, tr("Please enter the password to decrypt the PKCS#12 file:\n%1").arg(compressFilename(fname)));
fp = fopen(QString2filename(fname), "rb");
fp = fopen_read(fname);
if (fp) {
PKCS12 *pkcs12 = d2i_PKCS12_fp(fp, NULL);
fclose(fp);
@ -114,7 +114,7 @@ void pki_pkcs12::writePKCS12(const QString fname)
my_error(tr("No key or no Cert and no pkcs12"));
}
FILE *fp = fopen(QString2filename(fname), "wb");
FILE *fp = fopen_write(fname);
if (fp != NULL) {
if (PwDialog::execute(&p, &pass, true) != 1) {
fclose(fp);

View File

@ -100,7 +100,7 @@ void pki_pkcs7::signCert(pki_x509 *crt, pki_x509 *contCert)
void pki_pkcs7::writeP7(QString fname,bool PEM)
{
FILE *fp;
fp = fopen(QString2filename(fname),"w");
fp = fopen_write(fname);
if (fp != NULL) {
if (p7){
if (PEM)
@ -145,7 +145,7 @@ void pki_pkcs7::fload(const QString fname)
{
FILE *fp;
PKCS7 *_p7;
fp = fopen(QString2filename(fname), "rb");
fp = fopen_read(fname);
if (fp) {
_p7 = PEM_read_PKCS7(fp, NULL, NULL, NULL);
if (!_p7) {

View File

@ -342,7 +342,7 @@ QByteArray pki_temp::toExportData()
void pki_temp::writeTemp(QString fname)
{
FILE *fp = fopen(QString2filename(fname),"wb");
FILE *fp = fopen_write(fname);
if (fp == NULL) {
fopen_error(fname);
@ -362,8 +362,13 @@ BIO *pki_temp::pem(BIO *b, int format)
QByteArray ba = toExportData();
if (!b)
b = BIO_new(BIO_s_mem());
#if OPENSSL_VERSION_NUMBER < 0x10000000L
PEM_write_bio(b, PEM_STRING_XCA_TEMPLATE, (char*)"",
(unsigned char*)(ba.data()), ba.size());
#else
PEM_write_bio(b, PEM_STRING_XCA_TEMPLATE, "",
(unsigned char*)ba.data(), ba.size());
(const unsigned char*)(ba.constData()), ba.size());
#endif
pki_openssl_error();
return b;
}

View File

@ -93,7 +93,7 @@ void pki_x509::fromPEM_BIO(BIO *bio, QString name)
void pki_x509::fload(const QString fname)
{
FILE *fp = fopen(QString2filename(fname), "r");
FILE *fp = fopen_read(fname);
X509 *_cert;
if (!fp) {
fopen_error(fname);
@ -566,9 +566,9 @@ void pki_x509::writeDefault(const QString fname)
void pki_x509::writeCert(const QString fname, bool PEM, bool append)
{
FILE *fp;
const char *p = "w";
const char *p = "wb";
if (append)
p = "a";
p = "ab";
fp = fopen(QString2filename(fname), p);
if (fp != NULL) {
if (cert){

View File

@ -113,7 +113,7 @@ void pki_x509req::fromPEM_BIO(BIO *bio, QString name)
void pki_x509req::fload(const QString fname)
{
FILE *fp = fopen(QString2filename(fname), "r");
FILE *fp = fopen_read(fname);
X509_REQ *_req;
int ret = 0;
@ -241,7 +241,7 @@ void pki_x509req::writeDefault(const QString fname)
void pki_x509req::writeReq(const QString fname, bool pem)
{
FILE *fp = fopen(QString2filename(fname), "w");
FILE *fp = fopen_write(fname);
if (fp) {
if (request){
if (pem)

View File

@ -127,7 +127,7 @@ void pki_x509super::opensslConf(QString fname)
"%2").arg(name).arg(extensions).
arg(ASN1_STRING_get_default_mask(), 0, 16);
FILE *fp = fopen(QString2filename(fname),"w");
FILE *fp = fopen_write(fname);
if (fp == NULL) {
fopen_error(fname);
return;

View File

@ -243,8 +243,8 @@ int MainWindow::open_default_db()
{
if (!dbfile.isEmpty())
return 0;
FILE *fp = fopen(QString2filename(getUserSettingsDir() +
QDir::separator() + "defaultdb"), "r");
FILE *fp = fopen_read(getUserSettingsDir() +
QDir::separator() + "defaultdb");
if (!fp)
return 0;
@ -273,7 +273,7 @@ void MainWindow::default_database()
}
d.mkpath(dir);
fp = fopen(QString2filename(file), "w");
fp = fopen_write(file);
if (fp) {
QByteArray ba;
ba = filename2bytearray(fi.canonicalFilePath() + "\n");

View File

@ -879,7 +879,7 @@ void MainWindow::generateDHparam()
fname, "All files ( * )", NULL);
if (fname == "")
throw errorEx("");
fp = fopen(QString2filename(fname), "w");
fp = fopen_write(fname);
if (fp == NULL) {
throw errorEx(tr("Error opening file: '%1': %2").
arg(fname).arg(strerror(errno)));