A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

106 lines
2.4 KiB

  1. /*
  2. * Copyright © 2018 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "crypto/rand"
  13. "github.com/writeas/web-core/log"
  14. "io/ioutil"
  15. "os"
  16. "path/filepath"
  17. )
  18. const (
  19. keysDir = "keys"
  20. encKeysBytes = 32
  21. )
  22. var (
  23. emailKeyPath = filepath.Join(keysDir, "email.aes256")
  24. cookieAuthKeyPath = filepath.Join(keysDir, "cookies_auth.aes256")
  25. cookieKeyPath = filepath.Join(keysDir, "cookies_enc.aes256")
  26. )
  27. type keychain struct {
  28. emailKey, cookieAuthKey, cookieKey []byte
  29. }
  30. func initKeys(app *app) error {
  31. var err error
  32. app.keys = &keychain{}
  33. emailKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, emailKeyPath)
  34. if debugging {
  35. log.Info(" %s", emailKeyPath)
  36. }
  37. app.keys.emailKey, err = ioutil.ReadFile(emailKeyPath)
  38. if err != nil {
  39. return err
  40. }
  41. cookieAuthKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieAuthKeyPath)
  42. if debugging {
  43. log.Info(" %s", cookieAuthKeyPath)
  44. }
  45. app.keys.cookieAuthKey, err = ioutil.ReadFile(cookieAuthKeyPath)
  46. if err != nil {
  47. return err
  48. }
  49. cookieKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieKeyPath)
  50. if debugging {
  51. log.Info(" %s", cookieKeyPath)
  52. }
  53. app.keys.cookieKey, err = ioutil.ReadFile(cookieKeyPath)
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. // generateKey generates a key at the given path used for the encryption of
  60. // certain user data. Because user data becomes unrecoverable without these
  61. // keys, this won't overwrite any existing key, and instead outputs a message.
  62. func generateKey(path string) error {
  63. // Check if key file exists
  64. if _, err := os.Stat(path); !os.IsNotExist(err) {
  65. log.Info("%s already exists. rm the file if you understand the consquences.", path)
  66. return nil
  67. }
  68. log.Info("Generating %s.", path)
  69. b, err := generateBytes(encKeysBytes)
  70. if err != nil {
  71. log.Error("FAILED. %s. Run writefreely --gen-keys again.", err)
  72. return err
  73. }
  74. err = ioutil.WriteFile(path, b, 0600)
  75. if err != nil {
  76. log.Error("FAILED writing file: %s", err)
  77. return err
  78. }
  79. log.Info("Success.")
  80. return nil
  81. }
  82. // generateBytes returns securely generated random bytes.
  83. func generateBytes(n int) ([]byte, error) {
  84. b := make([]byte, n)
  85. _, err := rand.Read(b)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return b, nil
  90. }