inproxy.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(checkBrokerPublicKeyList *[]string) 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 checkBrokerPublicKeyList != nil && !common.Contains(*checkBrokerPublicKeyList, spec.BrokerPublicKey) {
  41. return errors.TraceNew("unknown broker public key")
  42. }
  43. if _, err := inproxy.ObfuscationSecretFromString(spec.BrokerRootObfuscationSecret); err != nil {
  44. return errors.Tracef("invalid broker root obfuscation secret: %w", err)
  45. }
  46. if len(spec.BrokerFrontingSpecs) == 0 {
  47. return errors.TraceNew("missing broker fronting spec")
  48. }
  49. // Broker fronting specs may specify SkipVerify, since the meek
  50. // payload has it's own transport security layer, the Noise sessions.
  51. // Broker fronting dials use MeekModeWrappedPlaintextRoundTrip.
  52. allowSkipVerify := true
  53. err := spec.BrokerFrontingSpecs.Validate(allowSkipVerify)
  54. if err != nil {
  55. return errors.Trace(err)
  56. }
  57. }
  58. return nil
  59. }
  60. // InproxyCompartmentIDsValue is a list of in-proxy common compartment IDs.
  61. type InproxyCompartmentIDsValue []string
  62. // Validate checks that the in-proxy common compartment ID values are
  63. // well-formed.
  64. func (IDs InproxyCompartmentIDsValue) Validate(checkCompartmentIDList *[]string) error {
  65. for _, ID := range IDs {
  66. if _, err := inproxy.IDFromString(ID); err != nil {
  67. return errors.Tracef("invalid compartment ID: %w", err)
  68. }
  69. if checkCompartmentIDList != nil && !common.Contains(*checkCompartmentIDList, ID) {
  70. return errors.TraceNew("unknown compartment ID")
  71. }
  72. }
  73. return nil
  74. }
  75. // InproxyDataChannelTrafficShapingParameters is type-compatible with
  76. // common/inproxy.DataChannelTrafficShapingParameters.
  77. type InproxyDataChannelTrafficShapingParametersValue struct {
  78. MinPaddedMessages int
  79. MaxPaddedMessages int
  80. MinPaddingSize int
  81. MaxPaddingSize int
  82. MinDecoyMessages int
  83. MaxDecoyMessages int
  84. MinDecoySize int
  85. MaxDecoySize int
  86. DecoyMessageProbability float64
  87. }
  88. func (p *InproxyDataChannelTrafficShapingParametersValue) Validate() error {
  89. if p.MinPaddedMessages < 0 ||
  90. p.MaxPaddedMessages < 0 ||
  91. p.MinPaddingSize < 0 ||
  92. p.MaxPaddingSize < 0 ||
  93. p.MinDecoyMessages < 0 ||
  94. p.MaxDecoyMessages < 0 ||
  95. p.MinDecoySize < 0 ||
  96. p.MaxDecoySize < 0 ||
  97. p.DecoyMessageProbability < 0.0 {
  98. return errors.TraceNew("invalid parameter")
  99. }
  100. return nil
  101. }