{"thread":{"id":"41885","subject":"[RFC PATCH] gpg: add support for gpgsm","startedAt":"2016-03-31T13:51:44Z","lastAt":"2016-03-31T17:30:25Z","messageCount":8,"participants":["Carlos Martín Nieto","Jeff King","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"282304","messageId":"1459432304-35779-1-git-send-email-cmn@dwim.me","threadId":"41885","inReplyTo":null,"subject":"[RFC PATCH] gpg: add support for gpgsm","fromName":"Carlos Martín Nieto","fromEmail":"cmn@dwim.me","sentAt":"2016-03-31T13:51:44Z","receivedAt":"2016-03-31T13:51:44Z","isPatch":true,"sender":{"key":"cmn@dwim.me","avatar":"https://avatars.githubusercontent.com/u/335443?v=4"},"body":"Detect the gpgsm block header and run this command instead of gpg.\nOn the signing side, ask gpgsm if it knows the signing key we're trying\nto use and fall back to gpg if it does not.\n\nThis lets the user more easily combine signing and verifying X509 and\nPGP signatures without having to choose a default for a particular\nrepository that may need to be occasionally overridden.\n\nSigned-off-by: Carlos Martín Nieto <cmn@dwim.me>\n\n---\n\nOut there in the so-called \"real world\", companies like using X509 to\nsign things. Currently you can set 'gpg.program' to gpgsm to get\ngpg-compatible verification, but if you're changing it to swap between\nPGP and X509, it's an extra variable to keep in mind when working with\nsigned commits and tags.\n\nWhile this does let us sign and verify, the probing is a bit\nawkward. gpgsm returns 0 regardless of whether it found the key, and if\nyou pass in an id for which you have the public key, it'll still output the\nfilename as a heading, so we would consider it known. I'm not aware of a\nway around that which doesn't involve parsing the output, which would\nprobably be even more fragile.\n\n Documentation/config.txt | 11 ++++++++\n gpg-interface.c          | 65 ++++++++++++++++++++++++++++++++++++++++++++++--\n 2 files changed, 74 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 2cd6bdd..40f3912 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1484,6 +1484,17 @@ gpg.program::\n \tsigned, and the program is expected to send the result to its\n \tstandard output.\n \n+gpgsm.program::\n+\tUse this custom program instead of \"gpgsm\" found on $PATH when\n+\tmaking or verifying a gpsm signature. The program must support the\n+\tsame command-line interface as GPG, namely, to verify a detached\n+\tsignature, \"gpgsm --verify $file - <$signature\" is run, and the\n+\tprogram is expected to signal a good signature by exiting with\n+\tcode 0, and to generate an ASCII-armored detached signature, the\n+\tstandard input of \"gpgsm -bsau $key\" is fed with the contents to be\n+\tsigned, and the program is expected to send the result to its\n+\tstandard output.\n+\n gui.commitMsgWidth::\n \tDefines how wide the commit message window is in the\n \tlinkgit:git-gui[1]. \"75\" is the default.\ndiff --git a/gpg-interface.c b/gpg-interface.c\nindex 3dc2fe3..194a6c6 100644\n--- a/gpg-interface.c\n+++ b/gpg-interface.c\n@@ -6,10 +6,13 @@\n \n static char *configured_signing_key;\n static const char *gpg_program = \"gpg\";\n+static const char *gpgsm_program = \"gpgsm\";\n \n #define PGP_SIGNATURE \"-----BEGIN PGP SIGNATURE-----\"\n #define PGP_MESSAGE \"-----BEGIN PGP MESSAGE-----\"\n \n+#define GPGSM_MESSAGE \"-----BEGIN SIGNED MESSAGE-----\"\n+\n void signature_check_clear(struct signature_check *sigc)\n {\n \tfree(sigc->payload);\n@@ -24,6 +27,20 @@ void signature_check_clear(struct signature_check *sigc)\n \tsigc->key = NULL;\n }\n \n+/*\n+ * Guess which program this signature was made with based on the block start.\n+ * Right now we just detect a gpgsm block and fall back to gpg otherwise.\n+ */\n+static const char *guess_program(const char *message, size_t message_len)\n+{\n+\tsize_t gpgsm_len = strlen(GPGSM_MESSAGE);\n+\n+\tif (message_len > gpgsm_len && !strncmp(message, GPGSM_MESSAGE, gpgsm_len))\n+\t\treturn gpgsm_program;\n+\n+\treturn gpg_program;\n+}\n+\n static struct {\n \tchar result;\n \tconst char *check;\n@@ -131,6 +148,11 @@ int git_gpg_config(const char *var, const char *value, void *cb)\n \t\t\treturn config_error_nonbool(var);\n \t\tgpg_program = xstrdup(value);\n \t}\n+\tif (!strcmp(var, \"gpgsm.program\")) {\n+\t\tif (!value)\n+\t\t\treturn config_error_nonbool(var);\n+\t\tgpgsm_program = xstrdup(value);\n+\t}\n \treturn 0;\n }\n \n@@ -142,6 +164,41 @@ const char *get_signing_key(void)\n }\n \n /*\n+ * Try to figure out if the given program contains given the key. Both\n+ * gpg and gpgsm have keys in hex format, so we don't necessarily know\n+ * which one to use.\n+ */\n+static int program_knows_key(const char *program, const char *signing_key)\n+{\n+\tstruct child_process gpg = CHILD_PROCESS_INIT;\n+\tstruct strbuf output = STRBUF_INIT;\n+\tconst char *args[4];\n+\tsize_t len;\n+\n+\tgpg.argv = args;\n+\tgpg.in = -1;\n+\tgpg.out = -1;\n+\targs[0] = program;\n+\targs[1] = \"-K\";\n+\targs[2] = signing_key;\n+\targs[3] = NULL;\n+\n+\tif (start_command(&gpg))\n+\t\treturn error(_(\"could not run '%s'\"), program);\n+\n+\tclose(gpg.in);\n+\tlen = strbuf_read(&output, gpg.out, 1024);\n+\tclose(gpg.out);\n+\n+\t/* If the command exits with an error, consider it as not found */\n+\tif (finish_command(&gpg))\n+\t\treturn 0;\n+\n+\t/* If the command showed the key we wanted, use it. */\n+\treturn !!len;\n+}\n+\n+/*\n  * Create a detached signature for the contents of \"buffer\" and append\n  * it after \"signature\"; \"buffer\" and \"signature\" can be the same\n  * strbuf instance, which would cause the detached signature appended\n@@ -154,10 +211,14 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig\n \tssize_t len;\n \tsize_t i, j, bottom;\n \n+\tif (program_knows_key(gpgsm_program, signing_key))\n+\t\targs[0] = gpgsm_program;\n+\telse\n+\t\targs[0] = gpg_program;\n+\n \tgpg.argv = args;\n \tgpg.in = -1;\n \tgpg.out = -1;\n-\targs[0] = gpg_program;\n \targs[1] = \"-bsau\";\n \targs[2] = signing_key;\n \targs[3] = NULL;\n@@ -216,7 +277,7 @@ int verify_signed_buffer(const char *payload, size_t payload_size,\n \tstruct strbuf buf = STRBUF_INIT;\n \tstruct strbuf *pbuf = &buf;\n \n-\targs_gpg[0] = gpg_program;\n+\targs_gpg[0] = guess_program(signature, signature_size);\n \tfd = git_mkstemp(path, PATH_MAX, \".git_vtag_tmpXXXXXX\");\n \tif (fd < 0)\n \t\treturn error(_(\"could not create temporary file '%s': %s\"),\n-- \n2.8.0.rc3\n"},{"id":"282309","messageId":"20160331142200.GB31116@sigill.intra.peff.net","threadId":"41885","inReplyTo":"1459432304-35779-1-git-send-email-cmn@dwim.me","subject":"Re: [RFC PATCH] gpg: add support for gpgsm","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2016-03-31T14:22:00Z","receivedAt":"2016-03-31T14:22:00Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Mar 31, 2016 at 03:51:44PM +0200, Carlos Martín Nieto wrote:\n\n> Detect the gpgsm block header and run this command instead of gpg.\n\nThis part makes sense to me, and is a strict improvement (though\noffhand, I wonder if any other systems use the generic \"BEGIN SIGNED\nMESSAGE\" header. The obvious option would be PEM from \"openssl smime\",\nbut it is \"BEGIN PKCS7\").\n\n> On the signing side, ask gpgsm if it knows the signing key we're trying\n> to use and fall back to gpg if it does not.\n\nThis part looks like we incur an extra fork/exec each time we sign with\ngpg, even if the user doesn't ever want to use gpgsm, or even have it\ninstalled.\n\nI wonder if there are any hints we can use from the key ident, but I\nsuppose not. In the default config, it comes straight from\n$GIT_COMMITTER_*, and is just a name/email.\n\nBut maybe we could pull this out to a separate config option, like\n\"commit.defaultSignatureType\", which could be either \"gpg\", \"gpgsm\", or\n\"auto\" to enable the behavior you have here.  Then savvy users can pick\nthe type they plan to use.  We can have a discussion then about whether\nto flip the default from \"gpg\" to \"auto\", but I'd vote to leave it at\ngpg unless gpgsm gets a huge amount of traction, and it really is 50/50\nwhat people would want.\n\nAnd regardless of the default type for creating signatures, we'd still\nautomatically verify signatures from either type.\n\n>  /*\n> + * Try to figure out if the given program contains given the key. Both\n> + * gpg and gpgsm have keys in hex format, so we don't necessarily know\n> + * which one to use.\n> + */\n> +static int program_knows_key(const char *program, const char *signing_key)\n> +{\n> +\tstruct child_process gpg = CHILD_PROCESS_INIT;\n> +\tstruct strbuf output = STRBUF_INIT;\n> +\tconst char *args[4];\n> +\tsize_t len;\n> +\n> +\tgpg.argv = args;\n> +\tgpg.in = -1;\n> +\tgpg.out = -1;\n> +\targs[0] = program;\n> +\targs[1] = \"-K\";\n> +\targs[2] = signing_key;\n> +\targs[3] = NULL;\n\nI think you'd want to send stderr to /dev/null here, as this is for\nspeculatively seeing \"does the user even have gpgsm set up?\".\n\n> +\n> +\tif (start_command(&gpg))\n> +\t\treturn error(_(\"could not run '%s'\"), program);\n\nLikewise, most users would start seeing \"could not run 'gpgsm'\" if they\ndo not even have it installed.\n\n-Peff\n"},{"id":"282316","messageId":"1459435490.2124.15.camel@dwim.me","threadId":"41885","inReplyTo":"20160331142200.GB31116@sigill.intra.peff.net","subject":"Re: [RFC PATCH] gpg: add support for gpgsm","fromName":"Carlos Martín Nieto","fromEmail":"cmn@dwim.me","sentAt":"2016-03-31T14:44:50Z","receivedAt":"2016-03-31T14:44:50Z","isPatch":true,"sender":{"key":"cmn@dwim.me","avatar":"https://avatars.githubusercontent.com/u/335443?v=4"},"body":"On Thu, 2016-03-31 at 10:22 -0400, Jeff King wrote:\n> On Thu, Mar 31, 2016 at 03:51:44PM +0200, Carlos Martín Nieto wrote:\n> \n> > \n> > Detect the gpgsm block header and run this command instead of gpg.\n> This part makes sense to me, and is a strict improvement (though\n> offhand, I wonder if any other systems use the generic \"BEGIN SIGNED\n> MESSAGE\" header. The obvious option would be PEM from \"openssl\n> smime\",\n> but it is \"BEGIN PKCS7\").\n\nYep. It's useful when you can tell what generated the signatures.\n\n> \n> > \n> > On the signing side, ask gpgsm if it knows the signing key we're\n> > trying\n> > to use and fall back to gpg if it does not.\n> This part looks like we incur an extra fork/exec each time we sign\n> with\n> gpg, even if the user doesn't ever want to use gpgsm, or even have it\n> installed.\n\nYes, this is unfortunate as I don't know any other way to tell whether\ngpgsm (or whatever else) knows about a key. We could try to find gpgsm\nin PATH but I suspect this would be expensive as well.\n\n> \n> I wonder if there are any hints we can use from the key ident, but I\n> suppose not. In the default config, it comes straight from\n> $GIT_COMMITTER_*, and is just a name/email.\n\nBoth gpg and gpgsm accept any string and try to match it against the\ninformation in the keys. So even though gpgsm keys are shown by the\nprogram itself with the \"0x\" prefix, 'gpgsm -k DigiCert' does show me\nthe public key I have for them.\n\n> \n> But maybe we could pull this out to a separate config option, like\n> \"commit.defaultSignatureType\", which could be either \"gpg\", \"gpgsm\",\n> or\n> \"auto\" to enable the behavior you have here.  Then savvy users can\n> pick\n> the type they plan to use.  We can have a discussion then about\n> whether\n> to flip the default from \"gpg\" to \"auto\", but I'd vote to leave it at\n> gpg unless gpgsm gets a huge amount of traction, and it really is\n> 50/50\n> what people would want.\n> \n> And regardless of the default type for creating signatures, we'd\n> still\n> automatically verify signatures from either type.\n\nThis makes sense. It allows for the automatic detection if that's\nwanted but it would stop us running gpgsm on each invocation.\n\n\n\n> >  /*\n> > + * Try to figure out if the given program contains given the key.\n> > Both\n> > + * gpg and gpgsm have keys in hex format, so we don't necessarily\n> > know\n> > + * which one to use.\n> > + */\n> > +static int program_knows_key(const char *program, const char\n> > *signing_key)\n> > +{\n> > +\tstruct child_process gpg = CHILD_PROCESS_INIT;\n> > +\tstruct strbuf output = STRBUF_INIT;\n> > +\tconst char *args[4];\n> > +\tsize_t len;\n> > +\n> > +\tgpg.argv = args;\n> > +\tgpg.in = -1;\n> > +\tgpg.out = -1;\n> > +\targs[0] = program;\n> > +\targs[1] = \"-K\";\n> > +\targs[2] = signing_key;\n> > +\targs[3] = NULL;\n> I think you'd want to send stderr to /dev/null here, as this is for\n> speculatively seeing \"does the user even have gpgsm set up?\".\n> \n> > \n> > +\n> > +\tif (start_command(&gpg))\n> > +\t\treturn error(_(\"could not run '%s'\"), program);\n> Likewise, most users would start seeing \"could not run 'gpgsm'\" if\n> they\n> do not even have it installed.\n\nAh yes, I completely forgot to take that into account.\n\n   cmn\n"},{"id":"282327","messageId":"xmqqa8ler6pu.fsf@gitster.mtv.corp.google.com","threadId":"41885","inReplyTo":"1459432304-35779-1-git-send-email-cmn@dwim.me","subject":"Re: [RFC PATCH] gpg: add support for gpgsm","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-03-31T15:46:05Z","receivedAt":"2016-03-31T15:46:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Carlos Martín Nieto <cmn@dwim.me> writes:\n\n> Detect the gpgsm block header and run this command instead of gpg.\n> On the signing side, ask gpgsm if it knows the signing key we're trying\n> to use and fall back to gpg if it does not.\n>\n> This lets the user more easily combine signing and verifying X509 and\n> PGP signatures without having to choose a default for a particular\n> repository that may need to be occasionally overridden.\n>\n> Signed-off-by: Carlos Martín Nieto <cmn@dwim.me>\n>\n> ---\n>\n> Out there in the so-called \"real world\", companies like using X509 to\n> sign things. Currently you can set 'gpg.program' to gpgsm to get\n> gpg-compatible verification,...\n\nI notice that you had to add GPGSM_MESSAGE string constant; does the\ncurrent code without any change really work correctly if you set\n'gpg.program' to gpgsm and do nothing else?\n\n> ... but if you're changing it to swap between\n> PGP and X509, it's an extra variable to keep in mind when working with\n> signed commits and tags.\n\n> +gpgsm.program::\n> +\tUse this custom program instead of \"gpgsm\" found on $PATH when\n> +\tmaking or verifying a gpsm signature. The program must support the\n\ngpsm signature, or gpgsm signature?\n"},{"id":"282328","messageId":"xmqq60w2r6kq.fsf@gitster.mtv.corp.google.com","threadId":"41885","inReplyTo":"20160331142200.GB31116@sigill.intra.peff.net","subject":"Re: [RFC PATCH] gpg: add support for gpgsm","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-03-31T15:49:09Z","receivedAt":"2016-03-31T15:49:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> But maybe we could pull this out to a separate config option, like\n> \"commit.defaultSignatureType\", which could be either \"gpg\", \"gpgsm\", or\n> \"auto\" to enable the behavior you have here.  Then savvy users can pick\n> the type they plan to use.\n\nYes, I think the topic should start without \"auto\" guessing mode\nto get the basics right; guessing mode can come after we are\ncomfortable with the solidness of the resulting codebase.\n\n> And regardless of the default type for creating signatures, we'd still\n> automatically verify signatures from either type.\n\nAgain, true, as long as we can tell which program to use to verify,\nthere is no guesswork involved, and this can and should be in the\nearly part of the topic.\n\nThanks.\n"},{"id":"282329","messageId":"20160331155753.GA1006@sigill.intra.peff.net","threadId":"41885","inReplyTo":"xmqqa8ler6pu.fsf@gitster.mtv.corp.google.com","subject":"Re: [RFC PATCH] gpg: add support for gpgsm","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2016-03-31T15:57:53Z","receivedAt":"2016-03-31T15:57:53Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Mar 31, 2016 at 08:46:05AM -0700, Junio C Hamano wrote:\n\n> Carlos Martín Nieto <cmn@dwim.me> writes:\n> \n> > Detect the gpgsm block header and run this command instead of gpg.\n> > On the signing side, ask gpgsm if it knows the signing key we're trying\n> > to use and fall back to gpg if it does not.\n> >\n> > This lets the user more easily combine signing and verifying X509 and\n> > PGP signatures without having to choose a default for a particular\n> > repository that may need to be occasionally overridden.\n> >\n> > Signed-off-by: Carlos Martín Nieto <cmn@dwim.me>\n> >\n> > ---\n> >\n> > Out there in the so-called \"real world\", companies like using X509 to\n> > sign things. Currently you can set 'gpg.program' to gpgsm to get\n> > gpg-compatible verification,...\n> \n> I notice that you had to add GPGSM_MESSAGE string constant; does the\n> current code without any change really work correctly if you set\n> 'gpg.program' to gpgsm and do nothing else?\n\nIt has been a few months since I fooled around with gpgsm, but IIRC, it\nworks for tags but not commits. Because verify-tag just blindly dumps\nthe tag to gpg.program, and gpgsm finds the correct signature. Whereas\nthe --show-signature option of git-log does not bother to call gpg if we\ndidn't see a signature.\n\nWhich makes me wonder whether verify-tag would send a gpgsm-signed tag\nto the right place with Carlos's patch (I didn't check).\n\n-Peff\n"},{"id":"282330","messageId":"1459440486.2124.18.camel@dwim.me","threadId":"41885","inReplyTo":"xmqqa8ler6pu.fsf@gitster.mtv.corp.google.com","subject":"Re: [RFC PATCH] gpg: add support for gpgsm","fromName":"Carlos Martín Nieto","fromEmail":"cmn@dwim.me","sentAt":"2016-03-31T16:08:06Z","receivedAt":"2016-03-31T16:08:06Z","isPatch":true,"sender":{"key":"cmn@dwim.me","avatar":"https://avatars.githubusercontent.com/u/335443?v=4"},"body":"On Thu, 2016-03-31 at 08:46 -0700, Junio C Hamano wrote:\n> Carlos Martín Nieto <cmn@dwim.me> writes:\n> \n> > \n> > Detect the gpgsm block header and run this command instead of gpg.\n> > On the signing side, ask gpgsm if it knows the signing key we're\n> > trying\n> > to use and fall back to gpg if it does not.\n> > \n> > This lets the user more easily combine signing and verifying X509\n> > and\n> > PGP signatures without having to choose a default for a particular\n> > repository that may need to be occasionally overridden.\n> > \n> > Signed-off-by: Carlos Martín Nieto <cmn@dwim.me>\n> > \n> > ---\n> > \n> > Out there in the so-called \"real world\", companies like using X509\n> > to\n> > sign things. Currently you can set 'gpg.program' to gpgsm to get\n> > gpg-compatible verification,...\n> I notice that you had to add GPGSM_MESSAGE string constant; does the\n> current code without any change really work correctly if you set\n> 'gpg.program' to gpgsm and do nothing else?\n\nIt does work for verify-commit which is what I've been playing around\nwith since it just sends the contents of the 'gpgsig' header field to\nthe verification function.\n\nI don't recall testing with verify-tag but there we might indeed have\nissues, since we parse the contents to see if we have the signature.\n\n> \n> > \n> > ... but if you're changing it to swap between\n> > PGP and X509, it's an extra variable to keep in mind when working\n> > with\n> > signed commits and tags.\n> > \n> > +gpgsm.program::\n> > +\tUse this custom program instead of \"gpgsm\" found on $PATH\n> > when\n> > +\tmaking or verifying a gpsm signature. The program must\n> > support the\n> gpsm signature, or gpgsm signature?\n\n\nNice catch. Thanks.\n\n  cmn\n"},{"id":"282351","messageId":"20160331173025.GA1809@sigill.intra.peff.net","threadId":"41885","inReplyTo":"1459440486.2124.18.camel@dwim.me","subject":"Re: [RFC PATCH] gpg: add support for gpgsm","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2016-03-31T17:30:25Z","receivedAt":"2016-03-31T17:30:25Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Mar 31, 2016 at 06:08:06PM +0200, Carlos Martín Nieto wrote:\n\n> > I notice that you had to add GPGSM_MESSAGE string constant; does the\n> > current code without any change really work correctly if you set\n> > 'gpg.program' to gpgsm and do nothing else?\n> \n> It does work for verify-commit which is what I've been playing around\n> with since it just sends the contents of the 'gpgsig' header field to\n> the verification function.\n> \n> I don't recall testing with verify-tag but there we might indeed have\n> issues, since we parse the contents to see if we have the signature.\n\nAh, right, I think I had it backwards in my earlier posting. The\n\"gpgsig\" headers are what trigger us for signed commits, not the \"BEGIN\nPGP\" line, and verify-tag does indeed parse the signature out.\n\nI think we'd also fail to pick up signatures from merges of signed tags.\n\n-Peff\n"}]}