LCOV - code coverage report
Current view: top level - agent - pkdecrypt.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 57 56.1 %
Date: 2016-09-12 12:29:17 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* pkdecrypt.c - public key decryption (well, acually using a secret key)
       2             :  *      Copyright (C) 2001, 2003 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       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.
      10             :  *
      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.
      15             :  *
      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/>.
      18             :  */
      19             : 
      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>
      29             : 
      30             : #include "agent.h"
      31             : 
      32             : 
      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.   The padding information is stored at R_PADDING with -1
      36             :    for not known.  */
      37             : int
      38         259 : agent_pkdecrypt (ctrl_t ctrl, const char *desc_text,
      39             :                  const unsigned char *ciphertext, size_t ciphertextlen,
      40             :                  membuf_t *outbuf, int *r_padding)
      41             : {
      42         259 :   gcry_sexp_t s_skey = NULL, s_cipher = NULL, s_plain = NULL;
      43         259 :   unsigned char *shadow_info = NULL;
      44             :   int rc;
      45         259 :   char *buf = NULL;
      46             :   size_t len;
      47             : 
      48         259 :   *r_padding = -1;
      49             : 
      50         259 :   if (!ctrl->have_keygrip)
      51             :     {
      52           0 :       log_error ("speculative decryption not yet supported\n");
      53           0 :       rc = gpg_error (GPG_ERR_NO_SECKEY);
      54           0 :       goto leave;
      55             :     }
      56             : 
      57         259 :   rc = gcry_sexp_sscan (&s_cipher, NULL, (char*)ciphertext, ciphertextlen);
      58         259 :   if (rc)
      59             :     {
      60           0 :       log_error ("failed to convert ciphertext: %s\n", gpg_strerror (rc));
      61           0 :       rc = gpg_error (GPG_ERR_INV_DATA);
      62           0 :       goto leave;
      63             :     }
      64             : 
      65         259 :   if (DBG_CRYPTO)
      66             :     {
      67           0 :       log_printhex ("keygrip:", ctrl->keygrip, 20);
      68           0 :       log_printhex ("cipher: ", ciphertext, ciphertextlen);
      69             :     }
      70         259 :   rc = agent_key_from_file (ctrl, NULL, desc_text,
      71         259 :                             ctrl->keygrip, &shadow_info,
      72             :                             CACHE_MODE_NORMAL, NULL, &s_skey, NULL);
      73         259 :   if (rc)
      74             :     {
      75           0 :       if (gpg_err_code (rc) != GPG_ERR_NO_SECKEY)
      76           0 :         log_error ("failed to read the secret key\n");
      77           0 :       goto leave;
      78             :     }
      79             : 
      80         259 :   if (shadow_info)
      81             :     { /* divert operation to the smartcard */
      82             : 
      83           0 :       if (!gcry_sexp_canon_len (ciphertext, ciphertextlen, NULL, NULL))
      84             :         {
      85           0 :           rc = gpg_error (GPG_ERR_INV_SEXP);
      86           0 :           goto leave;
      87             :         }
      88             : 
      89           0 :       rc = divert_pkdecrypt (ctrl, ciphertext, shadow_info,
      90             :                              &buf, &len, r_padding);
      91           0 :       if (rc)
      92             :         {
      93           0 :           log_error ("smartcard decryption failed: %s\n", gpg_strerror (rc));
      94           0 :           goto leave;
      95             :         }
      96             : 
      97           0 :       put_membuf_printf (outbuf, "(5:value%u:", (unsigned int)len);
      98           0 :       put_membuf (outbuf, buf, len);
      99           0 :       put_membuf (outbuf, ")", 2);
     100             :     }
     101             :   else
     102             :     { /* No smartcard, but a private key */
     103             : /*       if (DBG_CRYPTO ) */
     104             : /*         { */
     105             : /*           log_debug ("skey: "); */
     106             : /*           gcry_sexp_dump (s_skey); */
     107             : /*         } */
     108             : 
     109         259 :       rc = gcry_pk_decrypt (&s_plain, s_cipher, s_skey);
     110         259 :       if (rc)
     111             :         {
     112           0 :           log_error ("decryption failed: %s\n", gpg_strerror (rc));
     113           0 :           goto leave;
     114             :         }
     115             : 
     116         259 :       if (DBG_CRYPTO)
     117             :         {
     118           0 :           log_debug ("plain: ");
     119           0 :           gcry_sexp_dump (s_plain);
     120             :         }
     121         259 :       len = gcry_sexp_sprint (s_plain, GCRYSEXP_FMT_CANON, NULL, 0);
     122         259 :       assert (len);
     123         259 :       buf = xmalloc (len);
     124         259 :       len = gcry_sexp_sprint (s_plain, GCRYSEXP_FMT_CANON, buf, len);
     125         259 :       assert (len);
     126         259 :       if (*buf == '(')
     127          24 :         put_membuf (outbuf, buf, len);
     128             :       else
     129             :         {
     130             :           /* Old style libgcrypt: This is only an S-expression
     131             :              part. Turn it into a complete S-expression. */
     132         235 :           put_membuf (outbuf, "(5:value", 8);
     133         235 :           put_membuf (outbuf, buf, len);
     134         235 :           put_membuf (outbuf, ")", 2);
     135             :         }
     136             :     }
     137             : 
     138             : 
     139             :  leave:
     140         259 :   gcry_sexp_release (s_skey);
     141         259 :   gcry_sexp_release (s_plain);
     142         259 :   gcry_sexp_release (s_cipher);
     143         259 :   xfree (buf);
     144         259 :   xfree (shadow_info);
     145         259 :   return rc;
     146             : }

Generated by: LCOV version 1.11