inproxy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2023, 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 parameters
  20. import (
  21. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/inproxy"
  24. )
  25. // InproxyBrokerSpecsValue is a list of in-proxy broker specs.
  26. type InproxyBrokerSpecsValue []*InproxyBrokerSpec
  27. // InproxyBrokerSpec specifies the configuration to use to establish a secure
  28. // connection to an in-proxy broker.
  29. type InproxyBrokerSpec struct {
  30. BrokerPublicKey string
  31. BrokerRootObfuscationSecret string
  32. BrokerFrontingSpecs FrontingSpecs
  33. }
  34. // Validate checks that the in-proxy broker specs values are well-formed.
  35. func (specs InproxyBrokerSpecsValue) Validate(checkBrokerSpecsList *InproxyBrokerSpecsValue) error {
  36. for _, spec := range specs {
  37. if _, err := inproxy.SessionPublicKeyFromString(spec.BrokerPublicKey); err != nil {
  38. return errors.Tracef("invalid broker public key: %w", err)
  39. }
  40. if checkBrokerSpecsList != nil {
  41. found := false
  42. for _, checkBrokerSpec := range *checkBrokerSpecsList {
  43. // Verify that the broker public key and root obfuscation
  44. // secret match an entry on the check list. The fronting
  45. // specs may differ and are not compared.
  46. if spec.BrokerPublicKey == checkBrokerSpec.BrokerPublicKey &&
  47. spec.BrokerRootObfuscationSecret == checkBrokerSpec.BrokerRootObfuscationSecret {
  48. found = true
  49. break
  50. }
  51. }
  52. if !found {
  53. return errors.TraceNew("unknown broker spec")
  54. }
  55. }
  56. if _, err := inproxy.ObfuscationSecretFromString(spec.BrokerRootObfuscationSecret); err != nil {
  57. return errors.Tracef("invalid broker root obfuscation secret: %w", err)
  58. }
  59. if len(spec.BrokerFrontingSpecs) == 0 {
  60. return errors.TraceNew("missing broker fronting spec")
  61. }
  62. // Broker fronting specs may specify SkipVerify, since the meek
  63. // payload has it's own transport security layer, the Noise sessions.
  64. // Broker fronting dials use MeekModeWrappedPlaintextRoundTrip.
  65. allowSkipVerify := true
  66. err := spec.BrokerFrontingSpecs.Validate(allowSkipVerify)
  67. if err != nil {
  68. return errors.Trace(err)
  69. }
  70. }
  71. return nil
  72. }
  73. // InproxyCompartmentIDsValue is a list of in-proxy common compartment IDs.
  74. type InproxyCompartmentIDsValue []string
  75. // Validate checks that the in-proxy common compartment ID values are
  76. // well-formed.
  77. func (IDs InproxyCompartmentIDsValue) Validate(checkCompartmentIDList *[]string) error {
  78. for _, ID := range IDs {
  79. if _, err := inproxy.IDFromString(ID); err != nil {
  80. return errors.Tracef("invalid compartment ID: %w", err)
  81. }
  82. if checkCompartmentIDList != nil && !common.Contains(*checkCompartmentIDList, ID) {
  83. return errors.TraceNew("unknown compartment ID")
  84. }
  85. }
  86. return nil
  87. }
  88. // InproxyTrafficShapingParametersValue is type-compatible with
  89. // common/inproxy.TrafficShapingParameters.
  90. type InproxyTrafficShapingParametersValue struct {
  91. MinPaddedMessages int
  92. MaxPaddedMessages int
  93. MinPaddingSize int
  94. MaxPaddingSize int
  95. MinDecoyMessages int
  96. MaxDecoyMessages int
  97. MinDecoySize int
  98. MaxDecoySize int
  99. DecoyMessageProbability float64
  100. }
  101. func (p *InproxyTrafficShapingParametersValue) Validate() error {
  102. if p.MinPaddedMessages < 0 ||
  103. p.MaxPaddedMessages < 0 ||
  104. p.MinPaddingSize < 0 ||
  105. p.MaxPaddingSize < 0 ||
  106. p.MinDecoyMessages < 0 ||
  107. p.MaxDecoyMessages < 0 ||
  108. p.MinDecoySize < 0 ||
  109. p.MaxDecoySize < 0 ||
  110. p.DecoyMessageProbability < 0.0 {
  111. return errors.TraceNew("invalid parameter")
  112. }
  113. return nil
  114. }