2006-10-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / agent / pkdecrypt.c
blobe7fc14240b8ca8660a0d7bfb29c5281675e54bf0
1 /* pkdecrypt.c - public key decryption (well, acually using a secret key)
2 * Copyright (C) 2001, 2003 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <assert.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
32 #include "agent.h"
35 /* DECRYPT the stuff in ciphertext which is expected to be a S-Exp.
36 Try to get the key from CTRL and write the decoded stuff back to
37 OUTFP. */
38 int
39 agent_pkdecrypt (ctrl_t ctrl, const char *desc_text,
40 const unsigned char *ciphertext, size_t ciphertextlen,
41 membuf_t *outbuf)
43 gcry_sexp_t s_skey = NULL, s_cipher = NULL, s_plain = NULL;
44 unsigned char *shadow_info = NULL;
45 int rc;
46 char *buf = NULL;
47 size_t len;
49 if (!ctrl->have_keygrip)
51 log_error ("speculative decryption not yet supported\n");
52 rc = gpg_error (GPG_ERR_NO_SECKEY);
53 goto leave;
56 rc = gcry_sexp_sscan (&s_cipher, NULL, (char*)ciphertext, ciphertextlen);
57 if (rc)
59 log_error ("failed to convert ciphertext: %s\n", gpg_strerror (rc));
60 rc = gpg_error (GPG_ERR_INV_DATA);
61 goto leave;
64 if (DBG_CRYPTO)
66 log_printhex ("keygrip:", ctrl->keygrip, 20);
67 log_printhex ("cipher: ", ciphertext, ciphertextlen);
69 rc = agent_key_from_file (ctrl, desc_text,
70 ctrl->keygrip, &shadow_info,
71 CACHE_MODE_NORMAL, &s_skey);
72 if (rc)
74 log_error ("failed to read the secret key\n");
75 goto leave;
78 if (!s_skey)
79 { /* divert operation to the smartcard */
81 if (!gcry_sexp_canon_len (ciphertext, ciphertextlen, NULL, NULL))
83 rc = gpg_error (GPG_ERR_INV_SEXP);
84 goto leave;
87 rc = divert_pkdecrypt (ctrl, ciphertext, shadow_info, &buf, &len );
88 if (rc)
90 log_error ("smartcard decryption failed: %s\n", gpg_strerror (rc));
91 goto leave;
95 char tmpbuf[60];
97 sprintf (tmpbuf, "(5:value%u:", (unsigned int)len);
98 put_membuf (outbuf, tmpbuf, strlen (tmpbuf));
99 put_membuf (outbuf, buf, len);
100 put_membuf (outbuf, ")", 2);
103 else
104 { /* No smartcard, but a private key */
105 /* if (DBG_CRYPTO ) */
106 /* { */
107 /* log_debug ("skey: "); */
108 /* gcry_sexp_dump (s_skey); */
109 /* } */
111 rc = gcry_pk_decrypt (&s_plain, s_cipher, s_skey);
112 if (rc)
114 log_error ("decryption failed: %s\n", gpg_strerror (rc));
115 goto leave;
118 if (DBG_CRYPTO)
120 log_debug ("plain: ");
121 gcry_sexp_dump (s_plain);
123 len = gcry_sexp_sprint (s_plain, GCRYSEXP_FMT_CANON, NULL, 0);
124 assert (len);
125 buf = xmalloc (len);
126 len = gcry_sexp_sprint (s_plain, GCRYSEXP_FMT_CANON, buf, len);
127 assert (len);
128 if (*buf == '(')
129 put_membuf (outbuf, buf, len);
130 else
132 /* Old style libgcrypt: This is only an S-expression
133 part. Turn it into a complete S-expression. */
134 put_membuf (outbuf, "(5:value", 8);
135 put_membuf (outbuf, buf, len);
136 put_membuf (outbuf, ")", 2);
141 leave:
142 gcry_sexp_release (s_skey);
143 gcry_sexp_release (s_plain);
144 gcry_sexp_release (s_cipher);
145 xfree (buf);
146 xfree (shadow_info);
147 return rc;