utils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // IsSignalled returns true when the signal channel yields
  31. // a value. To be used with the idiom in which a shared
  32. // channel is closed to broadcast a signal.
  33. func IsSignalled(signal chan bool) bool {
  34. select {
  35. case <-signal:
  36. return true
  37. default:
  38. }
  39. return false
  40. }
  41. // Contains is a helper function that returns true
  42. // if the target string is in the list.
  43. func Contains(list []string, target string) bool {
  44. for _, listItem := range list {
  45. if listItem == target {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. // MakeSecureRandomInt is a helper function that wraps
  52. // crypto/rand.Int.
  53. func MakeSecureRandomInt(max int) (int, error) {
  54. randomInt, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
  55. if err != nil {
  56. return 0, ContextError(err)
  57. }
  58. return int(randomInt.Uint64()), nil
  59. }
  60. // MakeSecureRandomBytes is a helper function that wraps
  61. // crypto/rand.Read.
  62. func MakeSecureRandomBytes(length int) ([]byte, error) {
  63. randomBytes := make([]byte, length)
  64. n, err := rand.Read(randomBytes)
  65. if err != nil {
  66. return nil, ContextError(err)
  67. }
  68. if n != length {
  69. return nil, ContextError(errors.New("insufficient random bytes"))
  70. }
  71. return randomBytes, nil
  72. }
  73. // ContextError prefixes an error message with the current function name
  74. func ContextError(err error) error {
  75. pc, _, _, _ := runtime.Caller(1)
  76. funcName := runtime.FuncForPC(pc).Name()
  77. return fmt.Errorf("%s: %s", funcName, err)
  78. }
  79. func MakeSessionId() (id string, err error) {
  80. randomId, err := MakeSecureRandomBytes(PSIPHON_API_CLIENT_SESSION_ID_LENGTH)
  81. if err != nil {
  82. return "", ContextError(err)
  83. }
  84. return hex.EncodeToString(randomId), nil
  85. }
  86. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  87. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  88. if err != nil {
  89. return nil, ContextError(err)
  90. }
  91. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  92. if err != nil {
  93. return nil, ContextError(err)
  94. }
  95. return certificate, nil
  96. }