summaryrefslogtreecommitdiffstats
path: root/sm
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2005-07-25 16:35:04 +0200
committerWerner Koch <wk@gnupg.org>2005-07-25 16:35:04 +0200
commita2d1673d6622e39c41f061e1bffb5ca05c803320 (patch)
tree292c3b03237aab155753bb3dddc141235e0c1e55 /sm
parent* gpgsm.c (main): New options --no-log-file and --debug-none. (diff)
downloadgnupg2-a2d1673d6622e39c41f061e1bffb5ca05c803320.tar.xz
gnupg2-a2d1673d6622e39c41f061e1bffb5ca05c803320.zip
* findkey.c (agent_public_key_from_file): Fixed array assignment.
This was the cause for random segvs. * call-agent.c (gpgsm_agent_readkey): New.
Diffstat (limited to 'sm')
-rw-r--r--sm/call-agent.c47
-rw-r--r--sm/certreqgen.c39
-rw-r--r--sm/gpgsm.h2
3 files changed, 78 insertions, 10 deletions
diff --git a/sm/call-agent.c b/sm/call-agent.c
index 92a29928c..c47f6b19a 100644
--- a/sm/call-agent.c
+++ b/sm/call-agent.c
@@ -1,5 +1,5 @@
/* call-agent.c - divert operations to the agent
- * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
+ * Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
@@ -433,6 +433,51 @@ gpgsm_agent_genkey (ctrl_t ctrl,
}
+/* Call the agent to read the public key part for a given keygrip. */
+int
+gpgsm_agent_readkey (ctrl_t ctrl, const char *hexkeygrip,
+ ksba_sexp_t *r_pubkey)
+{
+ int rc;
+ membuf_t data;
+ size_t len;
+ unsigned char *buf;
+ char line[ASSUAN_LINELENGTH];
+
+ *r_pubkey = NULL;
+ rc = start_agent (ctrl);
+ if (rc)
+ return rc;
+
+ rc = assuan_transact (agent_ctx, "RESET",NULL, NULL, NULL, NULL, NULL, NULL);
+ if (rc)
+ return map_assuan_err (rc);
+
+ snprintf (line, DIM(line)-1, "READKEY %s", hexkeygrip);
+ line[DIM(line)-1] = 0;
+
+ init_membuf (&data, 1024);
+ rc = assuan_transact (agent_ctx, line,
+ membuf_data_cb, &data,
+ NULL, NULL, NULL, NULL);
+ if (rc)
+ {
+ xfree (get_membuf (&data, &len));
+ return map_assuan_err (rc);
+ }
+ buf = get_membuf (&data, &len);
+ if (!buf)
+ return gpg_error (GPG_ERR_ENOMEM);
+ if (!gcry_sexp_canon_len (buf, len, NULL, NULL))
+ {
+ xfree (buf);
+ return gpg_error (GPG_ERR_INV_SEXP);
+ }
+ *r_pubkey = buf;
+ return 0;
+}
+
+
/* Ask the agent whether the certificate is in the list of trusted
keys */
int
diff --git a/sm/certreqgen.c b/sm/certreqgen.c
index c9a092046..c523c992a 100644
--- a/sm/certreqgen.c
+++ b/sm/certreqgen.c
@@ -63,6 +63,9 @@ The format of the native parameter file is follows:
algorithm is "rsa".
Key-Length: <length-in-bits>
Length of the key in bits. Default is 1024.
+ Key-Grip: hexstring
+ This is optional and used to generate a request for an already
+ existsing key. Key-Length will be ignored when given,
Key-Usage: <usage-list>
Space or comma delimited list of key usage, allowed values are
"encrypt" and "sign". This is used to generate the KeyUsage extension.
@@ -111,6 +114,7 @@ EOF
enum para_name {
pKEYTYPE,
pKEYLENGTH,
+ pKEYGRIP,
pKEYUSAGE,
pNAMEDN,
pNAMEEMAIL,
@@ -252,6 +256,7 @@ read_parameters (ctrl_t ctrl, FILE *fp, ksba_writer_t writer)
} keywords[] = {
{ "Key-Type", pKEYTYPE},
{ "Key-Length", pKEYLENGTH },
+ { "Key-Grip", pKEYGRIP },
{ "Key-Usage", pKEYUSAGE },
{ "Name-DN", pNAMEDN },
{ "Name-Email", pNAMEEMAIL, 1 },
@@ -502,16 +507,32 @@ proc_parameters (ctrl_t ctrl,
}
}
- sprintf (numbuf, "%u", nbits);
- snprintf ((char*)keyparms, DIM (keyparms)-1,
- "(6:genkey(3:rsa(5:nbits%d:%s)))", (int)strlen (numbuf), numbuf);
- rc = gpgsm_agent_genkey (ctrl, keyparms, &public);
- if (rc)
+ s = get_parameter_value (para, pKEYGRIP, 0);
+ if (s) /* Use existing key. */
{
- r = get_parameter (para, pKEYTYPE, 0);
- log_error (_("line %d: key generation failed: %s\n"),
- r->lnr, gpg_strerror (rc));
- return rc;
+ rc = gpgsm_agent_readkey (ctrl, s, &public);
+ if (rc)
+ {
+ r = get_parameter (para, pKEYTYPE, 0);
+ log_error (_("line %d: error getting key by keygrip `%s': %s\n"),
+ r->lnr, s, gpg_strerror (rc));
+ return rc;
+ }
+ }
+ else /* Generate new key. */
+ {
+ sprintf (numbuf, "%u", nbits);
+ snprintf ((char*)keyparms, DIM (keyparms)-1,
+ "(6:genkey(3:rsa(5:nbits%d:%s)))",
+ (int)strlen (numbuf), numbuf);
+ rc = gpgsm_agent_genkey (ctrl, keyparms, &public);
+ if (rc)
+ {
+ r = get_parameter (para, pKEYTYPE, 0);
+ log_error (_("line %d: key generation failed: %s\n"),
+ r->lnr, gpg_strerror (rc));
+ return rc;
+ }
}
rc = create_request (ctrl, para, public, outctrl);
diff --git a/sm/gpgsm.h b/sm/gpgsm.h
index 2f3e83485..63d07a8c2 100644
--- a/sm/gpgsm.h
+++ b/sm/gpgsm.h
@@ -299,6 +299,8 @@ int gpgsm_agent_pkdecrypt (ctrl_t ctrl, const char *keygrip, const char *desc,
char **r_buf, size_t *r_buflen);
int gpgsm_agent_genkey (ctrl_t ctrl,
ksba_const_sexp_t keyparms, ksba_sexp_t *r_pubkey);
+int gpgsm_agent_readkey (ctrl_t ctrl, const char *hexkeygrip,
+ ksba_sexp_t *r_pubkey);
int gpgsm_agent_istrusted (ctrl_t ctrl, ksba_cert_t cert);
int gpgsm_agent_havekey (ctrl_t ctrl, const char *hexkeygrip);
int gpgsm_agent_marktrusted (ctrl_t ctrl, ksba_cert_t cert);