/* ############################################################################# * * Description initialize the GPGME library * Author Harry Brueckner * Date 2005-03-30 * Arguments void * Return void */ void initGPG(void) { gpgme_error_t error; gpgme_check_version(NULL); TRACE(99, "initGPG()", NULL); error = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); if (error) { gpgError(error); exit(1); } if (!gpgme_check_version(GPG_VERSION_MINIMUM)) { fprintf(stderr, _("GpgMe error: invalid library version (%s).\n"), gpgme_check_version(NULL)); exit(1); } retries = 0; signers = 0; lastrealm = NULL; }
char* gpgData2Char(gpgme_data_t dh, int* newsize) { size_t tmpsize; char* newbuffer; char* tmpbuffer; TRACE(99, "gpgData2Char()", NULL); newbuffer = NULL; *newsize = 0; /* we have to rewind the buffer */ if (gpgme_data_seek(dh, 0, SEEK_SET)) { gpgError(gpgme_err_code_from_errno(errno)); return NULL; } tmpbuffer = memAlloc(__FILE__, __LINE__, BUFFERSIZE + 1); while ((tmpsize = gpgme_data_read(dh, tmpbuffer, BUFFERSIZE)) > 0) { newbuffer = memRealloc(__FILE__, __LINE__, newbuffer, *newsize, *newsize + tmpsize); /* Flawfinder: ignore */ memcpy(newbuffer + *newsize, tmpbuffer, tmpsize); *newsize += tmpsize; } memFree(__FILE__, __LINE__, tmpbuffer, BUFFERSIZE + 1); if (tmpsize < 0) { gpgError(gpgme_err_code_from_errno(errno)); return NULL; } return newbuffer; }
GnuPGConnector::GnuPGConnector(QDeclarativeItem *parent) : QDeclarativeItem(parent) { qDebug() << "GnuPGConnector!"; this->gpgHistory.append("=== CryptMee started"); this->myKeyReader = new KeyReader(); this->myKeyReader->parseGnuPGOutput(""); this->process_gpg = new QProcess(this); this->currentState = GPG_IDLE; this->gpgStdOutput = ""; this->gpgErrOutput = "No Errors"; connect(this->process_gpg, SIGNAL(finished(int)), this, SLOT(gpgFinished(int))); connect(this->process_gpg, SIGNAL(error(QProcess::ProcessError)), this, SLOT(gpgError(QProcess::ProcessError))); // Init Settings QSettings settings; if(!settings.contains("SETTINGS_RELEASE")) { // First start, fill values this->settingsReset(); // TODO: // Do something after the first start... } //settings.remove("SETTINGS_RELEASE"); this->gpgBinaryPath = settings.value("SETTINGS_GPGPATH", GPGBIN).toString(); this->gpgKeyserverURL = settings.value("SETTINGS_GPGKEYSERVER", KEYSERVER).toString(); this->localMailPath = settings.value("SETTINGS_MAILDIR", MAIL_PATH).toString(); this->localMailDB = settings.value("SETTINGS_MAILDB", MAIL_DB).toString(); this->useOwnKey = settings.value("SETTINGS_USEOWNKEY", "0").toString(); // Initial check GnuPG version this->checkGPGVersion(this->gpgBinaryPath); }
/* ############################################################################# * * Description find a fingerprint for the given key * Author Harry Brueckner * Date 2005-04-07 * Arguments keyname - description string * secret_only - if set to 1, only secret keys are listed * Return char* with the found fingerprint; the string must be freed * by the caller */ char* gpgGetFingerprint(char* keyname, int secret_only) { gpgme_ctx_t context; gpgme_key_t key; gpgme_error_t error; char* identifier = NULL; TRACE(99, "gpgGetFingerprint()", NULL); if (!config -> encryptdata) { return NULL; } /* get a new context */ error = gpgme_new(&context); if (error) { gpgme_release(context); gpgError(error); return NULL; } /* start cycling through the list of keys */ error = gpgme_op_keylist_start(context, keyname, secret_only); if (error) { gpgme_release(context); gpgError(error); return NULL; } /* first we look for secret keys */ while (!identifier && !(error = gpgme_op_keylist_next(context, &key))) { /* take the first key we find */ if (!identifier && !key -> disabled && !key -> expired && !key -> invalid && !key -> revoked) { /* we just use keys we can encrypt for */ identifier = memAlloc(__FILE__, __LINE__, strlen(key -> subkeys -> fpr) + 1); strStrncpy(identifier, key -> subkeys -> fpr, strlen(key -> subkeys -> fpr) + 1); } gpgme_key_unref(key); } if (error && gpg_err_code(error) != GPG_ERR_EOF) { /* we validate the last value of the 'next' operation */ gpgme_release(context); gpgError(error); return NULL; } /* finish the key listing */ error = gpgme_op_keylist_end(context); if (error) { gpgme_release(context); gpgError(error); return NULL; } gpgme_release(context); return identifier; }
/* ############################################################################# * * Description validate the given encryption key * Author Harry Brueckner * Date 2005-03-31 * Arguments char* key - the key to validate * Return char* NULL on error, otherwise the name and mail address */ char* gpgValidateEncryptionKey(char* keyname) { gpgme_ctx_t context; gpgme_key_t key; gpgme_error_t error; int secret, size; char* identifier = NULL; char* tcomment; char* tname; TRACE(99, "gpgValidateEncryptionKey()", NULL); if (!config -> encryptdata) { return NULL; } /* get a new context */ error = gpgme_new(&context); if (error) { gpgme_release(context); gpgError(error); return NULL; } for (secret = 1; secret >= 0 && !identifier; secret--) { /* start cycling through the list of keys */ error = gpgme_op_keylist_start(context, keyname, (secret == 1) ? LIST_SECRET : LIST_ALL); if (error) { gpgme_release(context); gpgError(error); return NULL; } while (!(error = gpgme_op_keylist_next(context, &key))) { /* take the first key we find */ #ifdef TEST_OPTION #ifdef KEY_DEBUG gpgDebugKey(key); #endif #endif if (key -> can_encrypt && !key -> disabled && !key -> expired && !key -> invalid && !key -> revoked) { /* we just use keys we can encrypt for and sign with */ tname = convert2terminal((unsigned char*)key -> uids -> name); if (key -> uids -> comment) { tcomment = key -> uids -> comment; } else { tcomment = NULL; } if (tcomment && strlen(tcomment)) { /* a comment exists for this key */ size = strlen(key -> subkeys -> keyid) + 1 + strlen(tname) + 1 + strlen(tcomment) + 2 + 1 + strlen(key -> uids -> email) + 2 + 1; identifier = memAlloc(__FILE__, __LINE__, size); snprintf(identifier, size, "%s %s (%s) <%s>", key -> subkeys -> keyid, tname, tcomment, key -> uids -> email); } else { /* no comment exists */ size = strlen(key -> subkeys -> keyid) + 1 + strlen(tname) + 1 + strlen(key -> uids -> email) + 2 + 1; identifier = memAlloc(__FILE__, __LINE__, size); snprintf(identifier, size, "%s %s <%s>", key -> subkeys -> keyid, tname, key -> uids -> email); } } gpgme_key_unref(key); if (identifier) { break; } } if (error && gpg_err_code(error) != GPG_ERR_EOF) { /* we validate the last value of the 'next' operation */ gpgme_release(context); gpgError(error); return NULL; } } /* finish the key listing */ error = gpgme_op_keylist_end(context); if (error) { gpgme_release(context); gpgError(error); return NULL; } gpgme_release(context); return identifier; }
/* ############################################################################# * * Description check if for the given keyname a secret key exists * Author Harry Brueckner * Date 2005-04-25 * Arguments char* keyname - the key to check * Return int 1 if there is a secret key, 0 if not and -1 if a gpg * error occured */ int gpgIsSecretKey(char* keyname) { gpgme_ctx_t context; gpgme_key_t key; gpgme_error_t error; int secret = 0; TRACE(99, "gpgIsSecretKey()", NULL); if (!config -> encryptdata) { return 0; } /* get a new context */ error = gpgme_new(&context); if (error) { gpgme_release(context); gpgError(error); return -1; } /* start cycling through the list of keys */ error = gpgme_op_keylist_start(context, keyname, LIST_SECRET); if (error) { gpgme_release(context); gpgError(error); return -1; } while (!(error = gpgme_op_keylist_next(context, &key))) { /* take the first usable key we find */ /* TODO: only choose usable secret keys */ if (key -> can_encrypt && key -> secret && !key -> disabled && !key -> expired && !key -> invalid && !key -> revoked) { /* we just use keys we can encrypt for */ secret = 1; } gpgme_key_unref(key); if (secret) { break; } } if (error && gpg_err_code(error) != GPG_ERR_EOF) { /* we validate the last value of the 'next' operation */ gpgme_release(context); gpgError(error); return -1; } /* finish the key listing */ error = gpgme_op_keylist_end(context); if (error) { gpgme_release(context); gpgError(error); return -1; } gpgme_release(context); return secret; }
int GnuPGConnector::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QDeclarativeItem::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { switch (_id) { case 0: ready(); break; case 1: errorOccured(); break; case 2: gpgFinished((*reinterpret_cast< int(*)>(_a[1]))); break; case 3: gpgError((*reinterpret_cast< QProcess::ProcessError(*)>(_a[1]))); break; case 4: { QString _r = encrypt((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 5: { QString _r = decrypt((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 6: { QString _r = showKeys(); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 7: { QString _r = showSecretKeys(); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 8: { QString _r = getData((*reinterpret_cast< bool(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 9: { QString _r = getFromClipboard(); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 10: setToClipboard((*reinterpret_cast< QString(*)>(_a[1]))); break; case 11: { QString _r = getKey((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< int(*)>(_a[2]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 12: { QString _r = getKey((*reinterpret_cast< int(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 13: { QString _r = getKeyByID((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 14: { QString _r = getPrivateKeyIDs((*reinterpret_cast< bool(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 15: { QString _r = getPrivateKeyIDs(); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 16: { int _r = getNumOfPubKeys((*reinterpret_cast< int(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; } break; case 17: { int _r = getNumOfPubKeys(); if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; } break; case 18: { bool _r = generateKeyPair((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2])),(*reinterpret_cast< QString(*)>(_a[3])),(*reinterpret_cast< QString(*)>(_a[4]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 19: { bool _r = setOwnerTrust((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 20: { bool _r = checkGPGVersion((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 21: { QString _r = getGPGVersionString(); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 22: { bool _r = importKeysFromFile((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 23: { bool _r = importKeysFromClipboard(); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 24: { bool _r = searchKeysOnKeyserver((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 25: { bool _r = importKeysFromKeyserver((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 26: { bool _r = deleteKey((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 27: { bool _r = signKey((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2])),(*reinterpret_cast< QString(*)>(_a[3]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 28: { bool _r = exportKeys((*reinterpret_cast< int(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 29: { QString _r = getHistory(); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 30: { bool _r = saveHistory((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break; case 31: settingsSetValue((*reinterpret_cast< QString(*)>(_a[1])),(*reinterpret_cast< QString(*)>(_a[2]))); break; case 32: { QString _r = settingsGetValue((*reinterpret_cast< QString(*)>(_a[1]))); if (_a[0]) *reinterpret_cast< QString*>(_a[0]) = _r; } break; case 33: settingsReset(); break; default: ; } _id -= 34; } return _id; }