2006-05-23 16:19:43 +00:00
|
|
|
/* pka.c - DNS Public Key Association RR access
|
2009-12-07 17:12:28 +00:00
|
|
|
* Copyright (C) 2005, 2009 Free Software Foundation, Inc.
|
2006-05-23 16:19:43 +00:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2006-05-23 16:19:43 +00:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2006-05-23 16:19:43 +00:00
|
|
|
* 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
|
2007-07-04 19:49:40 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2006-05-23 16:19:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef USE_DNS_PKA
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef _WIN32
|
2014-03-07 09:46:44 +01:00
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
# include <winsock2.h>
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
2006-05-23 16:19:43 +00:00
|
|
|
#else
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#endif
|
|
|
|
#endif /* USE_DNS_PKA */
|
2009-12-07 15:52:27 +00:00
|
|
|
#ifdef USE_ADNS
|
|
|
|
# include <adns.h>
|
|
|
|
#endif
|
2006-05-23 16:19:43 +00:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "pka.h"
|
|
|
|
|
|
|
|
#ifdef USE_DNS_PKA
|
|
|
|
/* Parse the TXT resource record. Format is:
|
|
|
|
|
|
|
|
v=pka1;fpr=a4d94e92b0986ab5ee9dcd755de249965b0358a2;uri=string
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
For simplicity white spaces are not allowed. Because we expect to
|
|
|
|
use a new RRTYPE for this in the future we define the TXT really
|
|
|
|
strict for simplicity: No white spaces, case sensitivity of the
|
|
|
|
names, order must be as given above. Only URI is optional.
|
|
|
|
|
|
|
|
This function modifies BUFFER. On success 0 is returned, the 20
|
|
|
|
byte fingerprint stored at FPR and BUFFER contains the URI or an
|
|
|
|
empty string.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_txt_record (char *buffer, unsigned char *fpr)
|
|
|
|
{
|
|
|
|
char *p, *pend;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
pend = strchr (p, ';');
|
|
|
|
if (!pend)
|
|
|
|
return -1;
|
|
|
|
*pend++ = 0;
|
|
|
|
if (strcmp (p, "v=pka1"))
|
|
|
|
return -1; /* Wrong or missing version. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
p = pend;
|
|
|
|
pend = strchr (p, ';');
|
|
|
|
if (pend)
|
|
|
|
*pend++ = 0;
|
|
|
|
if (strncmp (p, "fpr=", 4))
|
|
|
|
return -1; /* Missing fingerprint part. */
|
|
|
|
p += 4;
|
|
|
|
for (i=0; i < 20 && hexdigitp (p) && hexdigitp (p+1); i++, p += 2)
|
|
|
|
fpr[i] = xtoi_2 (p);
|
|
|
|
if (i != 20)
|
|
|
|
return -1; /* Fingerprint consists not of exactly 40 hexbytes. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
p = pend;
|
|
|
|
if (!p || !*p)
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
*buffer = 0;
|
2006-05-23 16:19:43 +00:00
|
|
|
return 0; /* Success (no URI given). */
|
|
|
|
}
|
|
|
|
if (strncmp (p, "uri=", 4))
|
|
|
|
return -1; /* Unknown part. */
|
|
|
|
p += 4;
|
|
|
|
/* There is an URI, copy it to the start of the buffer. */
|
|
|
|
while (*p)
|
|
|
|
*buffer++ = *p++;
|
|
|
|
*buffer = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* For the given email ADDRESS lookup the PKA information in the DNS.
|
|
|
|
|
|
|
|
On success the 20 byte SHA-1 fingerprint is stored at FPR and the
|
|
|
|
URI will be returned in an allocated buffer. Note that the URI
|
2008-03-06 18:28:47 +00:00
|
|
|
might be an zero length string as this information is optional.
|
2006-05-23 16:19:43 +00:00
|
|
|
Caller must xfree the returned string.
|
|
|
|
|
|
|
|
On error NULL is returned and the 20 bytes at FPR are not
|
|
|
|
defined. */
|
|
|
|
char *
|
|
|
|
get_pka_info (const char *address, unsigned char *fpr)
|
|
|
|
{
|
2009-12-07 15:52:27 +00:00
|
|
|
#ifdef USE_ADNS
|
|
|
|
int rc;
|
|
|
|
adns_state state;
|
|
|
|
const char *domain;
|
|
|
|
char *name;
|
|
|
|
adns_answer *answer = NULL;
|
|
|
|
char *buffer = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 15:52:27 +00:00
|
|
|
domain = strrchr (address, '@');
|
|
|
|
if (!domain || domain == address || !domain[1])
|
|
|
|
return NULL; /* Invalid mail address given. */
|
|
|
|
name = xtrymalloc (strlen (address) + 5 + 1);
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
memcpy (name, address, domain - address);
|
|
|
|
strcpy (stpcpy (name + (domain-address), "._pka."), domain+1);
|
|
|
|
|
|
|
|
rc = adns_init (&state, adns_if_noerrprint, NULL);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("error initializing adns: %s\n", strerror (errno));
|
|
|
|
xfree (name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = adns_synchronous (state, name, adns_r_txt, adns_qf_quoteok_query,
|
|
|
|
&answer);
|
|
|
|
xfree (name);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("DNS query failed: %s\n", strerror (errno));
|
|
|
|
adns_finish (state);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
if (answer->status != adns_s_ok
|
2009-12-07 15:52:27 +00:00
|
|
|
|| answer->type != adns_r_txt || !answer->nrrs)
|
|
|
|
{
|
|
|
|
log_error ("DNS query returned an error: %s (%s)\n",
|
|
|
|
adns_strerror (answer->status),
|
|
|
|
adns_errabbrev (answer->status));
|
|
|
|
adns_free (answer);
|
|
|
|
adns_finish (state);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We use a PKA records iff there is exactly one record. */
|
|
|
|
if (answer->nrrs == 1 && answer->rrs.manyistr[0]->i != -1)
|
|
|
|
{
|
|
|
|
buffer = xtrystrdup (answer->rrs.manyistr[0]->str);
|
|
|
|
if (parse_txt_record (buffer, fpr))
|
|
|
|
{
|
|
|
|
xfree (buffer);
|
|
|
|
buffer = NULL; /* Not a valid gpg trustdns RR. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adns_free (answer);
|
|
|
|
adns_finish (state);
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
#else /*!USE_ADNS*/
|
2006-05-23 16:19:43 +00:00
|
|
|
unsigned char answer[PACKETSZ];
|
|
|
|
int anslen;
|
2011-08-10 14:11:30 +02:00
|
|
|
int qdcount, ancount;
|
2006-05-23 16:19:43 +00:00
|
|
|
int rc;
|
|
|
|
unsigned char *p, *pend;
|
|
|
|
const char *domain;
|
|
|
|
char *name;
|
2010-04-20 01:11:35 +00:00
|
|
|
HEADER header;
|
2006-05-23 16:19:43 +00:00
|
|
|
|
|
|
|
domain = strrchr (address, '@');
|
|
|
|
if (!domain || domain == address || !domain[1])
|
|
|
|
return NULL; /* invalid mail address given. */
|
|
|
|
|
2008-04-07 19:31:12 +00:00
|
|
|
name = xtrymalloc (strlen (address) + 5 + 1);
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
2006-05-23 16:19:43 +00:00
|
|
|
memcpy (name, address, domain - address);
|
|
|
|
strcpy (stpcpy (name + (domain-address), "._pka."), domain+1);
|
|
|
|
|
|
|
|
anslen = res_query (name, C_IN, T_TXT, answer, PACKETSZ);
|
|
|
|
xfree (name);
|
|
|
|
if (anslen < sizeof(HEADER))
|
|
|
|
return NULL; /* DNS resolver returned a too short answer. */
|
2010-04-20 01:11:35 +00:00
|
|
|
|
|
|
|
/* Don't despair: A good compiler should optimize this away, as
|
|
|
|
header is just 32 byte and constant at compile time. It's
|
|
|
|
one way to comply with strict aliasing rules. */
|
|
|
|
memcpy (&header, answer, sizeof (header));
|
|
|
|
|
|
|
|
if ( (rc=header.rcode) != NOERROR )
|
2006-05-23 16:19:43 +00:00
|
|
|
return NULL; /* DNS resolver returned an error. */
|
|
|
|
|
|
|
|
/* We assume that PACKETSZ is large enough and don't do dynmically
|
|
|
|
expansion of the buffer. */
|
|
|
|
if (anslen > PACKETSZ)
|
|
|
|
return NULL; /* DNS resolver returned a too long answer */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2010-04-20 01:11:35 +00:00
|
|
|
qdcount = ntohs (header.qdcount);
|
|
|
|
ancount = ntohs (header.ancount);
|
2006-05-23 16:19:43 +00:00
|
|
|
|
|
|
|
if (!ancount)
|
|
|
|
return NULL; /* Got no answer. */
|
|
|
|
|
|
|
|
p = answer + sizeof (HEADER);
|
|
|
|
pend = answer + anslen; /* Actually points directly behind the buffer. */
|
|
|
|
|
|
|
|
while (qdcount-- && p < pend)
|
|
|
|
{
|
|
|
|
rc = dn_skipname (p, pend);
|
|
|
|
if (rc == -1)
|
|
|
|
return NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
p += rc + QFIXEDSZ;
|
2006-05-23 16:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ancount > 1)
|
|
|
|
return NULL; /* more than one possible gpg trustdns record - none used. */
|
|
|
|
|
|
|
|
while (ancount-- && p <= pend)
|
|
|
|
{
|
|
|
|
unsigned int type, class, txtlen, n;
|
|
|
|
char *buffer, *bufp;
|
|
|
|
|
|
|
|
rc = dn_skipname (p, pend);
|
|
|
|
if (rc == -1)
|
|
|
|
return NULL;
|
|
|
|
p += rc;
|
|
|
|
if (p >= pend - 10)
|
|
|
|
return NULL; /* RR too short. */
|
|
|
|
|
|
|
|
type = *p++ << 8;
|
|
|
|
type |= *p++;
|
|
|
|
class = *p++ << 8;
|
|
|
|
class |= *p++;
|
|
|
|
p += 4;
|
|
|
|
txtlen = *p++ << 8;
|
|
|
|
txtlen |= *p++;
|
|
|
|
if (type != T_TXT || class != C_IN)
|
|
|
|
return NULL; /* Answer does not match the query. */
|
|
|
|
|
|
|
|
buffer = bufp = xmalloc (txtlen + 1);
|
|
|
|
while (txtlen && p < pend)
|
|
|
|
{
|
|
|
|
for (n = *p++, txtlen--; txtlen && n && p < pend; txtlen--, n--)
|
|
|
|
*bufp++ = *p++;
|
|
|
|
}
|
|
|
|
*bufp = 0;
|
|
|
|
if (parse_txt_record (buffer, fpr))
|
|
|
|
{
|
|
|
|
xfree (buffer);
|
|
|
|
return NULL; /* Not a valid gpg trustdns RR. */
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2009-12-07 15:52:27 +00:00
|
|
|
#endif /*!USE_ADNS*/
|
2006-05-23 16:19:43 +00:00
|
|
|
}
|
2009-12-07 15:52:27 +00:00
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
#else /* !USE_DNS_PKA */
|
|
|
|
|
|
|
|
/* Dummy version of the function if we can't use the resolver
|
|
|
|
functions. */
|
|
|
|
char *
|
|
|
|
get_pka_info (const char *address, unsigned char *fpr)
|
|
|
|
{
|
2010-03-22 12:46:05 +00:00
|
|
|
(void)address;
|
|
|
|
(void)fpr;
|
2006-05-23 16:19:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* !USE_DNS_PKA */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
int
|
|
|
|
main(int argc,char *argv[])
|
|
|
|
{
|
|
|
|
unsigned char fpr[20];
|
|
|
|
char *uri;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "usage: pka mail-addresses\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
for (; argc; argc--, argv++)
|
|
|
|
{
|
|
|
|
uri = get_pka_info ( *argv, fpr );
|
|
|
|
printf ("%s", *argv);
|
|
|
|
if (uri)
|
|
|
|
{
|
|
|
|
putchar (' ');
|
|
|
|
for (i=0; i < 20; i++)
|
|
|
|
printf ("%02X", fpr[i]);
|
|
|
|
if (*uri)
|
|
|
|
printf (" %s", uri);
|
|
|
|
xfree (uri);
|
|
|
|
}
|
|
|
|
putchar ('\n');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TEST */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables:
|
2009-12-07 15:52:27 +00:00
|
|
|
compile-command: "cc -DUSE_DNS_PKA -DTEST -I.. -I../include -Wall -g -o pka pka.c -lresolv ../tools/no-libgcrypt.o ../jnlib/libjnlib.a"
|
2006-05-23 16:19:43 +00:00
|
|
|
End:
|
|
|
|
*/
|