2004-03-16 19:59:21 +01:00
|
|
|
/* app-help.c - Application helper functions
|
2009-05-08 17:07:45 +02:00
|
|
|
* Copyright (C) 2004, 2009 Free Software Foundation, Inc.
|
2004-03-16 19:59:21 +01:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2004-03-16 19:59:21 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2004-03-16 19:59:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "scdaemon.h"
|
|
|
|
#include "iso7816.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/tlv.h"
|
2004-03-16 19:59:21 +01:00
|
|
|
|
2009-05-08 17:07:45 +02:00
|
|
|
|
2019-02-26 16:42:50 +01:00
|
|
|
/* Count the number of bits, assuming that A represents an unsigned
|
|
|
|
* big integer of length LEN bytes. If A is NULL a length of 0 is
|
|
|
|
* returned. */
|
2009-05-08 17:07:45 +02:00
|
|
|
unsigned int
|
|
|
|
app_help_count_bits (const unsigned char *a, size_t len)
|
|
|
|
{
|
|
|
|
unsigned int n = len * 8;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!a)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (; len && !*a; len--, a++, n -=8)
|
|
|
|
;
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
for (i=7; i && !(*a & (1<<i)); i--)
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 17:31:09 +02:00
|
|
|
/* Return the KEYGRIP for the canonical encoded public key (PK,PKLEN)
|
|
|
|
* as an hex encoded string in the user provided buffer HEXKEYGRIP
|
2020-03-31 19:55:15 +02:00
|
|
|
* which must be of at least 41 bytes. If R_PKEY is not NULL and the
|
2020-05-05 08:07:11 +02:00
|
|
|
* function succeeded, the S-expression representing the key is stored
|
|
|
|
* there. The caller needs to call gcry_sexp_release on that. If
|
|
|
|
* R_ALGO is not NULL the public key algorithm id of Libgcrypt is
|
2020-09-21 14:47:53 +02:00
|
|
|
* stored there. If R_ALGOSTR is not NULL and the function succeeds a
|
2021-04-08 19:27:25 +02:00
|
|
|
* newly allocated algo string (e.g. "rsa2048") is stored there.
|
|
|
|
* HEXKEYGRIP may be NULL if the caller is not interested in it. */
|
2004-03-16 19:59:21 +01:00
|
|
|
gpg_error_t
|
2020-03-31 19:55:15 +02:00
|
|
|
app_help_get_keygrip_string_pk (const void *pk, size_t pklen, char *hexkeygrip,
|
2020-09-21 14:47:53 +02:00
|
|
|
gcry_sexp_t *r_pkey, int *r_algo,
|
|
|
|
char **r_algostr)
|
2004-03-16 19:59:21 +01:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_sexp_t s_pkey;
|
2019-04-03 17:31:09 +02:00
|
|
|
unsigned char array[KEYGRIP_LEN];
|
2004-03-16 19:59:21 +01:00
|
|
|
|
2020-03-31 19:55:15 +02:00
|
|
|
if (r_pkey)
|
|
|
|
*r_pkey = NULL;
|
2020-09-21 14:47:53 +02:00
|
|
|
if (r_algostr)
|
|
|
|
*r_algostr = NULL;
|
2020-03-31 19:55:15 +02:00
|
|
|
|
2019-04-03 17:31:09 +02:00
|
|
|
err = gcry_sexp_sscan (&s_pkey, NULL, pk, pklen);
|
2004-03-16 19:59:21 +01:00
|
|
|
if (err)
|
|
|
|
return err; /* Can't parse that S-expression. */
|
2021-03-29 14:48:11 +02:00
|
|
|
|
2021-04-08 19:27:25 +02:00
|
|
|
if (hexkeygrip && !gcry_pk_get_keygrip (s_pkey, array))
|
2004-03-16 19:59:21 +01:00
|
|
|
{
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
return gpg_error (GPG_ERR_GENERAL); /* Failed to calculate the keygrip.*/
|
|
|
|
}
|
2020-03-31 19:55:15 +02:00
|
|
|
|
2020-05-05 08:07:11 +02:00
|
|
|
if (r_algo)
|
|
|
|
*r_algo = get_pk_algo_from_key (s_pkey);
|
|
|
|
|
2020-09-21 14:47:53 +02:00
|
|
|
if (r_algostr)
|
|
|
|
{
|
|
|
|
*r_algostr = pubkey_algo_string (s_pkey, NULL);
|
|
|
|
if (!*r_algostr)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
gcry_sexp_release (s_pkey);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:55:15 +02:00
|
|
|
if (r_pkey)
|
|
|
|
*r_pkey = s_pkey;
|
|
|
|
else
|
|
|
|
gcry_sexp_release (s_pkey);
|
2004-03-16 19:59:21 +01:00
|
|
|
|
2021-04-08 19:27:25 +02:00
|
|
|
if (hexkeygrip)
|
|
|
|
bin2hex (array, KEYGRIP_LEN, hexkeygrip);
|
2004-03-16 19:59:21 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 17:31:09 +02:00
|
|
|
/* Return the KEYGRIP for the certificate CERT as an hex encoded
|
2020-03-31 19:55:15 +02:00
|
|
|
* string in the user provided buffer HEXKEYGRIP which must be of at
|
|
|
|
* least 41 bytes. If R_PKEY is not NULL and the function succeeded,
|
|
|
|
* the S-expression representing the key is stored there. The caller
|
2020-05-05 08:07:11 +02:00
|
|
|
* needs to call gcry_sexp_release on that. If R_ALGO is not NULL the
|
|
|
|
* public key algorithm id of Libgcrypt is stored there. */
|
2019-04-03 17:31:09 +02:00
|
|
|
gpg_error_t
|
2020-03-31 19:55:15 +02:00
|
|
|
app_help_get_keygrip_string (ksba_cert_t cert, char *hexkeygrip,
|
2020-05-05 08:07:11 +02:00
|
|
|
gcry_sexp_t *r_pkey, int *r_algo)
|
2019-04-03 17:31:09 +02:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
ksba_sexp_t p;
|
|
|
|
size_t n;
|
|
|
|
|
2020-03-31 19:55:15 +02:00
|
|
|
if (r_pkey)
|
|
|
|
*r_pkey = NULL;
|
|
|
|
|
2019-04-03 17:31:09 +02:00
|
|
|
p = ksba_cert_get_public_key (cert);
|
|
|
|
if (!p)
|
|
|
|
return gpg_error (GPG_ERR_BUG);
|
|
|
|
n = gcry_sexp_canon_len (p, 0, NULL, NULL);
|
|
|
|
if (!n)
|
|
|
|
return gpg_error (GPG_ERR_INV_SEXP);
|
2020-05-05 08:07:11 +02:00
|
|
|
err = app_help_get_keygrip_string_pk ((void*)p, n, hexkeygrip,
|
2020-09-21 14:47:53 +02:00
|
|
|
r_pkey, r_algo, NULL);
|
2019-04-03 17:31:09 +02:00
|
|
|
ksba_free (p);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-26 16:42:50 +01:00
|
|
|
gpg_error_t
|
|
|
|
app_help_pubkey_from_cert (const void *cert, size_t certlen,
|
|
|
|
unsigned char **r_pk, size_t *r_pklen)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
ksba_cert_t kc;
|
2021-03-29 14:48:11 +02:00
|
|
|
unsigned char *pk, *fixed_pk;
|
|
|
|
size_t pklen, fixed_pklen;
|
2019-02-26 16:42:50 +01:00
|
|
|
|
|
|
|
*r_pk = NULL;
|
|
|
|
*r_pklen = 0;
|
|
|
|
|
2021-03-29 14:48:11 +02:00
|
|
|
pk = NULL; /*(avoid cc warning)*/
|
|
|
|
|
2019-02-26 16:42:50 +01:00
|
|
|
err = ksba_cert_new (&kc);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = ksba_cert_init_from_mem (kc, cert, certlen);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
pk = ksba_cert_get_public_key (kc);
|
|
|
|
if (!pk)
|
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_NO_PUBKEY);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
pklen = gcry_sexp_canon_len (pk, 0, NULL, &err);
|
|
|
|
|
2021-03-29 14:48:11 +02:00
|
|
|
err = uncompress_ecc_q_in_canon_sexp (pk, pklen, &fixed_pk, &fixed_pklen);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
if (fixed_pk)
|
|
|
|
{
|
|
|
|
ksba_free (pk); pk = NULL;
|
|
|
|
pk = fixed_pk;
|
|
|
|
pklen = fixed_pklen;
|
|
|
|
}
|
|
|
|
|
2019-02-26 16:42:50 +01:00
|
|
|
leave:
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
*r_pk = pk;
|
|
|
|
*r_pklen = pklen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ksba_free (pk);
|
|
|
|
ksba_cert_release (kc);
|
|
|
|
return err;
|
|
|
|
}
|
2004-03-16 19:59:21 +01:00
|
|
|
|
|
|
|
/* Given the SLOT and the File ID FID, return the length of the
|
|
|
|
certificate contained in that file. Returns 0 if the file does not
|
|
|
|
exists or does not contain a certificate. If R_CERTOFF is not
|
|
|
|
NULL, the length the header will be stored at this address; thus to
|
|
|
|
parse the X.509 certificate a read should start at that offset.
|
|
|
|
|
|
|
|
On success the file is still selected.
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
app_help_read_length_of_cert (int slot, int fid, size_t *r_certoff)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
unsigned char *buffer;
|
|
|
|
const unsigned char *p;
|
|
|
|
size_t buflen, n;
|
|
|
|
int class, tag, constructed, ndef;
|
|
|
|
size_t resultlen, objlen, hdrlen;
|
|
|
|
|
2017-03-06 05:39:46 +01:00
|
|
|
err = iso7816_select_file (slot, fid, 0);
|
2004-03-16 19:59:21 +01:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_info ("error selecting FID 0x%04X: %s\n", fid, gpg_strerror (err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = iso7816_read_binary (slot, 0, 32, &buffer, &buflen);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_info ("error reading certificate from FID 0x%04X: %s\n",
|
|
|
|
fid, gpg_strerror (err));
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-03-16 19:59:21 +01:00
|
|
|
if (!buflen || *buffer == 0xff)
|
|
|
|
{
|
|
|
|
log_info ("no certificate contained in FID 0x%04X\n", fid);
|
|
|
|
xfree (buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
n = buflen;
|
|
|
|
err = parse_ber_header (&p, &n, &class, &tag, &constructed,
|
|
|
|
&ndef, &objlen, &hdrlen);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_info ("error parsing certificate in FID 0x%04X: %s\n",
|
|
|
|
fid, gpg_strerror (err));
|
|
|
|
xfree (buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All certificates should commence with a SEQUENCE except for the
|
|
|
|
special ROOT CA which are enclosed in a SET. */
|
|
|
|
if ( !(class == CLASS_UNIVERSAL && constructed
|
|
|
|
&& (tag == TAG_SEQUENCE || tag == TAG_SET)))
|
|
|
|
{
|
2009-03-30 14:46:06 +02:00
|
|
|
log_info ("data at FID 0x%04X does not look like a certificate\n", fid);
|
2004-03-16 19:59:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
resultlen = objlen + hdrlen;
|
|
|
|
if (r_certoff)
|
|
|
|
{
|
|
|
|
/* The callers want the offset to the actual certificate. */
|
|
|
|
*r_certoff = hdrlen;
|
|
|
|
|
|
|
|
err = parse_ber_header (&p, &n, &class, &tag, &constructed,
|
|
|
|
&ndef, &objlen, &hdrlen);
|
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (class == CLASS_UNIVERSAL && tag == TAG_OBJECT_ID && !constructed)
|
|
|
|
{
|
|
|
|
/* The certificate seems to be contained in a
|
|
|
|
userCertificate container. Assume the following sequence
|
|
|
|
is the certificate. */
|
|
|
|
*r_certoff += hdrlen + objlen;
|
|
|
|
if (*r_certoff > resultlen)
|
2004-04-20 18:42:55 +02:00
|
|
|
{
|
|
|
|
*r_certoff = 0;
|
|
|
|
return 0; /* That should never happen. */
|
|
|
|
}
|
2004-03-16 19:59:21 +01:00
|
|
|
}
|
2004-04-20 18:42:55 +02:00
|
|
|
else
|
|
|
|
*r_certoff = 0;
|
2004-03-16 19:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resultlen;
|
|
|
|
}
|