webpg-npapi 0.6.1
webpgPluginAPI.h
00001 /**********************************************************\ 
00002 Original Author: Kyle L. Huff (kylehuff)
00003 
00004 Created:    Jan 14, 2011
00005 License:    GNU General Public License, version 2
00006             http://www.gnu.org/licenses/gpl-2.0.html
00007             
00008 Copyright 2011 Kyle L. Huff, CURETHEITCH development team
00009 \**********************************************************/
00059 #include <string>
00060 #include <sstream>
00061 #include <iostream>
00062 #include <fstream>
00063 #include <boost/weak_ptr.hpp>
00064 #include "JSAPIAuto.h"
00065 #include "BrowserHost.h"
00066 #include "webpgPlugin.h"
00067 
00068 #ifdef HAVE_W32_SYSTEM
00069 #include "libs/libgpgme/WINNT_x86-msvc/gpgme.h"
00070 #elif FB_MACOSX
00071 #include "libs/libgpgme/Darwin_x86_64-gcc/gpgme.h"
00072 #else
00073 #include "libs/libgpgme/Linux_x86-gcc/gpgme.h"
00074 #endif
00075 #include "json/json.h"
00076 #include "fbjson.h"
00077 
00078 #ifndef H_webpgPluginAPI
00079 #define H_webpgPluginAPI
00080 
00081 struct genKeyParams {
00082     std::string key_type;
00083     std::string key_length;
00084     std::string subkey_type;
00085     std::string subkey_length;
00086     std::string name_real;
00087     std::string name_comment;
00088     std::string name_email;
00089     std::string expire_date;
00090     std::string passphrase;
00091 };
00092 
00093 struct genSubKeyParams {
00094     std::string keyid;
00095     std::string subkey_type;
00096     std::string subkey_length;
00097     std::string subkey_expire;
00098     bool sign_flag;
00099     bool enc_flag;
00100     bool auth_flag;
00101 };
00102 
00108 class webpgPluginAPI : public FB::JSAPIAuto
00109 {
00110 public:
00111     webpgPluginAPI(const webpgPluginPtr& plugin,
00112         const FB::BrowserHostPtr& host);
00113     virtual ~webpgPluginAPI();
00114 
00115     webpgPluginPtr getPlugin();
00116 
00117     FB::VariantMap webpg_status_map;
00125     FB::VariantMap get_webpg_status();
00126 
00132     void init();
00133 
00139     gpgme_ctx_t get_gpgme_ctx();
00140 
00148     FB::VariantMap getKeyList(const std::string& name, int secret_only);
00149 
00157     FB::variant getNamedKey(const std::string& name);
00158 
00166     FB::variant getPublicKeyList();
00167 
00175     FB::variant getPrivateKeyList();
00176 
00185     std::string get_preference(const std::string& preference);
00186 
00196     FB::variant gpgSetPreference(const std::string& preference,
00197         const std::string& pref_value="");
00198 
00207     FB::variant gpgGetPreference(const std::string& preference);
00208 
00215     std::string getGPGConfigFilename();
00216 
00223     FB::variant setTempGPGOption(const std::string& option,
00224         const std::string& value=NULL);
00225 
00231     FB::variant restoreGPGConfig();
00232 
00240     FB::variant gpgSetHomeDir(const std::string& data);
00241     FB::variant gpgGetHomeDir();
00242 
00248     FB::variant getTemporaryPath();
00249 
00260     FB::variant gpgEncrypt(const std::string& data,
00261         const FB::VariantList& enc_to_keyids, bool sign=false);
00262 
00274     FB::variant gpgSymmetricEncrypt(const std::string& data,  
00275         bool sign=false);
00276 
00289     FB::variant gpgDecryptVerify(const std::string& data, int use_agent);
00290 
00299     FB::variant gpgDecrypt(const std::string& data);
00300 
00301     FB::variant gpgVerify(const std::string& data);
00302 
00309     FB::variant gpgSignText(const FB::VariantList& signers,
00310         const std::string& plain_text, int sign_mode);
00311 
00325     FB::variant gpgSignUID(const std::string& keyid, long uid,
00326         const std::string& with_keyid, long local_only=NULL,
00327         long trust_sign=NULL, long trust_level=NULL);
00328 
00338     FB::variant gpgDeleteUIDSign(const std::string& keyid, long sign_uid,
00339         long signature);
00340 
00348     FB::variant gpgEnableKey(const std::string& keyid);
00349 
00357     FB::variant gpgDisableKey(const std::string& keyid);
00358 
00377     std::string gpgGenKey(const std::string& key_type,
00378         const std::string& key_length, const std::string& subkey_type,
00379         const std::string& subkey_length, const std::string& name_real,
00380         const std::string& name_comment, const std::string& name_email,
00381         const std::string& expire_date, const std::string& passphrase);
00382 
00390     void threaded_gpgGenKey(genKeyParams params);
00391 
00399     void threaded_gpgGenSubKey(genSubKeyParams params);
00400 
00408     FB::variant gpgImportKey(const std::string& ascii_key);
00409 
00418     FB::variant gpgDeleteKey(const std::string& keyid, int allow_secret);
00419 
00427     FB::variant gpgDeletePublicKey(const std::string& keyid);
00428 
00436     FB::variant gpgDeletePrivateKey(const std::string& keyid);
00437 
00446     FB::variant gpgDeletePrivateSubKey(const std::string& keyid, int key_idx);
00447 
00463     FB::variant gpgGenSubKey(const std::string& keyid, 
00464         const std::string& subkey_type, const std::string& subkey_length,
00465         const std::string& subkey_expire, bool sign_flag, bool enc_flag,
00466         bool auth_flag);
00467 
00476     FB::variant gpgSetKeyTrust(const std::string& keyid, long trust_level);
00477 
00489     FB::variant gpgAddUID(const std::string& keyid, const std::string& name,
00490         const std::string& email, const std::string& comment);
00491 
00500     FB::variant gpgDeleteUID(const std::string& keyid, long uid_idx);
00501 
00510     FB::variant gpgSetPrimaryUID(const std::string& keyid, long uid_idx);
00511 
00521     FB::variant gpgSetKeyExpire(const std::string& keyid, long key_idx,
00522         long expire);
00523 
00532     FB::variant gpgSetPubkeyExpire(const std::string& keyid, long expire);
00533 
00543     FB::variant gpgSetSubkeyExpire(const std::string& keyid, long key_idx,
00544         long expire);
00545 
00551     FB::variant gpgExportPublicKey(const std::string& keyid);
00552 
00567     FB::variant gpgRevokeItem(const std::string& keyid,
00568         const std::string& item, int key_idx, int uid_idx, int sig_idx,
00569         int reason_idx, const std::string& desc);
00570 
00582     FB::variant gpgRevokeKey(const std::string& keyid, int key_idx, int reason,
00583         const std::string &desc);
00584 
00596     FB::variant gpgRevokeUID(const std::string& keyid, int uid_idx, int reason,
00597         const std::string &desc);
00598 
00611     FB::variant gpgRevokeSignature(const std::string& keyid, int uid_idx,
00612         int sig_idx, int reason, const std::string &desc);
00613 
00621     FB::variant gpgChangePassphrase(const std::string& keyid);
00622 
00628     std::string get_version();
00629 
00635     bool gpgconf_detected();
00636 
00637     std::string original_gpg_config;
00638 
00651     static void progress_cb(
00652         void *self, const char *what,
00653         int type, int current, int total
00654     );
00655 
00656 //    gpgme_error_t passdefunct_cb(
00657 //        void *self, const char *uid_hint,
00658 //        const char *passphrase_info, int prev_was_bad, int fd
00659 //    );
00660 
00688     std::string gpgGenKeyWorker(const std::string& key_type, 
00689         const std::string& key_length,
00690         const std::string& subkey_type,
00691         const std::string& subkey_length,
00692         const std::string& name_real,
00693         const std::string& name_comment,
00694         const std::string& name_email,
00695         const std::string& expire_date,
00696         const std::string& passphrase,
00697         void* APIObj, void(*cb_status)(void *self,
00698             const char *what,
00699             int type,
00700             int current,
00701             int total
00702         )
00703     );
00704 
00705     /*
00706         static class method which accepts the calling object as a parameter
00707             so it can thread a member function
00708     */
00709     static void genKeyThreadCaller(webpgPluginAPI* api,
00710         genKeyParams params)
00711     {
00712         api->threaded_gpgGenKey(params);
00713     };
00714 
00737     FB::variant gpgGenSubKeyWorker(const std::string& keyid, 
00738         const std::string& subkey_type,
00739         const std::string& subkey_length,
00740         const std::string& subkey_expire,
00741         bool sign_flag,
00742         bool enc_flag,
00743         bool auth_flag,
00744         void* APIObj, void(*cb_status)(void *self,
00745             const char *what,
00746             int type,
00747             int current,
00748             int total
00749         )
00750     );
00751 
00752     static void genSubKeyThreadCaller(webpgPluginAPI* api,
00753         genSubKeyParams params)
00754     {
00755         api->threaded_gpgGenSubKey(params);
00756     };
00757 
00758     // Event helpers
00759     FB_JSAPI_EVENT(fired, 3, (const FB::variant&, bool, int));
00760     FB_JSAPI_EVENT(echo, 2, (const FB::variant&, const int));
00761     FB_JSAPI_EVENT(notify, 0, ());
00762 
00763 private:
00764     webpgPluginWeakPtr m_plugin;
00765     FB::BrowserHostPtr m_host;
00766 
00767 };
00768 
00769 #endif // H_webpgPluginAPI
00770 
 All Classes Functions