utils.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2015, 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. "errors"
  25. "fmt"
  26. "math/big"
  27. "runtime"
  28. )
  29. // Contains is a helper function that returns true
  30. // if the target string is in the list.
  31. func Contains(list []string, target string) bool {
  32. for _, listItem := range list {
  33. if listItem == target {
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. // MakeSecureRandomInt is a helper function that wraps
  40. // crypto/rand.Int, which returns a uniform random value in [0, max).
  41. func MakeSecureRandomInt(max int) (int, error) {
  42. randomInt, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
  43. if err != nil {
  44. return 0, ContextError(err)
  45. }
  46. return int(randomInt.Uint64()), nil
  47. }
  48. // MakeSecureRandomBytes is a helper function that wraps
  49. // crypto/rand.Read.
  50. func MakeSecureRandomBytes(length int) ([]byte, error) {
  51. randomBytes := make([]byte, length)
  52. n, err := rand.Read(randomBytes)
  53. if err != nil {
  54. return nil, ContextError(err)
  55. }
  56. if n != length {
  57. return nil, ContextError(errors.New("insufficient random bytes"))
  58. }
  59. return randomBytes, nil
  60. }
  61. // TrimError removes the middle of over-long error message strings
  62. func TrimError(err error) error {
  63. const MAX_LEN = 100
  64. message := fmt.Sprintf("%s", err)
  65. if len(message) > MAX_LEN {
  66. return errors.New(message[:MAX_LEN/2] + "..." + message[len(message)-MAX_LEN/2:])
  67. }
  68. return err
  69. }
  70. // ContextError prefixes an error message with the current function name
  71. func ContextError(err error) error {
  72. if err == nil {
  73. return nil
  74. }
  75. pc, _, line, _ := runtime.Caller(1)
  76. funcName := runtime.FuncForPC(pc).Name()
  77. return fmt.Errorf("%s#%d: %s", funcName, line, err)
  78. }
  79. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  80. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  81. if err != nil {
  82. return nil, ContextError(err)
  83. }
  84. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  85. if err != nil {
  86. return nil, ContextError(err)
  87. }
  88. return certificate, nil
  89. }