ref: b8662330e5a2aed7af1568e6cddb044597857f5c
fs/fs.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
package fs import ( "notabug.org/apiote/next-eeze/password" "crypto/aes" "crypto/cipher" "crypto/rand" "errors" "io" "io/ioutil" "log" "os" "os/user" "path/filepath" "golang.org/x/crypto/argon2" "git.sr.ht/~sircmpwn/go-bare" ) type EncryptedContent struct { Salt []byte CipherText []byte } type Credentials struct { Server string Username string Password string } func getDataLocation() string { usr, _ := user.Current() dir := usr.HomeDir path := filepath.Join(dir, "/.local/share/next-eeze/") return path } // todo memguard passwords, masterPassword func SaveBare(passwords []password.BarePassword, masterPassword string) error { salt := makeSalt() // todo memguard key := deriveKey(masterPassword, salt) result, err := os.Create(getDataLocation() + "/passwords.bare") if err != nil { log.Fatal("Error creating passwords file. ", err) return err } defer result.Close() // todo memguard bytes, err := bare.Marshal(&passwords) if err != nil { log.Fatal("Error marshalling passwords. ", err) return err } cipherText, err := encrypt(bytes, key) if err != nil { log.Fatal("Error encrypting credentials. ", err) return err } enc := EncryptedContent{ Salt: salt, CipherText: cipherText, } bytes, err = bare.Marshal(&enc) if err != nil { log.Fatal("Error marshalling encrypted credentials. ", err) return err } _, err = result.Write(bytes) if err != nil { log.Fatal("Error writing to file. ", err) return err } _, err = result.Write(bytes) if err != nil { log.Fatal("Error writing to file. ", err) return err } return nil } // todo memguard passwords, masterPassword func Save(passwords []password.NextPassword, masterPassword string) error { // todo memguard barePasswords := []password.BarePassword{} // todo memguard for _, p := range passwords { barePasswords = append(barePasswords, p.ToBarePassword()) } err := SaveBare(barePasswords, masterPassword) return err } // todo memguard masterPassword func Read(masterPassword string) ([]password.BarePassword, error) { enc := EncryptedContent{} // todo memguard passwords := []password.BarePassword{} content, err := ioutil.ReadFile(getDataLocation() + "/passwords.bare") if err != nil { log.Fatal("Error reading to file. ", err) return nil, err } err = bare.Unmarshal(content, &enc) if err != nil { log.Fatal("Error unmarshalling encrypted. ", err) return passwords, err } // todo memguard key := deriveKey(masterPassword, enc.Salt) // todo memguard plaintext, err := decrypt(enc.CipherText, key) if err != nil { log.Fatal("Error decrypting passwords. ", err) return passwords, err } err = bare.Unmarshal(plaintext, &passwords) if err != nil { log.Fatal("Error unmarshalling. ", err) return nil, err } return passwords, nil } // todo memguard credentials, masterPassword func SaveCredentials(credentials Credentials, masterPassword string) error { salt := makeSalt() // todo memguard key := deriveKey(masterPassword, salt) result, err := os.Create(getDataLocation() + "/credentials.bare") if err != nil { log.Fatal("Error creating credentials file. ", err) return err } defer result.Close() // todo memguard bytes, err := bare.Marshal(&credentials) if err != nil { log.Fatal("Error marshalling credentials. ", err) return err } cipherText, err := encrypt(bytes, key) if err != nil { log.Fatal("Error encrypting credentials. ", err) return err } enc := EncryptedContent{ Salt: salt, CipherText: cipherText, } bytes, err = bare.Marshal(&enc) if err != nil { log.Fatal("Error marshalling encrypted credentials. ", err) return err } _, err = result.Write(bytes) if err != nil { log.Fatal("Error writing to file. ", err) return err } return nil } // todo memguard masterPassword func ReadCredentials(masterPassword string) (Credentials, error) { enc := EncryptedContent{} // todo memguard credentials := Credentials{} content, err := ioutil.ReadFile(getDataLocation() + "/credentials.bare") if err != nil { log.Fatal("Error reading to file. ", err) return credentials, err } err = bare.Unmarshal(content, &enc) if err != nil { log.Fatal("Error unmarshalling encrypted. ", err) return credentials, err } // todo memguard key := deriveKey(masterPassword, enc.Salt) // todo memguard plaintext, err := decrypt(enc.CipherText, key) if err != nil { log.Fatal("Error decrypting credentials. ", err) return credentials, err } err = bare.Unmarshal(plaintext, &credentials) return credentials, nil } func makeSalt() []byte { key := [32]byte{} _, err := io.ReadFull(rand.Reader, key[:]) if err != nil { panic(err) } return key[:] } // todo memguard password func deriveKey(password string, salt []byte) [32]byte { // todo memguard key := argon2.IDKey([]byte(password), salt, 1, 64*1024, 4, 32) // todo memguard var keyArr [32]byte copy(keyArr[:], key) return keyArr } // todo memguard plaintext, key func encrypt(plaintext []byte, key [32]byte) (ciphertext []byte, err error) { block, err := aes.NewCipher(key[:]) if err != nil { return nil, err } gcm, err := cipher.NewGCM(block) if err != nil { return nil, err } nonce := make([]byte, gcm.NonceSize()) _, err = io.ReadFull(rand.Reader, nonce) if err != nil { return nil, err } return gcm.Seal(nonce, nonce, plaintext, nil), nil } // todo memguard key func decrypt(ciphertext []byte, key [32]byte) (plaintext []byte, err error) { block, err := aes.NewCipher(key[:]) if err != nil { return nil, err } gcm, err := cipher.NewGCM(block) if err != nil { return nil, err } if len(ciphertext) < gcm.NonceSize() { return nil, errors.New("malformed ciphertext") } return gcm.Open(nil, ciphertext[:gcm.NonceSize()], ciphertext[gcm.NonceSize():], nil, ) } |