4 ### Key management and distribution
6 ### (c) 2006 Straylight/Edgeware
9 ###----- Licensing notice ---------------------------------------------------
11 ### This file is part of Trivial IP Encryption (TrIPE).
13 ### TrIPE is free software; you can redistribute it and/or modify
14 ### it under the terms of the GNU General Public License as published by
15 ### the Free Software Foundation; either version 2 of the License, or
16 ### (at your option) any later version.
18 ### TrIPE is distributed in the hope that it will be useful,
19 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ### GNU General Public License for more details.
23 ### You should have received a copy of the GNU General Public License
24 ### along with TrIPE; if not, write to the Free Software Foundation,
25 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ###--------------------------------------------------------------------------
28 ### External dependencies.
37 from cStringIO import StringIO
41 ###--------------------------------------------------------------------------
42 ### Useful regular expressions
44 ## Match a comment or blank line.
45 rx_comment = RX.compile(r'^\s*(#|$)')
47 ## Match a KEY = VALUE assignment.
48 rx_keyval = RX.compile(r'^\s*([-\w]+)(?:\s+(?!=)|\s*=\s*)(|\S|\S.*\S)\s*$')
50 ## Match a ${KEY} substitution.
51 rx_dollarsubst = RX.compile(r'\$\{([-\w]+)\}')
53 ## Match a @TAG@ substitution.
54 rx_atsubst = RX.compile(r'@([-\w]+)@')
56 ## Match a single non-alphanumeric character.
57 rx_nonalpha = RX.compile(r'\W')
59 ## Match the literal string "<SEQ>".
60 rx_seq = RX.compile(r'\<SEQ\>')
62 ## Match a shell metacharacter.
63 rx_shmeta = RX.compile('[\\s`!"#$&*()\\[\\];\'|<>?\\\\]')
65 ## Match a character which needs escaping in a shell double-quoted string.
66 rx_shquote = RX.compile(r'["`$\\]')
68 ###--------------------------------------------------------------------------
69 ### Utility functions.
72 class SubprocessError (Exception): pass
73 class VerifyError (Exception): pass
75 ## Program name and identification.
76 quis = OS.path.basename(SYS.argv[0])
81 """Report MSG to standard error."""
82 SYS.stderr.write('%s: %s\n' % (quis, msg))
85 """Report MSG to standard error, and exit with code RC."""
89 def subst(s, rx, map):
91 Substitute values into a string.
93 Repeatedly match RX (a compiled regular expression) against the string S.
94 For each match, extract group 1, and use it as a key to index the MAP;
95 replace the match by the result. Finally, return the fully-substituted
100 for m in rx.finditer(s):
101 out.write(s[i:m.start()] + map[m.group(1)])
104 return out.getvalue()
106 def shell_quotify(arg):
108 Quotify ARG to keep the shell happy.
110 This isn't actually used for invoking commands, just for presentation
111 purposes; but correctness is still nice.
113 if not rx_shmeta.search(arg):
115 elif arg.find("'") == -1:
118 return '"%s"' % rx_shquote.sub(lambda m: '\\' + m.group(0), arg)
121 """Delete the directory tree given by PATH."""
125 if err.errno == ENOENT:
128 if not S_ISDIR(st.st_mode):
134 for i in OS.listdir('.'):
141 """Delete the named FILE if it exists; otherwise do nothing."""
145 if err.errno == ENOENT: return
150 Run a subprocess whose arguments are given by the string ARGS.
152 The ARGS are split at word boundaries, and then subjected to configuration
153 variable substitution (see conf_subst). Individual argument elements
154 beginning with `!' are split again into multiple arguments at word
157 args = map(conf_subst, args.split())
160 if len(a) > 0 and a[0] != '!':
163 nargs += a[1:].split()
165 print '+ %s' % ' '.join([shell_quotify(arg) for arg in args])
167 rc = OS.spawnvp(OS.P_WAIT, args[0], args)
169 raise SubprocessError, rc
171 def hexhyphens(bytes):
173 Convert a byte string BYTES into hex, with hyphens at each 4-byte boundary.
176 for i in xrange(0, len(bytes)):
177 if i > 0 and i % 4 == 0: out.write('-')
178 out.write('%02x' % ord(bytes[i]))
179 return out.getvalue()
181 def fingerprint(kf, ktag):
183 Compute the fingerprint of a key, using the user's selected hash.
185 KF is the name of a keyfile; KTAG is the tag of the key.
187 h = C.gchashes[conf['fingerprint-hash']]()
188 k = C.KeyFile(kf)[ktag].fingerprint(h, '-secret')
191 ###--------------------------------------------------------------------------
192 ### The configuration file.
195 class ConfigFileError (Exception): pass
197 ## The configuration dictionary.
202 Apply configuration substitutions to S.
204 That is, for each ${KEY} in S, replace it with the current value of the
205 configuration variable KEY.
207 return subst(s, rx_dollarsubst, conf)
211 Read the file F and insert assignments into the configuration dictionary.
216 if rx_comment.match(line): continue
217 if line[-1] == '\n': line = line[:-1]
218 match = rx_keyval.match(line)
220 raise ConfigFileError, "%s:%d: bad line `%s'" % (f, lno, line)
221 k, v = match.groups()
222 conf[k] = conf_subst(v)
226 Apply defaults to the configuration dictionary.
228 Fill in all the interesting configuration variables based on the existing
229 contents, as described in the manual.
231 for k, v in [('repos-base', 'tripe-keys.tar.gz'),
232 ('sig-base', 'tripe-keys.sig-<SEQ>'),
233 ('repos-url', '${base-url}${repos-base}'),
234 ('sig-url', '${base-url}${sig-base}'),
235 ('sig-file', '${base-dir}${sig-base}'),
236 ('repos-file', '${base-dir}${repos-base}'),
237 ('conf-file', '${base-dir}tripe-keys.conf'),
238 ('upload-hook', ': run upload hook'),
240 ('kx-param', lambda: {'dh': '-LS -b3072 -B256',
241 'ec': '-Cnist-p256'}[conf['kx']]),
242 ('kx-expire', 'now + 1 year'),
243 ('cipher', 'rijndael-cbc'),
245 ('master-keygen-flags', '-l'),
246 ('mgf', '${hash}-mgf'),
247 ('mac', lambda: '%s-hmac/%d' %
249 C.gchashes[conf['hash']].hashsz * 4)),
250 ('sig', lambda: {'dh': 'dsa', 'ec': 'ecdsa'}[conf['kx']]),
251 ('sig-fresh', 'always'),
252 ('sig-genalg', lambda: {'kcdsa': 'dh',
257 'eckcdsa': 'ec'}[conf['sig']]),
258 ('sig-param', lambda: {'dh': '-LS -b3072 -B256',
259 'dsa': '-b3072 -B256',
261 'rsa': '-b3072'}[conf['sig-genalg']]),
262 ('sig-hash', '${hash}'),
263 ('sig-expire', 'forever'),
264 ('fingerprint-hash', '${hash}')]:
266 if k in conf: continue
268 conf[k] = conf_subst(v)
271 except KeyError, exc:
272 if len(exc.args) == 0: raise
273 conf[k] = '<missing-var %s>' % exc.args[0]
275 ###--------------------------------------------------------------------------
276 ### Key-management utilities.
280 Iterate over the master keys.
282 if not OS.path.exists('master'):
284 for k in C.KeyFile('master').itervalues():
285 if (k.type != 'tripe-keys-master' or
287 not k.tag.startswith('master-')):
291 def master_sequence(k):
293 Return the sequence number of the given master key as an integer.
295 No checking is done that K is really a master key.
297 return int(k.tag[7:])
299 def max_master_sequence():
301 Find the master key with the highest sequence number and return this
305 for k in master_keys():
306 q = master_sequence(k)
312 Return the value of the configuration variable X, with <SEQ> replaced by
315 return rx_seq.sub(str(q), conf[x])
317 ###--------------------------------------------------------------------------
318 ### Commands: help [COMMAND...]
320 def version(fp = SYS.stdout):
321 fp.write('%s, %s version %s\n' % (quis, PACKAGE, VERSION))
324 fp.write('Usage: %s SUBCOMMAND [ARGS...]\n' % quis)
332 Key management utility for TrIPE.
336 -h, --help Show this help message.
337 -v, --version Show the version number.
338 -u, --usage Show pointlessly short usage string.
340 Subcommands available:
342 args = commands.keys()
345 func, min, max, help = commands[c]
346 print '%s %s' % (c, help)
348 ###--------------------------------------------------------------------------
349 ### Commands: newmaster
351 def cmd_newmaster(args):
352 seq = max_master_sequence() + 1
353 run('''key -kmaster add
354 -a${sig-genalg} !${sig-param}
355 -e${sig-expire} !${master-keygen-flags} -tmaster-%d tripe-keys-master
356 sig=${sig} hash=${sig-hash}''' % seq)
357 run('key -kmaster extract -f-secret repos/master.pub')
359 ###--------------------------------------------------------------------------
364 run('''key -krepos/param add
365 -a${kx}-param !${kx-param}
366 -eforever -tparam tripe-${kx}-param
367 cipher=${cipher} hash=${hash} mac=${mac} mgf=${mgf}''')
370 ###--------------------------------------------------------------------------
373 def cmd_upload(args):
375 ## Sanitize the repository directory
376 umask = OS.umask(0); OS.umask(umask)
378 for f in OS.listdir('repos'):
379 ff = OS.path.join('repos', f)
380 if (f.startswith('master') or f.startswith('peer-')) \
381 and f.endswith('.old'):
388 OS.symlink('../repos', 'tmp/repos')
392 ## Build the configuration file
393 seq = max_master_sequence()
394 v = {'MASTER-SEQUENCE': str(seq),
395 'HK-MASTER': hexhyphens(fingerprint('repos/master.pub',
397 fin = file('tripe-keys.master')
398 fout = file('tmp/tripe-keys.conf', 'w')
400 fout.write(subst(line, rx_atsubst, v))
401 fin.close(); fout.close()
402 SH.copyfile('tmp/tripe-keys.conf', conf_subst('${conf-file}.new'))
403 commit = [conf['repos-file'], conf['conf-file']]
405 ## Make and sign the repository archive
407 run('tar chozf ${repos-file}.new .')
409 for k in master_keys():
410 seq = master_sequence(k)
411 sigfile = seqsubst('sig-file', seq)
412 run('''catsign -kmaster sign -abdC -kmaster-%d
413 -o%s.new ${repos-file}.new''' % (seq, sigfile))
414 commit.append(sigfile)
416 ## Commit the changes
418 new = '%s.new' % base
423 run('sh -c ${upload-hook}')
425 ###--------------------------------------------------------------------------
426 ### Commands: rebuild
428 def cmd_rebuild(args):
430 for i in OS.listdir('repos'):
431 if i.startswith('peer-') and i.endswith('.pub'):
432 run('key -kkeyring.pub merge %s' % OS.path.join('repos', i))
434 ###--------------------------------------------------------------------------
437 def cmd_update(args):
442 ## Fetch a new distribution
445 seq = int(conf['master-sequence'])
446 run('curl -s -o tripe-keys.tar.gz ${repos-url}')
447 run('curl -s -o tripe-keys.sig %s' % seqsubst('sig-url', seq))
448 run('tar xfz tripe-keys.tar.gz')
450 ## Verify the signature
451 want = C.bytes(rx_nonalpha.sub('', conf['hk-master']))
452 got = fingerprint('repos/master.pub', 'master-%d' % seq)
453 if want != got: raise VerifyError
454 run('''catsign -krepos/master.pub verify -avC -kmaster-%d
455 -t${sig-fresh} tripe-keys.sig tripe-keys.tar.gz''' % seq)
457 ## OK: update our copy
459 if OS.path.exists('repos'): OS.rename('repos', 'repos.old')
460 OS.rename('tmp/repos', 'repos')
461 if not FC.cmp('tmp/tripe-keys.conf', 'tripe-keys.conf'):
462 moan('configuration file changed: recommend running another update')
463 OS.rename('tmp/tripe-keys.conf', 'tripe-keys.conf')
471 ###--------------------------------------------------------------------------
472 ### Commands: generate TAG
474 def cmd_generate(args):
476 keyring_pub = 'peer-%s.pub' % tag
477 zap('keyring'); zap(keyring_pub)
478 run('key -kkeyring merge repos/param')
479 run('key -kkeyring add -a${kx} -pparam -e${kx-expire} -t%s tripe-${kx}' %
481 run('key -kkeyring extract -f-secret %s %s' % (keyring_pub, tag))
483 ###--------------------------------------------------------------------------
489 for i in OS.listdir('.'):
491 if r.endswith('.old'): r = r[:-4]
492 if (r == 'master' or r == 'param' or
493 r == 'keyring' or r == 'keyring.pub' or r.startswith('peer-')):
496 ###--------------------------------------------------------------------------
500 mtu, = (lambda mtu = '1500': (mtu,))(*args)
503 blksz = C.gcciphers[conf['cipher']].blksz
505 index = conf['mac'].find('/')
507 tagsz = C.gcmacs[conf['mac']].tagsz
509 tagsz = int(conf['mac'][index + 1:])/8
511 mtu -= 20 # Minimum IP header
512 mtu -= 8 # UDP header
513 mtu -= 1 # TrIPE packet type octet
514 mtu -= tagsz # MAC tag
515 mtu -= 4 # Sequence number
516 mtu -= blksz # Initialization vector
520 ###--------------------------------------------------------------------------
524 class UsageError (Exception): pass
526 commands = {'help': (cmd_help, 0, 1, ''),
527 'newmaster': (cmd_newmaster, 0, 0, ''),
528 'setup': (cmd_setup, 0, 0, ''),
529 'upload': (cmd_upload, 0, 0, ''),
530 'update': (cmd_update, 0, 0, ''),
531 'clean': (cmd_clean, 0, 0, ''),
532 'mtu': (cmd_mtu, 0, 1, '[PATH-MTU]'),
533 'generate': (cmd_generate, 1, 1, 'TAG'),
534 'rebuild': (cmd_rebuild, 0, 0, '')}
538 Load the appropriate configuration file and set up the configuration
541 for f in ['tripe-keys.master', 'tripe-keys.conf']:
542 if OS.path.exists(f):
549 Main program: parse options and dispatch to appropriate command handler.
552 opts, args = O.getopt(argv[1:], 'hvu',
553 ['help', 'version', 'usage'])
554 except O.GetoptError, exc:
559 if o in ('-h', '--help'):
562 elif o in ('-v', '--version'):
565 elif o in ('-u', '--usage'):
572 func, min, max, help = commands[c]
574 if len(args) < min or (max > 0 and len(args) > max):
575 raise UsageError, (c, help)
578 ###----- That's all, folks --------------------------------------------------
580 if __name__ == '__main__':