summaryrefslogtreecommitdiffstats
path: root/tools/make-dns-cert.c
blob: 9a7e20d10b8fb4229f6c51b9603f5da06844a941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* make-dns-cert.c - An OpenPGP-to-DNS CERT conversion tool
 * Copyright (C) 2006, 2008 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
 * the Free Software Foundation; either version 3 of the License, or
 * (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
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.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)
    {
      fprintf(stderr,"Cannot open key file %s: %s\n",keyfile,strerror(errno));
      return 1;
    }

  err=fstat(fd,&statbuf);
  if(err==-1)
    {
      fprintf(stderr,"Unable to stat key file %s: %s\n",
	      keyfile,strerror(errno));
      goto fail;
    }

  if(statbuf.st_size>65536)
    {
      fprintf(stderr,"Key %s too large for CERT encoding\n",keyfile);
      goto fail;
    }

  if(statbuf.st_size>16384)
    fprintf(stderr,"Warning: key file %s is larger than the default"
	    " GnuPG max-cert-size\n",keyfile);

  printf("%s\tTYPE37\t\\# %u 0003 0000 00 ",
	 name,(unsigned int)statbuf.st_size+5);

  err=1;
  while(err!=0)
    {
      unsigned char buffer[1024];

      do 
        err = read (fd,buffer,1024);
      while (err == -1 && errno == EINTR);
      if(err==-1)
	{
	  fprintf(stderr,"Unable to read key file %s: %s\n",
		  keyfile,strerror(errno));
	  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)
    {
      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++;
	}

      if(fprlen%2)
	{
	  fprintf(stderr,"Fingerprint must be an even number of characters\n");
	  return 1;
	}

      fprlen/=2;
      len+=fprlen;
    }

  if(url)
    len+=strlen(url);

  if(!fpr && !url)
    {
      fprintf(stderr,
	      "Cannot generate a CERT without either a fingerprint or URL\n");
      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
usage(FILE *stream)
{
  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");
}

int
main(int argc,char *argv[])
{
  int arg,err=1;
  char *fpr=NULL,*url=NULL,*keyfile=NULL,*name=NULL;

  if(argc==1)
    {
      usage(stderr);
      return 1;
    }
  else if(argc>1 && strcmp(argv[1],"--version")==0)
    {
#if defined(HAVE_CONFIG_H) && defined(VERSION)
      printf ("make-dns-cert (GnuPG) " VERSION "\n");
#else
      printf ("make-dns-cert gnupg-svn%d\n", atoi (10+"$Revision$"));
#endif
      return 0;
    }
  else if(argc>1 && strcmp(argv[1],"--help")==0)
    {
      usage(stdout);
      return 0;
    }

  while((arg=getopt(argc,argv,"hf:u:k:n:"))!=-1)
    switch(arg)
      {
      default:
      case 'h':
	usage(stdout);
	exit(0);

      case 'f':
	fpr=optarg;
	break;

      case 'u':
	url=optarg;
	break;

      case 'k':
	keyfile=optarg;
	break;

      case 'n':
	name=optarg;
	break;
      }

  if(!name)
    {
      fprintf(stderr,"No name provided\n");
      return 1;
    }

  if(keyfile && (fpr || url))
    {
      fprintf(stderr,"Cannot generate a CERT record with both a keyfile and"
	      " a fingerprint or URL\n");
      return 1;
    }

  if(keyfile)
    err=cert_key(name,keyfile);
  else
    err=url_key(name,fpr,url);

  return err;
}