2006-04-05 00:19:13 +02:00
|
|
|
/* make-dns-cert.c - An OpenPGP-to-DNS CERT conversion tool
|
|
|
|
* Copyright (C) 2006 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* 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-10-23 12:48:09 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2006-04-05 00:19:13 +02: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
|
2007-10-23 12:48:09 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2006-04-05 00:19:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
/* We use TYPE37 instead of CERT since not all nameservers can handle
|
|
|
|
CERT yet... */
|
|
|
|
|
|
|
|
static int
|
|
|
|
cert_key(const char *name,const char *keyfile)
|
|
|
|
{
|
|
|
|
int fd,ret=1,err,i;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
fd=open(keyfile,O_RDONLY);
|
|
|
|
if(fd==-1)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,"Cannot open key file %s: %s\n",keyfile,strerror(errno));
|
2006-04-05 00:19:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err=fstat(fd,&statbuf);
|
|
|
|
if(err==-1)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,"Unable to stat key file %s: %s\n",
|
|
|
|
keyfile,strerror(errno));
|
2006-04-05 00:19:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2006-04-05 16:25:40 +02:00
|
|
|
if(statbuf.st_size>65536)
|
2006-04-05 00:19:13 +02:00
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,"Key %s too large for CERT encoding\n",keyfile);
|
2006-04-05 00:19:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2006-04-05 16:25:40 +02:00
|
|
|
if(statbuf.st_size>16384)
|
|
|
|
fprintf(stderr,"Warning: key file %s is larger than the default"
|
|
|
|
" GnuPG max-cert-size\n",keyfile);
|
|
|
|
|
2006-04-05 00:19:13 +02:00
|
|
|
printf("%s\tTYPE37\t\\# %u 0003 0000 00 ",
|
|
|
|
name,(unsigned int)statbuf.st_size+5);
|
|
|
|
|
|
|
|
err=1;
|
|
|
|
while(err!=0)
|
|
|
|
{
|
|
|
|
unsigned char buffer[1024];
|
|
|
|
|
|
|
|
err=read(fd,buffer,1024);
|
|
|
|
if(err==-1)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,"Unable to read key file %s: %s\n",
|
|
|
|
keyfile,strerror(errno));
|
2006-04-05 00:19:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0;i<err;i++)
|
|
|
|
printf("%02X",buffer[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
ret=0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
url_key(const char *name,const char *fpr,const char *url)
|
|
|
|
{
|
|
|
|
int len=6,fprlen=0;
|
|
|
|
|
|
|
|
if(fpr)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
const char *tmp = fpr;
|
|
|
|
while (*tmp)
|
|
|
|
{
|
|
|
|
if ((*tmp >= 'A' && *tmp <= 'F') ||
|
|
|
|
(*tmp >= 'a' && *tmp <= 'f') ||
|
|
|
|
(*tmp >= '0' && *tmp <= '9'))
|
|
|
|
{
|
|
|
|
fprlen++;
|
|
|
|
}
|
|
|
|
else if (*tmp != ' ' && *tmp != '\t')
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Fingerprint must consist of only hex digits"
|
|
|
|
" and whitespace\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
|
2006-04-05 00:19:13 +02:00
|
|
|
if(fprlen%2)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,"Fingerprint must be an even number of characters\n");
|
2006-04-05 00:19:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprlen/=2;
|
|
|
|
len+=fprlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(url)
|
|
|
|
len+=strlen(url);
|
|
|
|
|
|
|
|
if(!fpr && !url)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"Cannot generate a CERT without either a fingerprint or URL\n");
|
2006-04-05 00:19:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s\tTYPE37\t\\# %d 0006 0000 00 %02X",name,len,fprlen);
|
|
|
|
|
|
|
|
if(fpr)
|
|
|
|
printf(" %s",fpr);
|
|
|
|
|
|
|
|
if(url)
|
|
|
|
{
|
|
|
|
const char *c;
|
|
|
|
printf(" ");
|
|
|
|
for(c=url;*c;c++)
|
|
|
|
printf("%02X",*c);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-05 16:25:40 +02:00
|
|
|
usage(FILE *stream)
|
2006-04-05 00:19:13 +02:00
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stream,"make-dns-cert\n");
|
|
|
|
fprintf(stream,"\t-f\tfingerprint\n");
|
|
|
|
fprintf(stream,"\t-u\tURL\n");
|
|
|
|
fprintf(stream,"\t-k\tkey file\n");
|
|
|
|
fprintf(stream,"\t-n\tDNS name\n");
|
2006-04-05 00:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc,char *argv[])
|
|
|
|
{
|
|
|
|
int arg,err=1;
|
|
|
|
char *fpr=NULL,*url=NULL,*keyfile=NULL,*name=NULL;
|
|
|
|
|
|
|
|
if(argc==1)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
usage(stderr);
|
2006-04-21 00:05:42 +02:00
|
|
|
return 1;
|
2006-04-05 00:19:13 +02:00
|
|
|
}
|
|
|
|
else if(argc>1 && strcmp(argv[1],"--version")==0)
|
|
|
|
{
|
|
|
|
printf("make-dns-cert (GnuPG) " VERSION "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(argc>1 && strcmp(argv[1],"--help")==0)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
usage(stdout);
|
2006-04-05 00:19:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((arg=getopt(argc,argv,"hf:u:k:n:"))!=-1)
|
|
|
|
switch(arg)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case 'h':
|
2006-04-05 16:25:40 +02:00
|
|
|
usage(stdout);
|
2006-04-05 00:19:13 +02:00
|
|
|
exit(0);
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
fpr=optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
url=optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'k':
|
|
|
|
keyfile=optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
name=optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!name)
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,"No name provided\n");
|
2006-04-05 00:19:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(keyfile && (fpr || url))
|
|
|
|
{
|
2006-04-05 16:25:40 +02:00
|
|
|
fprintf(stderr,"Cannot generate a CERT record with both a keyfile and"
|
|
|
|
" a fingerprint or URL\n");
|
2006-04-05 00:19:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(keyfile)
|
|
|
|
err=cert_key(name,keyfile);
|
|
|
|
else
|
|
|
|
err=url_key(name,fpr,url);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|