Messaging Patterns LabPython · ZeroMQ

Chapter 25

Encrypt a connection

Separate an encrypted exchange from permission to act.

What you’ll learn

Find what the client checks about the server and what the application would still need to check about the client.

Prerequisite: What is the wire contract?. Course map →

The situation

A team enables encrypted transport and assumes every connected client is now authorized to issue a payment.

ZeroMQ’s CURVE mechanism protects messages and uses key pairs, each with a public and a private part. The client configures the public key it expects from the server. ZeroMQ checks that key during the handshake. The lesson generates temporary keys inside the guest and displays no private keys.

CURVE protects the actual local exchange
  1. Client trustPin server public keyUse a fresh test key for each run.
  2. Real mechanismCURVE handshakelibzmq establishes the protected connection.
  3. ApplicationRequest → ACKZeroMQ protects the traffic. The handler reads the received message and sends ACK.

Conceptual diagram. The starter exercises encrypted transport. A deployment must separately decide which client keys it trusts and which requests those clients may make.

Before you run

Does a freshly generated client key grant a business permission?

Run the experiment

One client and one local server exchange a request and ACK over a CURVE-protected connection.

For the original example, click Restore code if it is shown, then click Run. Compare your output with the worked result.

Python prepares in the background while you read. First use can download about 71 MiB. Your code runs only when you select Run; each run starts with clean experiment state.

Your edits are saved in this browser when storage is available.

Troubleshooting

If Python stops responding, restart the environment and try again. Your code and last completed result are kept.

Downloads

Worked result

server_key_pinned reports that the client was configured to expect this server key. reply contains ACK. client_authorization describes the missing authorization policy.

Full output from the original example
{
  "curve_supported": true,
  "reply": "ACK",
  "server_key_pinned": true,
  "client_authorization": "No ZAP allowlist is configured; possession of a fresh client key does not establish application permission."
}

The build ran the original editor code to produce this output.

Read the implementation

Open Inspect the socket implementation. Find where the client sets curve_serverkey, then read client_authorization in the output. What does the pinned server key establish? What decides whether a client may perform an action?

Inspect the socket implementation

The editor calls this read-only helper. Read all socket helpers or read the separate-process code. The code download includes both files.

def curve():
    if not zmq.has('curve'):raise RuntimeError('This build lacks CURVE')
    public,private=zmq.curve_keypair();client_public,client_private=zmq.curve_keypair()
    with lab() as socket:
        server=socket(zmq.REP);server.curve_publickey=public;server.curve_secretkey=private;server.curve_server=True
        port=server.bind_to_random_port('tcp://127.0.0.1')
        client=socket(zmq.REQ);client.curve_publickey=client_public;client.curve_secretkey=client_private;client.curve_serverkey=public
        client.connect(f'tcp://127.0.0.1:{port}');client.send(b'private request');server.recv();server.send(b'ACK');reply=client.recv().decode()
        return {'curve_supported':True,'reply':reply,'server_key_pinned':True,'client_authorization':'No ZAP allowlist is configured; possession of a fresh client key does not establish application permission.'}
Costs, waiting and recovery
AspectWhat to account for
WorkStarting a protected connection and protecting the bytes cost time. Measure overhead on the network and workload you use.
Stored informationThe application needs to protect private keys and decide how to replace them.
Message pathThe protected connection carries messages between sockets inside the temporary Linux system.
WaitingZeroMQ checks the expected server key. The application still needs to check whether the request is allowed.
RecoveryPlan how to revoke a key and test what happens to pending requests when the key changes.

Check your understanding

What evidence would a production security review need beyond a successful encrypted round trip?

Show hints
  1. Hint 1

    How does the service decide which client keys to trust?

  2. Hint 2

    Include a request that the client should be denied.

Show explanation

Decide which client keys to trust and which actions each client may request. Test that unknown keys and forbidden actions are rejected. Plan how to replace or revoke keys and how to handle repeated requests.

ReferencesFunction documentation and helper source

References open in a new tab; course helpers are documented separately from library APIs. PyZMQ API links use version 27.3.0. ZeroMQ manuals are pinned to libzmq 4.3.5.

Experiment scope

The protected TCP connection stays inside the guest. This example trusts the configured server key and implements no client allowlist or permission check.

ZeroMQ reference · Runtime and model boundaries