<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rfc [
  <!ENTITY nbsp "&#160;">
  <!ENTITY zwsp "&#8203;">
  <!ENTITY nbhy "&#8209;">
  <!ENTITY wj "&#8288;">
]>

<rfc xmlns:xi="http://www.w3.org/2001/XInclude"
     ipr="trust200902"
     docName="draft-azima-ncbp-00"
     category="exp"
     submissionType="independent"
     version="3">

  <front>
    <title abbrev="NCBP">Network ClipBoard Protocol (NCBP)</title>

    <seriesInfo name="Internet-Draft" value="draft-azima-ncbp-00"/>

    <author fullname="Zeyad Abdelazim" initials="Z." surname="Abdelazim">
      <organization/>
      <address>
        <email>contact@zeyadazima.com</email>
      </address>
    </author>

    <date year="2026" month="December"/>

    <area>Applications</area>

    <keyword>clipboard</keyword>
    <keyword>synchronization</keyword>
    <keyword>LAN</keyword>
    <keyword>binary protocol</keyword>

    <abstract>
      <t>
        The Network ClipBoard Protocol (NCBP) enables real-time clipboard
        synchronization across devices on a shared local network. NCBP
        defines a client-server architecture where a central relay server
        accepts clipboard content from connected clients and broadcasts
        updates to all other authenticated peers. The protocol operates
        over TCP with mandatory TLS 1.3 encryption and Pre-Shared Key
        (PSK) authentication derived via PBKDF2-HMAC-SHA256. This
        document specifies the binary message format, connection
        lifecycle, keepalive mechanism, service discovery via mDNS/DNS-SD,
        targeted delivery, and security considerations for NCBP version 1.0.
      </t>
    </abstract>
  </front>

  <middle>

    <!-- ============================================================ -->
    <section anchor="introduction">
      <name>Introduction</name>
      <t>
        Modern workflows span multiple devices -- laptops, desktops,
        phones, and tablets. Copying text on one device and pasting it
        on another currently requires third-party cloud services, which
        introduce latency, privacy concerns, and internet dependency.
      </t>
      <t>
        NCBP solves this by providing a lightweight, LAN-optimized
        protocol for clipboard synchronization that:
      </t>
      <ul>
        <li>Operates entirely within the local network (no cloud dependency)</li>
        <li>Uses strong encryption (TLS 1.3) for all clipboard data in transit</li>
        <li>Authenticates peers via Pre-Shared Keys</li>
        <li>Supports automatic server discovery via mDNS/DNS-SD</li>
        <li>Minimizes latency through persistent TCP connections</li>
      </ul>

      <section anchor="scope">
        <name>Scope</name>
        <t>
          This specification (version 1.0) covers plain text clipboard
          content only. Future versions MAY extend the protocol to
          support images, rich text, files, and other MIME types via the
          content type registry defined in <xref target="content-types"/>.
        </t>
      </section>

      <section anchor="goals">
        <name>Design Goals</name>
        <ul>
          <li>Sub-100ms clipboard propagation on a LAN</li>
          <li>Zero-configuration discovery</li>
          <li>End-to-end encryption of clipboard content</li>
          <li>Support for heterogeneous device networks</li>
          <li>Graceful handling of device join/leave events</li>
        </ul>
      </section>

      <section anchor="non-goals">
        <name>Non-Goals</name>
        <ul>
          <li>Internet/WAN clipboard sync (out of scope for version 1)</li>
          <li>Conflict resolution for simultaneous edits</li>
          <li>Clipboard history or versioning at the protocol level</li>
          <li>File transfer beyond text content</li>
        </ul>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="terminology">
      <name>Terminology</name>
      <t>
        The key words "MUST", "MUST NOT", "REQUIRED", "SHALL",
        "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED",
        "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document
        are to be interpreted as described in BCP 14
        <xref target="RFC2119"/> <xref target="RFC8174"/> when,
        and only when, they appear in all capitals, as shown here.
      </t>
      <dl>
        <dt>NCBP Server</dt>
        <dd>The central relay node that accepts client connections
            and broadcasts clipboard updates.</dd>
        <dt>NCBP Client</dt>
        <dd>A device that connects to the server, sends clipboard
            updates, and receives broadcasts.</dd>
        <dt>Peer</dt>
        <dd>Any NCBP client connected to the same server.</dd>
        <dt>PSK</dt>
        <dd>Pre-Shared Key -- a secret passphrase known to all
            authorized devices in a clipboard group.</dd>
        <dt>Clip</dt>
        <dd>A unit of clipboard content transmitted via the protocol.</dd>
        <dt>Device ID</dt>
        <dd>A 128-bit (16-byte) randomly generated unique identifier
            assigned to each client instance.</dd>
        <dt>Session</dt>
        <dd>The period from a successful HELLO/WELCOME exchange to
            disconnection.</dd>
      </dl>
    </section>

    <!-- ============================================================ -->
    <section anchor="protocol-overview">
      <name>Protocol Overview</name>
      <figure>
        <artwork type="ascii-art"><![CDATA[
+----------+          +-------------+          +----------+
| Client A | -------> | NCBP Server | -------> | Client B |
| (copy)   |  CLIP    |   (relay)   | CLIP_    | (paste)  |
|          |          |             | NOTIFY   |          |
+----------+          +-------------+          +----------+
                            |
                            +-----> +----------+
                            |       | Client C |
                            +-----> | (paste)  |
                                    +----------+
]]></artwork>
      </figure>
      <ol>
        <li>All clients establish persistent TCP+TLS connections to
            the server.</li>
        <li>When a user copies text on Client A, the client sends a
            CLIP message to the server.</li>
        <li>The server broadcasts a CLIP_NOTIFY to all other connected
            clients (or a targeted subset).</li>
        <li>Each receiving client updates its local clipboard.</li>
      </ol>
    </section>

    <!-- ============================================================ -->
    <section anchor="transport">
      <name>Transport Layer</name>

      <section anchor="tcp">
        <name>TCP</name>
        <t>
          NCBP operates over TCP <xref target="RFC9293"/>.
          Implementations MUST use TCP as the transport protocol.
        </t>
      </section>

      <section anchor="default-port">
        <name>Default Port</name>
        <t>
          The default port for NCBP is 31337/tcp. Implementations
          SHOULD allow configuration of an alternate port.
        </t>
      </section>

      <section anchor="tls">
        <name>TLS</name>
        <t>
          All NCBP connections MUST be encrypted using TLS 1.3
          <xref target="RFC8446"/> or later. Implementations
          MUST NOT fall back to unencrypted TCP in production use.
        </t>
        <t>
          The TLS layer MUST be established before any NCBP messages
          are exchanged. The NCBP handshake (HELLO/WELCOME) occurs
          within the encrypted TLS channel.
        </t>

        <section anchor="psk-auth">
          <name>PSK Authentication</name>
          <t>NCBP uses a Pre-Shared Key model for authentication:</t>
          <ol>
            <li>All devices in a clipboard group share a common
                passphrase.</li>
            <li>The passphrase is used to derive a 256-bit key using
                PBKDF2-HMAC-SHA256 <xref target="RFC8018"/> with
                a minimum of 100,000 iterations.</li>
            <li>During the NCBP HELLO, the client proves knowledge of
                the PSK by sending HMAC-SHA256(PSK, nonce).</li>
            <li>The server verifies the HMAC before granting session
                access.</li>
          </ol>
          <t>The PSK derivation parameters:</t>
          <artwork><![CDATA[
salt       = "NCBP-v1-salt"  (12 bytes, ASCII)
iterations = 100000
dklen      = 32 bytes (256 bits)
psk        = PBKDF2-HMAC-SHA256(passphrase, salt, iterations, dklen)
]]></artwork>
        </section>
      </section>

      <section anchor="connection-params">
        <name>Connection Parameters</name>
        <ul>
          <li>TCP Keepalive: Implementations SHOULD enable TCP-level
              keepalive.</li>
          <li>TCP_NODELAY: Implementations SHOULD set TCP_NODELAY to
              minimize clipboard propagation latency.</li>
          <li>Maximum Message Size: 1,048,576 bytes (1 MiB). Messages
              exceeding this size MUST be rejected with error code
              PAYLOAD_TOO_LARGE (0x0003).</li>
        </ul>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="message-format">
      <name>Message Format</name>
      <t>
        All NCBP messages use a fixed-size binary header followed by a
        variable-length payload.
      </t>

      <section anchor="header-format">
        <name>Header Format</name>
        <t>The header is 36 bytes:</t>
        <artwork type="ascii-art"><![CDATA[
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Magic (0x4E42)        |   Version     |     Type      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|             Flags             |          Message ID           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Payload Length                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                     Timestamp (64-bit)                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                                                               |
|                     Device ID (128-bit)                       |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
        <table>
          <thead>
            <tr><th>Field</th><th>Offset</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Magic</td><td>0</td><td>2 bytes</td>
                <td>Protocol identifier: 0x4E42 ("NB" in ASCII)</td></tr>
            <tr><td>Version</td><td>2</td><td>1 byte</td>
                <td>Protocol version (0x01 for NCBP/1.0)</td></tr>
            <tr><td>Type</td><td>3</td><td>1 byte</td>
                <td>Message type (see <xref target="message-types"/>)</td></tr>
            <tr><td>Flags</td><td>4</td><td>2 bytes</td>
                <td>Bitfield flags (see <xref target="flags"/>)</td></tr>
            <tr><td>Message ID</td><td>6</td><td>2 bytes</td>
                <td>Sequence number, wraps at 65535</td></tr>
            <tr><td>Payload Length</td><td>8</td><td>4 bytes</td>
                <td>Length of payload in bytes (big-endian)</td></tr>
            <tr><td>Timestamp</td><td>12</td><td>8 bytes</td>
                <td>Unix epoch milliseconds (big-endian)</td></tr>
            <tr><td>Device ID</td><td>20</td><td>16 bytes</td>
                <td>UUID of the sending device</td></tr>
          </tbody>
        </table>
        <t>
          All multi-byte integer fields are in network byte order
          (big-endian).
        </t>
      </section>

      <section anchor="flags">
        <name>Flags</name>
        <artwork type="ascii-art"><![CDATA[
 0                   1
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|A|R|E|          Reserved       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
        <table>
          <thead>
            <tr><th>Bit</th><th>Name</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>0</td><td>ACK_REQ</td>
                <td>Sender requests acknowledgment</td></tr>
            <tr><td>1</td><td>ACK_RSP</td>
                <td>This message is an acknowledgment</td></tr>
            <tr><td>2</td><td>ENCRYPT</td>
                <td>Payload is additionally encrypted (reserved for future use)</td></tr>
            <tr><td>3-15</td><td>Reserved</td>
                <td>MUST be zero</td></tr>
          </tbody>
        </table>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="message-types">
      <name>Message Types</name>
      <table>
        <thead>
          <tr><th>Value</th><th>Name</th><th>Direction</th><th>Description</th></tr>
        </thead>
        <tbody>
          <tr><td>0x01</td><td>HELLO</td><td>Client -&gt; Server</td>
              <td>Authentication request</td></tr>
          <tr><td>0x02</td><td>WELCOME</td><td>Server -&gt; Client</td>
              <td>Authentication accepted</td></tr>
          <tr><td>0x03</td><td>CLIP</td><td>Client -&gt; Server</td>
              <td>Clipboard content update</td></tr>
          <tr><td>0x04</td><td>CLIP_NOTIFY</td><td>Server -&gt; Client</td>
              <td>Broadcast clipboard to peers</td></tr>
          <tr><td>0x05</td><td>PING</td><td>Bidirectional</td>
              <td>Keepalive request</td></tr>
          <tr><td>0x06</td><td>PONG</td><td>Bidirectional</td>
              <td>Keepalive response</td></tr>
          <tr><td>0x07</td><td>PEER_JOIN</td><td>Server -&gt; Client</td>
              <td>New peer connected notification</td></tr>
          <tr><td>0x08</td><td>PEER_LEAVE</td><td>Server -&gt; Client</td>
              <td>Peer disconnected notification</td></tr>
          <tr><td>0x09</td><td>ERROR</td><td>Server -&gt; Client</td>
              <td>Error notification</td></tr>
          <tr><td>0x0A</td><td>BYE</td><td>Bidirectional</td>
              <td>Graceful disconnect</td></tr>
          <tr><td>0x0B</td><td>SECURE_SEND</td><td>Client -&gt; Server</td>
              <td>E2E encrypted direct message</td></tr>
          <tr><td>0x0C</td><td>SECURE_RECV</td><td>Server -&gt; Client</td>
              <td>Forwarded encrypted direct message</td></tr>
          <tr><td>0x0D</td><td>SECURE_ACK</td><td>Bidirectional</td>
              <td>Delivery/read receipt for secure message</td></tr>
        </tbody>
      </table>
      <t>
        Values 0x00 and 0x0E-0xFF are reserved for future use.
      </t>
    </section>

    <!-- ============================================================ -->
    <section anchor="payload-structures">
      <name>Payload Structures</name>

      <section anchor="hello-payload">
        <name>HELLO (0x01)</name>
        <t>Sent by the client immediately after TLS handshake.</t>
        <artwork type="ascii-art"><![CDATA[
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                   PSK Auth Hash (32 bytes)                    |
|                            ...                                |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                      Nonce (16 bytes)                         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      Device Name Length       |    Device Name (UTF-8) ...    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>PSK Auth Hash</td><td>32 bytes</td>
                <td>HMAC-SHA256(PSK, nonce)</td></tr>
            <tr><td>Nonce</td><td>16 bytes</td>
                <td>Cryptographically random nonce</td></tr>
            <tr><td>Device Name Length</td><td>2 bytes</td>
                <td>Length of device name in bytes</td></tr>
            <tr><td>Device Name</td><td>Variable</td>
                <td>UTF-8 encoded device name (max 255 bytes)</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="welcome-payload">
        <name>WELCOME (0x02)</name>
        <t>Sent by the server upon successful authentication.</t>
        <artwork type="ascii-art"><![CDATA[
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Session ID                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Peer Count            |        Peer List ...          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
        <t>Each peer entry in the peer list:</t>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Device ID</td><td>16 bytes</td>
                <td>Peer's UUID</td></tr>
            <tr><td>Device Name Length</td><td>2 bytes</td>
                <td>Length of device name</td></tr>
            <tr><td>Device Name</td><td>Variable</td>
                <td>UTF-8 device name</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="clip-payload">
        <name>CLIP (0x03)</name>
        <t>Sent by a client when its clipboard content changes.</t>
        <artwork type="ascii-art"><![CDATA[
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Content Type  |                                               |
+-+-+-+-+-+-+-+-+                                               |
|                   Content Hash (32 bytes)                     |
|                            ...                                |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Content Length                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Content (UTF-8) ...                     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Target Count |   Target Device IDs (16 bytes each) ...      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Content Type</td><td>1 byte</td>
                <td>0x01 = text/plain (version 1.0 only)</td></tr>
            <tr><td>Content Hash</td><td>32 bytes</td>
                <td>SHA-256 of the content bytes</td></tr>
            <tr><td>Content Length</td><td>4 bytes</td>
                <td>Length of content in bytes</td></tr>
            <tr><td>Content</td><td>Variable</td>
                <td>The clipboard text (UTF-8 encoded)</td></tr>
            <tr><td>Target Count</td><td>1 byte</td>
                <td>Number of target devices (0 = broadcast to all)</td></tr>
            <tr><td>Target Device IDs</td><td>16 bytes each</td>
                <td>Device IDs of intended recipients</td></tr>
          </tbody>
        </table>
        <t>
          When Target Count is 0 or the field is absent, the server
          broadcasts the clip to all connected peers except the sender.
          Clients that do not understand targeting stop reading after
          Content Length bytes and ignore trailing data, providing
          backward compatibility.
        </t>
      </section>

      <section anchor="clip-notify-payload">
        <name>CLIP_NOTIFY (0x04)</name>
        <t>
          Server broadcasts to all peers except the sender (or targeted
          subset). The payload format is identical to CLIP
          (<xref target="clip-payload"/>). The header's Device ID field
          contains the originating device's ID.
        </t>
      </section>

      <section anchor="ping-payload">
        <name>PING (0x05)</name>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Ping Data</td><td>8 bytes</td>
                <td>Opaque data to be echoed in PONG</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="pong-payload">
        <name>PONG (0x06)</name>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Pong Data</td><td>8 bytes</td>
                <td>Echoed data from PING</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="peer-join-payload">
        <name>PEER_JOIN (0x07)</name>
        <t>Broadcast when a new peer joins.</t>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Device ID</td><td>16 bytes</td>
                <td>New peer's UUID</td></tr>
            <tr><td>Device Name Length</td><td>2 bytes</td>
                <td>Length of device name</td></tr>
            <tr><td>Device Name</td><td>Variable</td>
                <td>UTF-8 device name</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="peer-leave-payload">
        <name>PEER_LEAVE (0x08)</name>
        <t>Broadcast when a peer disconnects.</t>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Device ID</td><td>16 bytes</td>
                <td>Departed peer's UUID</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="error-payload">
        <name>ERROR (0x09)</name>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Error Code</td><td>2 bytes</td>
                <td>Numeric error code</td></tr>
            <tr><td>Message Length</td><td>2 bytes</td>
                <td>Length of error message</td></tr>
            <tr><td>Error Message</td><td>Variable</td>
                <td>UTF-8 error description</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="bye-payload">
        <name>BYE (0x0A)</name>
        <t>
          No payload. The header alone signals graceful disconnect.
          After sending BYE, the sender MUST NOT send further messages
          and SHOULD close the connection after a reasonable timeout
          (5 seconds).
        </t>
      </section>

      <section anchor="secure-send-payload">
        <name>SECURE_SEND (0x0B)</name>
        <t>
          Sent by a client to deliver end-to-end encrypted content to
          a specific target peer. The server routes it to the target
          without being able to decrypt the payload.
        </t>
        <artwork type="ascii-art"><![CDATA[
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                 Target Device ID (16 bytes)                   |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|    Flags      |                 TTL (seconds)                 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|            Ephemeral Public Key (32 bytes, X25519)            |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Nonce (12 bytes)                        |
|                                               +-+-+-+-+-+-+-+-+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+               |
|                   Ciphertext Length                            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                 Ciphertext (AES-256-GCM) ...                  |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                   Auth Tag (16 bytes)                         |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]]></artwork>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Target Device ID</td><td>16 bytes</td>
                <td>Recipient's UUID</td></tr>
            <tr><td>Flags</td><td>1 byte</td>
                <td>Bit 0: BURN_AFTER_READ; Bit 1: REQUIRE_ACK; Bit 2: HIGH_SENSITIVITY</td></tr>
            <tr><td>TTL</td><td>4 bytes</td>
                <td>Seconds until undelivered message expires (0 = never)</td></tr>
            <tr><td>Ephemeral Public Key</td><td>32 bytes</td>
                <td>Sender's per-message X25519 public key</td></tr>
            <tr><td>Nonce</td><td>12 bytes</td>
                <td>AES-256-GCM nonce (unique per message)</td></tr>
            <tr><td>Ciphertext Length</td><td>4 bytes</td>
                <td>Length of encrypted content</td></tr>
            <tr><td>Ciphertext</td><td>Variable</td>
                <td>AES-256-GCM encrypted plaintext</td></tr>
            <tr><td>Auth Tag</td><td>16 bytes</td>
                <td>GCM authentication tag</td></tr>
          </tbody>
        </table>
        <t>Key derivation:</t>
        <artwork><![CDATA[
shared_secret  = X25519(ephemeral_private, recipient_public_key)
encryption_key = HKDF-SHA256(
    ikm   = shared_secret,
    salt  = "NCBP-secret-send",
    info  = sender_device_id || target_device_id,
    dklen = 32
)
]]></artwork>
        <t>
          The server reads ONLY the Target Device ID. If the target is
          connected, the server wraps the payload unchanged into a
          SECURE_RECV and forwards it. If the target is not connected,
          the server queues the message until the TTL expires, then
          drops it. The server MUST NOT log, inspect, or modify the
          ciphertext.
        </t>
      </section>

      <section anchor="secure-recv-payload">
        <name>SECURE_RECV (0x0C)</name>
        <t>
          The server forwards SECURE_SEND payloads to the target peer
          using this message type. The payload is identical to
          SECURE_SEND. The header's Device ID field contains the
          original sender's Device ID.
        </t>
      </section>

      <section anchor="secure-ack-payload">
        <name>SECURE_ACK (0x0D)</name>
        <table>
          <thead>
            <tr><th>Field</th><th>Size</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>Original Sender Device ID</td><td>16 bytes</td>
                <td>Who sent the SECURE_SEND</td></tr>
            <tr><td>Original Message ID</td><td>2 bytes</td>
                <td>Message ID of the SECURE_SEND</td></tr>
            <tr><td>Status</td><td>1 byte</td>
                <td>0x01=DELIVERED, 0x02=READ, 0x03=EXPIRED, 0x04=REJECTED</td></tr>
          </tbody>
        </table>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="connection-lifecycle">
      <name>Connection Lifecycle</name>

      <section anchor="connection-establishment">
        <name>Connection Establishment</name>
        <ol>
          <li>Client resolves server address (via mDNS or configuration).</li>
          <li>Client opens TCP connection to server on port 31337.</li>
          <li>TLS 1.3 handshake completes.</li>
          <li>Client sends HELLO with PSK authentication material.</li>
          <li>Server validates the PSK hash. If valid: sends WELCOME
              with session info and peer list. If invalid: sends ERROR
              (AUTH_FAILED) and closes connection.</li>
          <li>Server broadcasts PEER_JOIN to all existing peers.</li>
          <li>Session is now ACTIVE.</li>
        </ol>
      </section>

      <section anchor="clipboard-exchange">
        <name>Clipboard Exchange</name>
        <ol>
          <li>Client detects local clipboard change.</li>
          <li>Client computes SHA-256 hash of new content.</li>
          <li>If hash differs from last sent hash, client sends CLIP.</li>
          <li>Server receives CLIP, validates payload.</li>
          <li>Server broadcasts CLIP_NOTIFY to all peers except sender
              (or targeted subset).</li>
          <li>Receiving clients update their local clipboard.</li>
        </ol>
      </section>

      <section anchor="graceful-disconnect">
        <name>Graceful Disconnect</name>
        <ol>
          <li>Client sends BYE message.</li>
          <li>Server removes client from peer list.</li>
          <li>Server broadcasts PEER_LEAVE to remaining peers.</li>
          <li>Both sides close the TLS/TCP connection.</li>
        </ol>
      </section>

      <section anchor="ungraceful-disconnect">
        <name>Ungraceful Disconnect</name>
        <t>
          If a client disconnects without BYE (crash, network failure):
        </t>
        <ol>
          <li>Server detects via TCP keepalive or PING timeout.</li>
          <li>Server removes client from peer list.</li>
          <li>Server broadcasts PEER_LEAVE to remaining peers.</li>
        </ol>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="state-machine">
      <name>State Machine</name>

      <section anchor="client-states">
        <name>Client States</name>
        <artwork type="ascii-art"><![CDATA[
                +---------------+
                | DISCONNECTED  |
                +-------+-------+
                        |
                  [TCP connect]
                        |
                +-------v-------+
                |   CONNECTED   |
                +-------+-------+
                        |
                  [send HELLO]
                        |
                +-------v-------+
                | AUTHENTICATING|
                +-------+-------+
                       / \
            [WELCOME] /   \ [ERROR]
                     /     \
            +-------v--+ +--v-----------+
            |  ACTIVE   | | DISCONNECTED |
            +-------+---+ +--------------+
                    |
          [BYE / error / timeout]
                    |
            +-------v-------+
            | DISCONNECTED  |
            +---------------+
]]></artwork>
      </section>

      <section anchor="server-states">
        <name>Server States (per client)</name>
        <artwork type="ascii-art"><![CDATA[
                +---------------+
                |   LISTENING   |
                +-------+-------+
                        |
                  [TCP accept]
                        |
                +-------v-------+
                |   CONNECTED   |
                +-------+-------+
                        |
                  [recv HELLO]
                        |
                +-------v-------+
                | AUTHENTICATING|
                +-------+-------+
                       / \
            [PSK OK]  /   \ [PSK FAIL]
                     /     \
            +-------v--+ +--v--------+
            |  ACTIVE   | |  CLOSED   |
            +-------+---+ +----------+
                    |
          [BYE / error / timeout]
                    |
            +-------v-------+
            |    CLOSED     |
            +---------------+
]]></artwork>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="keepalive">
      <name>Keepalive Mechanism</name>
      <t>
        The server SHOULD send PING to each client every 30 seconds.
        Clients MUST respond to PING with PONG within 10 seconds. If
        no PONG is received within 10 seconds, the server SHOULD
        consider the client disconnected. Clients MAY also send PING
        to the server to verify connectivity.
      </t>
      <table>
        <thead>
          <tr><th>Parameter</th><th>Value</th><th>Description</th></tr>
        </thead>
        <tbody>
          <tr><td>PING Interval</td><td>30 sec</td>
              <td>Time between server PINGs</td></tr>
          <tr><td>PONG Timeout</td><td>10 sec</td>
              <td>Max wait for PONG response</td></tr>
          <tr><td>HELLO Timeout</td><td>5 sec</td>
              <td>Max wait for HELLO after connect</td></tr>
          <tr><td>BYE Timeout</td><td>5 sec</td>
              <td>Max wait after BYE before close</td></tr>
        </tbody>
      </table>
    </section>

    <!-- ============================================================ -->
    <section anchor="error-handling">
      <name>Error Handling</name>

      <section anchor="error-codes">
        <name>Error Codes</name>
        <table>
          <thead>
            <tr><th>Code</th><th>Name</th><th>Description</th></tr>
          </thead>
          <tbody>
            <tr><td>0x0001</td><td>AUTH_FAILED</td>
                <td>PSK authentication failed</td></tr>
            <tr><td>0x0002</td><td>VERSION_MISMATCH</td>
                <td>Unsupported protocol version</td></tr>
            <tr><td>0x0003</td><td>PAYLOAD_TOO_LARGE</td>
                <td>Message exceeds maximum size</td></tr>
            <tr><td>0x0004</td><td>INVALID_MESSAGE</td>
                <td>Malformed or unexpected message</td></tr>
            <tr><td>0x0005</td><td>SERVER_FULL</td>
                <td>Server at maximum client capacity</td></tr>
            <tr><td>0x0006</td><td>INTERNAL_ERROR</td>
                <td>Server internal error</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="error-recovery">
        <name>Error Recovery</name>
        <ul>
          <li>AUTH_FAILED: Client MUST NOT retry with the same PSK
              without user intervention. Rate limiting of 3 attempts
              per minute SHOULD be enforced.</li>
          <li>VERSION_MISMATCH: Client MAY downgrade if it supports
              the server's version.</li>
          <li>PAYLOAD_TOO_LARGE: Client SHOULD truncate or skip the
              content.</li>
          <li>INVALID_MESSAGE: Receiver SHOULD log and ignore the
              message.</li>
          <li>SERVER_FULL: Client SHOULD retry with exponential
              backoff.</li>
        </ul>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="service-discovery">
      <name>Service Discovery</name>

      <section anchor="mdns">
        <name>mDNS/DNS-SD</name>
        <t>
          NCBP servers SHOULD announce their presence using DNS-SD
          <xref target="RFC6763"/> over mDNS <xref target="RFC6762"/>.
        </t>
        <t>Service Type: _ncbp._tcp.local.</t>
        <t>TXT Record Fields:</t>
        <table>
          <thead>
            <tr><th>Key</th><th>Description</th><th>Example</th></tr>
          </thead>
          <tbody>
            <tr><td>version</td><td>NCBP protocol version</td><td>"1"</td></tr>
            <tr><td>name</td><td>Human-readable server name</td><td>"HomeNet"</td></tr>
            <tr><td>psk</td><td>Whether PSK is required</td><td>"true"</td></tr>
          </tbody>
        </table>
      </section>

      <section anchor="discovery-flow">
        <name>Discovery Flow</name>
        <ol>
          <li>Client sends mDNS query for _ncbp._tcp.local.</li>
          <li>Server responds with SRV record (hostname + port) and
              TXT record.</li>
          <li>Client resolves hostname to IP via mDNS A/AAAA record.</li>
          <li>Client connects to resolved address and port.</li>
        </ol>
        <t>Example mDNS records:</t>
        <artwork><![CDATA[
_ncbp._tcp.local.   SRV  0 0 31337 ncbp-server.local.
_ncbp._tcp.local.   TXT  "version=1" "name=HomeNet" "psk=true"
ncbp-server.local.  A    192.168.1.100
]]></artwork>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="security">
      <name>Security Considerations</name>

      <section anchor="threat-model">
        <name>Threat Model</name>
        <t>
          NCBP assumes a local network environment with the following
          threats:
        </t>
        <ul>
          <li>Eavesdropping: Mitigated by mandatory TLS 1.3
              <xref target="RFC8446"/>.</li>
          <li>Impersonation: Mitigated by PSK authentication via
              HMAC-SHA256.</li>
          <li>Replay attacks: Mitigated by cryptographic nonce in
              HELLO and TLS replay protection.</li>
          <li>Clipboard exfiltration: Mitigated by PSK -- only devices
              with the shared secret can join.</li>
        </ul>
      </section>

      <section anchor="psk-management">
        <name>PSK Management</name>
        <ul>
          <li>PSKs SHOULD be at least 8 characters with mixed case,
              digits, and symbols.</li>
          <li>PSKs MUST be transmitted out-of-band (not over NCBP
              itself).</li>
          <li>Implementations SHOULD store PSKs in OS-level secure
              storage (keychain, credential manager).</li>
          <li>PSKs SHOULD be rotatable without restarting the
              server.</li>
        </ul>
      </section>

      <section anchor="content-security">
        <name>Content Security</name>
        <ul>
          <li>Clipboard content is transmitted within the TLS tunnel.
              No additional application-layer encryption is applied
              in version 1.0. The SECURE_SEND extension
              (<xref target="secure-send-payload"/>) provides
              end-to-end encryption for sensitive content using
              X25519 <xref target="RFC7748"/> key agreement and
              HKDF <xref target="RFC5869"/> key derivation.</li>
          <li>Implementations SHOULD NOT log clipboard content to
              disk.</li>
          <li>Implementations SHOULD clear sensitive clipboard content
              from memory after a configurable timeout.</li>
        </ul>
      </section>

      <section anchor="rate-limiting">
        <name>Rate Limiting</name>
        <ul>
          <li>Servers SHOULD rate-limit CLIP messages to prevent
              flooding (RECOMMENDED: 10 messages per second per
              client).</li>
          <li>Servers SHOULD rate-limit failed authentication
              attempts (RECOMMENDED: 3 per minute per IP).</li>
        </ul>
      </section>

      <section anchor="dos">
        <name>Denial of Service</name>
        <ul>
          <li>Servers SHOULD enforce a maximum number of concurrent
              clients (RECOMMENDED default: 50).</li>
          <li>Servers MUST reject payloads exceeding 1 MiB.</li>
        </ul>
      </section>
    </section>

    <!-- ============================================================ -->
    <section anchor="iana">
      <name>IANA Considerations</name>

      <section anchor="port-registration">
        <name>Port Number</name>
        <t>This document requests registration of the following port:</t>
        <ul>
          <li>Service Name: ncbp</li>
          <li>Transport Protocol: TCP</li>
          <li>Port Number: 31337</li>
          <li>Description: Network ClipBoard Protocol</li>
          <li>Reference: This document</li>
        </ul>
      </section>

      <section anchor="service-name-registration">
        <name>Service Name for DNS-SD</name>
        <ul>
          <li>Service Type: _ncbp._tcp</li>
          <li>Description: Network ClipBoard Protocol service discovery</li>
        </ul>
      </section>

      <section anchor="message-type-registry">
        <name>Message Type Registry</name>
        <t>
          A new registry "NCBP Message Types" is requested with the
          initial values from <xref target="message-types"/>. Values
          0x00 and 0x0E-0xFF are reserved for future use. New
          assignments require Specification Required policy.
        </t>
      </section>
    </section>

  </middle>

  <back>
    <references>
      <name>References</name>

      <references>
        <name>Normative References</name>

        <reference anchor="RFC2119" target="https://www.rfc-editor.org/info/rfc2119">
          <front>
            <title>Key words for use in RFCs to Indicate Requirement Levels</title>
            <author fullname="S. Bradner" initials="S." surname="Bradner"/>
            <date month="March" year="1997"/>
          </front>
          <seriesInfo name="BCP" value="14"/>
          <seriesInfo name="RFC" value="2119"/>
          <seriesInfo name="DOI" value="10.17487/RFC2119"/>
        </reference>

        <reference anchor="RFC8174" target="https://www.rfc-editor.org/info/rfc8174">
          <front>
            <title>Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words</title>
            <author fullname="B. Leiba" initials="B." surname="Leiba"/>
            <date month="May" year="2017"/>
          </front>
          <seriesInfo name="BCP" value="14"/>
          <seriesInfo name="RFC" value="8174"/>
          <seriesInfo name="DOI" value="10.17487/RFC8174"/>
        </reference>

        <reference anchor="RFC8446" target="https://www.rfc-editor.org/info/rfc8446">
          <front>
            <title>The Transport Layer Security (TLS) Protocol Version 1.3</title>
            <author fullname="E. Rescorla" initials="E." surname="Rescorla"/>
            <date month="August" year="2018"/>
          </front>
          <seriesInfo name="RFC" value="8446"/>
          <seriesInfo name="DOI" value="10.17487/RFC8446"/>
        </reference>

        <reference anchor="RFC9293" target="https://www.rfc-editor.org/info/rfc9293">
          <front>
            <title>Transmission Control Protocol (TCP)</title>
            <author fullname="W. Eddy" initials="W." surname="Eddy"/>
            <date month="August" year="2022"/>
          </front>
          <seriesInfo name="STD" value="7"/>
          <seriesInfo name="RFC" value="9293"/>
          <seriesInfo name="DOI" value="10.17487/RFC9293"/>
        </reference>

        <reference anchor="RFC8018" target="https://www.rfc-editor.org/info/rfc8018">
          <front>
            <title>PKCS #5: Password-Based Cryptography Specification Version 2.1</title>
            <author fullname="K. Moriarty" initials="K." surname="Moriarty"/>
            <author fullname="B. Kaliski" initials="B." surname="Kaliski"/>
            <author fullname="A. Rusch" initials="A." surname="Rusch"/>
            <date month="January" year="2017"/>
          </front>
          <seriesInfo name="RFC" value="8018"/>
          <seriesInfo name="DOI" value="10.17487/RFC8018"/>
        </reference>

        <reference anchor="RFC6762" target="https://www.rfc-editor.org/info/rfc6762">
          <front>
            <title>Multicast DNS</title>
            <author fullname="S. Cheshire" initials="S." surname="Cheshire"/>
            <author fullname="M. Krochmal" initials="M." surname="Krochmal"/>
            <date month="February" year="2013"/>
          </front>
          <seriesInfo name="RFC" value="6762"/>
          <seriesInfo name="DOI" value="10.17487/RFC6762"/>
        </reference>

        <reference anchor="RFC6763" target="https://www.rfc-editor.org/info/rfc6763">
          <front>
            <title>DNS-Based Service Discovery</title>
            <author fullname="S. Cheshire" initials="S." surname="Cheshire"/>
            <author fullname="M. Krochmal" initials="M." surname="Krochmal"/>
            <date month="February" year="2013"/>
          </front>
          <seriesInfo name="RFC" value="6763"/>
          <seriesInfo name="DOI" value="10.17487/RFC6763"/>
        </reference>

      </references>

      <references>
        <name>Informative References</name>

        <reference anchor="RFC7322" target="https://www.rfc-editor.org/info/rfc7322">
          <front>
            <title>RFC Style Guide</title>
            <author fullname="H. Flanagan" initials="H." surname="Flanagan"/>
            <author fullname="S. Ginoza" initials="S." surname="Ginoza"/>
            <date month="September" year="2014"/>
          </front>
          <seriesInfo name="RFC" value="7322"/>
          <seriesInfo name="DOI" value="10.17487/RFC7322"/>
        </reference>

        <reference anchor="RFC7748" target="https://www.rfc-editor.org/info/rfc7748">
          <front>
            <title>Elliptic Curves for Security</title>
            <author fullname="A. Langley" initials="A." surname="Langley"/>
            <author fullname="M. Hamburg" initials="M." surname="Hamburg"/>
            <author fullname="S. Turner" initials="S." surname="Turner"/>
            <date month="January" year="2016"/>
          </front>
          <seriesInfo name="RFC" value="7748"/>
          <seriesInfo name="DOI" value="10.17487/RFC7748"/>
        </reference>

        <reference anchor="RFC5869" target="https://www.rfc-editor.org/info/rfc5869">
          <front>
            <title>HMAC-based Extract-and-Expand Key Derivation Function (HKDF)</title>
            <author fullname="H. Krawczyk" initials="H." surname="Krawczyk"/>
            <author fullname="P. Eronen" initials="P." surname="Eronen"/>
            <date month="May" year="2010"/>
          </front>
          <seriesInfo name="RFC" value="5869"/>
          <seriesInfo name="DOI" value="10.17487/RFC5869"/>
        </reference>

      </references>
    </references>

    <!-- ============================================================ -->
    <section anchor="constants">
      <name>Protocol Constants</name>
      <artwork><![CDATA[
MAGIC           = 0x4E42
VERSION         = 0x01
HEADER_SIZE     = 36 bytes
MAX_PAYLOAD     = 1,048,576 bytes (1 MiB)
MAX_DEVICE_NAME = 255 bytes
DEFAULT_PORT    = 31337
PING_INTERVAL   = 30 seconds
PONG_TIMEOUT    = 10 seconds
HELLO_TIMEOUT   = 5 seconds
BYE_TIMEOUT     = 5 seconds
MAX_CLIENTS     = 50
PSK_ITERATIONS  = 100,000
PSK_SALT        = "NCBP-v1-salt"
PSK_DKLEN       = 32 bytes
]]></artwork>
    </section>

    <!-- ============================================================ -->
    <section anchor="content-types">
      <name>Content Type Registry</name>
      <table>
        <thead>
          <tr><th>Value</th><th>MIME Type</th><th>Status</th></tr>
        </thead>
        <tbody>
          <tr><td>0x01</td><td>text/plain</td><td>Required</td></tr>
          <tr><td>0x02</td><td>image/png</td><td>Reserved</td></tr>
          <tr><td>0x03</td><td>image/jpeg</td><td>Reserved</td></tr>
          <tr><td>0x04</td><td>text/html</td><td>Reserved</td></tr>
          <tr><td>0x05</td><td>text/uri-list</td><td>Reserved</td></tr>
          <tr><td>0x06-0xFE</td><td>-</td><td>Reserved</td></tr>
          <tr><td>0xFF</td><td>-</td><td>Custom/Extension</td></tr>
        </tbody>
      </table>
    </section>

    <!-- ============================================================ -->
    <section anchor="example-exchange">
      <name>Example Message Exchange</name>
      <artwork type="ascii-art"><![CDATA[
Client A                    Server                     Client B
   |                          |                           |
   |--- [TLS Handshake] ---->|                           |
   |<-- [TLS Established] ---|                           |
   |                          |                           |
   |--- HELLO --------------->|                           |
   |<-- WELCOME --------------|                           |
   |                          |                           |
   |                          |<-- [TLS Handshake] ------|
   |                          |--- [TLS Established] --->|
   |                          |                           |
   |                          |<-- HELLO ----------------|
   |<-- PEER_JOIN ------------|--- WELCOME ------------->|
   |                          |                           |
   |--- CLIP ---------------->|                           |
   |                          |--- CLIP_NOTIFY --------->|
   |                          |                           |
   |                          |<-- CLIP -----------------|
   |<-- CLIP_NOTIFY ----------|                           |
   |                          |                           |
   |--- BYE ----------------->|                           |
   |                          |--- PEER_LEAVE ---------->|
   |                          |                           |
]]></artwork>
    </section>

    <section anchor="ack">
      <name>Acknowledgments</name>
      <t>
        The author wishes to thank the contributors to the NCBP
        reference implementation for their testing and feedback.
        This document follows the guidance in <xref target="RFC7322"/>.
      </t>
    </section>

  </back>
</rfc>
