func (sf *obfs4ServerFactory) WrapConn(conn net.Conn) (net.Conn, error) { // Not much point in having a separate newObfs4ServerConn routine when // wrapping requires using values from the factory instance. // Generate the session keypair *before* consuming data from the peer, to // attempt to mask the rejection sampling due to use of Elligator2. This // might be futile, but the timing differential isn't very large on modern // hardware, and there are far easier statistical attacks that can be // mounted as a distinguisher. sessionKey, err := ntor.NewKeypair(true) if err != nil { return nil, err } lenDist := probdist.New(sf.lenSeed, 0, framing.MaximumSegmentLength, biasedDist) var iatDist *probdist.WeightedDist if sf.iatSeed != nil { iatDist = probdist.New(sf.iatSeed, 0, maxIATDelay, biasedDist) } c := &obfs4Conn{conn, true, lenDist, iatDist, sf.iatMode, bytes.NewBuffer(nil), bytes.NewBuffer(nil), nil, nil} startTime := time.Now() if err = c.serverHandshake(sf, sessionKey); err != nil { c.closeAfterDelay(sf, startTime) return nil, err } return c, nil }
func (cf *obfs4ClientFactory) ParseArgs(args *pt.Args) (interface{}, error) { var nodeID *ntor.NodeID var publicKey *ntor.PublicKey // The "new" (version >= 0.0.3) bridge lines use a unified "cert" argument // for the Node ID and Public Key. certStr, ok := args.Get(certArg) if ok { cert, err := serverCertFromString(certStr) if err != nil { return nil, err } nodeID, publicKey = cert.unpack() } else { // The "old" style (version <= 0.0.2) bridge lines use separate Node ID // and Public Key arguments in Base16 encoding and are a UX disaster. nodeIDStr, ok := args.Get(nodeIDArg) if !ok { return nil, fmt.Errorf("missing argument '%s'", nodeIDArg) } var err error if nodeID, err = ntor.NodeIDFromHex(nodeIDStr); err != nil { return nil, err } publicKeyStr, ok := args.Get(publicKeyArg) if !ok { return nil, fmt.Errorf("missing argument '%s'", publicKeyArg) } if publicKey, err = ntor.PublicKeyFromHex(publicKeyStr); err != nil { return nil, err } } // IAT config is common across the two bridge line formats. iatStr, ok := args.Get(iatArg) if !ok { return nil, fmt.Errorf("missing argument '%s'", iatArg) } iatMode, err := strconv.Atoi(iatStr) if err != nil || iatMode < iatNone || iatMode > iatParanoid { return nil, fmt.Errorf("invalid iat-mode '%d'", iatMode) } // Generate the session key pair before connectiong to hide the Elligator2 // rejection sampling from network observers. sessionKey, err := ntor.NewKeypair(true) if err != nil { return nil, err } return &obfs4ClientArgs{nodeID, publicKey, sessionKey, iatMode}, nil }
func newJSONServerState(stateDir string, js *jsonServerState) (err error) { // Generate everything a server needs, using the cryptographic PRNG. var st obfs4ServerState rawID := make([]byte, ntor.NodeIDLength) if err = csrand.Bytes(rawID); err != nil { return } if st.nodeID, err = ntor.NewNodeID(rawID); err != nil { return } if st.identityKey, err = ntor.NewKeypair(false); err != nil { return } if st.drbgSeed, err = drbg.NewSeed(); err != nil { return } st.iatMode = iatNone // Encode it into JSON format and write the state file. js.NodeID = st.nodeID.Hex() js.PrivateKey = st.identityKey.Private().Hex() js.PublicKey = st.identityKey.Public().Hex() js.DrbgSeed = st.drbgSeed.Hex() js.IATMode = st.iatMode var encoded []byte if encoded, err = json.Marshal(js); err != nil { return } if err = ioutil.WriteFile(path.Join(stateDir, stateFile), encoded, 0600); err != nil { return err } return nil }