Initial commit
[yaffs-website] / node_modules / sshpk / bin / sshpk-verify
1 #!/usr/bin/env node
2 // -*- mode: js -*-
3 // vim: set filetype=javascript :
4 // Copyright 2015 Joyent, Inc.  All rights reserved.
5
6 var dashdash = require('dashdash');
7 var sshpk = require('../lib/index');
8 var fs = require('fs');
9 var path = require('path');
10
11 var options = [
12         {
13                 names: ['hash', 'H'],
14                 type: 'string',
15                 help: 'Hash algorithm (sha1, sha256, sha384, sha512)'
16         },
17         {
18                 names: ['verbose', 'v'],
19                 type: 'bool',
20                 help: 'Display verbose info about key and hash used'
21         },
22         {
23                 names: ['identity', 'i'],
24                 type: 'string',
25                 help: 'Path to (public) key to use'
26         },
27         {
28                 names: ['file', 'f'],
29                 type: 'string',
30                 help: 'Input filename'
31         },
32         {
33                 names: ['format', 't'],
34                 type: 'string',
35                 help: 'Signature format (asn1, ssh, raw)'
36         },
37         {
38                 names: ['signature', 's'],
39                 type: 'string',
40                 help: 'base64-encoded signature data'
41         },
42         {
43                 names: ['help', 'h'],
44                 type: 'bool',
45                 help: 'Shows this help text'
46         }
47 ];
48
49 if (require.main === module) {
50         var parser = dashdash.createParser({
51                 options: options
52         });
53
54         try {
55                 var opts = parser.parse(process.argv);
56         } catch (e) {
57                 console.error('sshpk-verify: error: %s', e.message);
58                 process.exit(3);
59         }
60
61         if (opts.help || opts._args.length > 1) {
62                 var help = parser.help({}).trimRight();
63                 console.error('sshpk-verify: sign data using an SSH key\n');
64                 console.error(help);
65                 process.exit(3);
66         }
67
68         if (!opts.identity) {
69                 var help = parser.help({}).trimRight();
70                 console.error('sshpk-verify: the -i or --identity option ' +
71                     'is required\n');
72                 console.error(help);
73                 process.exit(3);
74         }
75
76         if (!opts.signature) {
77                 var help = parser.help({}).trimRight();
78                 console.error('sshpk-verify: the -s or --signature option ' +
79                     'is required\n');
80                 console.error(help);
81                 process.exit(3);
82         }
83
84         var keyData = fs.readFileSync(opts.identity);
85
86         var key;
87         try {
88                 key = sshpk.parseKey(keyData);
89         } catch (e) {
90                 console.error('sshpk-verify: error loading key "' +
91                     opts.identity + '": ' + e.name + ': ' + e.message);
92                 process.exit(2);
93         }
94
95         var fmt = opts.format || 'asn1';
96         var sigData = new Buffer(opts.signature, 'base64');
97
98         var sig;
99         try {
100                 sig = sshpk.parseSignature(sigData, key.type, fmt);
101         } catch (e) {
102                 console.error('sshpk-verify: error parsing signature: ' +
103                     e.name + ': ' + e.message);
104                 process.exit(2);
105         }
106
107         var hash = opts.hash || key.defaultHashAlgorithm();
108
109         var verifier;
110         try {
111                 verifier = key.createVerify(hash);
112         } catch (e) {
113                 console.error('sshpk-verify: error creating verifier: ' +
114                     e.name + ': ' + e.message);
115                 process.exit(2);
116         }
117
118         if (opts.verbose) {
119                 console.error('sshpk-verify: using %s-%s with a %d bit key',
120                     key.type, hash, key.size);
121         }
122
123         var inFile = process.stdin;
124         var inFileName = 'stdin';
125
126         var inFilePath;
127         if (opts.file) {
128                 inFilePath = opts.file;
129         } else if (opts._args.length === 1) {
130                 inFilePath = opts._args[0];
131         }
132
133         if (inFilePath)
134                 inFileName = path.basename(inFilePath);
135
136         try {
137                 if (inFilePath) {
138                         fs.accessSync(inFilePath, fs.R_OK);
139                         inFile = fs.createReadStream(inFilePath);
140                 }
141         } catch (e) {
142                 console.error('sshpk-verify: error opening input file' +
143                      ': ' + e.name + ': ' + e.message);
144                 process.exit(2);
145         }
146
147         inFile.pipe(verifier);
148         inFile.on('end', function () {
149                 var ret;
150                 try {
151                         ret = verifier.verify(sig);
152                 } catch (e) {
153                         console.error('sshpk-verify: error verifying data: ' +
154                             e.name + ': ' + e.message);
155                         process.exit(1);
156                 }
157
158                 if (ret) {
159                         console.error('OK');
160                         process.exit(0);
161                 }
162
163                 console.error('NOT OK');
164                 process.exit(1);
165         });
166 }