Info

This is an exploratory post as part of my MIDI-VPN project.

Here I take a look at how MIDI sessions are established, with the aim of finding an acceptable method for storing them.

In RTP-MIDIs original RTC, there is no session standard - implementations are expected to use other session protocols (RFC 6295, p. 27, para. 6). Apple’s implementation (oddly named AppleMIDI instead of iMIDI) is both the most common implementation of RTP-MIDI and does have this. MIDI 2.0 has a clearly defined session initialisation handshake, which includes a second back and forth for authentication!

Here’s the established handshake process for both standards:

AppleMIDI’s Session Handshake

sequenceDiagram
    participant C as Client
    participant H as Host
    C->>H: Control port invitation (IN) · 16 bytes + N bytes (for session name)
    H-->>C: Control port acceptance (OK) · 16 bytes + N bytes (for session name)
    C->>H: MIDI port invitation (IN) · 16 bytes + N bytes (for session name)
    H-->>C: MIDI port acceptance (OK) · 16 bytes + N bytes (for session name)
    C->>H: Clock sync (CK 0) · 36 bytes
    H-->>C: Clock sync (CK 1) · 36 bytes
    C->>H: Clock sync (CK 2) · 36 bytes

MIDI 2.0

sequenceDiagram
    participant C as Client
    participant H as Host
    C->>H: Invitation (0x01) · 8 bytes + 8–144 bytes (for endpoint name and product instance ID)
    opt Host needs more time
        H-->>C: Pending (0x11) · 8 bytes + 8–144 bytes (for endpoint name and product instance ID)
    end
    opt Host requires authentication
        alt Shared secret
            H-->>C: Challenge (0x12) · 24 bytes + N bytes (for endpoint name and product instance ID)
            C->>H: Authentication response (0x02) · 40 bytes
        else Username and password
            H-->>C: Challenge (0x13) · 24 bytes + N bytes (for endpoint name and product instance ID)
            C->>H: Authentication response (0x03) · 40 bytes + U bytes (for username)
        end
    end
    H-->>C: Accepted (0x10) · 8 bytes + 8–144 bytes (for endpoint name and product instance ID)

It’s MIDI 2.0’s shared secret authentication we can use here. For AppleMIDI, authenticating would require pretending to establish a complete connection and then using SysEx7 traffic to establish the connection, and falling out if it fails, which is much more unusual than MIDI 2.0’s established connection protocol failing out.

Remember the constraint is to make the traffic look as MIDI like as possible. If it’s common for MIDI traffic to establish connections only to pass SysEx traffic then disconnect, this may prove fine. I’d expect a clearer failure to authenticate to pass the sniff test better, which is why I’m favouring MIDI 2.0.

Next Question: How will we authenticate the user? We’ve got the standard MIDI 2.0 session init method, using a nonce and secret hashed together through SHA-256. For an MVP, the simplest way will be using a pre-shared secret. For sharing that secret in future, could maybe establish it through an alternative method of communication, or even through a MIDI 2.0 connection that doesn’t use authentication, but that uncovers the SysEx issue I discussed in Theoretical Goodput. I discovered pretty quickly the issues around this - that VPN usage in countries that diligently watch internet traffic should be achieved first by installing and running it somewhere safe first.