KEYRING *pgpdb_open(char *keyring, BUFFER *encryptkey, int writer, int type) { KEYRING *keydb; assert(! ((writer) && (type == PGP_TYPE_UNDEFINED))); keydb = pgpdb_new(keyring, -1, encryptkey, type); #ifdef DEBUG keydb->writer = writer; #endif if (writer) keydb->lock = lockfile(keyring); keydb->filetype = pgp_readkeyring(keydb->db, keyring); #if 0 if (keydb->filetype == -1) { pgpdb_close(keydb); return (NULL); } #endif /* if 0 */ if (encryptkey && encryptkey->length && pgp_isconventional(keydb->db) && pgp_decrypt(keydb->db, encryptkey, NULL, NULL, NULL) < 0) { user_delpass(); return (NULL); } return (keydb); }
LPSTR __cdecl pgp_decode(HANDLE context, LPCSTR szEncMsg) { pCNTX ptr = get_context_on_id(context); if (!ptr) return NULL; mir_free(ptr->tmp); LPSTR szNewMsg = NULL; LPSTR szOldMsg = pgp_decrypt(ptr, szEncMsg); if (szOldMsg) { if (!is_7bit_string(szOldMsg) && !is_utf8_string(szOldMsg)) { szNewMsg = mir_utf8encode(szOldMsg); } else szNewMsg = mir_strdup(szOldMsg); } return ptr->tmp = szNewMsg; }
int t1_getreply(BUFFER *msg, BUFFER *ek, int len) { BUFFER *key, *decrypt; int err = -1; int hop = 0; key = buf_new(); decrypt = buf_new(); do { t1_ek(key, ek, hop); buf_set(decrypt, msg); if (pgp_decrypt(decrypt, key, NULL, NULL, NULL) == 0 && decrypt->data != NULL) err = 0, buf_move(msg, decrypt); } while (hop++ < len); return (err); }
LPSTR __cdecl pgp_decode(HANDLE context, LPCSTR szEncMsg) { pCNTX ptr = get_context_on_id(context); if(!ptr) return NULL; LPSTR szNewMsg = NULL; LPSTR szOldMsg = pgp_decrypt(ptr, szEncMsg); if(szOldMsg) { if( !is_7bit_string(szOldMsg) && !is_utf8_string(szOldMsg) ) { int slen = strlen(szOldMsg)+1; LPWSTR wszMsg = (LPWSTR) alloca(slen*sizeof(WCHAR)); MultiByteToWideChar(CP_ACP, 0, szOldMsg, -1, wszMsg, slen*sizeof(WCHAR)); szNewMsg = _strdup(utf8encode(wszMsg)); } else { szNewMsg = _strdup(szOldMsg); } } SAFE_FREE(ptr->tmp); ptr->tmp = szNewMsg; return szNewMsg; }
static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args) { int err; MBuf *src = NULL, *dst = NULL; uint8 tmp[VARHDRSZ]; uint8 *restmp; bytea *res; int res_len; PGP_Context *ctx = NULL; struct debug_expect ex; int got_unicode = 0; init_work(&ctx, need_text, args, &ex); src = mbuf_create_from_data((uint8 *) VARDATA(data), VARSIZE(data) - VARHDRSZ); dst = mbuf_create(VARSIZE(data) + 2048); /* * reserve room for header */ mbuf_append(dst, tmp, VARHDRSZ); /* * set key */ if (is_pubenc) { uint8 *psw = NULL; int psw_len = 0; MBuf *kbuf; if (keypsw) { psw = (uint8 *) VARDATA(keypsw); psw_len = VARSIZE(keypsw) - VARHDRSZ; } kbuf = create_mbuf_from_vardata(key); err = pgp_set_pubkey(ctx, kbuf, psw, psw_len, 1); mbuf_free(kbuf); } else err = pgp_set_symkey(ctx, (uint8 *) VARDATA(key), VARSIZE(key) - VARHDRSZ); /* * decrypt */ if (err >= 0) err = pgp_decrypt(ctx, src, dst); /* * failed? */ if (err < 0) goto out; if (ex.expect) check_expect(ctx, &ex); /* remember the setting */ got_unicode = pgp_get_unicode_mode(ctx); out: if (src) mbuf_free(src); if (ctx) pgp_free(ctx); if (err) { px_set_debug_handler(NULL); if (dst) mbuf_free(dst); ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("%s", px_strerror(err)))); } res_len = mbuf_steal_data(dst, &restmp); mbuf_free(dst); /* res_len includes VARHDRSZ */ res = (bytea *) restmp; SET_VARSIZE(res, res_len); if (need_text && got_unicode) { text *utf = convert_from_utf8(res); if (utf != res) { clear_and_pfree(res); res = utf; } } px_set_debug_handler(NULL); /* * add successfull decryptions also into RNG */ add_entropy(res, key, keypsw); return res; }
int main(int argc, char *argv[]) { int i, j, k, rc; FILE *log; char *pgppath, *tmppath, *temp; rc = 0; tmppath = getenv("TMP"); if (tmppath == NULL) { tmppath="."; } sprintf(sigfile,"%s\\%s",tmppath,SIGFILE); sprintf(logfile,"%s\\%s",tmppath,LOGFILE); debug = (getenv("PGPFAKE_DEBUG") != NULL); encript_self = (getenv("PGPFAKE_ENCRIPT_TO_SELF") != NULL); /* switch on debug mode manually */ /* debug = 1; */ /* switch on self encript manually */ /* encript_self = 1; */ if (debug) { debug = 1; log = fopen(logfile,"w"); for (i=1; i<argc; i++) fprintf(log, "%d: %s\n",i,argv[i]); fprintf(log, "Num of Args: %d\n",argc); } pgppath = getenv("PGPPATH"); if (pgppath == NULL) { dprintf(log, "\nPGP not installed correctly!\n"); if (debug) fclose(log); return(-1); } // As you can see, PGPFake decides what it's being called to do // Based on the number of arguments. This is due to it originally // Only being a shell for PMMail. This can lead to a few odd // Problems (see Bugs section of readme), but I can't see the // Point of re-doing it, so if you can be bothered, go ahead! // I'd recomend doing it in a similar way to the new bit above // (The bit I wrote) // Nick Burch <*****@*****.**> switch (argc) { case 1: /* no arguments, print version then PGP Help*/ fprintf(stderr,"\nPGPfake, Version %s, (C) 1998 by Thomas Vandahl, 1999 by Nick Burch,\n",VERSION); fprintf(stderr,"2001 by Dieter Werner\n"); rc = pgp_call_no_args( pgppath ); break; case 9: /* called to verify signature */ dprintf(log, "check signature\n"); rc = pgp_check_signature(log, pgppath, sigfile); break; case 11: /* called to extract public key */ /* argv[10]: my userid */ /* Check We're not being asked to Fingerprint */ if( strcmp(argv[9],"-fkvcat") == 0) { // Fingerprint not supported: Needs a passphrase with pgp 5.0 // Do nothing except log the problem dprintf(log, "Fingerprint key: not supported\n"); } else { // Extract, which we can do dprintf(log, "extract key\n"); rc = pgp_extract_my_key(pgppath, argv[10]); } break; case 12: /* called to decrypt or to add key information */ /* decrypt: argv[9] -> passphrase addkey: argv[9] = "-a" */ if (argv[9][1] == 'a') /* called to add public key */ { dprintf(log, "addkey\n"); rc = pgp_add_new_key(log, pgppath, sigfile); } else /* called to decrypt */ { dprintf(log, "decrypt\n"); rc = pgp_decrypt(log, pgppath, sigfile, argv[9]+2); } break; case 13: /* called to encrypt */ /* argv[11]: userid to encrypt to argv[12]: my userid */ dprintf(log, "encrypt\n"); rc = pgp_encrypt(log, pgppath, argv[11], argv[12]); break; case 14: /* called to sign */ /* argv[11]: passphrase argv[13]: my userid */ dprintf(log, "sign\n"); rc = pgp_sign(log, pgppath, argv[11]+2, argv[13]); break; /* Added by Dieter Werner <*****@*****.**> */ case 16: /* called to sign & encrypt */ /* argv[11]: passphrase argv[12]: userid to encrypt to argv[13]: my userid */ dprintf(log, "encrypt and sign\n"); rc = pgp_signencr(log, pgppath, argv[11]+2, argv[12]+2, argv[13]); break; /* Based on case 16, coded by Nick Burch <*****@*****.**> */ case 17: /* called to sign & encrypt to 2 people */ /* argv[11]: passphrase argv[12]: userid to encrypt to argv[13]: other userid to encript to argv[14]: my userid */ i = strlen(argv[12]); j = strlen(argv[13]); temp = malloc( i + j + 4 ); memmove( temp, argv[12], i); memmove(temp + i , " -r ",4); memmove( temp + i + 4, argv[13], j); dprintf(log, "encrypt and sign, 2 recipients\n"); rc = pgp_signencr(log, pgppath, argv[11]+2, temp, argv[14]); break; /* Based on case 16, coded by Nick Burch <*****@*****.**> */ case 18: /* called to sign & encrypt to 3 people */ /* argv[11]: passphrase argv[12]: userid to encrypt to argv[13]: other userid to encript to argv[14]: final userid to encript to argv[15]: my userid */ i = strlen(argv[12]); j = strlen(argv[13]); k = strlen(argv[14]); temp = malloc( i + j + k + 8 ); memmove( temp, argv[12] ,i); memmove( temp + i, " -r ", 4 ); memmove( temp + i + 4, argv[13], j); memmove( temp + i + j + 4, " -r ",4 ); memmove( temp + i + j + 8, argv[14],k); dprintf(log, "encrypt and sign, 3 recipients\n"); rc = pgp_signencr(log, pgppath, argv[11]+2, temp, argv[15]); break; default: /* unknown call */ /* Anylise command line, and call as needed */ rc = pgp_check_command_line( log, argc, argv, pgppath ); } dprintf(log,"\nreturn code: %d\n",rc); if (debug) fclose(log); return(rc); }