ab7d35ab1c7bae1de002101fd308733d9d16448b
[ntk/apt.git] / apt-pkg / contrib / gpgv.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Helpers to deal with gpgv better and more easily
6
7 ##################################################################### */
8 /*}}}*/
9 #ifndef CONTRIB_GPGV_H
10 #define CONTRIB_GPGV_H
11
12 #include <string>
13 #include <vector>
14
15 #include <apt-pkg/fileutl.h>
16
17 #if __GNUC__ >= 4
18 #define APT_noreturn __attribute__ ((noreturn))
19 #else
20 #define APT_noreturn /* no support */
21 #endif
22
23 /** \brief generates and run the command to verify a file with gpgv
24 *
25 * If File and FileSig specify the same file it is assumed that we
26 * deal with a clear-signed message. In that case the file will be
27 * rewritten to be in a good-known format without uneeded whitespaces
28 * and additional messages (unsigned or signed).
29 *
30 * @param File is the message (unsigned or clear-signed)
31 * @param FileSig is the signature (detached or clear-signed)
32 */
33 void ExecGPGV(std::string const &File, std::string const &FileSig,
34 int const &statusfd, int fd[2]) APT_noreturn;
35 inline void ExecGPGV(std::string const &File, std::string const &FileSig,
36 int const &statusfd = -1) {
37 int fd[2];
38 ExecGPGV(File, FileSig, statusfd, fd);
39 };
40
41 #undef APT_noreturn
42
43 /** \brief Split an inline signature into message and signature
44 *
45 * Takes a clear-signed message and puts the first signed message
46 * in the content file and all signatures following it into the
47 * second. Unsigned messages, additional messages as well as
48 * whitespaces are discarded. The resulting files are suitable to
49 * be checked with gpgv.
50 *
51 * If one or all Fds are -1 they will not be used and the content
52 * which would have been written to them is discarded.
53 *
54 * The code doesn't support dash-encoded lines as these are not
55 * expected to be present in files we have to deal with.
56 *
57 * The content of the split files is undefined if the splitting was
58 * unsuccessful.
59 *
60 * Note that trying to split an unsigned file will fail, but
61 * not generate an error message.
62 *
63 * @param InFile is the clear-signed file
64 * @param ContentFile is the Fd the message will be written to
65 * @param ContentHeader is a list of all required Amored Headers for the message
66 * @param SignatureFile is the Fd all signatures will be written to
67 * @return true if the splitting was successful, false otherwise
68 */
69 bool SplitClearSignedFile(std::string const &InFile, int const ContentFile,
70 std::vector<std::string> * const ContentHeader, int const SignatureFile);
71
72 /** \brief recombines message and signature to an inline signature
73 *
74 * Reverses the splitting down by #SplitClearSignedFile by writing
75 * a well-formed clear-signed message without unsigned messages,
76 * additional signed messages or just trailing whitespaces
77 *
78 * @param OutFile will be clear-signed file
79 * @param ContentFile is the Fd the message will be read from
80 * @param ContentHeader is a list of all required Amored Headers for the message
81 * @param SignatureFile is the Fd all signatures will be read from
82 */
83 bool RecombineToClearSignedFile(std::string const &OutFile, int const ContentFile,
84 std::vector<std::string> const &ContentHeader, int const SignatureFile);
85
86 /** \brief open a file which might be clear-signed
87 *
88 * This method tries to extract the (signed) message of a file.
89 * If the file isn't signed it will just open the given filename.
90 * Otherwise the message is extracted to a temporary file which
91 * will be opened instead.
92 *
93 * @param ClearSignedFileName is the name of the file to open
94 * @param[out] MessageFile is the FileFd in which the file will be opened
95 * @return true if opening was successful, otherwise false
96 */
97 bool OpenMaybeClearSignedFile(std::string const &ClearSignedFileName, FileFd &MessageFile);
98
99 #endif