File: paperkey.c

package info (click to toggle)
paperkey 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,340 kB
  • sloc: ansic: 4,648; sh: 3,844; makefile: 28
file content (216 lines) | stat: -rw-r--r-- 5,559 bytes parent folder | download
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
/*
 * Copyright (C) 2007, 2008, 2009, 2012, 2016 David Shaw <dshaw@jabberwocky.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "output.h"
#include "extract.h"
#include "restore.h"

int verbose=0,ignore_crc_error=0;
unsigned int output_width=78;
char *comment=NULL;

enum options
  {
    OPT_HELP=256,
    OPT_VERSION,
    OPT_VERBOSE,
    OPT_OUTPUT,
    OPT_INPUT_TYPE,
    OPT_OUTPUT_TYPE,
    OPT_OUTPUT_WIDTH,
    OPT_SECRET_KEY,
    OPT_PUBRING,
    OPT_SECRETS,
    OPT_IGNORE_CRC_ERROR,
    OPT_FILE_FORMAT,
    OPT_COMMENT
  };

static struct option long_options[]=
  {
    {"help",no_argument,NULL,OPT_HELP},
    {"version",no_argument,NULL,OPT_VERSION},
    {"verbose",no_argument,NULL,OPT_VERBOSE},
    {"output",required_argument,NULL,OPT_OUTPUT},
    {"input-type",required_argument,NULL,OPT_INPUT_TYPE},
    {"output-type",required_argument,NULL,OPT_OUTPUT_TYPE},
    {"output-width",required_argument,NULL,OPT_OUTPUT_WIDTH},
    {"secret-key",required_argument,NULL,OPT_SECRET_KEY},
    {"pubring",required_argument,NULL,OPT_PUBRING},
    {"secrets",required_argument,NULL,OPT_SECRETS},
    {"ignore-crc-error",no_argument,NULL,OPT_IGNORE_CRC_ERROR},
    {"file-format",no_argument,NULL,OPT_FILE_FORMAT},
    {"comment",required_argument,NULL,OPT_COMMENT},
    {NULL,0,NULL,0}
  };

static void
usage(void)
{
  printf("Usage: paperkey [OPTIONS]\n");
  printf("  --help (-h)\n");
  printf("  --version (-V)\n");
  printf("  --verbose (-v)  be more verbose\n");
  printf("  --output (-o)   write output to this file\n");
  printf("  --input-type    auto, base16 or raw (binary)\n");
  printf("  --output-type   base16 or raw (binary)\n");
  printf("  --output-width  maximum width of base16 output\n");
  printf("  --secret-key"
	  "    extract secret data from this secret key\n");
  printf("  --pubring"
	  "       public keyring to find non-secret data\n");
  printf("  --secrets       file containing secret"
	  " data to join with the public key\n");
  printf("  --ignore-crc-error  don't reject corrupted input\n");
  printf("  --file-format   show the paperkey file format\n");
  printf("  --comment       add a comment to the base16 output\n");
}

int
main(int argc,char *argv[])
{
  int arg,err;
  FILE *secret_key,*secrets,*pubring=NULL;
  const char *outname=NULL;
  enum data_type output_type=BASE16;
  enum data_type input_type=AUTO;

  set_binary_mode(stdin);

  secret_key=secrets=stdin;

  /* Force the umask to go-rwx, as we are going to be creating files
     that contain secret key material. */
  umask(077);

  while((arg=getopt_long(argc,argv,"hVvo:",long_options,NULL))!=-1)
    switch(arg)
      {
      case OPT_HELP:
      case 'h':
      default:
        usage();
        exit(0);

      case OPT_VERSION:
      case 'V':
	printf("%s (%s)\n",PACKAGE_STRING,HOST_OS);
	printf("%s\n",COPYRIGHT_STRING);
	printf("This is free software.  You may redistribute copies of it"
	       " under the terms of\n");
	printf("the GNU General Public License"
	       " <http://www.gnu.org/licenses/gpl.html>.\n");
	printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
	exit(0);

      case OPT_VERBOSE:
      case 'v':
	verbose++;
	break;

      case OPT_OUTPUT:
      case 'o':
	outname=optarg;
	break;

      case OPT_INPUT_TYPE:
	if(strcmp(optarg,"auto")==0)
	  input_type=AUTO;
	else if(strcmp(optarg,"base16")==0)
	  input_type=BASE16;
	else if(strcmp(optarg,"raw")==0)
	  input_type=RAW;
	else
	  {
	    fprintf(stderr,"Unknown input type \"%s\"\n",optarg);
	    exit(1);
	  }
	break;

      case OPT_OUTPUT_TYPE:
	if(strcmp(optarg,"base16")==0)
	  output_type=BASE16;
	else if(strcmp(optarg,"raw")==0)
	  output_type=RAW;
	else
	  {
	    fprintf(stderr,"Unknown output type \"%s\"\n",optarg);
	    exit(1);
	  }
	break;

      case OPT_OUTPUT_WIDTH:
	output_width=atoi(optarg);
	break;

      case OPT_SECRET_KEY:
	secret_key=fopen(optarg,"rb");
	if(!secret_key)
	  {
	    fprintf(stderr,"Unable to open %s: %s\n",optarg,strerror(errno));
	    exit(1);
	  }
	break;

      case OPT_PUBRING:
	pubring=fopen(optarg,"rb");
	if(!pubring)
	  {
	    fprintf(stderr,"Unable to open pubring %s: %s\n",
		    optarg,strerror(errno));
	    exit(1);
	  }
	break;

      case OPT_SECRETS:
	secrets=fopen(optarg,"rb");
	if(!secrets)
	  {
	    fprintf(stderr,"Unable to open %s: %s\n",optarg,strerror(errno));
	    exit(1);
	  }
	break;

      case OPT_IGNORE_CRC_ERROR:
	ignore_crc_error=1;
	break;

      case OPT_FILE_FORMAT:
	output_file_format(stdout,"");
	exit(0);

      case OPT_COMMENT:
	comment=optarg;
	break;
      }

  if(pubring)
    err=restore(pubring,secrets,input_type,outname);
  else
    err=extract(secret_key,outname,output_type);

  return err;
}