Updated the german translation
[gnupg.git] / agent / pkdecrypt.c
blob9e1c47d16ae7abf55351f983407fc29f0de5b3bd
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <assert.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
30 #include "agent.h"
33 /* DECRYPT the stuff in ciphertext which is expected to be a S-Exp.
34 Try to get the key from CTRL and write the decoded stuff back to
35 OUTFP. */
36 int
37 agent_pkdecrypt (ctrl_t ctrl, const char *desc_text,
38 const unsigned char *ciphertext, size_t ciphertextlen,
39 membuf_t *outbuf)
41 gcry_sexp_t s_skey = NULL, s_cipher = NULL, s_plain = NULL;
42 unsigned char *shadow_info = NULL;
43 int rc;
44 char *buf = NULL;
45 size_t len;
47 if (!ctrl->have_keygrip)
49 log_error ("speculative decryption not yet supported\n");
50 rc = gpg_error (GPG_ERR_NO_SECKEY);
51 goto leave;
54 rc = gcry_sexp_sscan (&s_cipher, NULL, (char*)ciphertext, ciphertextlen);
55 if (rc)
57 log_error ("failed to convert ciphertext: %s\n", gpg_strerror (rc));
58 rc = gpg_error (GPG_ERR_INV_DATA);
59 goto leave;
62 if (DBG_CRYPTO)
64 log_printhex ("keygrip:", ctrl->keygrip, 20);
65 log_printhex ("cipher: ", ciphertext, ciphertextlen);
67 rc = agent_key_from_file (ctrl, desc_text,
68 ctrl->keygrip, &shadow_info,
69 CACHE_MODE_NORMAL, NULL, &s_skey);
70 if (rc)
72 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
73 rc = gpg_error (GPG_ERR_NO_SECKEY);
74 else
75 log_error ("failed to read the secret key\n");
76 goto leave;
79 if (!s_skey)
80 { /* divert operation to the smartcard */
82 if (!gcry_sexp_canon_len (ciphertext, ciphertextlen, NULL, NULL))
84 rc = gpg_error (GPG_ERR_INV_SEXP);
85 goto leave;
88 rc = divert_pkdecrypt (ctrl, ciphertext, shadow_info, &buf, &len );
89 if (rc)
91 log_error ("smartcard decryption failed: %s\n", gpg_strerror (rc));
92 goto leave;
96 char tmpbuf[60];
98 sprintf (tmpbuf, "(5:value%u:", (unsigned int)len);
99 put_membuf (outbuf, tmpbuf, strlen (tmpbuf));
100 put_membuf (outbuf, buf, len);
101 put_membuf (outbuf, ")", 2);
104 else
105 { /* No smartcard, but a private key */
106 /* if (DBG_CRYPTO ) */
107 /* { */
108 /* log_debug ("skey: "); */
109 /* gcry_sexp_dump (s_skey); */
110 /* } */
112 rc = gcry_pk_decrypt (&s_plain, s_cipher, s_skey);
113 if (rc)
115 log_error ("decryption failed: %s\n", gpg_strerror (rc));
116 goto leave;
119 if (DBG_CRYPTO)
121 log_debug ("plain: ");
122 gcry_sexp_dump (s_plain);
124 len = gcry_sexp_sprint (s_plain, GCRYSEXP_FMT_CANON, NULL, 0);
125 assert (len);
126 buf = xmalloc (len);
127 len = gcry_sexp_sprint (s_plain, GCRYSEXP_FMT_CANON, buf, len);
128 assert (len);
129 if (*buf == '(')
130 put_membuf (outbuf, buf, len);
131 else
133 /* Old style libgcrypt: This is only an S-expression
134 part. Turn it into a complete S-expression. */
135 put_membuf (outbuf, "(5:value", 8);
136 put_membuf (outbuf, buf, len);
137 put_membuf (outbuf, ")", 2);
142 leave:
143 gcry_sexp_release (s_skey);
144 gcry_sexp_release (s_plain);
145 gcry_sexp_release (s_cipher);
146 xfree (buf);
147 xfree (shadow_info);
148 return rc;