├── .classpath
├── .gitignore
├── .project
├── COPYING
├── README.md
├── TODO
├── build.properties
├── build.xml
├── etc
├── SOURCEFOOTER
├── SOURCEHEADER
├── fdc.javadoc.png
├── guardianproject-logo.png
├── overview.html
└── sourceforge
│ ├── index.html
│ └── logobig.gif
├── examples
├── KeySearch.java
└── run.sh
├── jni
├── GNUmakefile
├── GnuPGContext.c
├── GnuPGData.c
├── GnuPGGenkeyResult.c
├── GnuPGKey.c
├── GnuPGSignature.c
├── data.h
├── format-code.sh
├── gpgmeutils.c
└── gpgmeutils.h
├── pom.xml
├── src
└── com
│ └── freiheit
│ └── gnupg
│ ├── GnuPGContext.java
│ ├── GnuPGData.java
│ ├── GnuPGException.java
│ ├── GnuPGGenkeyResult.java
│ ├── GnuPGKey.java
│ ├── GnuPGPeer.java
│ ├── GnuPGSignature.java
│ ├── package.html
│ └── tests
│ └── GnuPGTestSuite.java
└── tests
├── android-signing-key.p12
├── icon-encrypted-not-signed.png.gpg
├── icon.png
├── icon.png.asc
├── icon.png.gpg
├── icon.png.sig
├── one-public-key.pkr
├── public-keys.pkr
├── public-keys.pkr.asc
├── public-keys.pkr.sig
├── pubring.gpg
├── secret
├── secret-keys.skr
├── secret.gpg
├── secring.gpg
└── trustdb.gpg
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | build/
3 | dist/
4 | hs_err_pid*.log
5 | jni/*.o
6 | jni/*.orig
7 | jni/TAGS
8 | # generated JNI headers
9 | jni/com_freiheit_gnupg_GnuPG*.h
10 | lib/libgnupg-for-java.so
11 | target/
12 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | gnupg-for-java
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 2.1, February 1999
3 |
4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc.
5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | [This is the first released version of the Lesser GPL. It also counts
10 | as the successor of the GNU Library Public License, version 2, hence
11 | the version number 2.1.]
12 |
13 | Preamble
14 |
15 | The licenses for most software are designed to take away your
16 | freedom to share and change it. By contrast, the GNU General Public
17 | Licenses are intended to guarantee your freedom to share and change
18 | free software--to make sure the software is free for all its users.
19 |
20 | This license, the Lesser General Public License, applies to some
21 | specially designated software packages--typically libraries--of the
22 | Free Software Foundation and other authors who decide to use it. You
23 | can use it too, but we suggest you first think carefully about whether
24 | this license or the ordinary General Public License is the better
25 | strategy to use in any particular case, based on the explanations below.
26 |
27 | When we speak of free software, we are referring to freedom of use,
28 | not price. Our General Public Licenses are designed to make sure that
29 | you have the freedom to distribute copies of free software (and charge
30 | for this service if you wish); that you receive source code or can get
31 | it if you want it; that you can change the software and use pieces of
32 | it in new free programs; and that you are informed that you can do
33 | these things.
34 |
35 | To protect your rights, we need to make restrictions that forbid
36 | distributors to deny you these rights or to ask you to surrender these
37 | rights. These restrictions translate to certain responsibilities for
38 | you if you distribute copies of the library or if you modify it.
39 |
40 | For example, if you distribute copies of the library, whether gratis
41 | or for a fee, you must give the recipients all the rights that we gave
42 | you. You must make sure that they, too, receive or can get the source
43 | code. If you link other code with the library, you must provide
44 | complete object files to the recipients, so that they can relink them
45 | with the library after making changes to the library and recompiling
46 | it. And you must show them these terms so they know their rights.
47 |
48 | We protect your rights with a two-step method: (1) we copyright the
49 | library, and (2) we offer you this license, which gives you legal
50 | permission to copy, distribute and/or modify the library.
51 |
52 | To protect each distributor, we want to make it very clear that
53 | there is no warranty for the free library. Also, if the library is
54 | modified by someone else and passed on, the recipients should know
55 | that what they have is not the original version, so that the original
56 | author's reputation will not be affected by problems that might be
57 | introduced by others.
58 |
59 | Finally, software patents pose a constant threat to the existence of
60 | any free program. We wish to make sure that a company cannot
61 | effectively restrict the users of a free program by obtaining a
62 | restrictive license from a patent holder. Therefore, we insist that
63 | any patent license obtained for a version of the library must be
64 | consistent with the full freedom of use specified in this license.
65 |
66 | Most GNU software, including some libraries, is covered by the
67 | ordinary GNU General Public License. This license, the GNU Lesser
68 | General Public License, applies to certain designated libraries, and
69 | is quite different from the ordinary General Public License. We use
70 | this license for certain libraries in order to permit linking those
71 | libraries into non-free programs.
72 |
73 | When a program is linked with a library, whether statically or using
74 | a shared library, the combination of the two is legally speaking a
75 | combined work, a derivative of the original library. The ordinary
76 | General Public License therefore permits such linking only if the
77 | entire combination fits its criteria of freedom. The Lesser General
78 | Public License permits more lax criteria for linking other code with
79 | the library.
80 |
81 | We call this license the "Lesser" General Public License because it
82 | does Less to protect the user's freedom than the ordinary General
83 | Public License. It also provides other free software developers Less
84 | of an advantage over competing non-free programs. These disadvantages
85 | are the reason we use the ordinary General Public License for many
86 | libraries. However, the Lesser license provides advantages in certain
87 | special circumstances.
88 |
89 | For example, on rare occasions, there may be a special need to
90 | encourage the widest possible use of a certain library, so that it becomes
91 | a de-facto standard. To achieve this, non-free programs must be
92 | allowed to use the library. A more frequent case is that a free
93 | library does the same job as widely used non-free libraries. In this
94 | case, there is little to gain by limiting the free library to free
95 | software only, so we use the Lesser General Public License.
96 |
97 | In other cases, permission to use a particular library in non-free
98 | programs enables a greater number of people to use a large body of
99 | free software. For example, permission to use the GNU C Library in
100 | non-free programs enables many more people to use the whole GNU
101 | operating system, as well as its variant, the GNU/Linux operating
102 | system.
103 |
104 | Although the Lesser General Public License is Less protective of the
105 | users' freedom, it does ensure that the user of a program that is
106 | linked with the Library has the freedom and the wherewithal to run
107 | that program using a modified version of the Library.
108 |
109 | The precise terms and conditions for copying, distribution and
110 | modification follow. Pay close attention to the difference between a
111 | "work based on the library" and a "work that uses the library". The
112 | former contains code derived from the library, whereas the latter must
113 | be combined with the library in order to run.
114 |
115 | GNU LESSER GENERAL PUBLIC LICENSE
116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
117 |
118 | 0. This License Agreement applies to any software library or other
119 | program which contains a notice placed by the copyright holder or
120 | other authorized party saying it may be distributed under the terms of
121 | this Lesser General Public License (also called "this License").
122 | Each licensee is addressed as "you".
123 |
124 | A "library" means a collection of software functions and/or data
125 | prepared so as to be conveniently linked with application programs
126 | (which use some of those functions and data) to form executables.
127 |
128 | The "Library", below, refers to any such software library or work
129 | which has been distributed under these terms. A "work based on the
130 | Library" means either the Library or any derivative work under
131 | copyright law: that is to say, a work containing the Library or a
132 | portion of it, either verbatim or with modifications and/or translated
133 | straightforwardly into another language. (Hereinafter, translation is
134 | included without limitation in the term "modification".)
135 |
136 | "Source code" for a work means the preferred form of the work for
137 | making modifications to it. For a library, complete source code means
138 | all the source code for all modules it contains, plus any associated
139 | interface definition files, plus the scripts used to control compilation
140 | and installation of the library.
141 |
142 | Activities other than copying, distribution and modification are not
143 | covered by this License; they are outside its scope. The act of
144 | running a program using the Library is not restricted, and output from
145 | such a program is covered only if its contents constitute a work based
146 | on the Library (independent of the use of the Library in a tool for
147 | writing it). Whether that is true depends on what the Library does
148 | and what the program that uses the Library does.
149 |
150 | 1. You may copy and distribute verbatim copies of the Library's
151 | complete source code as you receive it, in any medium, provided that
152 | you conspicuously and appropriately publish on each copy an
153 | appropriate copyright notice and disclaimer of warranty; keep intact
154 | all the notices that refer to this License and to the absence of any
155 | warranty; and distribute a copy of this License along with the
156 | Library.
157 |
158 | You may charge a fee for the physical act of transferring a copy,
159 | and you may at your option offer warranty protection in exchange for a
160 | fee.
161 |
162 | 2. You may modify your copy or copies of the Library or any portion
163 | of it, thus forming a work based on the Library, and copy and
164 | distribute such modifications or work under the terms of Section 1
165 | above, provided that you also meet all of these conditions:
166 |
167 | a) The modified work must itself be a software library.
168 |
169 | b) You must cause the files modified to carry prominent notices
170 | stating that you changed the files and the date of any change.
171 |
172 | c) You must cause the whole of the work to be licensed at no
173 | charge to all third parties under the terms of this License.
174 |
175 | d) If a facility in the modified Library refers to a function or a
176 | table of data to be supplied by an application program that uses
177 | the facility, other than as an argument passed when the facility
178 | is invoked, then you must make a good faith effort to ensure that,
179 | in the event an application does not supply such function or
180 | table, the facility still operates, and performs whatever part of
181 | its purpose remains meaningful.
182 |
183 | (For example, a function in a library to compute square roots has
184 | a purpose that is entirely well-defined independent of the
185 | application. Therefore, Subsection 2d requires that any
186 | application-supplied function or table used by this function must
187 | be optional: if the application does not supply it, the square
188 | root function must still compute square roots.)
189 |
190 | These requirements apply to the modified work as a whole. If
191 | identifiable sections of that work are not derived from the Library,
192 | and can be reasonably considered independent and separate works in
193 | themselves, then this License, and its terms, do not apply to those
194 | sections when you distribute them as separate works. But when you
195 | distribute the same sections as part of a whole which is a work based
196 | on the Library, the distribution of the whole must be on the terms of
197 | this License, whose permissions for other licensees extend to the
198 | entire whole, and thus to each and every part regardless of who wrote
199 | it.
200 |
201 | Thus, it is not the intent of this section to claim rights or contest
202 | your rights to work written entirely by you; rather, the intent is to
203 | exercise the right to control the distribution of derivative or
204 | collective works based on the Library.
205 |
206 | In addition, mere aggregation of another work not based on the Library
207 | with the Library (or with a work based on the Library) on a volume of
208 | a storage or distribution medium does not bring the other work under
209 | the scope of this License.
210 |
211 | 3. You may opt to apply the terms of the ordinary GNU General Public
212 | License instead of this License to a given copy of the Library. To do
213 | this, you must alter all the notices that refer to this License, so
214 | that they refer to the ordinary GNU General Public License, version 2,
215 | instead of to this License. (If a newer version than version 2 of the
216 | ordinary GNU General Public License has appeared, then you can specify
217 | that version instead if you wish.) Do not make any other change in
218 | these notices.
219 |
220 | Once this change is made in a given copy, it is irreversible for
221 | that copy, so the ordinary GNU General Public License applies to all
222 | subsequent copies and derivative works made from that copy.
223 |
224 | This option is useful when you wish to copy part of the code of
225 | the Library into a program that is not a library.
226 |
227 | 4. You may copy and distribute the Library (or a portion or
228 | derivative of it, under Section 2) in object code or executable form
229 | under the terms of Sections 1 and 2 above provided that you accompany
230 | it with the complete corresponding machine-readable source code, which
231 | must be distributed under the terms of Sections 1 and 2 above on a
232 | medium customarily used for software interchange.
233 |
234 | If distribution of object code is made by offering access to copy
235 | from a designated place, then offering equivalent access to copy the
236 | source code from the same place satisfies the requirement to
237 | distribute the source code, even though third parties are not
238 | compelled to copy the source along with the object code.
239 |
240 | 5. A program that contains no derivative of any portion of the
241 | Library, but is designed to work with the Library by being compiled or
242 | linked with it, is called a "work that uses the Library". Such a
243 | work, in isolation, is not a derivative work of the Library, and
244 | therefore falls outside the scope of this License.
245 |
246 | However, linking a "work that uses the Library" with the Library
247 | creates an executable that is a derivative of the Library (because it
248 | contains portions of the Library), rather than a "work that uses the
249 | library". The executable is therefore covered by this License.
250 | Section 6 states terms for distribution of such executables.
251 |
252 | When a "work that uses the Library" uses material from a header file
253 | that is part of the Library, the object code for the work may be a
254 | derivative work of the Library even though the source code is not.
255 | Whether this is true is especially significant if the work can be
256 | linked without the Library, or if the work is itself a library. The
257 | threshold for this to be true is not precisely defined by law.
258 |
259 | If such an object file uses only numerical parameters, data
260 | structure layouts and accessors, and small macros and small inline
261 | functions (ten lines or less in length), then the use of the object
262 | file is unrestricted, regardless of whether it is legally a derivative
263 | work. (Executables containing this object code plus portions of the
264 | Library will still fall under Section 6.)
265 |
266 | Otherwise, if the work is a derivative of the Library, you may
267 | distribute the object code for the work under the terms of Section 6.
268 | Any executables containing that work also fall under Section 6,
269 | whether or not they are linked directly with the Library itself.
270 |
271 | 6. As an exception to the Sections above, you may also combine or
272 | link a "work that uses the Library" with the Library to produce a
273 | work containing portions of the Library, and distribute that work
274 | under terms of your choice, provided that the terms permit
275 | modification of the work for the customer's own use and reverse
276 | engineering for debugging such modifications.
277 |
278 | You must give prominent notice with each copy of the work that the
279 | Library is used in it and that the Library and its use are covered by
280 | this License. You must supply a copy of this License. If the work
281 | during execution displays copyright notices, you must include the
282 | copyright notice for the Library among them, as well as a reference
283 | directing the user to the copy of this License. Also, you must do one
284 | of these things:
285 |
286 | a) Accompany the work with the complete corresponding
287 | machine-readable source code for the Library including whatever
288 | changes were used in the work (which must be distributed under
289 | Sections 1 and 2 above); and, if the work is an executable linked
290 | with the Library, with the complete machine-readable "work that
291 | uses the Library", as object code and/or source code, so that the
292 | user can modify the Library and then relink to produce a modified
293 | executable containing the modified Library. (It is understood
294 | that the user who changes the contents of definitions files in the
295 | Library will not necessarily be able to recompile the application
296 | to use the modified definitions.)
297 |
298 | b) Use a suitable shared library mechanism for linking with the
299 | Library. A suitable mechanism is one that (1) uses at run time a
300 | copy of the library already present on the user's computer system,
301 | rather than copying library functions into the executable, and (2)
302 | will operate properly with a modified version of the library, if
303 | the user installs one, as long as the modified version is
304 | interface-compatible with the version that the work was made with.
305 |
306 | c) Accompany the work with a written offer, valid for at
307 | least three years, to give the same user the materials
308 | specified in Subsection 6a, above, for a charge no more
309 | than the cost of performing this distribution.
310 |
311 | d) If distribution of the work is made by offering access to copy
312 | from a designated place, offer equivalent access to copy the above
313 | specified materials from the same place.
314 |
315 | e) Verify that the user has already received a copy of these
316 | materials or that you have already sent this user a copy.
317 |
318 | For an executable, the required form of the "work that uses the
319 | Library" must include any data and utility programs needed for
320 | reproducing the executable from it. However, as a special exception,
321 | the materials to be distributed need not include anything that is
322 | normally distributed (in either source or binary form) with the major
323 | components (compiler, kernel, and so on) of the operating system on
324 | which the executable runs, unless that component itself accompanies
325 | the executable.
326 |
327 | It may happen that this requirement contradicts the license
328 | restrictions of other proprietary libraries that do not normally
329 | accompany the operating system. Such a contradiction means you cannot
330 | use both them and the Library together in an executable that you
331 | distribute.
332 |
333 | 7. You may place library facilities that are a work based on the
334 | Library side-by-side in a single library together with other library
335 | facilities not covered by this License, and distribute such a combined
336 | library, provided that the separate distribution of the work based on
337 | the Library and of the other library facilities is otherwise
338 | permitted, and provided that you do these two things:
339 |
340 | a) Accompany the combined library with a copy of the same work
341 | based on the Library, uncombined with any other library
342 | facilities. This must be distributed under the terms of the
343 | Sections above.
344 |
345 | b) Give prominent notice with the combined library of the fact
346 | that part of it is a work based on the Library, and explaining
347 | where to find the accompanying uncombined form of the same work.
348 |
349 | 8. You may not copy, modify, sublicense, link with, or distribute
350 | the Library except as expressly provided under this License. Any
351 | attempt otherwise to copy, modify, sublicense, link with, or
352 | distribute the Library is void, and will automatically terminate your
353 | rights under this License. However, parties who have received copies,
354 | or rights, from you under this License will not have their licenses
355 | terminated so long as such parties remain in full compliance.
356 |
357 | 9. You are not required to accept this License, since you have not
358 | signed it. However, nothing else grants you permission to modify or
359 | distribute the Library or its derivative works. These actions are
360 | prohibited by law if you do not accept this License. Therefore, by
361 | modifying or distributing the Library (or any work based on the
362 | Library), you indicate your acceptance of this License to do so, and
363 | all its terms and conditions for copying, distributing or modifying
364 | the Library or works based on it.
365 |
366 | 10. Each time you redistribute the Library (or any work based on the
367 | Library), the recipient automatically receives a license from the
368 | original licensor to copy, distribute, link with or modify the Library
369 | subject to these terms and conditions. You may not impose any further
370 | restrictions on the recipients' exercise of the rights granted herein.
371 | You are not responsible for enforcing compliance by third parties with
372 | this License.
373 |
374 | 11. If, as a consequence of a court judgment or allegation of patent
375 | infringement or for any other reason (not limited to patent issues),
376 | conditions are imposed on you (whether by court order, agreement or
377 | otherwise) that contradict the conditions of this License, they do not
378 | excuse you from the conditions of this License. If you cannot
379 | distribute so as to satisfy simultaneously your obligations under this
380 | License and any other pertinent obligations, then as a consequence you
381 | may not distribute the Library at all. For example, if a patent
382 | license would not permit royalty-free redistribution of the Library by
383 | all those who receive copies directly or indirectly through you, then
384 | the only way you could satisfy both it and this License would be to
385 | refrain entirely from distribution of the Library.
386 |
387 | If any portion of this section is held invalid or unenforceable under any
388 | particular circumstance, the balance of the section is intended to apply,
389 | and the section as a whole is intended to apply in other circumstances.
390 |
391 | It is not the purpose of this section to induce you to infringe any
392 | patents or other property right claims or to contest validity of any
393 | such claims; this section has the sole purpose of protecting the
394 | integrity of the free software distribution system which is
395 | implemented by public license practices. Many people have made
396 | generous contributions to the wide range of software distributed
397 | through that system in reliance on consistent application of that
398 | system; it is up to the author/donor to decide if he or she is willing
399 | to distribute software through any other system and a licensee cannot
400 | impose that choice.
401 |
402 | This section is intended to make thoroughly clear what is believed to
403 | be a consequence of the rest of this License.
404 |
405 | 12. If the distribution and/or use of the Library is restricted in
406 | certain countries either by patents or by copyrighted interfaces, the
407 | original copyright holder who places the Library under this License may add
408 | an explicit geographical distribution limitation excluding those countries,
409 | so that distribution is permitted only in or among countries not thus
410 | excluded. In such case, this License incorporates the limitation as if
411 | written in the body of this License.
412 |
413 | 13. The Free Software Foundation may publish revised and/or new
414 | versions of the Lesser General Public License from time to time.
415 | Such new versions will be similar in spirit to the present version,
416 | but may differ in detail to address new problems or concerns.
417 |
418 | Each version is given a distinguishing version number. If the Library
419 | specifies a version number of this License which applies to it and
420 | "any later version", you have the option of following the terms and
421 | conditions either of that version or of any later version published by
422 | the Free Software Foundation. If the Library does not specify a
423 | license version number, you may choose any version ever published by
424 | the Free Software Foundation.
425 |
426 | 14. If you wish to incorporate parts of the Library into other free
427 | programs whose distribution conditions are incompatible with these,
428 | write to the author to ask for permission. For software which is
429 | copyrighted by the Free Software Foundation, write to the Free
430 | Software Foundation; we sometimes make exceptions for this. Our
431 | decision will be guided by the two goals of preserving the free status
432 | of all derivatives of our free software and of promoting the sharing
433 | and reuse of software generally.
434 |
435 | NO WARRANTY
436 |
437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
446 |
447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
456 | DAMAGES.
457 |
458 | END OF TERMS AND CONDITIONS
459 |
460 | How to Apply These Terms to Your New Libraries
461 |
462 | If you develop a new library, and you want it to be of the greatest
463 | possible use to the public, we recommend making it free software that
464 | everyone can redistribute and change. You can do so by permitting
465 | redistribution under these terms (or, alternatively, under the terms of the
466 | ordinary General Public License).
467 |
468 | To apply these terms, attach the following notices to the library. It is
469 | safest to attach them to the start of each source file to most effectively
470 | convey the exclusion of warranty; and each file should have at least the
471 | "copyright" line and a pointer to where the full notice is found.
472 |
473 |
474 | Copyright (C)
475 |
476 | This library is free software; you can redistribute it and/or
477 | modify it under the terms of the GNU Lesser General Public
478 | License as published by the Free Software Foundation; either
479 | version 2.1 of the License, or (at your option) any later version.
480 |
481 | This library is distributed in the hope that it will be useful,
482 | but WITHOUT ANY WARRANTY; without even the implied warranty of
483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
484 | Lesser General Public License for more details.
485 |
486 | You should have received a copy of the GNU Lesser General Public
487 | License along with this library; if not, write to the Free Software
488 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
489 |
490 | Also add information on how to contact you by electronic and paper mail.
491 |
492 | You should also get your employer (if you work as a programmer) or your
493 | school, if any, to sign a "copyright disclaimer" for the library, if
494 | necessary. Here is a sample; alter the names:
495 |
496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the
497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker.
498 |
499 | , 1 April 1990
500 | Ty Coon, President of Vice
501 |
502 | That's all there is to it!
503 |
504 |
505 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | GnuPG for Java
2 | ==============
3 |
4 | GnuPG for Java is a Java wrapper for the gpgme (GnuPG Made Easy) library for
5 | working with the GnuPG encryption suite. It is a native binding to the gpgme
6 | using JNI. It requires gpgme 1.4.3 or newer, and expects to work with GnuPG
7 | 2.x as the "engine" in gpgme. With GnuPG 2.x, gpg-agent will handle prompting
8 | the user for the passphrase, as well as passphrase caching.
9 |
10 | gpgpme is the standard library for developing third-party apps on top of GnuPG.
11 |
12 | Stefan Richter originally wrote it for 32-Bit Intel GNU/Linux platforms. Some
13 | of his colleagues added 64-Bit Intel GNU/Linux support. It should build and
14 | run on other UNIX platforms too, but that has not been tested. The Guardian
15 | Project then ported it to GnuPG 2.x and Android as the basis of Gnu Privacy
16 | Guard for Android, and added lots of features and fixed lots of bugs in the
17 | process.
18 |
19 | It should be easy to add Windows support by compiling a DLL, adding this to
20 | the jar and extend the loading mechanism to load a DLL on a Windows platform
21 | instead of loading a .so lib.
22 |
23 |
24 | ## Setup for Building
25 |
26 | You will need a JUnit jar to run the tests. If your system does not provide
27 | one already, you can download the jar and put it into the build/ directory of
28 | this project.
29 |
30 | ### Debian/Ubuntu/Mint/etc
31 |
32 | sudo apt-get install default-jdk make ant build-essential \
33 | libgpgme11-dev libgpg-error-dev
34 |
35 |
36 | ### Windows
37 |
38 | * Install MinGW (for 32-bit): http://mingw.org/
39 | * Install Gpg4win: http://gpg4win.org/download.html
40 | ** Signing Key Fingerprint: `61AC 3F5E E4BE 593C 13D6 8B1E 7CBD 620B EC70 B1B8`
41 |
42 |
43 | ### Mac OS X
44 |
45 | You need to install GnuPG2 from one of a couple sources. You can get
46 | it from Homebrew, MacPorts, or Fink. Or you can install
47 | "GPGTools":https://gpgtools.org and then build gpgme from source.
48 |
49 |
50 | ## Building
51 |
52 | To build the `gnupg-for-java.jar` and `libgnupg-for-java.so` in build/, run
53 | this:
54 |
55 | ant clean release
56 |
57 | You can run the test suite using `ant` also:
58 |
59 | ant test
60 |
61 | If you want the optional javadoc in build/docs/, run:
62 |
63 | ant javadoc
64 |
65 |
66 | ## Hacking Notes
67 |
68 | Please conform to our code format standard. For C files use
69 | ./format-code.sh. For Java files, use Eclipse with the Android mode from the
70 | Android project. The default Eclipse formatting is usually close enough.
71 |
72 |
--------------------------------------------------------------------------------
/TODO:
--------------------------------------------------------------------------------
1 | What is missing to complete the first public release?
2 | -----------------------------------------------------
3 |
4 | MISSING CODE
5 | + Call check_version in static initializer (init_lib)
6 | + Set Locale-Code in static initializer (init_lib)
7 | + Implement destroy-Method and finalizer for GnuPGKey (release_ref...)
8 | + setter/getter for Ascii-Armor
9 | + Search for keys and return GnuPGKey-List (Array...?!)
10 |
11 | - Read complete Key-Structure (all subkeys, all attributes)
12 |
13 | - Password-Entry via Console (take it from henplus)
14 | - Strip spaces from fingerprints
15 | - complete signing code: add_signers to context
16 | - change the access to global env using GetEnv()
17 |
18 | - check for gpg-agent/pinentry and dont require Java-Listener
19 |
20 | - make setField() a utils-function
21 |
22 | [- Synchronize GnuPGContext and GnuPGData for multithreading (and calls to wait())]
23 |
24 | Optional:
25 | [- Make the lib completely stream-based (no large data alloc is required)]
26 |
27 | PROBLEMS/BUGS
28 | - Fix the problems with the passphrase callback (null-Value Passwords etc.)
29 | - Fix all warnings (integer pointer ...)
30 | - Read from InputStream to GnuPGData-Object
31 | - Handle errors, if no key could be retrieved with given fingerprint
32 | - ?
33 |
34 | TESTING
35 | + Check for correct encoding (like special german characters)
36 | + Generate Test-Environment that can be distributed with the code
37 | + Write JUnit-Testcode for all existing methods
38 | - Run tests with large data
39 | [- Use a separate test key-ring (maybe from gpg)]
40 |
41 | VERSION-REQUIREMENTS
42 | - Use seek instead of rewind
43 | - Check for required library
44 | - Check for required jvm version
45 | - Check for required os (must be linux)
46 |
47 | SRC-DISTRIBUTION
48 | + Integrate ant/make compilation parts
49 | - Add LGPL-License and LGPL headers to all files
50 | - Write example code
51 | - Ask, how to transfer license-responsibility to GNU
52 | - Add this as a new SourceForge-Project
53 |
54 | DOCUMENTATION
55 | + Write Javadoc-Comments
56 | - Write Documentation
57 | - Put Documentation as HTML and JavaDoc on SourceForge Project-Page
58 |
--------------------------------------------------------------------------------
/build.properties:
--------------------------------------------------------------------------------
1 | # default build.properties
2 |
3 | app.name=gnupg-for-java
4 | app.version=0.2-pre
5 | app.customer=LGPL
6 | app.vendor=freiheit.com technologies gmbh
7 | app.year=2014
8 |
9 | build.compiler=modern
10 | compile.debug=true
11 | compile.deprecation=false
12 | compile.optimize=true
13 |
14 | #where to put etc stuff :-)
15 | etc=etc
16 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
98 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
--------------------------------------------------------------------------------
/etc/SOURCEFOOTER:
--------------------------------------------------------------------------------
1 | /*
2 | * Local variables:
3 | * c-basic-offset: 4
4 | * indent-tabs-mode: nil
5 | * compile-command: "ant -emacs -find build.xml"
6 | * End:
7 | */
8 |
--------------------------------------------------------------------------------
/etc/SOURCEHEADER:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: SOURCEHEADER,v 1.1 2005/01/24 13:56:49 stefan Exp $
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 |
--------------------------------------------------------------------------------
/etc/fdc.javadoc.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guardianproject/gnupg-for-java/f5b4d5b92ece838598f25e493a6075d2a70fe91e/etc/fdc.javadoc.png
--------------------------------------------------------------------------------
/etc/guardianproject-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guardianproject/gnupg-for-java/f5b4d5b92ece838598f25e493a6075d2a70fe91e/etc/guardianproject-logo.png
--------------------------------------------------------------------------------
/etc/overview.html:
--------------------------------------------------------------------------------
1 |
2 | GnuPG for Java is a (currently GNU/Linux only) GnuPG-library based on the GPGME library.
3 |
4 | GPGME is the standard way to implement language bindings on top of GnuPG. You can use this library
5 | to encrypt, decrypt and sign data and verify signatures on data using an underlying gnupg/gpgme system.
6 |
7 | The key management features are currently not finished. But the library can find/list keys with their signatures.
8 | Future versions will be able to do everything that gpgme can do.
9 |
10 | This library uses JNI (Java Native Interface) to call GPGME-Library functions. Therefore, this library
11 | runs only on platforms for which GPGME is available. Furthermore, this library is tested only under
12 | 32-Bit Intel-based GNU/Linux.
13 |
14 | This is an alpha release. Please expect problems. Although this is working code, this library is not finished and has known and unknown bugs.
15 |
16 | Java for GnuPG is free software; you can redistribute it and/or modify
17 | it under the terms of the GNU Lesser General Public Licenseas published by the Free Software Foundation; either version 2.1
18 | of the License, or (at your option) any later version.
19 |
20 | Please see COPYING in the source distribution for the complete licence.
21 |
22 | So, if you need this library for another platform, if you find a bug or if you need a feature,
23 | please contact Stefan Richter.
24 |
25 |
26 |
27 | Quickstart to encryption:
28 |
29 |
30 | GnuPGContext ctx = new GnuPGContext();
31 |
32 | GnuPGKey[] recipient = new GnuPGKey[1];
33 | recipient[0] = ctx.getKeyByFingerprint("gpg-fingerprint of recipient (must be from your keyring)");
34 | //attention: fingerprint should not contain spaces...
35 |
36 | GnuPGData plain = ctx.createDataObject("I am a secret message.");
37 | GnuPGData cipher = ctx.createDataObject();//this will contain the cipher after encryption
38 |
39 | ctx.encrypt(recipient, plain, cipher);
40 | System.out.println("Plain text: " + plain);
41 | System.out.println("Cipher text: " + cipher);
42 |
GnuPG for Java is a (currently GNU/Linux only) GnuPG-library based on the GPGME library. GPGME is the standard way to implement language bindings on top of GnuPG. You can use this library to encrypt, decrypt and sign data and verify signatures on data using an underlying gnupg/gpgme system.
25 | The key management features are currently not finished. But the library can find/list keys with their signatures. Future versions will be able to do everything that gpgme can do.
26 | This library uses JNI (Java Native Interface) to call GPGME-Library functions. Therefore, this library runs only on platforms for which GPGME is available. Furthermore, this library is tested only under 32-Bit Intel-based GNU/Linux. This is free software under LGPL.
27 |
28 |
News
29 |
24.01.2005 First public alpha release 0.1.2 of GnuPG for Java.
35 | *
36 | * @author Stefan Richter, stefan@freiheit.com
37 | */
38 |
39 | public class GnuPGContext extends GnuPGPeer {
40 | public static final String TAG = "GnuPGContext";
41 | static {
42 | System.loadLibrary("gnupg-for-java");
43 | }
44 |
45 | private String _version;
46 | private String _filename;
47 | private String _reqversion;
48 | private int _protocol;
49 |
50 | /** Creates a new Context (use on context for one thread!) */
51 | public GnuPGContext() {
52 | gpgmeGetEngineInfo();
53 | setInternalRepresentation(gpgmeNew());
54 | }
55 |
56 | /**
57 | * Returns the version of the underlying GPGME lib version.
58 | *
59 | * @return library version as String (like "1.1.1")
60 | */
61 | public String getVersion() {
62 | return _version;
63 | }
64 |
65 | /**
66 | * Returns the executable gpg with path
67 | *
68 | * @return gpg executable as String (like "/usr/bin/gpg")
69 | */
70 | public String getFilename() {
71 | return _filename;
72 | }
73 |
74 | /**
75 | * Returns the required version of the underlying GPGME lib.
76 | *
77 | * @return required library version as String (like "1.1.1")
78 | */
79 | public String getRequiredVersion() {
80 | return _reqversion;
81 | }
82 |
83 | /**
84 | * Returns the used protocol of the crypto backend.
85 | *
86 | * @return protocol
87 | */
88 | public int getProtocol() {
89 | return _protocol;
90 | }
91 |
92 | /**
93 | * Tells you, is the gpg engine uses ASCII armor. Please check GPG/GPGME for
94 | * documentation if you don't know what this means. (Try 'man gpg' on the
95 | * command line and look for 'armor')
96 | *
97 | * @return boolean true if results will be ASCII armored, if false binary
98 | */
99 | public boolean isArmor() {
100 | return gpgmeGetArmor(getInternalRepresentation());
101 | }
102 |
103 | /**
104 | * Tell the gpg engine to use ASCII armor. Please check GPG/GPGME for
105 | * documentation if you don't know what this means. (Try 'man gpg' on the
106 | * command line and look for 'armor')
107 | *
108 | * @param state set true if results should be ASCII armored, set false if
109 | * binary
110 | */
111 | public void setArmor(boolean state) {
112 | gpgmeSetArmor(getInternalRepresentation(), state);
113 | }
114 |
115 | /**
116 | * Tells you, is the gpg engine is in text mode. Please check GPG/GPGME for
117 | * documentation if you don't know what this means.
118 | *
119 | * @return boolean true means text mode is on
120 | */
121 | public boolean isTextmode() {
122 | return gpgmeGetTextmode(getInternalRepresentation());
123 | }
124 |
125 | /**
126 | * Get the current text mode from the gpg engine. Please check GPG/GPGME for
127 | * documentation if you don't know what this means.
128 | *
129 | * @return boolean if textmode is currently enabled
130 | */
131 | public boolean getTextmode() {
132 | return gpgmeGetTextmode(getInternalRepresentation());
133 | }
134 |
135 | /**
136 | * Tell the gpg engine to set the text mode. Please check GPG/GPGME for
137 | * documentation if you don't know what this means.
138 | *
139 | * @param state set true if you want text mode switched on
140 | */
141 | public void setTextmode(boolean state) {
142 | gpgmeSetTextmode(getInternalRepresentation(), state);
143 | }
144 |
145 | private long[] getInternalRepresentationFromRecipients(GnuPGKey[] recipients) {
146 | // note that these are pointers to addresses in the gnupg-for-java
147 | // shared lib
148 | long recipientsInternals[] = new long[recipients.length];
149 | for (int i = 0; i < recipients.length; i++) {
150 | if (recipients[i] != null)
151 | recipientsInternals[i] = recipients[i].getInternalRepresentation();
152 | else
153 | ; // FIXME: throw exception here? Just skip over it for now.
154 | }
155 | return recipientsInternals;
156 | }
157 |
158 | private boolean hasNoRecipients(GnuPGKey[] recipients) {
159 | return !(recipients != null && recipients.length > 0);
160 | }
161 |
162 | /**
163 | * Gets the public key with the supplied fingerprint from the keyring. This
164 | * is also kind of a factory method to generate key objects, because you
165 | * always need a context to access the keys in your keyring.
166 | *
167 | * @param fingerprint 16 char hex fingerprint of key in your keyring
168 | * @return GnuPGKey the public key that matches the fingerprint
169 | * @see com.freiheit.gnupg.GnuPGKey
170 | */
171 | public GnuPGKey getKeyByFingerprint(String fingerprint) throws GnuPGException {
172 | if (fingerprint == null || fingerprint.length() < 1) {
173 | return null;
174 | } else {
175 | return new GnuPGKey(this, fingerprint, false);
176 | }
177 | }
178 |
179 | /**
180 | * Gets the secret key with the supplied fingerprint from the keyring. This
181 | * is also kind of a factory method to generate secret key objects, because
182 | * you always need a context to access the keys in your keyring.
183 | *
184 | * @param fingerprint 16 char hex fingerprint of key in your keyring
185 | * @return GnuPGKey the secret key that matches the fingerprint
186 | * @see com.freiheit.gnupg.GnuPGKey
187 | */
188 | public GnuPGKey getSecretKeyByFingerprint(String fingerprint)
189 | throws GnuPGException {
190 | if (fingerprint == null || fingerprint.length() < 1) {
191 | return null;
192 | } else {
193 | return new GnuPGKey(this, fingerprint, true);
194 | }
195 | }
196 |
197 | /**
198 | * Factory method to generate a GnuPGData instance from a File.
199 | *
200 | * @param f should not be null or a directory and must be readable
201 | * @return GnuPGData instance with data from f, unless
202 | * f is has no readable data in it, then it returns
203 | * null
204 | * @throws IOException
205 | * @see com.freiheit.gnupg.GnuPGData
206 | */
207 | public GnuPGData createDataObject(File f) throws GnuPGException, IOException {
208 | if (f != null && f.exists() && !f.isDirectory() && f.canRead()) {
209 | return new GnuPGData(f);
210 | }
211 | return null;
212 | }
213 |
214 | /**
215 | * Factory method to generate a GnuPGData instance from a String.
216 | *
217 | * @param data should not be null and should have a length > 0
218 | * @return GnuPGData instance based on data, unless
219 | * data is null or has a length of 0, then it returns
220 | * null
221 | * @see com.freiheit.gnupg.GnuPGData
222 | */
223 | public GnuPGData createDataObject(String data) throws GnuPGException {
224 | if (data == null || data.length() < 1) {
225 | return null;
226 | }
227 | else {
228 | return new GnuPGData(data);
229 | }
230 | }
231 |
232 | /**
233 | * Factory method to generate a GnuPGData instance from a byte array.
234 | *
235 | * @param data should not be null and should have a length > 0
236 | * @return GnuPGData instance based on data, unless
237 | * data is null or has a length of 0, then it returns
238 | * null
239 | * @see com.freiheit.gnupg.GnuPGData
240 | */
241 | public GnuPGData createDataObject(byte[] data) throws GnuPGException {
242 | if (data == null || data.length < 1) {
243 | return null;
244 | }
245 | else {
246 | return new GnuPGData(data);
247 | }
248 | }
249 |
250 | /**
251 | * Factory method to generate an empty GnuPGData instance.
252 | *
253 | * @return an empty GnuPGData instance
254 | * @see com.freiheit.gnupg.GnuPGData
255 | */
256 | public GnuPGData createDataObject() throws GnuPGException {
257 | return new GnuPGData();
258 | }
259 |
260 | /**
261 | * Convenience method to generate empty key arrays. To encrypt data, you
262 | * need to supply a list of recipients in an array. This is a little helper.
263 | * It will not search for keys! Its just an empty array.
264 | *
265 | * @param withLengthOf length of the array to generate (number of
266 | * recipients)
267 | * @return GnuPGKey empty array of keys
268 | * @see com.freiheit.gnupg.GnuPGKey
269 | */
270 | public GnuPGKey[] generateEmptyKeyArray(int withLengthOf) {
271 | if (withLengthOf < 1) {
272 | return null;
273 | }
274 | else {
275 | return new GnuPGKey[withLengthOf];
276 | }
277 | }
278 |
279 | /**
280 | * List all public keys in keyring.
281 | *
282 | * @return GnuPGKey array of key objects with all public keys
283 | * @see com.freiheit.gnupg.GnuPGKey
284 | */
285 | public GnuPGKey[] listKeys() throws GnuPGException {
286 | return searchKeys("");
287 | }
288 |
289 | /**
290 | * List all secret keys in keyring.
291 | *
292 | * @return GnuPGKey array of key objects with all secret keys
293 | * @see com.freiheit.gnupg.GnuPGKey
294 | */
295 | public GnuPGKey[] listSecretKeys() throws GnuPGException {
296 | return searchSecretKeys("");
297 | }
298 |
299 | /**
300 | * Find all public keys matching query in keyring.
301 | *
302 | * @param query allows the same expressions as gpg on command line
303 | * @return GnuPGKey array of key objects with all matching keys
304 | * @see com.freiheit.gnupg.GnuPGKey
305 | */
306 | public GnuPGKey[] searchKeys(String query) throws GnuPGException {
307 | if (query == null) {
308 | query = new String("");
309 | }
310 | return gpgmeKeylist(getInternalRepresentation(), query, false);
311 | }
312 |
313 | /**
314 | * Find all keys matching query in keyring.
315 | *
316 | * @param query allows the same expressions as gpg on command line
317 | * @return GnuPGKey array of key objects with all matching keys
318 | * @see com.freiheit.gnupg.GnuPGKey
319 | */
320 | public GnuPGKey[] searchSecretKeys(String query) throws GnuPGException {
321 | if (query == null) {
322 | query = new String("");
323 | }
324 | return gpgmeKeylist(getInternalRepresentation(), query, true);
325 | }
326 |
327 | /**
328 | * Encrypt plain text and return ASCII-armored text.
329 | *
330 | * @param recipients array of keys to encrypt to
331 | * @param plain the plain text to be encrypted
332 | * @return String encrypted data in ASCII-armored text
333 | */
334 | public String encryptToAscii(GnuPGKey[] recipients, String plain) {
335 | final GnuPGData plainData = createDataObject(plain);
336 | final String ret = encryptToAscii(recipients, plainData);
337 | plainData.destroy();
338 | return ret;
339 | }
340 |
341 | /**
342 | * Encrypt a byte array and return ASCII-armored text.
343 | *
344 | * @param recipients array of keys to encrypt to
345 | * @param plain the byte[] to be encrypted
346 | * @return String encrypted data in ASCII-armored text
347 | */
348 | public String encryptToAscii(GnuPGKey[] recipients, byte[] plain) {
349 | final GnuPGData plainData = createDataObject(plain);
350 | final String ret = encryptToAscii(recipients, plainData);
351 | plainData.destroy();
352 | return ret;
353 | }
354 |
355 | /**
356 | * Encrypt plain text and return ASCII-armored text.
357 | *
358 | * @param recipients array of keys to encrypt to
359 | * @param plain the GnuPGData to be encrypted
360 | * @return String encrypted data in ASCII-armored text
361 | */
362 | public String encryptToAscii(GnuPGKey[] recipients, GnuPGData plain) {
363 | long l = getInternalRepresentation();
364 | boolean previous = gpgmeGetArmor(l);
365 | gpgmeSetArmor(l, true);
366 | GnuPGData cipher = createDataObject();
367 | encrypt(recipients, plain, cipher);
368 | final String ret = cipher.toString();
369 | cipher.destroy();
370 | if (previous == false) // maintain the original ASCII-Armor state
371 | gpgmeSetArmor(l, false);
372 | return ret;
373 | }
374 |
375 | /**
376 | * Encrypt a byte array and return encrypted data in binary form.
377 | *
378 | * @param recipients array of keys to encrypt to
379 | * @param plain the plain binary data to be encrypted
380 | * @return byte[] encrypted data in binary data
381 | */
382 | public byte[] encryptToBinary(GnuPGKey[] recipients, byte[] plain) {
383 | final GnuPGData plainData = createDataObject(plain);
384 | final byte[] ret = encryptToBinary(recipients, plainData);
385 | plainData.destroy();
386 | return ret;
387 | }
388 |
389 | /**
390 | * Encrypt plain text and return encrypted data in binary form.
391 | *
392 | * @param recipients array of keys to encrypt to
393 | * @param plain the plain text to be encrypted
394 | * @return byte[] encrypted data in binary data
395 | */
396 | public byte[] encryptToBinary(GnuPGKey[] recipients, String plain) {
397 | final GnuPGData plainData = createDataObject(plain);
398 | final byte[] ret = encryptToBinary(recipients, plainData);
399 | plainData.destroy();
400 | return ret;
401 | }
402 |
403 | /**
404 | * Encrypt plain data and return encrypted data in binary form.
405 | *
406 | * @param recipients array of keys to encrypt to
407 | * @param plain the GnuPGData to be encrypted
408 | * @return String encrypted data in ASCII-armored text
409 | */
410 | public byte[] encryptToBinary(GnuPGKey[] recipients, GnuPGData plain) {
411 | long l = getInternalRepresentation();
412 | boolean previous = gpgmeGetArmor(l);
413 | gpgmeSetArmor(l, true);
414 | GnuPGData cipher = createDataObject();
415 | encrypt(recipients, plain, cipher);
416 | ByteArrayOutputStream baos = new ByteArrayOutputStream(plain.size());
417 | BufferedOutputStream out = new BufferedOutputStream(baos, 8192);
418 | try {
419 | cipher.write(out);
420 | } catch (IOException e) {
421 | e.printStackTrace();
422 | }
423 | cipher.destroy();
424 | if (previous == false) // maintain the original ASCII-Armor state
425 | gpgmeSetArmor(l, false);
426 | return baos.toByteArray();
427 | }
428 |
429 | /**
430 | * Encrypts the text in plain with the public key of each
431 | * recipient. The result is returned as GnuPGData.
432 | *
433 | * @param recipients array of the keys to encrypt to
434 | * @param plain bytes to be encrypted
435 | * @return GnuPGData the encrypted data
436 | * @see com.freiheit.gnupg.GnuPGData
437 | * @see com.freiheit.gnupg.GnuPGKey
438 | */
439 | public GnuPGData encrypt(GnuPGKey[] recipients, byte[] plain)
440 | throws GnuPGException {
441 | if (hasNoRecipients(recipients) || plain == null || plain.length == 0)
442 | throw new GnuPGException("Encryption arguments not complete.");
443 |
444 | final GnuPGData plainData = createDataObject(plain);
445 | GnuPGData cipherData = createDataObject();
446 |
447 | gpgmeOpEncrypt(this.getInternalRepresentation(),
448 | getInternalRepresentationFromRecipients(recipients),
449 | plainData.getInternalRepresentation(),
450 | cipherData.getInternalRepresentation());
451 | plainData.destroy();
452 | return cipherData;
453 | }
454 |
455 | /**
456 | * Encrypts the text in plain with the public key of each
457 | * recipient. The result is returned as GnuPGData.
458 | *
459 | * @param recipients array of the keys to encrypt to
460 | * @param plain the text to be encrypted
461 | * @return GnuPGData the encrypted data
462 | * @see com.freiheit.gnupg.GnuPGData
463 | * @see com.freiheit.gnupg.GnuPGKey
464 | */
465 | public GnuPGData encrypt(GnuPGKey[] recipients, String plain)
466 | throws GnuPGException {
467 | if (hasNoRecipients(recipients) || plain == null || plain.equals(""))
468 | throw new GnuPGException("Encryption arguments not complete.");
469 |
470 | final GnuPGData plainData = createDataObject(plain);
471 | GnuPGData cipherData = createDataObject();
472 |
473 | gpgmeOpEncrypt(this.getInternalRepresentation(),
474 | getInternalRepresentationFromRecipients(recipients),
475 | plainData.getInternalRepresentation(),
476 | cipherData.getInternalRepresentation());
477 | plainData.destroy();
478 | return cipherData;
479 | }
480 |
481 | /**
482 | * Encrypts the data from plain with the public key of each
483 | * recipient. The result is stored in cipher.
484 | *
485 | * @param recipients Array with the public keys of all recipients
486 | * @param plain text, that should be encrypted
487 | * @param cipher text, the encrypted plain text after method call
488 | * @see com.freiheit.gnupg.GnuPGData
489 | * @see com.freiheit.gnupg.GnuPGKey
490 | */
491 | public void encrypt(GnuPGKey[] recipients, GnuPGData plain, GnuPGData cipher)
492 | throws GnuPGException {
493 | if (hasNoRecipients(recipients) || plain == null || cipher == null)
494 | throw new GnuPGException("Encryption arguments not complete.");
495 |
496 | // note that these are pointers to addresses in the gnupg-for-java
497 | // shared lib
498 | long context = this.getInternalRepresentation();
499 | if (gpgmeGetSignersLength(context) == 0)
500 | gpgmeOpEncrypt(context,
501 | getInternalRepresentationFromRecipients(recipients),
502 | plain.getInternalRepresentation(),
503 | cipher.getInternalRepresentation());
504 | else
505 | gpgmeOpEncryptSign(context,
506 | getInternalRepresentationFromRecipients(recipients),
507 | plain.getInternalRepresentation(),
508 | cipher.getInternalRepresentation());
509 | }
510 |
511 | public void encryptAndSign(GnuPGKey[] recipients, GnuPGData plain, GnuPGData cipher)
512 | throws GnuPGException {
513 | if (hasNoRecipients(recipients) || plain == null || cipher == null)
514 | throw new GnuPGException("Encryption-Arguments not complete.");
515 |
516 | gpgmeOpEncryptSign(this.getInternalRepresentation(),
517 | getInternalRepresentationFromRecipients(recipients),
518 | plain.getInternalRepresentation(),
519 | cipher.getInternalRepresentation());
520 | }
521 |
522 | /**
523 | * Decrypts the data from cipher and returns the result.
524 | *
525 | * @param cipher the data to be decrypted
526 | * @return plain the resulting decrypted data
527 | * @see com.freiheit.gnupg.GnuPGData
528 | */
529 | public GnuPGData decrypt(byte[] cipher) throws GnuPGException {
530 | if (cipher == null || cipher.length == 0)
531 | throw new GnuPGException("Encryption arguments not complete.");
532 |
533 | GnuPGData plainData = createDataObject();
534 | final GnuPGData cipherData = createDataObject(cipher);
535 |
536 | gpgmeOpDecrypt(this.getInternalRepresentation(),
537 | cipherData.getInternalRepresentation(),
538 | plainData.getInternalRepresentation());
539 | cipherData.destroy();
540 | return plainData;
541 | }
542 |
543 | /**
544 | * Decrypts the data from cipher and returns the result.
545 | *
546 | * @param cipher the data to be decrypted
547 | * @return plain the resulting decrypted data
548 | * @see com.freiheit.gnupg.GnuPGData
549 | */
550 | public GnuPGData decrypt(String cipher) throws GnuPGException {
551 | if (cipher == null || cipher.length() == 0)
552 | throw new GnuPGException("Encryption arguments not complete.");
553 |
554 | GnuPGData plainData = createDataObject();
555 | final GnuPGData cipherData = createDataObject(cipher);
556 |
557 | gpgmeOpDecrypt(this.getInternalRepresentation(),
558 | cipherData.getInternalRepresentation(),
559 | plainData.getInternalRepresentation());
560 | cipherData.destroy();
561 | return plainData;
562 | }
563 |
564 | /**
565 | * Decrypts the data from cipher and stores the result in
566 | * plain.
567 | *
568 | * @param cipher text, holds the cipher to be decrypted
569 | * @param plain text, holds the decrypted text after decryption
570 | * @see com.freiheit.gnupg.GnuPGData
571 | */
572 | public void decrypt(GnuPGData cipher, GnuPGData plain) throws GnuPGException {
573 | if (cipher == null || plain == null)
574 | return;
575 |
576 | gpgmeOpDecrypt(this.getInternalRepresentation(),
577 | cipher.getInternalRepresentation(), plain.getInternalRepresentation());
578 | }
579 |
580 | /**
581 | * Decrypts the data from cipher and stores the result in
582 | * plain, then verifies the signature of the data. If the
583 | * decryption or verification fails, it throws a GnuPGException
584 | * .
585 | *
586 | * @param cipher holds the data to be decrypted and verified
587 | * @param plain holds the decrypted data after decryption
588 | * @see com.freiheit.gnupg.GnuPGData
589 | */
590 | public void decryptVerify(GnuPGData cipher, GnuPGData plain) throws GnuPGException {
591 | if (cipher == null || plain == null)
592 | return;
593 |
594 | gpgmeOpDecryptVerify(this.getInternalRepresentation(),
595 | cipher.getInternalRepresentation(),
596 | plain.getInternalRepresentation());
597 | }
598 |
599 | public void changePassphrase(GnuPGKey key) throws GnuPGException {
600 | if (key == null)
601 | return;
602 |
603 | gpgmeOpChangePassphrase(this.getInternalRepresentation(), key.getInternalRepresentation());
604 |
605 | }
606 |
607 | /*
608 | * Not finished. Decrypts the data in cipher and verfies the
609 | * signature on the cipher.
610 | * @param cipher encrypted and signed data
611 | * @param plain will contain the result after the method call
612 | */
613 | // public void decryptAndVerify(GnuPGData cipher, GnuPGData plain) throws
614 | // GnuPGException{
615 | // if (cipher == null || plain == null) return;
616 |
617 | // gpgmeOpDecryptVerify(this.getInternalRepresentation(),
618 | // cipher.getInternalRepresentation(), plain.getInternalRepresentation());
619 | // }
620 |
621 | /**
622 | * Signs the data in plain and stores the result in
623 | * signature.
624 | *
625 | * @param plain data that you want to sign
626 | * @param signature result of the operation
627 | */
628 | public void sign(GnuPGData plain, GnuPGData signature) throws GnuPGException {
629 | if (plain == null || signature == null)
630 | throw new GnuPGException("Parameters not complete or null.");
631 |
632 | gpgmeOpSign(this.getInternalRepresentation(),
633 | plain.getInternalRepresentation(), signature.getInternalRepresentation());
634 | }
635 |
636 | /**
637 | * Verifies a signature.
638 | *
639 | * @param signature TODO
640 | * @param signed TODO
641 | * @param plain TODO
642 | */
643 | public void verify(GnuPGData signature, GnuPGData signed, GnuPGData plain)
644 | throws GnuPGException {
645 | if (signature == null || signed == null || plain == null)
646 | throw new GnuPGException("Parameters not complete or null.");
647 | gpgmeOpVerify(this.getInternalRepresentation(),
648 | signature.getInternalRepresentation(),
649 | signed.getInternalRepresentation(),
650 | plain.getInternalRepresentation());
651 | }
652 |
653 | /**
654 | * Adds a Signer to this context. All signature operation will uses
655 | * this/these signer(s), until you clear the signers from the context. You
656 | * remove all signers at once with a call to clearSigners().
657 | *
658 | * @param key that should be used for signing operations
659 | * @see com.freiheit.gnupg.GnuPGKey
660 | */
661 | public void addSigner(GnuPGKey key) throws GnuPGException {
662 | if (key == null)
663 | throw new GnuPGException("Parameters not complete or null.");
664 | gpgmeAddSigners(getInternalRepresentation(), key.getInternalRepresentation());
665 | }
666 |
667 | /**
668 | * Removes all signers from this context. You add Signers with addSigner().
669 | */
670 | public void clearSigners() throws GnuPGException {
671 | gpgmeClearSigners(getInternalRepresentation());
672 | }
673 |
674 | /**
675 | * Get a specific Signer at a given index. You add Signers with addSigner().
676 | *
677 | * @param index which signer in the list of Signers
678 | * @return the GnuPGKey at index, or null if it doesn't exist
679 | * @see com.freiheit.gnupg.GnuPGKey
680 | */
681 | public GnuPGKey getSigner(int index) throws GnuPGException {
682 | return new GnuPGKey(gpgmeGetSigner(getInternalRepresentation(), index));
683 | }
684 |
685 | /**
686 | * Gets the number of Signers currently in the GnuPGContext. You add Signers
687 | * with addSigner().
688 | *
689 | * @return long the number of Signers currently in the GnuPGContext
690 | * @see com.freiheit.gnupg.GnuPGKey
691 | */
692 | public long getSignersLength() throws GnuPGException {
693 | return gpgmeGetSignersLength(getInternalRepresentation());
694 | }
695 |
696 | /**
697 | * Imports a Key (private or public). You can supply the key in ASCII armor.
698 | */
699 | public void importKey(GnuPGData keydata) throws GnuPGException {
700 | gpgmeOpImport(getInternalRepresentation(), keydata.getInternalRepresentation());
701 | }
702 |
703 | /**
704 | * Imports a Key (private or public). You can supply the key in ASCII armor.
705 | */
706 | public void importKey(File file) throws GnuPGException, IOException {
707 | GnuPGData keydata = createDataObject(file);
708 | if (keydata == null) {
709 | System.out.println("importkey: parsing key data failed");
710 | return;
711 | }
712 | gpgmeOpImport(getInternalRepresentation(), keydata.getInternalRepresentation());
713 | }
714 |
715 | /**
716 | * This calls immediately the release method for the context in the
717 | * underlying gpgme library. This method is called by the finalizer of the
718 | * class anyway, but you can call it yourself if you want to get rid of a
719 | * context at once and don't want to wait for the non-deterministic
720 | * garbage-collector of the JVM.
721 | */
722 | public void destroy() {
723 | if (getInternalRepresentation() != 0) {
724 | gpgmeRelease(getInternalRepresentation());
725 | setInternalRepresentation(0);
726 | }
727 | }
728 |
729 | /**
730 | * Releases underlying datastructures. Simple calls the destroy() method.
731 | */
732 | @Override
733 | protected void finalize() {
734 | destroy();
735 | }
736 |
737 | /**
738 | * Generates a new PGP key and stores it in the keyring.
739 | */
740 | public void genPgpKey(String params) throws GnuPGException {
741 | gpgmeOpGenKey(getInternalRepresentation(), params);
742 | }
743 |
744 | /**
745 | * Generates a new Key.
746 | */
747 | public void genKey(String params, GnuPGData pub, GnuPGData secret) throws GnuPGException {
748 | gpgmeOpGenKey(getInternalRepresentation(), params);
749 | }
750 |
751 | /**
752 | * Sets the engine info for the context
753 | */
754 | public void setEngineInfo(int proto, String fileName, String homeDir) {
755 |
756 | // note that this is a pointer to and address in the gnupg-for-java
757 | // shared
758 | // lib
759 | long ctx = getInternalRepresentation();
760 | gpgmeCtxSetEngineInfo(ctx, proto, fileName, homeDir);
761 | }
762 |
763 | /**
764 | * Returns the result of a key generation. This must bedirectly called,
765 | * after the key has been generated.
766 | *
767 | * @return the result of a key generation
768 | */
769 | public GnuPGGenkeyResult getGenkeyResult() {
770 | return gpgmeOpGenkeyResult(getInternalRepresentation());
771 |
772 | }
773 |
774 | /**
775 | * Export the keys defined by the pattern.
776 | *
777 | * @param pattern pattern for the keys
778 | * @param reserved not used, must be set 0. For later use revered.
779 | * @param data empty data object. Will be filled with the keys.
780 | */
781 | public void export(String pattern, long reserved, GnuPGData data) {
782 | gpgmeOpExport(getInternalRepresentation(), pattern, 0, data.getInternalRepresentation());
783 | }
784 |
785 | /**
786 | * Deletes a Key from key ring. When allowSecret, a secret Key will be
787 | * deleted
788 | *
789 | * @param key key to delete
790 | * @param allowSecret if a secret key shall be deleted.
791 | */
792 | public void delete(GnuPGKey key, boolean allowSecret) {
793 | gpgmeOpDelete(getInternalRepresentation(), key.getInternalRepresentation(), allowSecret);
794 | }
795 |
796 | /*
797 | * Native methods: All these methods are implemented as JNI calls in:
798 | * GnuPGContext.c The naming is as close as possible to the corresponding
799 | * GPGME methods. So, if you want to know what these methods are actually
800 | * doing: Please refer to the GPGME docs.
801 | */
802 | private native void gpgmeGetEngineInfo();
803 |
804 | private native long gpgmeNew();
805 |
806 | private native void gpgmeOpEncrypt(long l, long[] recipientsInternals, long m, long n);
807 |
808 | private native void gpgmeOpDecrypt(long l, long m, long n);
809 |
810 | private native void gpgmeOpChangePassphrase(long l, long m);
811 |
812 | private native void gpgmeRelease(long l);
813 |
814 | private native void gpgmeOpEncryptSign(long context, long[] recipients, long plain, long cipher);
815 |
816 | private native void gpgmeOpDecryptVerify(long context, long cipher, long plain);
817 |
818 | private native void gpgmeOpSign(long context, long l, long m);
819 |
820 | private native void gpgmeOpVerify(long context, long l, long m, long n);
821 |
822 | private native GnuPGKey[] gpgmeKeylist(long l, String query, boolean secret_only);
823 |
824 | private native void gpgmeAddSigners(long l, long m);
825 |
826 | private native void gpgmeClearSigners(long context);
827 |
828 | private native long gpgmeGetSigner(long context, int index);
829 |
830 | private native long gpgmeGetSignersLength(long context);
831 |
832 | private native void gpgmeOpImport(long context, long l);
833 |
834 | private native void gpgmeOpExport(long context, String pattern, long reserved, long l);
835 |
836 | private native void gpgmeOpGenKey(long context, String params);
837 |
838 | private native void gpgmeCtxSetEngineInfo(long context, int proto, String fileName,
839 | String homeDir);
840 |
841 | private native GnuPGGenkeyResult gpgmeOpGenkeyResult(long context);
842 |
843 | private native void gpgmeOpDelete(long context, long l, boolean allowSecret);
844 |
845 | // getters/setters for members, no caching, always direct access to gpgme
846 | private native boolean gpgmeGetArmor(long l);
847 |
848 | private native void gpgmeSetArmor(long l, boolean state);
849 |
850 | private native boolean gpgmeGetTextmode(long l);
851 |
852 | private native void gpgmeSetTextmode(long l, boolean state);
853 |
854 | }
855 |
856 | /* Local Variables: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; End: */
857 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/GnuPGData.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: GnuPGData.java,v 1.1 2005/01/24 13:56:58 stefan Exp $
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 |
15 | package com.freiheit.gnupg;
16 |
17 | import java.io.ByteArrayOutputStream;
18 | import java.io.File;
19 | import java.io.FileNotFoundException;
20 | import java.io.IOException;
21 | import java.io.InputStream;
22 | import java.io.OutputStream;
23 |
24 | /**
25 | * Holds the data that you want to work on and stores the results of crypto
26 | * operations.
27 | *
28 | * @author Stefan Richter, stefan@freiheit.com
29 | */
30 | public class GnuPGData extends GnuPGPeer {
31 |
32 | /**
33 | * Use the factory methods from GnuPGContext to create GnuPGData instances.
34 | * Generates an empty data object. Use this, if you want to create a data
35 | * object to hold a result from a crypto operation.
36 | */
37 | protected GnuPGData() {
38 | setInternalRepresentation(gpgmeDataNew());
39 | }
40 |
41 | /**
42 | * Use the factory methods in GnuPGContext to create GnuPGData instances.
43 | * Generates a new data object based on read/write stream access to a file.
44 | *
45 | * @param file your file
46 | * @throws IOException
47 | */
48 | protected GnuPGData(File file) throws IOException, GnuPGException {
49 | this(file, "r+");
50 | }
51 |
52 | /**
53 | * Use the factory methods in GnuPGContext to create GnuPGData instances.
54 | * Generates a new data object based on stream access to a file, with
55 | * settable open mode.
56 | *
57 | * @param file your file
58 | * @param mode the POSIX mode to open the file, e.g "r" or "w+"
59 | * @throws IOException
60 | */
61 | protected GnuPGData(File file, String mode) throws IOException, GnuPGException {
62 | if (file == null || !file.exists())
63 | throw new FileNotFoundException();
64 | if (!file.canRead())
65 | throw new IOException("Cannot read: " + file);
66 | if (!file.canRead())
67 | throw new IOException("Is a directory: " + file);
68 | setInternalRepresentation(gpgmeDataNewFromFilename(file.getCanonicalPath(), mode));
69 | }
70 |
71 | /**
72 | * Use the factory methods from GnuPGContext to create GnuPGData instances.
73 | * Generates a new data object containing the given String.
74 | *
75 | * @param str your string
76 | */
77 | protected GnuPGData(String str) {
78 | this(str.getBytes());
79 | }
80 |
81 | /**
82 | * Use the factory methods from GnuPGContext to create GnuPGData instances.
83 | * Generates a new Data-Object containing the given byte array.
84 | *
85 | * @param data your data
86 | */
87 | protected GnuPGData(byte[] data) {
88 | long gpgmeDataNewFromMem = gpgmeDataNewFromMem(data);
89 | setInternalRepresentation(gpgmeDataNewFromMem);
90 | }
91 |
92 | /*
93 | * FIXME: This is not working! Use it, and it will crash the JVM.
94 | */
95 | public void read(InputStream in) throws IOException {
96 | if (in != null) {
97 | gpgmeDataRead(getInternalRepresentation(), in);
98 | }
99 | }
100 |
101 | /**
102 | * Writes the data/string contained in this data object to the given Java
103 | * OutputStream.
104 | *
105 | * @param out OutputStream, where the data/string should be written
106 | */
107 | public void write(OutputStream out) throws IOException {
108 | if (out != null) {
109 | gpgmeDataWrite(getInternalRepresentation(), out);
110 | }
111 | }
112 |
113 | /**
114 | * Helper method to print out the data/string from this data object.
115 | *
116 | * @return String representation of the data contained in this data object
117 | * (expect weird results with binary data)
118 | */
119 | @Override
120 | public String toString() {
121 | String result = null;
122 | ByteArrayOutputStream baos = new ByteArrayOutputStream(this.size());
123 | try {
124 | this.write(baos);
125 | result = baos.toString();
126 | } catch (IOException ioe) {
127 | result = ioe.getMessage();
128 | }
129 | return result;
130 | }
131 |
132 | /**
133 | * This calls immediately the release method for the datastructure in the
134 | * underlying gpgme library. This method is called by the finalizer of the
135 | * class anyway, but you can call it yourself if you want to get rid of this
136 | * datastructure at once and don't want to wait for the non-deterministic
137 | * garbage-collector of the JVM.
138 | */
139 | public void destroy() {
140 | if (getInternalRepresentation() != 0) {
141 | gpgmeDataRelease(getInternalRepresentation());
142 | setInternalRepresentation(0);
143 | }
144 | }
145 |
146 | /**
147 | * Releases underlying datastructures. Simple calls the destroy() method.
148 | */
149 | @Override
150 | protected void finalize() {
151 | destroy();
152 | }
153 |
154 | public int size() {
155 | return gpgmeSize(getInternalRepresentation());
156 | }
157 |
158 | private native int gpgmeSize(long l);
159 |
160 | private native long gpgmeDataNewFromMem(byte[] plain);
161 |
162 | private native long gpgmeDataNewFromFilename(String filename, String mode);
163 |
164 | private native long gpgmeDataNew();
165 |
166 | private native void gpgmeDataWrite(long l, OutputStream out) throws IOException;
167 |
168 | private native void gpgmeDataRelease(long l);
169 |
170 | private native void gpgmeDataRead(long data, InputStream in) throws IOException;
171 | }
172 |
173 | /* Local Variables: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; End: */
174 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/GnuPGException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: GnuPGException.java,v 1.1 2005/01/24 13:56:58 stefan Exp $
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 |
15 | package com.freiheit.gnupg;
16 |
17 | /**
18 | * If the underlying gpgme library reports an error, this exception is thrown.
19 | * Wraps all errors messages from inside gpgme.
20 | *
21 | * @author Stefan Richter, stefan@freiheit.com
22 | */
23 | public class GnuPGException extends RuntimeException {
24 |
25 | private static final long serialVersionUID = -775599686124698560L;
26 |
27 | /**
28 | * This Exception is normally only thrown from within the native part of
29 | * this library.
30 | *
31 | * @param msg is an error message text from gpgme
32 | */
33 | GnuPGException(String msg) {
34 | super(msg);
35 | }
36 | }
37 |
38 | /* Local Variables: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; End: */
39 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/GnuPGGenkeyResult.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id$
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 | /**
15 | *
16 | */
17 |
18 | package com.freiheit.gnupg;
19 |
20 | /**
21 | * Class to handle Generate Key Results
22 | * Created on: 23. Mai 06
23 | *
24 | * @author Stefan Neumann
25 | * @version $Id$
26 | */
27 | /**
28 | * TODO: DESCRIBE ME
29 | * Created on: 23. Mai 06
30 | *
31 | * @author Stefan Neumann
32 | * @version $Id$
33 | */
34 | /**
35 | * TODO: DESCRIBE ME
36 | * Created on: 23. Mai 06
37 | *
38 | * @author Stefan Neumann
39 | * @version $Id$
40 | */
41 | public class GnuPGGenkeyResult {
42 |
43 | private String _fpr;
44 | private boolean _primary;
45 | private boolean _sub;
46 |
47 | protected GnuPGGenkeyResult() {
48 | }
49 |
50 | /**
51 | * This is the fingerprint of the key that was created. If both a primary
52 | * and a sub key were generated, the fingerprint of the primary key will be
53 | * returned. If the crypto engine does not provide the fingerprint, `it will
54 | * return a null pointer.
55 | *
56 | * @return fingerprint of the created key
57 | */
58 | public String getFpr() {
59 | return _fpr;
60 | }
61 |
62 | /**
63 | * This is a flag that is set to true if a primary key was created and to
64 | * false if not.
65 | *
66 | * @return flag, if a primary key was created
67 | */
68 | public boolean isPrimary() {
69 | return _primary;
70 | }
71 |
72 | /**
73 | * This is a flag that is set to true if a subkey was created and to false
74 | * if not.
75 | *
76 | * @return flag, if a subkey was created
77 | */
78 | public boolean isSub() {
79 | return _sub;
80 | }
81 | }
82 |
83 | /* Local Variables: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; End: */
84 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/GnuPGKey.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: GnuPGKey.java,v 1.1 2005/01/24 13:56:58 stefan Exp $
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 |
15 | package com.freiheit.gnupg;
16 |
17 | import java.util.ArrayList;
18 | import java.util.Date;
19 | import java.util.Iterator;
20 | import java.util.List;
21 |
22 | /**
23 | * Represents a key. You can manage and find keys using GnuPGContext. You can
24 | * not instantiate a key by yourself. You always need a context.
25 | *
26 | * @see com.freiheit.gnupg.GnuPGContext
27 | * @author Stefan Richter, stefan@freiheit.com
28 | */
29 | public class GnuPGKey extends GnuPGPeer {
30 | /**
31 | * This constructor is only called from within the JNI routines. This is for
32 | * example used, when a keylist search returns keys. This is how then each
33 | * key is instantiated on the java side from c.
34 | */
35 | protected GnuPGKey(long ptr) {
36 | // note that this is a pointer to an address in the gnupg-for-java shared lib
37 | setInternalRepresentation(ptr);
38 | }
39 |
40 | /**
41 | * Fetch a public key by its 16 hex char fingerprint String
42 | *
43 | * @param context - the current GnuPGContext
44 | * @param fingerprint - the 16 hex char fingerprint String
45 | */
46 | protected GnuPGKey(GnuPGContext context, String fingerprint) {
47 | setInternalRepresentation(gpgmeGetKey(context.getInternalRepresentation(), fingerprint,
48 | false));
49 | }
50 |
51 | /**
52 | * Fetch a key by its 16 hex char fingerprint String
53 | *
54 | * @param context the current GnuPGContext
55 | * @param fingerprint the 16 hex char fingerprint String
56 | * @param secret_only whether to return only secret keys
57 | */
58 | protected GnuPGKey(GnuPGContext context, String fingerprint, boolean secret_only) {
59 | setInternalRepresentation(gpgmeGetKey(context.getInternalRepresentation(), fingerprint,
60 | secret_only));
61 | }
62 |
63 | /**
64 | * Get the Name of the default key/userid.
65 | */
66 | public String getName() {
67 | return gpgmeGetName(getInternalRepresentation());
68 | }
69 |
70 | /**
71 | * Get the Email-Address of the default key/userid.
72 | */
73 | public String getEmail() {
74 | return gpgmeGetEmail(getInternalRepresentation());
75 | }
76 |
77 | /**
78 | * Get the Key-ID of the default key/userid.
79 | */
80 | public String getKeyID() {
81 | return gpgmeGetKeyID(getInternalRepresentation());
82 | }
83 |
84 | public Date getTimestamp() {
85 | return new Date(gpgmeGetTimestamp(getInternalRepresentation()) / 1000);
86 | }
87 |
88 | /**
89 | * Get the Fingerprint of the default key/userid.
90 | */
91 | public String getFingerprint() {
92 | return gpgmeGetFingerprint(getInternalRepresentation());
93 | }
94 |
95 | /**
96 | * Get the Comment of the default key/userid.
97 | */
98 | public String getComment() {
99 | return gpgmeGetComment(getInternalRepresentation());
100 | }
101 |
102 | /**
103 | * Get the User-ID of the default key/userid.
104 | */
105 | public String getUserID() {
106 | return gpgmeGetUserID(getInternalRepresentation());
107 | }
108 |
109 | public boolean canEncrypt() {
110 | return gpgmeCanEncrypt(getInternalRepresentation());
111 | }
112 |
113 | public boolean canSign() {
114 | return gpgmeCanSign(getInternalRepresentation());
115 | }
116 |
117 | public boolean canCertify() {
118 | return gpgmeCanCertify(getInternalRepresentation());
119 | }
120 |
121 | public boolean canAuthenticate() {
122 | return gpgmeCanAuthenticate(getInternalRepresentation());
123 | }
124 |
125 | public boolean isRevoked() {
126 | return gpgmeIsRevoked(getInternalRepresentation());
127 | }
128 |
129 | public boolean isExpired() {
130 | return gpgmeIsExpired(getInternalRepresentation());
131 | }
132 |
133 | public boolean isDisabled() {
134 | return gpgmeIsDisabled(getInternalRepresentation());
135 | }
136 |
137 | public boolean isInvalid() {
138 | return gpgmeIsInvalid(getInternalRepresentation());
139 | }
140 |
141 | public boolean isQualified() {
142 | return gpgmeIsQualified(getInternalRepresentation());
143 | }
144 |
145 | public boolean isSecret() {
146 | return gpgmeIsSecret(getInternalRepresentation());
147 | }
148 |
149 | public boolean hasSecretKey() {
150 | return gpgmeHasSecretKey(getInternalRepresentation());
151 | }
152 |
153 | /**
154 | * Lists all signatures of the default key/userid. Every key can have
155 | * multiple signatures. Signatures can be incomplete. This means, that not
156 | * all details (name, email-address etc.) were downloaded from a keyserver.
157 | * But you will at least see the key-id of the signature. Use GnuPG to
158 | * --refresh-keys if you want to see all signature details.
159 | *
160 | * Currently I am not supporting java generics for a type safe iterator,
161 | * because there are to many people still using jdk-1.4.x without generics
162 | * support. This will be changed on increasing demand.
163 | *
164 | *
165 | * @return Iterator of GnuPGSignature objects
166 | * @see com.freiheit.gnupg.GnuPGSignature
167 | */
168 | public Iterator getSignatures() {
169 | List siglist = null;
170 | GnuPGSignature sig = getSignature();
171 | while (sig != null) {
172 | if (siglist == null) {
173 | siglist = new ArrayList();
174 | }
175 | siglist.add(sig);
176 | sig = sig.getNextSignature();
177 | }
178 | if (siglist == null)
179 | return null;
180 | else
181 | return siglist.listIterator();
182 | }
183 |
184 | /**
185 | * Helper to list signatures in the toString()-method.
186 | */
187 | private String listSignatures() {
188 | Iterator iter = getSignatures();
189 | GnuPGSignature sig;
190 | StringBuffer buf = new StringBuffer();
191 |
192 | while (iter != null && iter.hasNext()) {
193 | sig = iter.next();
194 | buf.append("\t").append(sig).append("\n");
195 | }
196 |
197 | return buf.toString();
198 | }
199 |
200 | /**
201 | * Helper to get the head of the linked list of signatures from the native
202 | * space.
203 | */
204 | private GnuPGSignature getSignature() {
205 | GnuPGSignature result = null;
206 |
207 | // note that thhis is a pointer to an address in the gnupg-for-java shared
208 | // lib
209 | long ptr = gpgmeGetSignature(getInternalRepresentation());
210 |
211 | if (ptr != 0) {
212 | result = new GnuPGSignature(ptr);
213 | }
214 |
215 | return result;
216 | }
217 |
218 | /**
219 | * Return this key with all of its signatures.
220 | *
221 | * @return String multiline text with one key and 0..many signatures
222 | */
223 | public String toString() {
224 | StringBuffer buf = new StringBuffer();
225 | buf.append(getKeyID()).
226 | append(": ").
227 | append(getName()).
228 | append(", ").
229 | append(getComment()).
230 | append(", ").
231 | append(getEmail()).
232 | append(", ").
233 | append("[").
234 | append(getFingerprint()).
235 | append("]").
236 | append("\n").
237 | append(listSignatures());
238 | return buf.toString();
239 | }
240 |
241 | /**
242 | * This calls immediately the release method for the datastructure in the
243 | * underlying gpgme library. This method is called by the finalizer of the
244 | * class anyway, but you can call it yourself if you want to get rid of this
245 | * datastructure at once and don't want to wait for the non-deterministic
246 | * garbage-collector of the JVM.
247 | */
248 | public void destroy() {
249 | if (getInternalRepresentation() != 0) {
250 | gpgmeKeyUnref(getInternalRepresentation());
251 | setInternalRepresentation(0);
252 | }
253 | }
254 |
255 | /**
256 | * Releases underlying datastructures. Simple calls the destroy() method.
257 | */
258 | protected void finalize() {
259 | destroy();
260 | }
261 |
262 | private native long gpgmeGetKey(long context, String fingerprint, boolean secret);
263 |
264 | private native long gpgmeKeyUnref(long keyptr);
265 |
266 | private native String gpgmeGetName(long keyptr);
267 |
268 | private native String gpgmeGetEmail(long keyptr);
269 |
270 | private native String gpgmeGetKeyID(long keyptr);
271 |
272 | private native long gpgmeGetTimestamp(long keyptr);
273 |
274 | private native String gpgmeGetFingerprint(long keyptr);
275 |
276 | private native String gpgmeGetComment(long keyptr);
277 |
278 | private native String gpgmeGetUserID(long keyptr);
279 |
280 | private native long gpgmeGetSignature(long keyptr);
281 |
282 | private native boolean gpgmeCanEncrypt(long keyptr);
283 |
284 | private native boolean gpgmeCanSign(long keyptr);
285 |
286 | private native boolean gpgmeCanCertify(long keyptr);
287 |
288 | private native boolean gpgmeCanAuthenticate(long keyptr);
289 |
290 | private native boolean gpgmeIsRevoked(long keyptr);
291 |
292 | private native boolean gpgmeIsExpired(long keyptr);
293 |
294 | private native boolean gpgmeIsDisabled(long keyptr);
295 |
296 | private native boolean gpgmeIsInvalid(long keyptr);
297 |
298 | private native boolean gpgmeIsQualified(long keyptr);
299 |
300 | private native boolean gpgmeIsSecret(long keyptr);
301 |
302 | private native boolean gpgmeHasSecretKey(long keyptr);
303 |
304 | }
305 |
306 | /* Local Variables: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; End: */
307 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/GnuPGPeer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: GnuPGPeer.java,v 1.1 2005/01/24 13:56:58 stefan Exp $
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 |
15 | package com.freiheit.gnupg;
16 |
17 | /**
18 | * Peer Class to hold the pointers to connect to the underlying gpgme library.
19 | * This is the base class for all GnuPG*.java Classes. DO NOT USE IT. It is not
20 | * intended for library users.
21 | *
22 | * @author Stefan Richter, stefan@freiheit.com
23 | */
24 | public class GnuPGPeer {
25 | // note that this is a pointer to an address in the gnupg-for-java shared lib
26 | protected long _internalRepresentation;
27 |
28 | /**
29 | * DO NOT USE IT. This is only use from inside the library.
30 | */
31 | protected void setInternalRepresentation(long ptr) {
32 | // note that this is a pointer to an address in the gnupg-for-java shared lib
33 | _internalRepresentation = ptr;
34 | }
35 |
36 | /**
37 | * DO NOT USE IT. This is only use from inside the library.
38 | */
39 | protected long getInternalRepresentation() {
40 | // note that this is a pointer to an address in the gnupg-for-java shared lib
41 | return _internalRepresentation;
42 | }
43 | }
44 |
45 | /* Local Variables: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; End: */
46 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/GnuPGSignature.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: GnuPGSignature.java,v 1.1 2005/01/24 13:56:58 stefan Exp $
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 |
15 | package com.freiheit.gnupg;
16 |
17 | /**
18 | * Represents a signature from a key. A signature doesn't stand alone. It is
19 | * always bound to a key. So if you want to see a signature, you first have to
20 | * get a key via the GnuPGContext.
21 | *
22 | * This class accesses directly the corresponding structure "under the hood" and
23 | * stores no data in java members. This means, that every call of a method also
24 | * results in a native call to access gpgme memory.
25 | *
26 | * @author Stefan Richter, stefan@freiheit.com
27 | * @see com.freiheit.gnupg.GnuPGContext
28 | */
29 | public class GnuPGSignature extends GnuPGPeer {
30 |
31 | /**
32 | * This constructor is only called from GnuPGKey. It is called when the lib
33 | * browses thru the linked list of signatures of each key.
34 | *
35 | * @see com.freiheit.gnupg.GnuPGKey
36 | */
37 | protected GnuPGSignature(long ptr) {
38 | // note that this is a pointer to an address in the gnupg-for-java shared lib
39 | setInternalRepresentation(ptr);
40 | }
41 |
42 | /**
43 | * Is signature key revoked?
44 | *
45 | * @return true, if revoked
46 | */
47 | public boolean isRevoked() {
48 | return gpgmeGetRevoked(getInternalRepresentation());
49 | }
50 |
51 | /**
52 | * Is signature key expired?
53 | *
54 | * @return true, if expired
55 | */
56 | public boolean isExpired() {
57 | return gpgmeGetExpired(getInternalRepresentation());
58 | }
59 |
60 | /**
61 | * Is signature key invalid?
62 | *
63 | * @return true, if invalid
64 | */
65 | public boolean isInvalid() {
66 | return gpgmeGetInvalid(getInternalRepresentation());
67 | }
68 |
69 | /**
70 | * Is signature key exportable?
71 | *
72 | * @return true, if exportable
73 | */
74 | public boolean isExportable() {
75 | return gpgmeGetExportable(getInternalRepresentation());
76 | }
77 |
78 | /**
79 | * Returns Key-ID of signature key.
80 | *
81 | * @return Key-ID
82 | */
83 | public String getKeyID() {
84 | return gpgmeGetKeyID(getInternalRepresentation());
85 | }
86 |
87 | /**
88 | * Returns User-ID of signer.
89 | *
90 | * @return User-ID
91 | */
92 | public String getUserID() {
93 | return gpgmeGetUserID(getInternalRepresentation());
94 | }
95 |
96 | /**
97 | * Returns Name of signer.
98 | *
99 | * @return Name
100 | */
101 | public String getName() {
102 | return gpgmeGetName(getInternalRepresentation());
103 | }
104 |
105 | /**
106 | * Returns Email-Address of signer.
107 | *
108 | * @return Email-Address
109 | */
110 | public String getEmail() {
111 | return gpgmeGetEmail(getInternalRepresentation());
112 | }
113 |
114 | /**
115 | * Returns Comment.
116 | *
117 | * @return Comment
118 | */
119 | public String getComment() {
120 | return gpgmeGetComment(getInternalRepresentation());
121 | }
122 |
123 | /**
124 | * Checks, if all signature details are available.
125 | *
126 | * @return true, if more info than the keyid is available
127 | */
128 | public boolean hasDetails() {
129 | return !getName().equals("");
130 | }
131 |
132 | /**
133 | * String-Representation of this Signature.
134 | *
135 | * @return String single line of text with name, comment etc.
136 | */
137 | public String toString() {
138 | StringBuffer buf = new StringBuffer();
139 | buf.append(getKeyID());
140 | if (hasDetails()) {
141 | buf.append(", ");
142 | buf.append(getName());
143 | buf.append(", ");
144 | buf.append(getComment());
145 | buf.append(", ");
146 | buf.append(getEmail());
147 | }
148 | else {
149 | buf.append(" ");
150 | }
151 | return buf.toString();
152 | }
153 |
154 | /**
155 | * Gets the next pointer in the gpgme_key_sig_t structure.
156 | *
157 | * @return new GnuPGSignature-Object
158 | */
159 | protected GnuPGSignature getNextSignature() {
160 | GnuPGSignature result = null;
161 |
162 | // note that this is a pointer to an address in the gnupg-for-java shared lib
163 | long next = gpgmeGetNextSignature(getInternalRepresentation());
164 |
165 | if (next != 0) {
166 | result = new GnuPGSignature(next);
167 | }
168 |
169 | return result;
170 | }
171 |
172 | // native method declarations:
173 | private native boolean gpgmeGetRevoked(long l);
174 |
175 | private native boolean gpgmeGetExpired(long l);
176 |
177 | private native boolean gpgmeGetInvalid(long l);
178 |
179 | private native boolean gpgmeGetExportable(long l);
180 |
181 | private native String gpgmeGetKeyID(long l);
182 |
183 | private native String gpgmeGetUserID(long l);
184 |
185 | private native String gpgmeGetName(long l);
186 |
187 | private native String gpgmeGetEmail(long l);
188 |
189 | private native String gpgmeGetComment(long l);
190 |
191 | private native long gpgmeGetNextSignature(long l);
192 |
193 | }
194 |
195 | /* Local Variables: tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; End: */
196 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/package.html:
--------------------------------------------------------------------------------
1 |
2 | Main package.
3 |
4 | Java for GnuPG is free software; you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public Licenseas published by the Free Software Foundation; either version 2.1
6 | of the License, or (at your option) any later version.
7 |
8 | Please see COPYING in the source distribution for the complete licence.
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/com/freiheit/gnupg/tests/GnuPGTestSuite.java:
--------------------------------------------------------------------------------
1 | /*
2 | * $Id: GnuPGTestSuite.java,v 1.3 2006/06/14 15:53:12 sneumann Exp $
3 | * (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
4 | *
5 | * This file is part of Java for GnuPG (http://www.freiheit.com).
6 | *
7 | * Java for GnuPG is free software; you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License
9 | * as published by the Free Software Foundation; either version 2.1
10 | * of the License, or (at your option) any later version.
11 | *
12 | * Please see COPYING for the complete licence.
13 | */
14 |
15 | package com.freiheit.gnupg.tests;
16 |
17 | import java.util.Iterator;
18 |
19 | import junit.framework.Test;
20 | import junit.framework.TestCase;
21 | import junit.framework.TestSuite;
22 |
23 | import com.freiheit.gnupg.GnuPGContext;
24 | import com.freiheit.gnupg.GnuPGData;
25 | import com.freiheit.gnupg.GnuPGKey;
26 | import com.freiheit.gnupg.GnuPGSignature;
27 |
28 | /**
29 | * I will improve this TestSuite later, that everybody can run it without
30 | * specific fingerprints. It should behave like the tests in gpgme.
31 | *