Class NamedProtocolHandshakeBuilder

java.lang.Object
com.eatthepath.noise.NamedProtocolHandshakeBuilder

public class NamedProtocolHandshakeBuilder extends Object

A NamedProtocolHandshakeBuilder constructs NoiseHandshake instances given a full Noise protocol name and a role (initiator or responder). In contrast to NoiseHandshakeBuilder, callers are responsible for identifying and providing all required key material, which may vary with handshake pattern and role. For example, the NN handshake pattern is defined as:

NN:
   -> e
   <- e, ee

…and so neither the initiator nor the responder requires any static or pre-shared keys:

final NoiseHandshake nnInitiatorHandshake =
    new NamedProtocolHandshakeBuilder(nnProtocolName, NoiseHandshake.Role.INITIATOR).build();

final NoiseHandshake nnResponderHandshake =
    new NamedProtocolHandshakeBuilder(nnProtocolName, NoiseHandshake.Role.RESPONDER).build();

By contrast, the IK handshake pattern is defined as:

IK:
   <- s
   ...
   -> e, es, s, ss
   <- e, ee, se

…and so the initiator needs a local static key pair and a remote static public key, while the responder needs only a local static key pair:

final NoiseHandshake ikInitiatorHandshake =
    new NamedProtocolHandshakeBuilder(ikProtocolName, NoiseHandshake.Role.INITIATOR)
        .setLocalStaticKeyPair(initiatorLocalStaticKeyPair)
        .setRemoteStaticPublicKey(initiatorRemoteStaticPublicKey)
        .build();

final NoiseHandshake ikResponderHandshake =
    new NamedProtocolHandshakeBuilder(ikProtocolName, NoiseHandshake.Role.RESPONDER)
        .setLocalStaticKeyPair(responderLocalStaticKeyPair)
        .build();
See Also:
  • Constructor Details

    • NamedProtocolHandshakeBuilder

      public NamedProtocolHandshakeBuilder(String noiseProtocolName, NoiseHandshake.Role role) throws NoSuchAlgorithmException, NoSuchPatternException
      Constructs a new Noise handshake for the given Noise protocol name and role.
      Parameters:
      noiseProtocolName - the full Noise protocol name for which to construct a handshake object
      role - the role for the handshake object
      Throws:
      NoSuchAlgorithmException - if one or more components of the Noise protocol was not recognized or is not supported in the current JVM
      NoSuchPatternException - if the handshake pattern in the Noise protocol name was not recognized or is invalid
  • Method Details

    • setPrologue

      public NamedProtocolHandshakeBuilder setPrologue(@Nullable byte[] prologue)
      Sets the prologue for this handshake.
      Parameters:
      prologue - the prologue for this handshake; may be null
      Returns:
      a reference to this handshake builder
    • setLocalStaticKeyPair

      public NamedProtocolHandshakeBuilder setLocalStaticKeyPair(KeyPair localStaticKeyPair)
      Sets the local static key pair for this handshake.
      Parameters:
      localStaticKeyPair - the local static key pair for this handshake; must not be null
      Returns:
      a reference to this handshake builder
      Throws:
      IllegalStateException - if the chosen handshake pattern does not allow for local static keys
      See Also:
      • HandshakePattern.requiresLocalStaticKeyPair(NoiseHandshake.Role)
    • setRemoteStaticPublicKey

      public NamedProtocolHandshakeBuilder setRemoteStaticPublicKey(PublicKey remoteStaticPublicKey)
      Sets the remote static public key for this handshake.
      Parameters:
      remoteStaticPublicKey - the remote static public key for this handshake; must not be null
      Returns:
      a reference to this builder
      Throws:
      IllegalStateException - if the chosen handshake pattern does not allow for remote static keys
      See Also:
      • HandshakePattern.requiresRemoteStaticPublicKey(NoiseHandshake.Role)
    • setPreSharedKeys

      public NamedProtocolHandshakeBuilder setPreSharedKeys(List<byte[]> preSharedKeys)
      Sets the pre-shared keys for this handshake.
      Parameters:
      preSharedKeys - the pre-shared keys for this handshake; must not be null
      Returns:
      a reference to this builder
      Throws:
      IllegalStateException - if the chosen handshake pattern does not allow for pre-shared keys
      IllegalArgumentException - if the given list of pre-shared keys has a length that does not match the number of pre-shared keys required by the chosen handshake pattern or if any key is not exactly 32 bytes in length
      See Also:
      • HandshakePattern.getRequiredPreSharedKeyCount()
    • build

      public NoiseHandshake build()
      Constructs a Noise handshake with the previously-configured protocol and keys.
      Returns:
      a Noise handshake with the previously-configured protocol and keys
      Throws:
      IllegalStateException - if any keys required by the chosen handshake pattern have not been set
      See Also:
      • HandshakePattern.requiresLocalStaticKeyPair(NoiseHandshake.Role)
      • HandshakePattern.requiresRemoteStaticPublicKey(NoiseHandshake.Role)
      • HandshakePattern.getRequiredPreSharedKeyCount()