Database.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Database.swift
  2. // This file is part of KeePass.swift
  3. //
  4. // Copyright © 2021 Maxime Epain. All rights reserved.
  5. //
  6. // KeePass.swift 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. // KeePass.swift 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 KeePass.swift. If not, see <https://www.gnu.org/licenses/>.
  18. import Binary
  19. import Crypto
  20. import Foundation
  21. public let FileSignature: UInt32 = 0x9AA2_D903
  22. public let FileFormat: UInt32 = 0xB54B_FB65
  23. public class Database {
  24. let header: Header
  25. public let root: Group
  26. public required init(from input: Input, compositeKey: CompositeKey) throws {
  27. header = try input.read()
  28. let data: Bytes = try input.read()
  29. let key = try header.masterKey(from: compositeKey)
  30. let cipher = try header.cipher(key: key)
  31. let content = try cipher.decrypt(data: data)
  32. guard SHA256.hash(content) == header.contentHash else {
  33. throw KDBError.invalidKey
  34. }
  35. let stream = Input(bytes: content)
  36. root = try Root(from: stream, groups: Int(header.groups), entries: Int(header.entries))
  37. }
  38. public convenience init(from file: URL, compositeKey: CompositeKey) throws {
  39. let bytes = try Bytes(contentsOf: file)
  40. let stream = Input(bytes: bytes)
  41. guard
  42. try stream.read() == FileSignature,
  43. try stream.read() == FileFormat
  44. else { throw KDBError.invalidFileFormat }
  45. try self.init(from: stream, compositeKey: compositeKey)
  46. }
  47. public func write(to output: Output, compositeKey: CompositeKey) throws {
  48. func groups(in group: Group) -> [Group] {
  49. return group.childs.reduce(group.childs) { $0 + groups(in: $1) }
  50. }
  51. func entries(in group: Group) -> [Entry] {
  52. return group.childs.reduce(group.entries) { $0 + entries(in: $1) }
  53. }
  54. let key = try header.masterKey(from: compositeKey)
  55. let cipher = try header.cipher(key: key)
  56. let groups = groups(in: root)
  57. let entries = entries(in: root)
  58. let content = Output()
  59. try content.write(groups)
  60. try content.write(entries)
  61. guard var data = content.bytes else { throw KDBError.noData }
  62. var header = header
  63. header.contentHash = SHA256.hash(data)
  64. header.groups = UInt32(groups.count)
  65. header.entries = UInt32(entries.count)
  66. try output.write(header)
  67. data = try cipher.encrypt(data: data)
  68. try output.write(data)
  69. }
  70. }
  71. private func Root(from input: Input, groups: Int, entries: Int) throws -> Group {
  72. let groups: [Group] = try input.read(maxLenght: groups)
  73. let entries: [Entry] = try input.read(maxLenght: entries)
  74. let root = Group()
  75. for (i, group) in groups.enumerated() {
  76. guard let level1: UInt16 = group[.groupLevel] else { throw KDBError.corruptedDatabase }
  77. if level1 == 0 {
  78. root.add(group)
  79. continue
  80. }
  81. for (j, parent) in groups[0 ..< i].enumerated().reversed() {
  82. guard let level2: UInt16 = parent[.groupLevel] else { throw KDBError.corruptedDatabase }
  83. if level2 < level1 {
  84. guard (level1 - level2) == 1 else { throw KDBError.corruptedDatabase }
  85. parent.add(group)
  86. break
  87. }
  88. guard j > 0 else { throw KDBError.corruptedDatabase }
  89. }
  90. }
  91. for entry in entries {
  92. guard let groupID: UInt32 = entry[.groupID] else { throw KDBError.corruptedDatabase }
  93. let group = try groups.first(column: .groupID, where: { $0 == groupID }) ?? root
  94. group.add(entry)
  95. }
  96. return root
  97. }