|
| 1 | +<?php |
| 2 | + |
| 3 | +// The Ironhouse Pattern |
| 4 | +// |
| 5 | +// Security doesn't get any stronger than this. An attacker is going to |
| 6 | +// have to break into your systems to see data before/after encryption. |
| 7 | + |
| 8 | +// Create context and start authentication engine |
| 9 | +$ctx = new ZMQContext(); |
| 10 | +$auth = new ZMQAuth($ctx); |
| 11 | +$auth->allow('127.0.0.1'); |
| 12 | + |
| 13 | +// Tell the authenticator how to handle CURVE requests |
| 14 | +$auth->configure(ZMQAuth::AUTH_TYPE_CURVE, '*', '.curve'); |
| 15 | + |
| 16 | +// We'll generate a new client certificate and save the public part |
| 17 | +// in the certificate store (in practice this would be done by hand |
| 18 | +// or some out-of-band process). |
| 19 | +$clientCert = new ZMQCert(); |
| 20 | +mkdir('.curve'); |
| 21 | +$clientCert->setMeta('name', 'Client test certificate'); |
| 22 | +$clientCert->savePublic('.curve/testcert.pub'); |
| 23 | + |
| 24 | +// Prepare the server certificate as we did in Stonehouse |
| 25 | +$serverCert = new ZMQCert(); |
| 26 | +$serverKey = $serverCert->getPublicTxt(); |
| 27 | + |
| 28 | +// Create and bind server socket |
| 29 | +$server = $ctx->getSocket(ZMQ::SOCKET_PUSH); |
| 30 | +$serverCert->apply($server); |
| 31 | +$server->setSockOpt(ZMQ::SOCKOPT_CURVE_SERVER, true); |
| 32 | +$server->bind('tcp://127.0.0.1:9000'); |
| 33 | + |
| 34 | +// Create and connect client socket |
| 35 | +$client = $ctx->getSocket(ZMQ::SOCKET_PULL); |
| 36 | +$clientCert->apply($client); |
| 37 | +$client->setSockOpt(ZMQ::SOCKOPT_CURVE_SERVERKEY, $serverKey); |
| 38 | +$client->connect('tcp://127.0.0.1:9000'); |
| 39 | + |
| 40 | +// Send a single message from server to client |
| 41 | +$server->send('Hello'); |
| 42 | +$message = $client->recv(); |
| 43 | +assert($message === 'Hello'); |
| 44 | +echo "Ironhouse test OK\n"; |
0 commit comments