PWSafe3/Databaseformat.pm untested

filter: Statementstmt n/a Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a SCAR -
1 =head1 NAME
2  
3 PasswordSafe database format description version 3.03
4  
5  
6 =head1 LICENSE
7  
8 Copyright (c) 2003-2008 Rony Shapiro <ronys@users.sourceforge.net>.
9 All rights reserved. Use of the code is allowed under the Artistic
10 License terms, as specified in the LICENSE file distributed with this
11 code, or available from
12 http://www.opensource.org/licenses/artistic-license-2.0.php
13  
14 =head1 1. Introduction
15  
16 This document defines a file format for the secure
17 storage of passwords and related data. The format is designed
18 according to current cryptographic best practices, and is beleived to
19 be secure, in the sense that without knowledge of the master
20 passphrase, only a brute-force attack or a flaw in the underlying
21 cryptographic algorithm will result in unauthorized access to the
22 data.
23  
24 1.1 Design Goals: The PasswordSafe database format is designed to be
25 secure, extensible and platform-independent.
26  
27 1.2 History: This specification is an evolution of previous
28 formats. The main differences between version 3 of the format and
29 previous versions are:
30 1.2.1. This version addresses a minor design flaw in previous versions
31 of the PasswordSafe database format.
32 1.2.3. This version replaces the underlying cryptographic functions
33 with more advanced versions.
34 1.2.4. This version allows the detection of a truncated or
35 corrupted/tampered database.
36  
37 Meeting these goals is impossible without breaking compatibility: This
38 format is NOT compatible with previous (major) versions. Note,
39 however, that since the data stored in previous versions is a proper
40 subset of the data described here, implementers may read a database
41 written in an older version and store the result in the format
42 described here.
43  
44 =head1 2. Format
45  
46 A V3 format PasswordSafe is structured as follows:
47  
48 TAG|SALT|ITER|H(P')|B1|B2|B3|B4|IV|HDR|R1|R2|...|Rn|EOF|HMAC
49  
50 Where:
51  
52 2.1 TAG is the sequence of 4 ASCII characters "PWS3". This is to serve as a
53 quick way for the application to identify the database as a PasswordSafe
54 version 3 file. This tag has no cryptographic value.
55  
56 2.1 SALT is a 256 bit random value, generated at file creation time.
57  
58 2.3 P' is the "stretched key" generated from the user's passphrase and
59 the SALT, as defined in by the hash-function-based key stretching
60 algorithm in [KEYSTRETCH] (Section 4.1), with SHA-256 [SHA256] as the
61 hash function, and ITER iterations (at least 2048, i.e., t = 11).
62  
63 2.4 ITER is the number of iterations on the hash function to calculate P',
64 stored as a 32 bit little-endian value. This value is stored here in order
65 to future-proof the file format against increases in processing power.
66  
67 2.5 H(P') is SHA-256(P'), and is used to verify that the user has the
68 correct passphrase.
69  
70 2.6 B1 and B2 are two 128-bit blocks encrypted with Twofish [TWOFISH]
71 using P' as the key, in ECB mode. These blocks contain the 256 bit
72 random key K that is used to encrypt the actual records. (This has the
73 property that there is no known or guessable information on the
74 plaintext encrypted with the passphrase-derived key that allows an
75 attacker to mount an attack that bypasses the key stretching
76 algorithm.)
77  
78 2.7 B3 and B4 are two 128-bit blocks encrypted with Twofish using P' as the
79 key, in ECB mode. These blocks contain the 256 bit random key L that is
80 used to calculate the HMAC (keyed-hash message authentication code) of the
81 encrypted data. See description of EOF field below for more details.
82 Implementation Note: K and L must NOT be related.
83  
84 2.8 IV is the 128-bit random Initial Value for CBC mode.
85  
86 2.9 All following records are encrypted using Twofish in CBC mode, with K
87 as the encryption key.
88  
89 2.9.1 HDR: The database header. The header consists of one or more typed
90 fields (as defined in section 3.2), terminated by the 'END' type field. The
91 version number field is mandatory. Aside from the 'END' field, no
92 order is assumed on the field types.
93  
94 2.9.2 R1..Rn: The actual database records. Each record consists of one or
95 more typed fields (as defined in Section 3.2), terminated by the 'END' type
96 field. The UUID, Title, and Password fields are mandatory. All non-
97 mandatory fields may either be absent or have zero length. When a field is
98 absent or zero-length, its default value shall be used. Aside from the
99 'END' field, no order is assumed on the field types.
100  
101 2.10 EOF: The ASCII characters "PWS3-EOFPWS3-EOF" (note that this is
102 exactly one block long), unencrypted. This is an implementation convenience
103 to inform the application that the following bytes are to be processed
104 differently.
105  
106 2.11 HMAC: The 256-bit keyed-hash MAC, as described in RFC2104, with SHA-
107 256 as the underlying hash function. The value is calculated over all of
108 the plaintext fields, that is, over all the data stored in all fields
109 (starting from the version number in the header, ending with the last field
110 of the last record). The key L as stored in B3 and B4 is used as the hash
111 key value.
112  
113 3. Fields: Data in PasswordSafe is stored in typed fields. Each field
114 consists of one or more blocks. The blocks are the blocks of the underlying
115 encryption algorithm - 16 bytes long for Twofish. The first block contains
116 the field length in the first 4 bytes (little-endian), followed by a one-
117 byte type identifier. The rest of the block contains up to 11 bytes of
118 record data. If the record has less than 11 bytes of data, the extra bytes
119 are filled with random values. The type of a field also defines the data
120 representation.
121  
122 =head1 3.1 Data representations
123  
124 =head2 3.1.1 UUID
125  
126 The UUID data type is 16 bytes long, as defined in RFC4122. Microsoft
127 Windows has functions for this, and the RFC has a sample
128 implementation.
129  
130 =head2 3.1.2 Text
131  
132 Text is represented in UTF-8 encoding (as defined in RFC3629), with
133 no byte order marker (BOM) and no end-of-string mark (e.g., null
134 byte). Note that the latter isn't neccessary since the length of the
135 field is provided explicitly. Note that ALL fields described as
136 "text" are UTF-8 encoded unless explicitly stated otherwise.
137  
138 =head2 3.1.3 Time
139  
140 Timestamps are stored as 32 bit, little endian, unsigned integers,
141 representing the number of seconds since Midnight, January 1, 1970, GMT.
142 (This is equivalent to the time_t type on Windows and POSIX. On the
143 Macintosh, the value needs to be adjusted by the constant value 2082844800
144 to account for the different epoch of its time_t type.)
145 Note that future versions of this format may allow time to be
146 specifed in 64 bits as well.
147  
148 =head2 3.2 Field types for the PasswordSafe database header:
149  
150 Currently
151 Name Value Type Implemented Comments
152 --------------------------------------------------------------------------
153 Version 0x00 2 bytes Y [1]
154 UUID 0x01 UUID Y [2]
155 Non-default preferences 0x02 Text Y [3]
156 Tree Display Status 0x03 Text Y [4]
157 Timestamp of last save 0x04 time_t Y [5]
158 Who performed last save 0x05 Text Y [DEPRECATED 6]
159 What performed last save 0x06 Text Y [7]
160 Last saved by user 0x07 Text Y [8]
161 Last saved on host 0x08 Text Y [9]
162 Database Name 0x09 Text Y [10]
163 Database Description 0x0a Text Y [11]
164 Database Filters 0x0b Text Y [12]
165 End of Entry 0xff [empty] Y [13]
166  
167 [1] The version number of the database format. For this version, the value
168 is 0x0305 (stored in little-endian format, that is, 0x05, 0x03).
169  
170 PasswordSafe V3.01 introduced Format 0x0300
171 PasswordSafe V3.03 introduced Format 0x0301
172 PasswordSafe V3.09 introduced Format 0x0302
173 PasswordSafe V3.12 introduced Format 0x0303
174 PasswordSafe V3.13 introduced Format 0x0304
175 PasswordSafe V3.14 introduced Format 0x0305
176  
177 [2] A universally unique identifier is needed in order to synchronize
178 databases, e.g., between a handheld pocketPC device and a
179 PC. Representation is as described in Section 3.1.1.
180  
181 [3] Non-default preferences are encoded in a string as follows: The string
182 is of the form "X nn vv X nn vv..." Where X=[BIS] for binary, integer and
183 string respectively, nn is the numeric value of the enum, and vv is the
184 value, {1 or 0} for bool, unsigned integer for int, and a delimited string
185 for String. Only non-default values are stored. See PWSprefs.cpp for more
186 details. Note: normally strings are delimited by the doublequote character.
187 However, if this character is in the string value, an arbitrary character
188 will be chosen to delimit the string.
189  
190 [4] If requested to be saved, this is a string of 1s and 0s indicating the
191 expanded state of the tree display when the database was saved. This can
192 be applied at database open time, if the user wishes, so that the tree is
193 displayed as it was. Alternatively, it can be ignored and the tree
194 displayed completely expanded or collapsed. Note that the mapping of
195 the string to the display state is implementation-specific. Introduced
196 in format 0x0301.
197  
198 [5] Representation is as described in Section 3.1.3. Note that prior
199 to PasswordSafe 3.09, this field was mistakenly represented as an
200 eight-byte hexadecimal ASCII string. Implementations SHOULD attempt to
201 parse 8-byte long timestamps as a hexadecimal ASCII string
202 representation of the timestamp value.
203  
204 [6] Text saved in the format: nnnnu..uh..h, where:
205 nnnn = 4 hexadecimal digits giving length of following user name field
206 u..u = user name
207 h..h = host computer name
208 Note: As of format 0x0302, this field is deprecated, and should be
209 replaced by fields 0x07 and 0x08. In databases prior to format
210 0x0302, this field should be maintained. 0x0302 and later may
211 either maintain this field in addition to fields 0x07 and 0x08,
212 for backwards compatability, or not write this field. If both this
213 field and 0x07, 0x08 exist, they MUST represent the same values.
214  
215 [7] Free form text giving the application that saved the database.
216 For example, the Windows PasswordSafe application will use the text
217 "Password Safe Vnn.mm", where nn and mm are the major and minor
218 version numbers. The major version will contain only the significant
219 digits whereas the minor version will be padded to the left with
220 zeroes e.g. "Password Safe V3.02".
221  
222 [8] Text containing the username (e.g., login, userid, etc.) of the
223 user who last saved the database, as determined by the appropriate
224 operating-system dependent function. This field was introduced in
225 format version 0x0302, as a replacement for field 0x05. See Comment
226 [6].
227  
228 [9] Text containing the hostname (e.g., machine name, hostid, etc.) of the
229 machine on which the database was last saved, as determined by the
230 appropriate operating-system dependent function. This field was
231 introduced in format version 0x0302, as a replacement for field
232 0x05. See Comment [6].
233  
234 [10] Database name. A logical name for a database which can be used by
235 applications in place of the possibly lengthy filepath notion. Note
236 that this field SHOULD be limited to what can be displayed in a single
237 line. This field was introduced in format version 0x0302.
238  
239 [11] Database Description. A purely informative description concerning
240 the purpose or other practical use of the database. This field was
241 introduced in format version 0x0302.
242  
243 [12] Specfic filters for this database. This is the text equivalent to
244 the XML export of the filters as defined by the filter schema. The text
245 'image' has no 'print formatting' e.g. tabs and carraige return/line feeds,
246 since XML processing does not require this. This field was introduced in
247 format version 0x0305.
248  
249 [13] An explicit end of entry field is useful for supporting new fields
250 without breaking backwards compatability.
251  
252 =head2 3.3 Field types for database Records:
253  
254 Currently
255 Name Value Type Implemented Comments
256 --------------------------------------------------------------------------
257 UUID 0x01 UUID Y [1]
258 Group 0x02 Text Y [2]
259 Title 0x03 Text Y
260 Username 0x04 Text Y
261 Notes 0x05 Text Y
262 Password 0x06 Text Y [3,4]
263 Creation Time 0x07 time_t Y [5]
264 Password Modification Time 0x08 time_t Y [5]
265 Last Access Time 0x09 time_t Y [5,6]
266 Password Expiry Time 0x0a time_t Y [5,7]
267 *RESERVED* 0x0b 4 bytes - [8]
268 Last Modification Time 0x0c time_t Y [5,9]
269 URL 0x0d Text Y [10]
270 Autotype 0x0e Text Y [11]
271 Password History 0x0f Text Y [12]
272 Password Policy 0x10 Text Y [13]
273 Password Expiry Interval 0x11 2 bytes Y [14]
274 End of Entry 0xff [empty] Y [15]
275  
276 [1] Per-record UUID to assist in sync, merge, etc. Representation is
277 as described in Section 3.1.1.
278  
279 [2] The "Group" supports displaying the entries in a tree-like manner.
280 Groups can be hierarchical, with elements separated by a period, supporting
281 groups such as "Finance.credit cards.Visa", "Finance.credit
282 cards.Mastercard", Finance.bank.web access", etc. Dots entered by the user
283 should be "escaped" by the application.
284  
285 [3] If the entry is an alias, the password will be saved in a special form
286 of "[[uuidstr]]", where "uuidstr" is a 32-character representation of the
287 alias' associated base entry's UUID (field type 0x01). This representation
288 is the same as the standard 36-character string representation as defined in
289 RFC4122 but with the four hyphens removed. If an entry with this UUID is not
290 in the database, this is treated just as an 'unusual' password. The alias
291 will only use its base's password entry when copying it to the clipboard or
292 during Autotype.
293  
294 [4] If the entry is a shortcut, the password will be saved in a special form
295 of "[~uuidstr~]", where "uuidstr" is a 32-character representation of the
296 shortcut's associated base entry's UUID (field type 0x01). This representation
297 is the same as the standard 36-character string representation as defined in
298 RFC4122 but with the four hyphens removed. If an entry with this UUID is not
299 in the database, this is treated just as an 'unusual' password. The shortcut
300 will use all its base's data when used in any action. It has no fields of
301 its own.
302  
303 [5] Representation is as described in Section 3.1.3.
304  
305 [6] This will be updated whenever any part of this entry is accessed
306 i.e., to copy its username, password or notes to the clipboard; to
307 perform autotype or to browse to url.
308  
309 [7] This will allow the user to enter an expiry date for an entry. The
310 application can then prompt the user about passwords that need to be
311 changed. A value of zero means "forever".
312  
313 [8] Although earmarked for Password Policy, the coding in versions prior
314 to V3.12 does not correctly handle the presence of this field. For this
315 reason, this value cannot be used for any future V3 field without causing
316 a potential issue when a user opens a V3.12 or later database with program
317 version V3.11 or earlier. See note [14].
318  
319 [9] This is the time that any field of the record was modified, useful for
320 merging databases.
321  
322 [10] The URL will be passed to the shell when the user chooses the "Browse
323 to" action for this entry. In version 2 of the format, this was extracted
324 from the Notes field. By placing it in a separate field, we are no longer
325 restricted to a URL - any action that may be executed by the shell may be
326 specified here.
327  
328 [11] The text to be 'typed' by PasswordSafe upon the "Perform Autotype"
329 action maybe specified here. If unspecified, the default value of
330 'username, tab, password, tab, enter' is used. In version 2 of the format,
331 this was extracted from the Notes field. Several codes are recognized here,
332 e.g, '%p' is replaced by the record's password. See the user documentation
333 for the complete list of codes. The replacement is done by the application
334 at runtime, and is not stored in the database.
335  
336 [12] Password History is an optional record. If it exists, it stores the
337 creation times and values of the last few passwords used in the current
338 entry, in the following format:
339 "fmmnnTLPTLP...TLP"
340 where:
341 f = {0,1} if password history is on/off
342 mm = 2 hexadecimal digits max size of history list (i.e. max = 255)
343 nn = 2 hexadecimal digits current size of history list
344 T = Time password was set (time_t written out in %08x)
345 L = 4 hexadecimal digit password length (in TCHAR)
346 P = Password
347 No history being kept for a record can be represented either by the lack of
348 the PWH field (preferred), or by a header of _T("00000"):
349 flag = 0, max = 00, num = 00
350 Note that 0aabb, where bb <= aa, is possible if password history was enabled
351 in the past and has then been disabled but the history hasn't been cleared.
352  
353 [13] This field allows a specific Password Policy per entry. The format is:
354  
355 "ffffnnnllluuudddsss"
356  
357 where:
358  
359 ffff = 4 hexadecimal digits representing the following flags
360 UseLowercase = 0x8000 - can have a minimum length
361 UseUppercase = 0x4000 - can have a minimum length
362 UseDigits = 0x2000 - can have a minimum length
363 UseSymbols = 0x1000 - can have a minimum length
364 UseHexDigits = 0x0800 (if set, then no other flags can be set)
365 UseEasyVision = 0x0400
366 MakePronounceable = 0x0200
367 Unused 0x01ff
368 nnn = 3 hexadecimal digits password total length
369 lll = 3 hexadecimal digits password minimum number of lowercase characters
370 uuu = 3 hexadecimal digits password minimum number of uppercase characters
371 ddd = 3 hexadecimal digits password minimum number of digit characters
372 sss = 3 hexadecimal digits password minimum number of symbol characters
373  
374 [14] Password Expiry Interval, in days, before this password expires. Once set,
375 this value is used when the password is first generated and thereafter whenever
376 the password is changed, until this value is unset. Valid values are 1-3650
377 corresponding to up to approximately 10 years. A value of zero is equivalent to
378 this field not being set.
379  
380 [15] An explicit end of entry field is useful for supporting new fields
381 without breaking backwards compatability.
382  
383 =head1 4. Extensibility
384  
385 4.1 Forward compatability: Implementations of this format SHOULD NOT
386 discard or report an error when encountering a filed of an unknown
387 type. Rather, the field(s) type and data should be read, and perserved
388 when the database is saved.
389  
390 4.2 Field type identifiers: This document specifies the field type
391 identifiers for the current version of the format. Compliant
392 implementations MUST support the mandatory fields, and SHOULD support
393 the other fields described herein. Future versions of the format may
394 specify other type identifiers.
395 4.2.1 Application-unique type identifiers: The type identifiers
396 0xc0-0xdf are available for application developers on a first-come
397 first-serve basis. Application developers interested in reserving a
398 type identifier for their application should contact the maintainer of
399 this document (Currently the PasswordSafe project administrator at
400 SourceForge).
401 4.2.2 Application-specific type identifiers: The type identifiers
402 0xe0-0xfe are reserved for implementation-specific purposes, and will
403 NOT be specified in this or future versions of the format
404 description.
405 4.2.3 All unassigned identifiers except as listed in the previous two
406 subsections are reserved, and should not be used by other
407 implementations of this format specification in the interest of
408 interoperablity.
409  
410 =head1 5. References:
411  
412 [TWOFISH] http://www.schneier.com/paper-twofish-paper.html
413 [SHA256]
414 http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
415 [KEYSTRETCH] http://www.schneier.com/paper-low-entropy.pdf
416  
417 End of Format description.
418  
419 =head1 SEE ALSO
420  
421 Original source of this file:
422  
423 http://passwordsafe.svn.sourceforge.net/viewvc/passwordsafe/trunk/pwsafe/pwsafe/docs/formatV3.txt?revision=2139
424  
425 =cut