2 * Copyright (C) 2001, 2002, 2003, 2007, 2008, 2010 Free Software
7 * This file is part of OpenCDK.
9 * The OpenCDK library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 /* Set a default cipher algorithm and a digest algorithm.
43 Even if AES and SHA-256 are not 'MUST' in the latest
44 OpenPGP draft, AES seems to be a good choice. */
45 #define DEFAULT_DIGEST_ALGO GNUTLS_DIG_SHA256
47 /* Use the passphrase callback in the handle HD or
48 return NULL if there is no valid callback. */
50 _cdk_passphrase_get (cdk_ctx_t hd, const char *prompt)
52 if (!hd || !hd->passphrase_cb)
54 return hd->passphrase_cb (hd->passphrase_cb_value, prompt);
59 handle_set_digest (cdk_ctx_t hd, int digest)
63 if (_gnutls_hash_get_algo_len (digest) <= 0)
64 digest = DEFAULT_DIGEST_ALGO;
65 hd->digest_algo = digest;
70 handle_set_s2k (cdk_ctx_t hd, int mode, int digest)
74 if (_gnutls_hash_get_algo_len (digest) <= 0)
75 digest = DEFAULT_DIGEST_ALGO;
76 if (mode != CDK_S2K_SIMPLE &&
77 mode != CDK_S2K_SALTED && mode != CDK_S2K_ITERSALTED)
78 mode = CDK_S2K_ITERSALTED;
80 hd->_s2k.digest_algo = digest;
85 handle_set_compress (cdk_ctx_t hd, int algo, int level)
89 if (algo < 0 || algo > 2)
91 hd->compress.algo = algo;
96 if (level > 0 && level < 10)
97 hd->compress.level = level;
99 hd->compress.level = 6;
105 * cdk_handle_control:
106 * @hd: session handle
107 * @action: flag which indicates whether put or get is requested
110 * Perform various control operations for the current session.
113 cdk_handle_control (cdk_ctx_t hd, int action, int cmd, ...)
116 int set = action == CDK_CTLF_SET, val = 0;
121 if (action != CDK_CTLF_SET && action != CDK_CTLF_GET)
123 va_start (arg_ptr, cmd);
128 hd->opt.armor = va_arg (arg_ptr, int);
135 handle_set_digest (hd, va_arg (arg_ptr, int));
137 val = hd->digest_algo;
140 case CDK_CTL_OVERWRITE:
142 hd->opt.overwrite = va_arg (arg_ptr, int);
144 val = hd->opt.overwrite;
147 case CDK_CTL_COMPRESS:
150 int algo = va_arg (arg_ptr, int);
151 int level = va_arg (arg_ptr, int);
152 handle_set_compress (hd, algo, level);
155 val = hd->compress.algo;
161 int mode = va_arg (arg_ptr, int);
162 int digest = va_arg (arg_ptr, int);
163 handle_set_s2k (hd, mode, digest);
169 case CDK_CTL_FORCE_DIGEST:
171 hd->opt.force_digest = va_arg (arg_ptr, int);
173 val = hd->opt.force_digest;
176 case CDK_CTL_BLOCKMODE_ON:
178 hd->opt.blockmode = va_arg (arg_ptr, int);
180 val = hd->opt.blockmode;
195 * @r_ctx: context to store the handle
197 * create a new session handle.
200 cdk_handle_new (cdk_ctx_t * r_ctx)
205 return CDK_Inv_Value;
207 c = cdk_calloc (1, sizeof *c);
209 return CDK_Out_Of_Core;
211 /* For S2K use the iterated and salted mode and use the
212 default digest and cipher algorithms. Because the MDC
213 feature will be used, the default cipher should use a
214 blocksize of 128 bits. */
215 c->_s2k.mode = CDK_S2K_ITERSALTED;
216 c->_s2k.digest_algo = DEFAULT_DIGEST_ALGO;
223 c->digest_algo = DEFAULT_DIGEST_ALGO;
225 c->compress.algo = CDK_COMPRESS_ZIP;
226 c->compress.level = 6;
234 * cdk_handle_set_keyring:
235 * @hd: session handle
236 * @type: public=0 or secret=1 keyring type
237 * @kringname: file name of the keyring which shall be used.
239 * Convenient function to set the keyring for the current session.
242 cdk_handle_set_keyring (cdk_ctx_t hd, int type, const char *kringname)
247 err = cdk_keydb_new_from_file (&db, type, kringname);
261 * cdk_handle_set_keydb:
262 * @hd: session handle
263 * @db: the database handle
265 * set the key database handle.
266 * the function automatically detects whether this is a public or
267 * secret keyring and the right handle is set.
270 cdk_handle_set_keydb (cdk_ctx_t hd, cdk_keydb_hd_t db)
274 if (_cdk_keydb_is_secret (db))
282 * cdk_handle_get_keydb:
283 * @hd: session handle
284 * @type: type of the keyring
286 * Return the keydb handle from the session handle.
287 * The caller should not free these handles.
290 cdk_handle_get_keydb (cdk_ctx_t hd, int type)
294 if (type == CDK_DBTYPE_PK_KEYRING)
296 else if (type == CDK_DBTYPE_SK_KEYRING)
303 * cdk_handle_set_passphrase_cb:
304 * @hd: session handle
305 * @cb: callback function
306 * @cb_value: the opaque value for the cb function
308 * set the passphrase callback.
311 cdk_handle_set_passphrase_cb (cdk_ctx_t hd,
312 char *(*cb) (void *opa, const char *prompt),
317 hd->passphrase_cb = cb;
318 hd->passphrase_cb_value = cb_value;
325 * Release the main handle.
328 cdk_handle_free (cdk_ctx_t hd)
333 /* If cdk_handle_set_keyring() were used, we need to free the key db
334 handles here because the handles are not controlled by the user. */
338 cdk_keydb_free (hd->db.pub);
340 cdk_keydb_free (hd->db.sec);
341 hd->db.pub = hd->db.sec = NULL;