Database3.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Database3.swift
  2. // This file is part of KeePassKit.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePassKit is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // KeePassKit is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with KeePassKit. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. import Binary
  20. import Crypto
  21. import Gzip
  22. import XML
  23. class Database3: Database {
  24. typealias Header = [TLV<OuterHeader, UInt16>]
  25. let header: Header
  26. let document: Document
  27. required init(from input: Input, compositeKey: CompositeKey) throws {
  28. header = try input.read()
  29. guard let startBytes = header[.streamStartBytes] else { throw KDBXError.corruptedDatabase }
  30. let data = try input.read() as Bytes
  31. var key = try header.masterKey(from: compositeKey)
  32. key = SHA256.hash( key )
  33. let cipher = try header.cipher(key: key)
  34. let hash = try cipher.decrypt(data: data)
  35. let stream = Input(bytes: hash)
  36. guard try stream.read(lenght: SHA256.Lenght) == startBytes else { throw KDBXError.invalidCompositeKey }
  37. var block: UInt32 = 0
  38. var content = Bytes()
  39. while true {
  40. guard try stream.read() == block else { throw KDBXError.corruptedDatabase }
  41. block += 1
  42. let hash = try stream.read(lenght: SHA256.Lenght)
  43. let size: UInt32 = try stream.read()
  44. guard size > 0 else { break }
  45. let data = try stream.read(lenght: Int(size))
  46. guard SHA256.hash( data ) == hash else { throw KDBXError.corruptedDatabase }
  47. content += data
  48. }
  49. if header[.compressionFlags] == Compression.gzip {
  50. content = try content.gunzipped()
  51. }
  52. var options = XML.Options()
  53. options.parserSettings.shouldTrimWhitespace = false
  54. document = try XML.Document(xml: content.data, options: options)
  55. }
  56. }
  57. extension Database3: Writable {
  58. func write(to output: Output) throws {
  59. try output.write(header)
  60. fatalError()
  61. }
  62. }
  63. extension Database3.Header: Readable {
  64. public init(from input: Input) throws {
  65. var header = Database3.Header()
  66. while true {
  67. let field: TLV<OuterHeader, UInt16> = try input.read()
  68. header.append(field)
  69. if field.type == .end { break }
  70. }
  71. self = header
  72. }
  73. }
  74. extension Database3.Header: Header {
  75. subscript(_ type: OuterHeader) -> Bytes? {
  76. return first(where: { $0.type == type })?.value
  77. }
  78. }