3 ### Management of a secure password database
5 ### (c) 2005 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to Catacomb.
12 ### Catacomb/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
17 ### Catacomb/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
22 ### You should have received a copy of the GNU General Public License along
23 ### with Catacomb/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ###--------------------------------------------------------------------------
29 from __future__ import with_statement
36 ###--------------------------------------------------------------------------
37 ### Underlying cryptography.
39 class DecryptError (Exception):
41 I represent a failure to decrypt a message.
43 Usually this means that someone used the wrong key, though it can also
44 mean that a ciphertext has been modified.
48 class Crypto (object):
50 I represent a symmetric crypto transform.
52 There's currently only one transform implemented, which is the obvious
53 generic-composition construction: given a message m, and keys K0 and K1, we
54 choose an IV v, and compute:
56 * y = v || E(K0, v; m)
59 The final ciphertext is t || y.
62 def __init__(me, c, h, m, ck, mk):
64 Initialize the Crypto object with a given algorithm selection and keys.
66 We need a GCipher subclass C, a GHash subclass H, a GMAC subclass M, and
67 keys CK and MK for C and M respectively.
75 Encrypt the message PT and return the resulting ciphertext.
77 blksz = me.c.__class__.blksz
80 iv = _C.rand.block(blksz)
83 b.put(me.c.encrypt(pt))
84 t = me.m().hash(b).done()
85 return t + str(buffer(b))
89 Decrypt the ciphertext CT, returning the plaintext.
91 Raises DecryptError if anything goes wrong.
93 blksz = me.c.__class__.blksz
94 tagsz = me.m.__class__.tagsz
104 if t != h.done(): raise DecryptError
105 return me.c.decrypt(x)
109 I represent a crypto transform whose keys are derived from a passphrase.
111 The password is salted and hashed; the salt is available as the `salt'
115 def __init__(me, pp, c, h, m, salt = None):
117 Initialize the PPK object with a passphrase and algorithm selection.
119 We want a passphrase PP, a GCipher subclass C, a GHash subclass H, a GMAC
120 subclass M, and a SALT. The SALT may be None, if we're generating new
121 keys, indicating that a salt should be chosen randomly.
123 if not salt: salt = _C.rand.block(h.hashsz)
124 tag = '%s\0%s' % (pp, salt)
125 Crypto.__init__(me, c, h, m,
126 h().hash('cipher:' + tag).done(),
127 h().hash('mac:' + tag).done())
130 ###--------------------------------------------------------------------------
133 class StorageBackendRefusal (Exception):
135 I signify that a StorageBackend subclass has refused to open a file.
137 This is used by the StorageBackend.open class method.
141 class StorageBackendClass (type):
143 I am a metaclass for StorageBackend classes.
145 My main feature is that I register my concrete instances (with a `NAME'
146 which is not `None') with the StorageBackend class.
148 def __init__(me, name, supers, dict):
150 Register a new concrete StorageBackend subclass.
152 super(StorageBackendClass, me).__init__(name, supers, dict)
153 if me.NAME is not None: StorageBackend.register_concrete_subclass(me)
155 class StorageBackend (object):
157 I provide basic protocol for password storage backends.
159 I'm an abstract class: you want one of my subclasses if you actually want
160 to do something useful. But I maintain a list of my subclasses and can
161 choose an appropriate one to open a database file you've found lying about.
163 Backends are responsible for storing and retrieving stuff, but not for the
164 cryptographic details. Backends need to store two kinds of information:
166 * metadata, consisting of a number of property names and their values;
169 * password mappings, consisting of a number of binary labels and
172 Backends need to implement the following ordinary methods. See the calling
173 methods for details of the subclass responsibilities.
175 BE._create(FILE) Create a new database in FILE; used by `create'.
177 BE._open(FILE, WRITEP)
178 Open the existing database FILE; used by `open'.
180 BE._close() Close the database, freeing up any resources.
182 BE._get_meta(NAME, DEFAULT)
183 Return the value of the metadata item with the given
184 NAME, or DEFAULT if it doesn't exist; used by
187 BE._put_meta(NAME, VALUE)
188 Set the VALUE of the metadata item with the given
189 NAME, creating one if necessary; used by `put_meta'.
191 BE._del_meta(NAME) Forget the metadata item with the given NAME; raise
192 `KeyError' if there is no such item; used by
195 BE._iter_meta() Return an iterator over the metadata (NAME, VALUE)
196 pairs; used by `iter_meta'.
198 BE._get_passwd(LABEL)
199 Return the password payload stored with the (binary)
200 LABEL; used by `get_passwd'.
202 BE._put_passwd(LABEL, PAYLOAD)
203 Associate the (binary) PAYLOAD with the LABEL,
204 forgetting any previous payload for that LABEL; used
207 BE._del_passwd(LABEL) Forget the password record with the given LABEL; used
210 BE._iter_passwds() Return an iterator over the password (LABEL, PAYLOAD)
211 pairs; used by `iter_passwds'.
213 Also, concrete subclasses should define the following class attributes.
215 NAME The name of the backend, so that the user can select
216 it when creating a new database.
218 PRIO An integer priority: backends are tried in decreasing
219 priority order when opening an existing database.
222 __metaclass__ = StorageBackendClass
226 ## The registry of subclasses.
232 def register_concrete_subclass(sub):
233 """Register a concrete subclass, so that `open' can try it."""
234 StorageBackend.CLASSES[sub.NAME] = sub
239 Return the concrete subclass with the given NAME.
241 Raise `KeyError' if the name isn't found.
243 return StorageBackend.CLASSES[name]
247 """Return an iterator over the concrete subclasses."""
248 return StorageBackend.CLASSES.itervalues()
251 def open(file, writep = False):
252 """Open a database FILE, using some appropriate backend."""
254 for cls in sorted(StorageBackend.CLASSES.values(), reverse = True,
255 key = lambda cls: cls.PRIO):
256 try: return cls(file, writep)
257 except StorageBackendRefusal: pass
258 raise StorageBackendRefusal
261 def create(cls, file):
263 Create a new database in the named FILE, using this backend.
265 Subclasses must implement the `_create' instance method.
267 return cls(writep = True, _magic = lambda me: me._create(file))
269 def __init__(me, file = None, writep = False, _magic = None, *args, **kw):
273 Subclasses are not, in general, expected to override this: there's a
274 somewhat hairy protocol between the constructor and some of the class
275 methods. Instead, the main hook for customization is the subclass's
276 `_open' method, which is invoked in the usual case.
278 super(StorageBackend, me).__init__(*args, **kw)
279 if me.NAME is None: raise ValueError, 'abstract class'
280 if _magic is not None: _magic(me)
281 elif file is None: raise ValueError, 'missing file parameter'
282 else: me._open(file, writep)
290 It is harmless to attempt to close a database which has been closed
291 already. Calls the subclass's `_close' method.
300 """Raise an error if the receiver has been closed."""
301 if not me._livep: raise ValueError, 'database is closed'
303 def _check_write(me):
304 """Raise an error if the receiver is not open for writing."""
306 if not me._writep: raise ValueError, 'database is read-only'
308 def _check_meta_name(me, name):
310 Raise an error unless NAME is a valid name for a metadata item.
312 Metadata names may not start with `$': such names are reserved for
315 if name.startswith('$'):
316 raise ValueError, "invalid metadata key `%s'" % name
321 """Context protocol: make sure the database is closed on exit."""
323 def __exit__(me, exctype, excvalue, exctb):
324 """Context protocol: see `__enter__'."""
329 def get_meta(me, name, default = FAIL):
331 Fetch the value for the metadata item NAME.
333 If no such item exists, then return DEFAULT if that was set; otherwise
336 This calls the subclass's `_get_meta' method, which should return the
337 requested item or return the given DEFAULT value. It may assume that the
338 name is valid and the database is open.
340 me._check_meta_name(name)
342 value = me._get_meta(name, default)
343 if value is StorageBackend.FAIL: raise KeyError, name
346 def put_meta(me, name, value):
348 Store VALUE in the metadata item called NAME.
350 This calls the subclass's `_put_meta' method, which may assume that the
351 name is valid and the database is open for writing.
353 me._check_meta_name(name)
355 me._put_meta(name, value)
357 def del_meta(me, name):
359 Forget about the metadata item with the given NAME.
361 This calls the subclass's `_del_meta' method, which may assume that the
362 name is valid and the database is open for writing.
364 me._check_meta_name(name)
370 Return an iterator over the name/value metadata items.
372 This calls the subclass's `_iter_meta' method, which may assume that the
376 return me._iter_meta()
378 def get_passwd(me, label):
380 Fetch and return the payload stored with the (opaque, binary) LABEL.
382 If there is no such payload then raise `KeyError'.
384 This calls the subclass's `_get_passwd' method, which may assume that the
388 return me._get_passwd(label)
390 def put_passwd(me, label, payload):
392 Associate the (opaque, binary) PAYLOAD with the (opaque, binary) LABEL.
394 Any previous payload for LABEL is forgotten.
396 This calls the subclass's `_put_passwd' method, which may assume that the
397 database is open for writing.
400 me._put_passwd(label, payload)
402 def del_passwd(me, label):
404 Forget any PAYLOAD associated with the (opaque, binary) LABEL.
406 If there is no such payload then raise `KeyError'.
408 This calls the subclass's `_del_passwd' method, which may assume that the
409 database is open for writing.
412 me._del_passwd(label, payload)
414 def iter_passwds(me):
416 Return an iterator over the stored password label/payload pairs.
418 This calls the subclass's `_iter_passwds' method, which may assume that
419 the database is open.
422 return me._iter_passwds()
424 class GDBMStorageBackend (StorageBackend):
426 My instances store password data in a GDBM database.
428 Metadata and password entries are mixed into the same database. The key
429 for a metadata item is simply its name; the key for a password entry is
430 the entry's label prefixed by `$', since we're guaranteed that no
431 metadata item name begins with `$'.
436 def _open(me, file, writep):
437 try: me._db = _G.open(file, writep and 'w' or 'r')
438 except _G.error, e: raise StorageBackendRefusal, e
440 def _create(me, file):
441 me._db = _G.open(file, 'n', 0600)
447 def _get_meta(me, name, default):
448 try: return me._db[name]
449 except KeyError: return default
451 def _put_meta(me, name, value):
454 def _del_meta(me, name):
458 k = me._db.firstkey()
460 if not k.startswith('$'): yield k, me._db[k]
461 k = me._db.nextkey(k)
463 def _get_passwd(me, label):
464 return me._db['$' + label]
466 def _put_passwd(me, label, payload):
467 me._db['$' + label] = payload
469 def _del_passwd(me, label):
470 del me._db['$' + label]
472 def _iter_passwds(me):
473 k = me._db.firstkey()
475 if k.startswith('$'): yield k[1:], me._db[k]
476 k = me._db.nextkey(k)
478 ###--------------------------------------------------------------------------
479 ### Password storage.
483 I represent a secure (ish) password store.
485 I can store short secrets, associated with textual names, in a way which
486 doesn't leak too much information about them.
488 I implement (some of) the Python mapping protocol.
490 I keep track of everything using a StorageBackend object. This contains
491 password entries, identified by cryptographic labels, and a number of
494 cipher Names the Catacomb cipher selected.
496 hash Names the Catacomb hash function selected.
498 key Cipher and MAC keys, each prefixed by a 16-bit big-endian
499 length and concatenated, encrypted using the master
502 mac Names the Catacomb message authentication code selected.
504 magic A magic string for obscuring password tag names.
506 salt The salt for hashing the passphrase.
508 tag The master passphrase's tag, for the Pixie's benefit.
510 Password entries are assigned labels of the form `$' || H(MAGIC || TAG);
511 the corresponding value consists of a pair (TAG, PASSWD), prefixed with
512 16-bit lengths, concatenated, padded to a multiple of 256 octets, and
513 encrypted using the stored keys.
516 def __init__(me, file, writep = False):
518 Initialize a PW object from the database in FILE.
520 If WRITEP is false (the default) then the database is opened read-only;
521 if true then it may be written. Requests the database password from the
522 Pixie, which may cause interaction.
525 ## Open the database.
526 me.db = StorageBackend.open(file, writep)
528 ## Find out what crypto to use.
529 c = _C.gcciphers[me.db.get_meta('cipher')]
530 h = _C.gchashes[me.db.get_meta('hash')]
531 m = _C.gcmacs[me.db.get_meta('mac')]
533 ## Request the passphrase and extract the master keys.
534 tag = me.db.get_meta('tag')
535 ppk = PPK(_C.ppread(tag), c, h, m, me.db.get_meta('salt'))
537 b = _C.ReadBuffer(ppk.decrypt(me.db.get_meta('key')))
543 if not b.endp: raise ValueError, 'trailing junk'
545 ## Set the key, and stash it and the tag-hashing secret.
546 me.k = Crypto(c, h, m, me.ck, me.mk)
547 me.magic = me.k.decrypt(me.db.get_meta('magic'))
550 def create(cls, dbcls, file, tag, c, h, m):
552 Create and initialize a new database FILE using StorageBackend DBCLS.
554 We want a GCipher subclass C, a GHash subclass H, and a GMAC subclass M;
555 and a Pixie passphrase TAG.
557 This doesn't return a working object: it just creates the database file
558 and gets out of the way.
561 ## Set up the cryptography.
562 pp = _C.ppread(tag, _C.PMODE_VERIFY)
563 ppk = PPK(pp, c, h, m)
564 ck = _C.rand.block(c.keysz.default)
565 mk = _C.rand.block(c.keysz.default)
566 k = Crypto(c, h, m, ck, mk)
568 ## Set up and initialize the database.
569 kct = ppk.encrypt(_C.WriteBuffer().putblk16(ck).putblk16(mk))
570 with dbcls.create(file) as db:
571 db.put_meta('tag', tag)
572 db.put_meta('salt', ppk.salt)
573 db.put_meta('cipher', c.name)
574 db.put_meta('hash', h.name)
575 db.put_meta('mac', m.name)
576 db.put_meta('key', kct)
577 db.put_meta('magic', k.encrypt(_C.rand.block(h.hashsz)))
579 def keyxform(me, key):
580 """Transform the KEY (actually a password tag) into a password label."""
581 return me.k.h().hash(me.magic).hash(key).done()
585 Change the database password.
587 Requests the new password from the Pixie, which will probably cause
590 tag = me.db.get_meta('tag')
592 ppk = PPK(_C.ppread(tag, _C.PMODE_VERIFY),
593 me.k.c.__class__, me.k.h, me.k.m.__class__)
594 kct = ppk.encrypt(_C.WriteBuffer().putblk16(me.ck).putblk16(me.mk))
595 me.db.put_meta('key', kct)
596 me.db.put_meta('salt', ppk.salt)
598 def pack(me, key, value):
599 """Pack the KEY and VALUE into a ciphertext, and return it."""
601 b.putblk16(key).putblk16(value)
602 b.zero(((b.size + 255) & ~255) - b.size)
603 return me.k.encrypt(b)
607 Unpack a ciphertext CT and return a (KEY, VALUE) pair.
609 Might raise DecryptError, of course.
611 b = _C.ReadBuffer(me.k.decrypt(ct))
618 def __getitem__(me, key):
619 """Return the password for the given KEY."""
620 try: return me.unpack(me.db.get_passwd(me.keyxform(key)))[1]
621 except KeyError: raise KeyError, key
623 def __setitem__(me, key, value):
624 """Associate the password VALUE with the KEY."""
625 me.db.put_passwd(me.keyxform(key), me.pack(key, value))
627 def __delitem__(me, key):
628 """Forget all about the KEY."""
629 try: me.db.del_passwd(me.keyxform(key))
630 except KeyError: raise KeyError, key
633 """Iterate over the known password tags."""
634 for _, pld in me.db.iter_passwds():
635 yield me.unpack(pld)[0]
641 def __exit__(me, excty, excval, exctb):
644 ###----- That's all, folks --------------------------------------------------