utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.
  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. // ContextError prefixes an error message with the current function name
  63. func ContextError(err error) error {
  64. pc, _, _, _ := runtime.Caller(1)
  65. funcName := runtime.FuncForPC(pc).Name()
  66. return fmt.Errorf("%s: %s", funcName, err)
  67. }
  68. func MakeSessionId() (id string, err error) {
  69. randomId, err := MakeSecureRandomBytes(PSIPHON_API_CLIENT_SESSION_ID_LENGTH)
  70. if err != nil {
  71. return "", ContextError(err)
  72. }
  73. return hex.EncodeToString(randomId), nil
  74. }
  75. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  76. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  77. if err != nil {
  78. return nil, ContextError(err)
  79. }
  80. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  81. if err != nil {
  82. return nil, ContextError(err)
  83. }
  84. return certificate, nil
  85. }