774021219da901fea1c0320826c1cbf28279e821
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
33 from cStringIO
import StringIO
as _StringIO
37 ###--------------------------------------------------------------------------
38 ### Text encoding utilities.
42 Answer whether S can be represented literally.
44 If True, then S can be stored literally, as a metadata item name or
45 value; if False, then S requires some kind of encoding.
47 return all(ch
.isalnum() or ch
in '-_:' for ch
in s
)
49 def _enc_metaname(name
):
50 """Encode NAME as a metadata item name, returning the result."""
57 if _literalp(ch
): sio
.write(ch
)
58 elif ch
== ' ': sio
.write('+')
59 else: sio
.write('%%%02x' % ord
(ch
))
62 def _dec_metaname(name
):
63 """Decode NAME as a metadata item name, returning the result."""
64 if not name
.startswith('!'):
75 sio
.write(chr(int(name
[i
:i
+ 2], 16)))
82 """Encode S as base64, without newlines, and trimming `=' padding."""
83 return s
.encode('base64').replace('\n', '').rstrip('=')
85 """Decode S as base64 with trimmed `=' padding."""
86 return (s
+ '='*((4 - len(s
))%4)).decode('base64')
88 def _enc_metaval(val
):
89 """Encode VAL as a metadata item value, returning the result."""
90 if _literalp(val
): return val
91 else: return '?' + _b64(val
)
93 def _dec_metaval(val
):
94 """Decode VAL as a metadata item value, returning the result."""
95 if not val
.startswith('?'): return val
96 else: return _unb64(val
[1:])
98 ###--------------------------------------------------------------------------
99 ### Underlying cryptography.
101 class DecryptError (Exception):
103 I represent a failure to decrypt a message.
105 Usually this means that someone used the wrong key, though it can also
106 mean that a ciphertext has been modified.
110 class Crypto (object):
112 I represent a symmetric crypto transform.
114 There's currently only one transform implemented, which is the obvious
115 generic-composition construction: given a message m, and keys K0 and K1, we
116 choose an IV v, and compute:
118 * y = v || E(K0, v; m)
121 The final ciphertext is t || y.
124 def __init__(me
, c
, h
, m
, ck
, mk
):
126 Initialize the Crypto object with a given algorithm selection and keys.
128 We need a GCipher subclass C, a GHash subclass H, a GMAC subclass M, and
129 keys CK and MK for C and M respectively.
137 Encrypt the message PT and return the resulting ciphertext.
139 blksz
= me
.c
.__class__
.blksz
142 iv
= _C
.rand
.block(blksz
)
145 b
.put(me
.c
.encrypt(pt
))
146 t
= me
.m().hash(b
).done()
147 return t
+ str(buffer(b
))
151 Decrypt the ciphertext CT, returning the plaintext.
153 Raises DecryptError if anything goes wrong.
155 blksz
= me
.c
.__class__
.blksz
156 tagsz
= me
.m
.__class__
.tagsz
157 b
= _C
.ReadBuffer(ct
)
166 if t
!= h
.done(): raise DecryptError
167 return me
.c
.decrypt(x
)
171 I represent a crypto transform whose keys are derived from a passphrase.
173 The password is salted and hashed; the salt is available as the `salt'
177 def __init__(me
, pp
, c
, h
, m
, salt
= None):
179 Initialize the PPK object with a passphrase and algorithm selection.
181 We want a passphrase PP, a GCipher subclass C, a GHash subclass H, a GMAC
182 subclass M, and a SALT. The SALT may be None, if we're generating new
183 keys, indicating that a salt should be chosen randomly.
185 if not salt
: salt
= _C
.rand
.block(h
.hashsz
)
186 tag
= '%s\0%s' %
(pp
, salt
)
187 Crypto
.__init__(me
, c
, h
, m
,
188 h().hash('cipher:' + tag
).done(),
189 h().hash('mac:' + tag
).done())
192 ###--------------------------------------------------------------------------
195 class StorageBackendRefusal (Exception):
197 I signify that a StorageBackend subclass has refused to open a file.
199 This is used by the StorageBackend.open class method.
203 class StorageBackendClass (type):
205 I am a metaclass for StorageBackend classes.
207 My main feature is that I register my concrete instances (with a `NAME'
208 which is not `None') with the StorageBackend class.
210 def __init__(me
, name
, supers
, dict):
212 Register a new concrete StorageBackend subclass.
214 super(StorageBackendClass
, me
).__init__(name
, supers
, dict)
215 if me
.NAME
is not None: StorageBackend
.register_concrete_subclass(me
)
217 class StorageBackend (object):
219 I provide basic protocol for password storage backends.
221 I'm an abstract class: you want one of my subclasses if you actually want
222 to do something useful. But I maintain a list of my subclasses and can
223 choose an appropriate one to open a database file you've found lying about.
225 Backends are responsible for storing and retrieving stuff, but not for the
226 cryptographic details. Backends need to store two kinds of information:
228 * metadata, consisting of a number of property names and their values;
231 * password mappings, consisting of a number of binary labels and
234 Backends need to implement the following ordinary methods. See the calling
235 methods for details of the subclass responsibilities.
237 BE._create(FILE) Create a new database in FILE; used by `create'.
239 BE._open(FILE, WRITEP)
240 Open the existing database FILE; used by `open'.
242 BE._close(ABRUPTP) Close the database, freeing up any resources. If
243 ABRUPTP then don't try to commit changes.
245 BE._get_meta(NAME, DEFAULT)
246 Return the value of the metadata item with the given
247 NAME, or DEFAULT if it doesn't exist; used by
250 BE._put_meta(NAME, VALUE)
251 Set the VALUE of the metadata item with the given
252 NAME, creating one if necessary; used by `put_meta'.
254 BE._del_meta(NAME) Forget the metadata item with the given NAME; raise
255 `KeyError' if there is no such item; used by
258 BE._iter_meta() Return an iterator over the metadata (NAME, VALUE)
259 pairs; used by `iter_meta'.
261 BE._get_passwd(LABEL)
262 Return the password payload stored with the (binary)
263 LABEL; used by `get_passwd'.
265 BE._put_passwd(LABEL, PAYLOAD)
266 Associate the (binary) PAYLOAD with the LABEL,
267 forgetting any previous payload for that LABEL; used
270 BE._del_passwd(LABEL) Forget the password record with the given LABEL; used
273 BE._iter_passwds() Return an iterator over the password (LABEL, PAYLOAD)
274 pairs; used by `iter_passwds'.
276 Also, concrete subclasses should define the following class attributes.
278 NAME The name of the backend, so that the user can select
279 it when creating a new database.
281 PRIO An integer priority: backends are tried in decreasing
282 priority order when opening an existing database.
285 __metaclass__
= StorageBackendClass
289 ## The registry of subclasses.
295 def register_concrete_subclass(sub
):
296 """Register a concrete subclass, so that `open' can try it."""
297 StorageBackend
.CLASSES
[sub
.NAME
] = sub
302 Return the concrete subclass with the given NAME.
304 Raise `KeyError' if the name isn't found.
306 return StorageBackend
.CLASSES
[name
]
310 """Return an iterator over the concrete subclasses."""
311 return StorageBackend
.CLASSES
.itervalues()
314 def open(file, writep
= False):
315 """Open a database FILE, using some appropriate backend."""
317 for cls
in sorted(StorageBackend
.CLASSES
.values(), reverse
= True,
318 key
= lambda cls
: cls
.PRIO
):
319 try: return cls(file, writep
)
320 except StorageBackendRefusal
: pass
321 raise StorageBackendRefusal
324 def create(cls
, file):
326 Create a new database in the named FILE, using this backend.
328 Subclasses must implement the `_create' instance method.
330 return cls(writep
= True, _magic
= lambda me
: me
._create(file))
332 def __init__(me
, file = None, writep
= False, _magic
= None, *args
, **kw
):
336 Subclasses are not, in general, expected to override this: there's a
337 somewhat hairy protocol between the constructor and some of the class
338 methods. Instead, the main hook for customization is the subclass's
339 `_open' method, which is invoked in the usual case.
341 super(StorageBackend
, me
).__init__(*args
, **kw
)
342 if me
.NAME
is None: raise ValueError('abstract class')
343 if _magic
is not None: _magic(me
)
344 elif file is None: raise ValueError('missing file parameter')
345 else: me
._open(file, writep
)
349 def close(me
, abruptp
= False):
353 It is harmless to attempt to close a database which has been closed
354 already. Calls the subclass's `_close' method.
363 """Raise an error if the receiver has been closed."""
364 if not me
._livep
: raise ValueError('database is closed')
366 def _check_write(me
):
367 """Raise an error if the receiver is not open for writing."""
369 if not me
._writep
: raise ValueError('database is read-only')
371 def _check_meta_name(me
, name
):
373 Raise an error unless NAME is a valid name for a metadata item.
375 Metadata names may not start with `$': such names are reserved for
378 if name
.startswith('$'):
379 raise ValueError("invalid metadata key `%s'" % name
)
384 """Context protocol: make sure the database is closed on exit."""
386 def __exit__(me
, exctype
, excvalue
, exctb
):
387 """Context protocol: see `__enter__'."""
388 me
.close(excvalue
is not None)
392 def get_meta(me
, name
, default
= FAIL
):
394 Fetch the value for the metadata item NAME.
396 If no such item exists, then return DEFAULT if that was set; otherwise
399 This calls the subclass's `_get_meta' method, which should return the
400 requested item or return the given DEFAULT value. It may assume that the
401 name is valid and the database is open.
403 me
._check_meta_name(name
)
405 value
= me
._get_meta(name
, default
)
406 if value
is StorageBackend
.FAIL
: raise KeyError(name
)
409 def put_meta(me
, name
, value
):
411 Store VALUE in the metadata item called NAME.
413 This calls the subclass's `_put_meta' method, which may assume that the
414 name is valid and the database is open for writing.
416 me
._check_meta_name(name
)
418 me
._put_meta(name
, value
)
420 def del_meta(me
, name
):
422 Forget about the metadata item with the given NAME.
424 This calls the subclass's `_del_meta' method, which may assume that the
425 name is valid and the database is open for writing.
427 me
._check_meta_name(name
)
433 Return an iterator over the name/value metadata items.
435 This calls the subclass's `_iter_meta' method, which may assume that the
439 return me
._iter_meta()
441 def get_passwd(me
, label
):
443 Fetch and return the payload stored with the (opaque, binary) LABEL.
445 If there is no such payload then raise `KeyError'.
447 This calls the subclass's `_get_passwd' method, which may assume that the
451 return me
._get_passwd(label
)
453 def put_passwd(me
, label
, payload
):
455 Associate the (opaque, binary) PAYLOAD with the (opaque, binary) LABEL.
457 Any previous payload for LABEL is forgotten.
459 This calls the subclass's `_put_passwd' method, which may assume that the
460 database is open for writing.
463 me
._put_passwd(label
, payload
)
465 def del_passwd(me
, label
):
467 Forget any PAYLOAD associated with the (opaque, binary) LABEL.
469 If there is no such payload then raise `KeyError'.
471 This calls the subclass's `_del_passwd' method, which may assume that the
472 database is open for writing.
475 me
._del_passwd(label
)
477 def iter_passwds(me
):
479 Return an iterator over the stored password label/payload pairs.
481 This calls the subclass's `_iter_passwds' method, which may assume that
482 the database is open.
485 return me
._iter_passwds()
487 try: import gdbm
as _G
488 except ImportError: pass
490 class GDBMStorageBackend (StorageBackend
):
492 My instances store password data in a GDBM database.
494 Metadata and password entries are mixed into the same database. The key
495 for a metadata item is simply its name; the key for a password entry is
496 the entry's label prefixed by `$', since we're guaranteed that no
497 metadata item name begins with `$'.
502 def _open(me
, file, writep
):
503 try: me
._db
= _G
.open(file, writep
and 'w' or 'r')
504 except _G
.error
, e
: raise StorageBackendRefusal(e
)
506 def _create(me
, file):
507 me
._db
= _G
.open(file, 'n', 0600)
509 def _close(me
, abruptp
):
513 def _get_meta(me
, name
, default
):
514 try: return me
._db
[name
]
515 except KeyError: return default
517 def _put_meta(me
, name
, value
):
520 def _del_meta(me
, name
):
524 k
= me
._db
.firstkey()
526 if not k
.startswith('$'): yield k
, me
._db
[k
]
527 k
= me
._db
.nextkey(k
)
529 def _get_passwd(me
, label
):
530 return me
._db
['$' + label
]
532 def _put_passwd(me
, label
, payload
):
533 me
._db
['$' + label
] = payload
535 def _del_passwd(me
, label
):
536 del me
._db
['$' + label
]
538 def _iter_passwds(me
):
539 k
= me
._db
.firstkey()
541 if k
.startswith('$'): yield k
[1:], me
._db
[k
]
542 k
= me
._db
.nextkey(k
)
544 try: import sqlite3
as _Q
545 except ImportError: pass
547 class SQLiteStorageBackend (StorageBackend
):
549 I represent a password database stored in SQLite.
551 Metadata and password items are stored in separate tables, so there's no
552 conflict. Some additional metadata is stored in the `meta' table, with
553 names beginning with `$' so as not to conflict with clients:
555 $version The schema version of the table.
561 def _open(me
, file, writep
):
563 me
._db
= _Q
.connect(file)
564 ver
= me
._query_scalar(
565 "SELECT value FROM meta WHERE name = '$version'",
567 except (_Q
.DatabaseError
, _Q
.OperationalError
), e
:
568 raise StorageBackendRefusal(e
)
569 if ver
is None: raise ValueError('database broken (missing $version)')
570 elif ver
< me
.VERSION
: me
._upgrade(ver
)
571 elif ver
> me
.VERSION
: raise ValueError \
572 ('unknown database schema version (%d > %d)' %
(ver
, me
.VERSION
))
574 def _create(me
, file):
575 fd
= _OS
.open(file, _OS
.O_WRONLY | _OS
.O_CREAT | _OS
.O_EXCL
, 0600)
578 me
._db
= _Q
.connect(file)
582 name TEXT PRIMARY KEY NOT NULL,
583 value BLOB NOT NULL);
586 CREATE TABLE passwd (
587 label BLOB PRIMARY KEY NOT NULL,
588 payload BLOB NOT NULL);
591 INSERT INTO meta (name, value) VALUES ('$version', ?);
594 try: _OS
.unlink(file)
598 def _upgrade(me
, ver
):
599 """Upgrade the database from schema version VER."""
600 assert False, 'how embarrassing'
602 def _close(me
, abruptp
):
603 if not abruptp
: me
._db
.commit()
607 def _fetch_scalar(me
, c
, what
, default
= None):
609 except StopIteration: val
= default
612 except StopIteration: pass
613 else: raise ValueError('multiple matching records for %s' % what
)
616 def _query_scalar(me
, query
, what
, default
= None, args
= []):
618 c
.execute(query
, args
)
619 return me
._fetch_scalar(c
, what
, default
)
621 def _get_meta(me
, name
, default
):
622 v
= me
._query_scalar("SELECT value FROM meta WHERE name = ?",
623 "metadata item `%s'" % name
,
624 default
= default
, args
= [name
])
625 if v
is default
: return v
628 def _put_meta(me
, name
, value
):
630 c
.execute("INSERT OR REPLACE INTO meta (name, value) VALUES (?, ?)",
631 [name
, buffer(value
)])
633 def _del_meta(me
, name
):
635 c
.execute("DELETE FROM meta WHERE name = ?", [name
])
636 if not c
.rowcount
: raise KeyError(name
)
640 c
.execute("SELECT name, value FROM meta WHERE name NOT LIKE '$%'")
641 for k
, v
in c
: yield k
, str(v
)
643 def _get_passwd(me
, label
):
644 pld
= me
._query_scalar("SELECT payload FROM passwd WHERE label = ?",
645 "password", default
= None,
646 args
= [buffer(label
)])
647 if pld
is None: raise KeyError(label
)
650 def _put_passwd(me
, label
, payload
):
652 c
.execute("INSERT OR REPLACE INTO passwd (label, payload) "
654 [buffer(label
), buffer(payload
)])
656 def _del_passwd(me
, label
):
658 c
.execute("DELETE FROM passwd WHERE label = ?", [label
])
659 if not c
.rowcount
: raise KeyError(label
)
661 def _iter_passwds(me
):
663 c
.execute("SELECT label, payload FROM passwd")
664 for k
, v
in c
: yield str(k
), str(v
)
666 class PlainTextBackend (StorageBackend
):
668 I'm a utility base class for storage backends which use plain text files.
670 I provide subclasses with the following capabilities.
672 * Creating files, with given modes, optionally ensuring that the file
673 doesn't exist already.
675 * Parsing flat text files, checking leading magic, skipping comments, and
676 providing standard encodings of troublesome characters and binary
677 strings in metadata and password records. See below.
679 * Maintenance of metadata and password records in in-memory dictionaries,
680 with ready implementations of the necessary StorageBackend subclass
681 responsibility methods. (Subclasses can override these if they want to
682 make different arrangements.)
684 Metadata records are written with an optional prefix string chosen by the
685 caller, followed by a `NAME=VALUE' pair. The NAME is form-urlencoded and
686 prefixed with `!' if it contains strange characters; the VALUE is base64-
687 encoded (without the pointless trailing `=' padding) and prefixed with `?'
690 Password records are written with an optional prefix string chosen by the
691 caller, followed by a LABEL=PAYLOAD pair, both of which are base64-encoded
694 The following attributes are available for subclasses:
696 _meta Dictionary mapping metadata item names to their values.
697 Populated by `_parse_meta' and managed by `_get_meta' and
700 _pw Dictionary mapping password labels to encrypted payloads.
701 Populated by `_parse_passwd' and managed by `_get_passwd' and
704 _dirtyp Boolean: set if either of the dictionaries has been modified.
707 def __init__(me
, *args
, **kw
):
709 Hook for initialization.
711 Sets up the published instance attributes.
716 super(PlainTextBackend
, me
).__init__(*args
, **kw
)
718 def _create_file(me
, file, mode
= 0600, freshp
= False):
720 Make sure FILE exists, creating it with the given MODE if necessary.
722 If FRESHP is true, then make sure the file did not exist previously.
723 Return a file object for the newly created file.
725 flags
= _OS
.O_CREAT | _OS
.O_WRONLY
726 if freshp
: flags |
= _OS
.O_EXCL
727 else: flags |
= _OS
.O_TRUNC
728 fd
= _OS
.open(file, flags
, mode
)
729 return _OS
.fdopen(fd
, 'w')
733 Set the `_dirtyp' flag.
735 Subclasses might find it useful to intercept this method.
739 def _eqsplit(me
, line
):
741 Extract the KEY, VALUE pair from a LINE of the form `KEY=VALUE'.
743 Raise `ValueError' if there is no `=' in the LINE.
746 return line
[:eq
], line
[eq
+ 1:]
748 def _parse_file(me
, file, magic
= None):
754 * Raise `StorageBackendRefusal' if that the first line doesn't match
755 MAGIC (if provided). MAGIC should not contain the terminating
758 * Ignore comments (beginning `#') and blank lines.
760 * Call `_parse_line' (provided by the subclass) for other lines.
762 with
open(file, 'r') as f
:
763 if magic
is not None:
764 if f
.readline().rstrip('\n') != magic
: raise StorageBackendRefusal
766 line
= line
.rstrip('\n')
767 if not line
or line
.startswith('#'): continue
770 def _write_file(me
, file, writebody
, mode
= 0600, magic
= None):
772 Update FILE atomically.
774 The newly created file will have the given MODE. If MAGIC is given, then
775 write that as the first line. Calls WRITEBODY(F) to write the main body
776 of the file where F is a file object for the new file.
779 with me
._create_file(new
, mode
) as f
:
780 if magic
is not None: f
.write(magic
+ '\n')
782 _OS
.rename(new
, file)
784 def _parse_meta(me
, line
):
785 """Parse LINE as a metadata NAME=VALUE pair, and updates `_meta'."""
786 k
, v
= me
._eqsplit(line
)
787 me
._meta
[_dec_metaname(k
)] = _dec_metaval(v
)
789 def _write_meta(me
, f
, prefix
= ''):
790 """Write the metadata records to F, each with the given PREFIX."""
791 f
.write('\n## Metadata.\n')
792 for k
, v
in me
._meta
.iteritems():
793 f
.write('%s%s=%s\n' %
(prefix
, _enc_metaname(k
), _enc_metaval(v
)))
795 def _get_meta(me
, name
, default
):
796 return me
._meta
.get(name
, default
)
797 def _put_meta(me
, name
, value
):
799 me
._meta
[name
] = value
800 def _del_meta(me
, name
):
804 return me
._meta
.iteritems()
806 def _parse_passwd(me
, line
):
807 """Parse LINE as a password LABEL=PAYLOAD pair, and updates `_pw'."""
808 k
, v
= me
._eqsplit(line
)
809 me
._pw
[_unb64(k
)] = _unb64(v
)
811 def _write_passwd(me
, f
, prefix
= ''):
812 """Write the password records to F, each with the given PREFIX."""
813 f
.write('\n## Password data.\n')
814 for k
, v
in me
._pw
.iteritems():
815 f
.write('%s%s=%s\n' %
(prefix
, _b64(k
), _b64(v
)))
817 def _get_passwd(me
, label
):
818 return me
._pw
[str(label
)]
819 def _put_passwd(me
, label
, payload
):
821 me
._pw
[str(label
)] = payload
822 def _del_passwd(me
, label
):
824 del me
._pw
[str(label
)]
825 def _iter_passwds(me
):
826 return me
._pw
.iteritems()
828 class FlatFileStorageBackend (PlainTextBackend
):
830 I maintain a password database in a plain text file.
832 The text file consists of lines, as follows.
834 * Empty lines, and lines beginning with `#' (in the leftmost column only)
837 * Lines of the form `$LABEL=PAYLOAD' store password data. Both LABEL and
838 PAYLOAD are base64-encoded, without `=' padding.
840 * Lines of the form `NAME=VALUE' store metadata. If the NAME contains
841 characters other than alphanumerics, hyphens, underscores, and colons,
842 then it is form-urlencoded, and prefixed wth `!'. If the VALUE
843 contains such characters, then it is base64-encoded, without `='
844 padding, and prefixed with `?'.
846 * Other lines are erroneous.
848 The file is rewritten from scratch when it's changed: any existing
849 commentary is lost, and items may be reordered. There is no file locking,
850 but the file is updated atomically, by renaming.
852 It is expected that the FlatFileStorageBackend is used mostly for
853 diagnostics and transfer, rather than for a live system.
858 MAGIC
= '### pwsafe password database'
860 def _open(me
, file, writep
):
861 if not _OS
.path
.isfile(file): raise StorageBackendRefusal
862 me
._parse_file(file, magic
= me
.MAGIC
)
863 def _parse_line(me
, line
):
864 if line
.startswith('$'): me
._parse_passwd(line
[1:])
865 else: me
._parse_meta(line
)
867 def _create(me
, file):
868 with me
._create_file(file, freshp
= True) as f
: pass
872 def _close(me
, abruptp
):
873 if not abruptp
and me
._dirtyp
:
874 me
._write_file(me
._file
, me
._write_body
, magic
= me
.MAGIC
)
876 def _write_body(me
, f
):
878 me
._write_passwd(f
, '$')
880 class DirectoryStorageBackend (PlainTextBackend
):
882 I maintain a password database in a directory, with one file per password.
884 This makes password databases easy to maintain in a revision-control system
887 The directory is structured as follows.
889 dir/meta Contains metadata, similar to the `FlatFileBackend'.
891 dir/pw/LABEL Contains the (raw binary) payload for the given password
892 LABEL (base64-encoded, without the useless `=' padding, and
893 with `/' replaced by `.').
895 dir/tmp/ Contains temporary files used by the implementation.
899 METAMAGIC
= '### pwsafe password directory metadata'
901 def _open(me
, file, writep
):
902 if not _OS
.path
.isdir(file) or \
903 not _OS
.path
.isdir(_OS
.path
.join(file, 'pw')) or \
904 not _OS
.path
.isdir(_OS
.path
.join(file, 'tmp')) or \
905 not _OS
.path
.isfile(_OS
.path
.join(file, 'meta')):
906 raise StorageBackendRefusal
908 me
._parse_file(_OS
.path
.join(file, 'meta'), magic
= me
.METAMAGIC
)
909 def _parse_line(me
, line
):
912 def _create(me
, file):
913 _OS
.mkdir(file, 0700)
914 _OS
.mkdir(_OS
.path
.join(file, 'pw'), 0700)
915 _OS
.mkdir(_OS
.path
.join(file, 'tmp'), 0700)
919 def _close(me
, abruptp
):
920 if not abruptp
and me
._dirtyp
:
921 me
._write_file(_OS
.path
.join(me
._dir
, 'meta'),
922 me
._write_meta
, magic
= me
.METAMAGIC
)
924 def _pwfile(me
, label
, dir = 'pw'):
925 return _OS
.path
.join(me
._dir
, dir, _b64(label
).replace('/', '.'))
926 def _get_passwd(me
, label
):
928 f
= open(me
._pwfile(label
), 'rb')
929 except (OSError, IOError), e
:
930 if e
.errno
== _E
.ENOENT
: raise KeyError(label
)
932 with f
: return f
.read()
933 def _put_passwd(me
, label
, payload
):
934 new
= me
._pwfile(label
, 'tmp')
935 fd
= _OS
.open(new
, _OS
.O_WRONLY | _OS
.O_CREAT | _OS
.O_TRUNC
, 0600)
937 with
open(new
, 'wb') as f
: f
.write(payload
)
938 _OS
.rename(new
, me
._pwfile(label
))
939 def _del_passwd(me
, label
):
941 _OS
.remove(me
._pwfile(label
))
942 except (OSError, IOError), e
:
943 if e
.errno
== _E
.ENOENT
: raise KeyError(label
)
945 def _iter_passwds(me
):
946 pw
= _OS
.path
.join(me
._dir
, 'pw')
947 for i
in _OS
.listdir(pw
):
948 with
open(_OS
.path
.join(pw
, i
), 'rb') as f
: pld
= f
.read()
949 yield _unb64(i
.replace('.', '/')), pld
951 ###--------------------------------------------------------------------------
952 ### Password storage.
956 I represent a secure (ish) password store.
958 I can store short secrets, associated with textual names, in a way which
959 doesn't leak too much information about them.
961 I implement (some of) the Python mapping protocol.
963 I keep track of everything using a StorageBackend object. This contains
964 password entries, identified by cryptographic labels, and a number of
967 cipher Names the Catacomb cipher selected.
969 hash Names the Catacomb hash function selected.
971 key Cipher and MAC keys, each prefixed by a 16-bit big-endian
972 length and concatenated, encrypted using the master
975 mac Names the Catacomb message authentication code selected.
977 magic A magic string for obscuring password tag names.
979 salt The salt for hashing the passphrase.
981 tag The master passphrase's tag, for the Pixie's benefit.
983 Password entries are assigned labels of the form `$' || H(MAGIC || TAG);
984 the corresponding value consists of a pair (TAG, PASSWD), prefixed with
985 16-bit lengths, concatenated, padded to a multiple of 256 octets, and
986 encrypted using the stored keys.
989 def __init__(me
, file, writep
= False):
991 Initialize a PW object from the database in FILE.
993 If WRITEP is false (the default) then the database is opened read-only;
994 if true then it may be written. Requests the database password from the
995 Pixie, which may cause interaction.
998 ## Open the database.
999 me
.db
= StorageBackend
.open(file, writep
)
1001 ## Find out what crypto to use.
1002 c
= _C
.gcciphers
[me
.db
.get_meta('cipher')]
1003 h
= _C
.gchashes
[me
.db
.get_meta('hash')]
1004 m
= _C
.gcmacs
[me
.db
.get_meta('mac')]
1006 ## Request the passphrase and extract the master keys.
1007 tag
= me
.db
.get_meta('tag')
1008 ppk
= PPK(_C
.ppread(tag
), c
, h
, m
, me
.db
.get_meta('salt'))
1010 b
= _C
.ReadBuffer(ppk
.decrypt(me
.db
.get_meta('key')))
1011 except DecryptError
:
1014 me
.ck
= b
.getblk16()
1015 me
.mk
= b
.getblk16()
1016 if not b
.endp
: raise ValueError('trailing junk')
1018 ## Set the key, and stash it and the tag-hashing secret.
1019 me
.k
= Crypto(c
, h
, m
, me
.ck
, me
.mk
)
1020 me
.magic
= me
.k
.decrypt(me
.db
.get_meta('magic'))
1023 def create(cls
, dbcls
, file, tag
, c
, h
, m
):
1025 Create and initialize a new database FILE using StorageBackend DBCLS.
1027 We want a GCipher subclass C, a GHash subclass H, and a GMAC subclass M;
1028 and a Pixie passphrase TAG.
1030 This doesn't return a working object: it just creates the database file
1031 and gets out of the way.
1034 ## Set up the cryptography.
1035 pp
= _C
.ppread(tag
, _C
.PMODE_VERIFY
)
1036 ppk
= PPK(pp
, c
, h
, m
)
1037 ck
= _C
.rand
.block(c
.keysz
.default
)
1038 mk
= _C
.rand
.block(c
.keysz
.default
)
1039 k
= Crypto(c
, h
, m
, ck
, mk
)
1041 ## Set up and initialize the database.
1042 kct
= ppk
.encrypt(_C
.WriteBuffer().putblk16(ck
).putblk16(mk
))
1043 with dbcls
.create(file) as db
:
1044 db
.put_meta('tag', tag
)
1045 db
.put_meta('salt', ppk
.salt
)
1046 db
.put_meta('cipher', c
.name
)
1047 db
.put_meta('hash', h
.name
)
1048 db
.put_meta('mac', m
.name
)
1049 db
.put_meta('key', kct
)
1050 db
.put_meta('magic', k
.encrypt(_C
.rand
.block(h
.hashsz
)))
1052 def keyxform(me
, key
):
1053 """Transform the KEY (actually a password tag) into a password label."""
1054 return me
.k
.h().hash(me
.magic
).hash(key
).done()
1058 Change the database password.
1060 Requests the new password from the Pixie, which will probably cause
1063 tag
= me
.db
.get_meta('tag')
1065 ppk
= PPK(_C
.ppread(tag
, _C
.PMODE_VERIFY
),
1066 me
.k
.c
.__class__
, me
.k
.h
, me
.k
.m
.__class__
)
1067 kct
= ppk
.encrypt(_C
.WriteBuffer().putblk16(me
.ck
).putblk16(me
.mk
))
1068 me
.db
.put_meta('key', kct
)
1069 me
.db
.put_meta('salt', ppk
.salt
)
1071 def pack(me
, key
, value
):
1072 """Pack the KEY and VALUE into a ciphertext, and return it."""
1073 b
= _C
.WriteBuffer()
1074 b
.putblk16(key
).putblk16(value
)
1075 b
.zero(((b
.size
+ 255) & ~
255) - b
.size
)
1076 return me
.k
.encrypt(b
)
1080 Unpack a ciphertext CT and return a (KEY, VALUE) pair.
1082 Might raise DecryptError, of course.
1084 b
= _C
.ReadBuffer(me
.k
.decrypt(ct
))
1086 value
= b
.getblk16()
1089 ## Mapping protocol.
1091 def __getitem__(me
, key
):
1092 """Return the password for the given KEY."""
1093 try: return me
.unpack(me
.db
.get_passwd(me
.keyxform(key
)))[1]
1094 except KeyError: raise KeyError(key
)
1096 def __setitem__(me
, key
, value
):
1097 """Associate the password VALUE with the KEY."""
1098 me
.db
.put_passwd(me
.keyxform(key
), me
.pack(key
, value
))
1100 def __delitem__(me
, key
):
1101 """Forget all about the KEY."""
1102 try: me
.db
.del_passwd(me
.keyxform(key
))
1103 except KeyError: raise KeyError(key
)
1106 """Iterate over the known password tags."""
1107 for _
, pld
in me
.db
.iter_passwds():
1108 yield me
.unpack(pld
)[0]
1110 ## Context protocol.
1114 def __exit__(me
, excty
, excval
, exctb
):
1115 me
.db
.close(excval
is not None)
1117 ###----- That's all, folks --------------------------------------------------