2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: Header for libfko.
10 * Copyright 2009-2010 Damien Stuart (dstuart@dstuart.org)
12 * License (GNU Public License):
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 *****************************************************************************
36 #include "rijndael.h" /* For encryption modes */
44 #define DLL_API __declspec(dllexport)
47 #define DLL_API __declspec(dllimport)
58 #define FKO_PROTOCOL_VERSION "1.9.12" /* The fwknop protocol version */
60 /* Supported FKO Message types...
66 FKO_CLIENT_TIMEOUT_ACCESS_MSG,
67 FKO_CLIENT_TIMEOUT_NAT_ACCESS_MSG,
68 FKO_LOCAL_NAT_ACCESS_MSG,
69 FKO_CLIENT_TIMEOUT_LOCAL_NAT_ACCESS_MSG,
70 FKO_LAST_MSG_TYPE /* Always leave this as the last one */
73 /* Supported digest types...
76 FKO_DIGEST_INVALID_DATA = -1,
77 FKO_DIGEST_UNKNOWN = 0,
83 FKO_LAST_DIGEST_TYPE /* Always leave this as the last one */
86 /* Supported encryption types...
89 FKO_ENCRYPTION_INVALID_DATA = -1,
90 FKO_ENCRYPTION_UNKNOWN = 0,
91 FKO_ENCRYPTION_RIJNDAEL,
93 FKO_LAST_ENCRYPTION_TYPE /* Always leave this as the last one */
94 } fko_encryption_type_t;
96 /* Symmetric encryption modes derived from rijndael.h
99 FKO_ENC_MODE_UNKNOWN = 0,
100 FKO_ENC_MODE_ECB = MODE_ECB,
101 FKO_ENC_MODE_CBC = MODE_CBC,
102 FKO_ENC_MODE_CFB = MODE_CFB,
103 FKO_ENC_MODE_PCBC = MODE_PCBC,
104 FKO_ENC_MODE_OFB = MODE_OFB,
105 FKO_ENC_MODE_CTR = MODE_CTR,
106 FKO_ENC_MODE_ASYMMETRIC, /* placeholder when GPG is used */
107 FKO_LAST_ENC_MODE /* Always leave this as the last one */
108 } fko_encryption_mode_t;
112 * Note: If you change this list in any way, please be sure to make the
113 * appropriate corresponding change to the error message list in
118 FKO_ERROR_CTX_NOT_INITIALIZED,
119 FKO_ERROR_MEMORY_ALLOCATION,
120 FKO_ERROR_FILESYSTEM_OPERATION,
121 FKO_ERROR_INVALID_DATA,
122 FKO_ERROR_DATA_TOO_LARGE,
123 FKO_ERROR_USERNAME_UNKNOWN,
124 FKO_ERROR_INCOMPLETE_SPA_DATA,
125 FKO_ERROR_MISSING_ENCODED_DATA,
126 FKO_ERROR_INVALID_DIGEST_TYPE,
127 FKO_ERROR_INVALID_ALLOW_IP,
128 FKO_ERROR_INVALID_SPA_COMMAND_MSG,
129 FKO_ERROR_INVALID_SPA_ACCESS_MSG,
130 FKO_ERROR_INVALID_SPA_NAT_ACCESS_MSG,
131 FKO_ERROR_INVALID_ENCRYPTION_TYPE,
132 FKO_ERROR_WRONG_ENCRYPTION_TYPE,
133 FKO_ERROR_DECRYPTION_SIZE,
134 FKO_ERROR_DECRYPTION_FAILURE,
135 FKO_ERROR_DIGEST_VERIFICATION_FAILED,
136 FKO_ERROR_UNSUPPORTED_FEATURE,
139 /* Start GPGME-related errors */
141 FKO_ERROR_MISSING_GPG_KEY_DATA,
142 FKO_ERROR_GPGME_NO_OPENPGP,
143 FKO_ERROR_GPGME_CONTEXT,
144 FKO_ERROR_GPGME_PLAINTEXT_DATA_OBJ,
145 FKO_ERROR_GPGME_SET_PROTOCOL,
146 FKO_ERROR_GPGME_CIPHER_DATA_OBJ,
147 FKO_ERROR_GPGME_BAD_PASSPHRASE,
148 FKO_ERROR_GPGME_ENCRYPT_SIGN,
149 FKO_ERROR_GPGME_CONTEXT_SIGNER_KEY,
150 FKO_ERROR_GPGME_SIGNER_KEYLIST_START,
151 FKO_ERROR_GPGME_SIGNER_KEY_NOT_FOUND,
152 FKO_ERROR_GPGME_SIGNER_KEY_AMBIGUOUS,
153 FKO_ERROR_GPGME_ADD_SIGNER,
154 FKO_ERROR_GPGME_CONTEXT_RECIPIENT_KEY,
155 FKO_ERROR_GPGME_RECIPIENT_KEYLIST_START,
156 FKO_ERROR_GPGME_RECIPIENT_KEY_NOT_FOUND,
157 FKO_ERROR_GPGME_RECIPIENT_KEY_AMBIGUOUS,
158 FKO_ERROR_GPGME_DECRYPT_FAILED,
159 FKO_ERROR_GPGME_DECRYPT_UNSUPPORTED_ALGORITHM,
160 FKO_ERROR_GPGME_BAD_GPG_EXE,
161 FKO_ERROR_GPGME_BAD_HOME_DIR,
162 FKO_ERROR_GPGME_SET_HOME_DIR,
163 FKO_ERROR_GPGME_NO_SIGNATURE,
164 FKO_ERROR_GPGME_BAD_SIGNATURE,
165 FKO_ERROR_GPGME_SIGNATURE_VERIFY_DISABLED,
170 /* Macro that returns true if the given error code is a gpg-related error.
172 #define IS_GPG_ERROR(x) (x > GPGME_ERR_START && x < FKO_LAST_ERROR)
176 #define FKO_DEFAULT_MSG_TYPE FKO_ACCESS_MSG
177 #define FKO_DEFAULT_DIGEST FKO_DIGEST_SHA256
178 #define FKO_DEFAULT_ENCRYPTION FKO_ENCRYPTION_RIJNDAEL
179 #define FKO_DEFAULT_ENC_MODE MODE_CBC
181 /* The context holds the global state and config options, as
182 * well as some intermediate results during processing. This
183 * is an opaque pointer.
186 typedef struct fko_context *fko_ctx_t;
188 /* Some gpg-specifc data types and constants.
193 FKO_GPG_NO_SIG_VERIFY_SIGS = 0x01,
194 FKO_GPG_ALLOW_BAD_SIG = 0x02,
195 FKO_GPG_NO_SIG_INFO = 0x04,
196 FKO_GPG_ALLOW_EXPIRED_SIG = 0x08,
197 FKO_GPG_ALLOW_REVOKED_SIG = 0x10
200 #define FKO_GPG_GOOD_SIGSUM 3
202 #endif /* HAVE_LIBGPGME */
204 /* Function prototypes */
208 DLL_API int fko_new(fko_ctx_t *ctx);
209 DLL_API int fko_new_with_data(fko_ctx_t *ctx, const char *enc_msg, const char *dec_key,
210 int encryption_mode);
211 DLL_API void fko_destroy(fko_ctx_t ctx);
212 DLL_API int fko_spa_data_final(fko_ctx_t ctx, const char *enc_key);
215 /* Set context data functions
217 DLL_API int fko_set_rand_value(fko_ctx_t ctx, const char *val);
218 DLL_API int fko_set_username(fko_ctx_t ctx, const char *spoof_user);
219 DLL_API int fko_set_timestamp(fko_ctx_t ctx, const int offset);
220 DLL_API int fko_set_spa_message_type(fko_ctx_t ctx, const short msg_type);
221 DLL_API int fko_set_spa_message(fko_ctx_t ctx, const char *msg_string);
222 DLL_API int fko_set_spa_nat_access(fko_ctx_t ctx, const char *nat_access);
223 DLL_API int fko_set_spa_server_auth(fko_ctx_t ctx, const char *server_auth);
224 DLL_API int fko_set_spa_client_timeout(fko_ctx_t ctx, const int timeout);
225 DLL_API int fko_set_spa_digest_type(fko_ctx_t ctx, const short digest_type);
226 DLL_API int fko_set_spa_digest(fko_ctx_t ctx);
227 DLL_API int fko_set_raw_spa_digest_type(fko_ctx_t ctx, const short raw_digest_type);
228 DLL_API int fko_set_raw_spa_digest(fko_ctx_t ctx);
229 DLL_API int fko_set_spa_encryption_type(fko_ctx_t ctx, const short encrypt_type);
230 DLL_API int fko_set_spa_encryption_mode(fko_ctx_t ctx, const int encrypt_mode);
231 DLL_API int fko_set_spa_data(fko_ctx_t ctx, const char *enc_msg);
233 /* Data processing and misc utility functions
235 DLL_API const char* fko_errstr(const int err_code);
236 DLL_API int fko_encryption_type(const char *enc_data);
238 DLL_API int fko_encode_spa_data(fko_ctx_t ctx);
239 DLL_API int fko_decode_spa_data(fko_ctx_t ctx);
240 DLL_API int fko_encrypt_spa_data(fko_ctx_t ctx, const char *enc_key);
241 DLL_API int fko_decrypt_spa_data(fko_ctx_t ctx, const char *dec_key);
243 DLL_API int fko_get_encoded_data(fko_ctx_t ctx, char **enc_data);
246 /* Get context data functions
248 DLL_API int fko_get_rand_value(fko_ctx_t ctx, char **rand_val);
249 DLL_API int fko_get_username(fko_ctx_t ctx, char **username);
250 DLL_API int fko_get_timestamp(fko_ctx_t ctx, time_t *ts);
251 DLL_API int fko_get_spa_message_type(fko_ctx_t ctx, short *spa_msg);
252 DLL_API int fko_get_spa_message(fko_ctx_t ctx, char **spa_message);
253 DLL_API int fko_get_spa_nat_access(fko_ctx_t ctx, char **nat_access);
254 DLL_API int fko_get_spa_server_auth(fko_ctx_t ctx, char **server_auth);
255 DLL_API int fko_get_spa_client_timeout(fko_ctx_t ctx, int *client_timeout);
256 DLL_API int fko_get_spa_digest_type(fko_ctx_t ctx, short *spa_digest_type);
257 DLL_API int fko_get_raw_spa_digest_type(fko_ctx_t ctx, short *raw_spa_digest_type);
258 DLL_API int fko_get_spa_digest(fko_ctx_t ctx, char **spa_digest);
259 DLL_API int fko_get_raw_spa_digest(fko_ctx_t ctx, char **raw_spa_digest);
260 DLL_API int fko_get_spa_encryption_type(fko_ctx_t ctx, short *spa_enc_type);
261 DLL_API int fko_get_spa_encryption_mode(fko_ctx_t ctx, int *spa_enc_mode);
262 DLL_API int fko_get_spa_data(fko_ctx_t ctx, char **spa_data);
264 DLL_API int fko_get_version(fko_ctx_t ctx, char **version);
266 /* GPG-related functions */
267 DLL_API int fko_set_gpg_exe(fko_ctx_t ctx, const char *gpg_exe);
268 DLL_API int fko_get_gpg_exe(fko_ctx_t ctx, char **gpg_exe);
270 DLL_API int fko_set_gpg_recipient(fko_ctx_t ctx, const char *recip);
271 DLL_API int fko_get_gpg_recipient(fko_ctx_t ctx, char **recip);
272 DLL_API int fko_set_gpg_signer(fko_ctx_t ctx, const char *signer);
273 DLL_API int fko_get_gpg_signer(fko_ctx_t ctx, char **signer);
274 DLL_API int fko_set_gpg_home_dir(fko_ctx_t ctx, const char *gpg_home_dir);
275 DLL_API int fko_get_gpg_home_dir(fko_ctx_t ctx, char **gpg_home_dir);
277 DLL_API const char* fko_gpg_errstr(fko_ctx_t ctx);
279 DLL_API int fko_set_gpg_signature_verify(fko_ctx_t ctx, const unsigned char val);
280 DLL_API int fko_get_gpg_signature_verify(fko_ctx_t ctx, unsigned char *val);
281 DLL_API int fko_set_gpg_ignore_verify_error(fko_ctx_t ctx, const unsigned char val);
282 DLL_API int fko_get_gpg_ignore_verify_error(fko_ctx_t ctx, unsigned char *val);
284 DLL_API int fko_get_gpg_signature_id(fko_ctx_t ctx, char **sig_id);
285 DLL_API int fko_get_gpg_signature_fpr(fko_ctx_t ctx, char **sig_fpr);
286 DLL_API int fko_get_gpg_signature_summary(fko_ctx_t ctx, int *sigsum);
287 DLL_API int fko_get_gpg_signature_status(fko_ctx_t ctx, int *sigstat);
289 DLL_API int fko_gpg_signature_id_match(fko_ctx_t ctx, const char *id, unsigned char *result);
290 DLL_API int fko_gpg_signature_fpr_match(fko_ctx_t ctx, const char *fpr, unsigned char *result);