31 |
* |
* |
32 |
*******************************************************************/ |
*******************************************************************/ |
33 |
#include <iostream> |
#include <iostream> |
34 |
|
#include <gpgme.h> |
35 |
|
#include <string> |
36 |
|
#include <stdio.h> |
37 |
|
|
38 |
#include "action.hh" |
#include "action.hh" |
39 |
#include "language/language.hh" |
#include "language/language.hh" |
40 |
|
#include "misc.hh" |
41 |
|
|
42 |
int crypt() |
#define MAXSIZE 2048 |
43 |
|
|
44 |
|
static int my_error(std::string msg, GpgmeError err) |
45 |
|
{ |
46 |
|
std::cerr << "ERROR : " << msg << " : " << gpgme_strerror (err) << std::endl; |
47 |
|
return 1; |
48 |
|
} |
49 |
|
|
50 |
|
static int gen_key (std::string real_name, std::string passphrase) |
51 |
{ |
{ |
52 |
|
GpgmeCtx ctx; |
53 |
|
GpgmeError err; |
54 |
|
std::string parms; |
55 |
|
|
56 |
|
err = gpgme_new (&ctx); |
57 |
|
if (err) return (1); |
58 |
|
|
59 |
|
parms = "<GnupgKeyParms format=\"internal\">\n" |
60 |
|
"Key-Type: DSA\n" |
61 |
|
"Key-Length: 1024\n" |
62 |
|
"Subkey-Type: ELG-E\n" |
63 |
|
"Subkey-Length: 1024\n" |
64 |
|
"Name-Real: " + real_name + "\n" |
65 |
|
"Name-Comment: The Marvin Project\n" |
66 |
|
"Name-Email: empty@empty.org\n" |
67 |
|
"Expire-Date: 0\n" |
68 |
|
"Passphrase: " + passphrase + "\n" |
69 |
|
"</GnupgKeyParms>\n"; |
70 |
|
err = gpgme_op_genkey (ctx, parms.c_str(), NULL, NULL ); |
71 |
|
if (err) |
72 |
|
return my_error("gpgme_op_genkey", err); |
73 |
|
gpgme_release (ctx); |
74 |
|
return 0; |
75 |
|
} |
76 |
|
|
77 |
|
static void change_ext(std::string path, std::string *outfilename) |
78 |
|
{ |
79 |
|
|
80 |
|
*outfilename = path; |
81 |
|
*outfilename += ".crypt"; |
82 |
|
return ; |
83 |
|
} |
84 |
|
|
85 |
|
int encrypt_message(std::string fingerprint, std::string path) |
86 |
|
{ |
87 |
|
size_t nread; |
88 |
|
GpgmeCtx ctx; |
89 |
|
GpgmeError err; |
90 |
|
GpgmeData in, out; |
91 |
|
GpgmeRecipients rset; |
92 |
|
char buf[MAXSIZE]; |
93 |
|
memset(buf, 0, MAXSIZE); |
94 |
|
char *encrypted_data; |
95 |
|
encrypted_data = (char *)malloc(MAXSIZE); |
96 |
|
|
97 |
|
FILE* outfile; |
98 |
|
std::string outfilename; |
99 |
|
change_ext(path, &outfilename); |
100 |
std::cout << BEGINCRYPT << std::endl; |
std::cout << BEGINCRYPT << std::endl; |
101 |
std::cout << ENDCRYPT << std::endl << std::endl; |
std::cout << "---------------------->" << outfilename << std::endl; |
102 |
return 0; |
|
103 |
|
if ((err = gpgme_check_engine()) != 0) |
104 |
|
return my_error("gpgme_check_engine", err); |
105 |
|
|
106 |
|
if ((err = gpgme_new(&ctx)) != 0) |
107 |
|
return my_error("gpgme_new", err); |
108 |
|
gpgme_set_armor(ctx, 1); |
109 |
|
|
110 |
|
if ((err = gpgme_data_new_from_file(&in, path.c_str(), 1)) != 0) |
111 |
|
return my_error("gpgme_data_new_from_file", err); |
112 |
|
|
113 |
|
if ((err = gpgme_data_new(&out)) != 0) |
114 |
|
return my_error("gpgme_data_new", err); |
115 |
|
|
116 |
|
if ((err = gpgme_recipients_new(&rset)) != 0) |
117 |
|
return my_error("gpgme_recipients_new", err); |
118 |
|
|
119 |
|
if ((err = gpgme_recipients_add_name_with_validity (rset, |
120 |
|
fingerprint.c_str(), |
121 |
|
GPGME_VALIDITY_NEVER)) != 0) |
122 |
|
return my_error("gpgme_recipients_add_name_with_validity", err); |
123 |
|
|
124 |
|
if ((err = gpgme_op_encrypt (ctx, rset, in, out)) != 0) |
125 |
|
return my_error("You haven't pgp key, create this", err); |
126 |
|
|
127 |
|
fflush (NULL); |
128 |
|
if ((err = gpgme_data_rewind(out)) != 0) |
129 |
|
return my_error("gpgme_data_rewind", err); |
130 |
|
|
131 |
|
outfile=fopen(outfilename.c_str(),"w"); |
132 |
|
while ((err = gpgme_data_read(out, buf, sizeof(buf), &nread)) == 0) |
133 |
|
{ |
134 |
|
fwrite ( buf, nread, 1, outfile ); |
135 |
|
} |
136 |
|
|
137 |
|
fclose(outfile); |
138 |
|
gpgme_recipients_release (rset); |
139 |
|
gpgme_data_release (in); |
140 |
|
gpgme_data_release (out); |
141 |
|
gpgme_release (ctx); |
142 |
|
std::cout << ENDCRYPT << std::endl; |
143 |
|
return(0); |
144 |
|
} |
145 |
|
|
146 |
|
int crypt(std::string path, std::string user_name, std::string passphrase) |
147 |
|
{ |
148 |
|
if (check_path(path) == 0) |
149 |
|
if (gen_key(user_name, passphrase) == 0) |
150 |
|
return encrypt_message(user_name, path); |
151 |
|
return 1; |
152 |
} |
} |