utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2014, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "crypto/rand"
  22. "crypto/x509"
  23. "encoding/base64"
  24. "encoding/hex"
  25. "errors"
  26. "fmt"
  27. "math/big"
  28. "runtime"
  29. )
  30. // Contains is a helper function that returns true
  31. // if the target string is in the list.
  32. func Contains(list []string, target string) bool {
  33. for _, listItem := range list {
  34. if listItem == target {
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. // MakeSecureRandomInt is a helper function that wraps
  41. // crypto/rand.Int, which returns a uniform random value in [0, max).
  42. func MakeSecureRandomInt(max int) (int, error) {
  43. randomInt, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
  44. if err != nil {
  45. return 0, ContextError(err)
  46. }
  47. return int(randomInt.Uint64()), nil
  48. }
  49. // MakeSecureRandomBytes is a helper function that wraps
  50. // crypto/rand.Read.
  51. func MakeSecureRandomBytes(length int) ([]byte, error) {
  52. randomBytes := make([]byte, length)
  53. n, err := rand.Read(randomBytes)
  54. if err != nil {
  55. return nil, ContextError(err)
  56. }
  57. if n != length {
  58. return nil, ContextError(errors.New("insufficient random bytes"))
  59. }
  60. return randomBytes, nil
  61. }
  62. // TrimError removes the middle of over-long error message strings
  63. func TrimError(err error) error {
  64. const MAX_LEN = 100
  65. message := fmt.Sprintf("%s", err)
  66. if len(message) > MAX_LEN {
  67. return errors.New(message[:MAX_LEN/2] + "..." + message[len(message)-MAX_LEN/2:])
  68. }
  69. return err
  70. }
  71. // ContextError prefixes an error message with the current function name
  72. func ContextError(err error) error {
  73. if err == nil {
  74. return nil
  75. }
  76. pc, _, line, _ := runtime.Caller(1)
  77. funcName := runtime.FuncForPC(pc).Name()
  78. return fmt.Errorf("%s#%d: %s", funcName, line, err)
  79. }
  80. func MakeSessionId() (id string, err error) {
  81. randomId, err := MakeSecureRandomBytes(PSIPHON_API_CLIENT_SESSION_ID_LENGTH)
  82. if err != nil {
  83. return "", ContextError(err)
  84. }
  85. return hex.EncodeToString(randomId), nil
  86. }
  87. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  88. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  89. if err != nil {
  90. return nil, ContextError(err)
  91. }
  92. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  93. if err != nil {
  94. return nil, ContextError(err)
  95. }
  96. return certificate, nil
  97. }