core.rst (12976B)
1 .. _CORE-Subsystem-Dev: 2 3 .. index:: 4 double: CORE; subsystem 5 6 CORE 7 ==== 8 9 The CORE API (defined in ``gnunet_core_service.h``) is the basic 10 messaging API used by P2P applications built using GNUnet. It provides 11 applications the ability to send and receive encrypted messages to the 12 peer's \"directly\" connected neighbours. 13 14 As CORE connections are generally \"direct\" connections, applications 15 must not assume that they can connect to arbitrary peers this way, as 16 \"direct\" connections may not always be possible. Applications using 17 CORE are notified about which peers are connected. Creating new 18 \"direct\" connections must be done using the TRANSPORT API. 19 20 The CORE API provides unreliable, out-of-order delivery. While the 21 implementation tries to ensure timely, in-order delivery, both message 22 losses and reordering are not detected and must be tolerated by the 23 application. Most important, the core will NOT perform retransmission if 24 messages could not be delivered. 25 26 Note that CORE allows applications to queue one message per connected 27 peer. The rate at which each connection operates is influenced by the 28 preferences expressed by local application as well as restrictions 29 imposed by the other peer. Local applications can express their 30 preferences for particular connections using the \"performance\" API of 31 the ATS service. 32 33 Applications that require more sophisticated transmission capabilities 34 such as TCP-like behavior, or if you intend to send messages to 35 arbitrary remote peers, should use the CADET API. 36 37 The typical use of the CORE API is to connect to the CORE service using 38 ``GNUNET_CORE_connect``, process events from the CORE service (such as 39 peers connecting, peers disconnecting and incoming messages) and send 40 messages to connected peers using ``GNUNET_CORE_notify_transmit_ready``. 41 Note that applications must cancel pending transmission requests if they 42 receive a disconnect event for a peer that had a transmission pending; 43 furthermore, queuing more than one transmission request per peer per 44 application using the service is not permitted. 45 46 The CORE API also allows applications to monitor all communications of 47 the peer prior to encryption (for outgoing messages) or after decryption 48 (for incoming messages). This can be useful for debugging, diagnostics 49 or to establish the presence of cover traffic (for anonymity). As 50 monitoring applications are often not interested in the payload, the 51 monitoring callbacks can be configured to only provide the message 52 headers (including the message type and size) instead of copying the 53 full data stream to the monitoring client. 54 55 The init callback of the ``GNUNET_CORE_connect`` function is called with 56 the hash of the public key of the peer. This public key is used to 57 identify the peer globally in the GNUnet network. Applications are 58 encouraged to check that the provided hash matches the hash that they 59 are using (as theoretically the application may be using a different 60 configuration file with a different private key, which would result in 61 hard to find bugs). 62 63 As with most service APIs, the CORE API isolates applications from 64 crashes of the CORE service. If the CORE service crashes, the 65 application will see disconnect events for all existing connections. 66 Once the connections are re-established, the applications will be 67 receive matching connect events. 68 69 core client-service protocol 70 .. _The-CORE-Client_002dService-Protocol: 71 72 The CORE Client-Service Protocol 73 -------------------------------- 74 75 This section describes the protocol between an application using the 76 CORE service (the client) and the CORE service process itself. 77 78 .. _Setup2: 79 80 Setup2 81 ^^^^^^ 82 83 When a client connects to the CORE service, it first sends a 84 ``InitMessage`` which specifies options for the connection and a set of 85 message type values which are supported by the application. The options 86 bitmask specifies which events the client would like to be notified 87 about. The options include: 88 89 **GNUNET_CORE_OPTION_NOTHING** 90 No notifications 91 92 **GNUNET_CORE_OPTION_STATUS_CHANGE** 93 Peers connecting and disconnecting 94 95 **GNUNET_CORE_OPTION_FULL_INBOUND** 96 All inbound messages (after decryption) with full payload 97 98 **GNUNET_CORE_OPTION_HDR_INBOUND** 99 Just the ``MessageHeader`` of all inbound messages 100 101 **GNUNET_CORE_OPTION_FULL_OUTBOUND** 102 All outbound messages (prior to encryption) with full payload 103 104 **GNUNET_CORE_OPTION_HDR_OUTBOUND** 105 Just the ``MessageHeader`` of all outbound messages 106 107 Typical applications will only monitor for connection status changes. 108 109 The CORE service responds to the ``InitMessage`` with an 110 ``InitReplyMessage`` which contains the peer's identity. Afterwards, 111 both CORE and the client can send messages. 112 113 .. _Notifications: 114 115 Notifications 116 ^^^^^^^^^^^^^ 117 118 The CORE will send ``ConnectNotifyMessage``\ s and 119 ``DisconnectNotifyMessage``\ s whenever peers connect or disconnect from 120 the CORE (assuming their type maps overlap with the message types 121 registered by the client). When the CORE receives a message that matches 122 the set of message types specified during the ``InitMessage`` (or if 123 monitoring is enabled in for inbound messages in the options), it sends 124 a ``NotifyTrafficMessage`` with the peer identity of the sender and the 125 decrypted payload. The same message format (except with 126 ``GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND`` for the message type) is 127 used to notify clients monitoring outbound messages; here, the peer 128 identity given is that of the receiver. 129 130 .. _Sending: 131 132 Sending 133 ^^^^^^^ 134 135 When a client wants to transmit a message, it first requests a 136 transmission slot by sending a ``SendMessageRequest`` which specifies 137 the priority, deadline and size of the message. Note that these values 138 may be ignored by CORE. When CORE is ready for the message, it answers 139 with a ``SendMessageReady`` response. The client can then transmit the 140 payload with a ``SendMessage`` message. Note that the actual message 141 size in the ``SendMessage`` is allowed to be smaller than the size in 142 the original request. A client may at any time send a fresh 143 ``SendMessageRequest``, which then superceeds the previous 144 ``SendMessageRequest``, which is then no longer valid. The client can 145 tell which ``SendMessageRequest`` the CORE service's 146 ``SendMessageReady`` message is for as all of these messages contain a 147 \"unique\" request ID (based on a counter incremented by the client for 148 each request). 149 150 CORE Peer-to-Peer Protocol 151 .. _The-CORE-Peer_002dto_002dPeer-Protocol: 152 153 The CORE Peer-to-Peer Protocol 154 ------------------------------ 155 156 EphemeralKeyMessage creation 157 .. _Creating-the-EphemeralKeyMessage: 158 159 Creating the EphemeralKeyMessage 160 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 161 162 When the CORE service starts, each peer creates a fresh ephemeral (ECC) 163 public-private key pair and signs the corresponding 164 ``EphemeralKeyMessage`` with its long-term key (which we usually call 165 the peer's identity; the hash of the public long term key is what 166 results in a ``struct GNUNET_PeerIdentity`` in all GNUnet APIs. The 167 ephemeral key is ONLY used for an ECDHE (`Elliptic-curve 168 Diffie---Hellman <http://en.wikipedia.org/wiki/Elliptic_curve_Diffie%E2%80%93Hellman>`__) 169 exchange by the CORE service to establish symmetric session keys. A peer 170 will use the same ``EphemeralKeyMessage`` for all peers for 171 ``REKEY_FREQUENCY``, which is usually 12 hours. After that time, it will 172 create a fresh ephemeral key (forgetting the old one) and broadcast the 173 new ``EphemeralKeyMessage`` to all connected peers, resulting in fresh 174 symmetric session keys. Note that peers independently decide on when to 175 discard ephemeral keys; it is not a protocol violation to discard keys 176 more often. Ephemeral keys are also never stored to disk; restarting a 177 peer will thus always create a fresh ephemeral key. The use of ephemeral 178 keys is what provides `forward 179 secrecy <http://en.wikipedia.org/wiki/Forward_secrecy>`__. 180 181 Just before transmission, the ``EphemeralKeyMessage`` is patched to 182 reflect the current sender_status, which specifies the current state of 183 the connection from the point of view of the sender. The possible values 184 are: 185 186 - ``KX_STATE_DOWN`` Initial value, never used on the network 187 188 - ``KX_STATE_KEY_SENT`` We sent our ephemeral key, do not know the key 189 of the other peer 190 191 - ``KX_STATE_KEY_RECEIVED`` This peer has received a valid ephemeral 192 key of the other peer, but we are waiting for the other peer to 193 confirm it's authenticity (ability to decode) via challenge-response. 194 195 - ``KX_STATE_UP`` The connection is fully up from the point of view of 196 the sender (now performing keep-alive) 197 198 - ``KX_STATE_REKEY_SENT`` The sender has initiated a rekeying 199 operation; the other peer has so far failed to confirm a working 200 connection using the new ephemeral key 201 202 .. _Establishing-a-connection: 203 204 Establishing a connection 205 ^^^^^^^^^^^^^^^^^^^^^^^^^ 206 207 Peers begin their interaction by sending a ``EphemeralKeyMessage`` to 208 the other peer once the TRANSPORT service notifies the CORE service 209 about the connection. A peer receiving an ``EphemeralKeyMessage`` with a 210 status indicating that the sender does not have the receiver's ephemeral 211 key, the receiver's ``EphemeralKeyMessage`` is sent in response. 212 Additionally, if the receiver has not yet confirmed the authenticity of 213 the sender, it also sends an (encrypted)\ ``PingMessage`` with a 214 challenge (and the identity of the target) to the other peer. Peers 215 receiving a ``PingMessage`` respond with an (encrypted) ``PongMessage`` 216 which includes the challenge. Peers receiving a ``PongMessage`` check 217 the challenge, and if it matches set the connection to ``KX_STATE_UP``. 218 219 .. _Encryption-and-Decryption: 220 221 Encryption and Decryption 222 ^^^^^^^^^^^^^^^^^^^^^^^^^ 223 224 All functions related to the key exchange and encryption/decryption of 225 messages can be found in ``gnunet-service-core_kx.c`` (except for the 226 cryptographic primitives, which are in ``util/crypto*.c``). Given the 227 key material from ECDHE, a Key derivation function (`Key derivation 228 function <https://en.wikipedia.org/wiki/Key_derivation_function>`__) is 229 used to derive two pairs of encryption and decryption keys for AES-256 230 and TwoFish, as well as initialization vectors and authentication keys 231 (for HMAC (`HMAC <https://en.wikipedia.org/wiki/HMAC>`__)). The HMAC is 232 computed over the encrypted payload. Encrypted messages include an 233 iv_seed and the HMAC in the header. 234 235 Each encrypted message in the CORE service includes a sequence number 236 and a timestamp in the encrypted payload. The CORE service remembers the 237 largest observed sequence number and a bit-mask which represents which 238 of the previous 32 sequence numbers were already used. Messages with 239 sequence numbers lower than the largest observed sequence number minus 240 32 are discarded. Messages with a timestamp that is less than 241 ``REKEY_TOLERANCE`` off (5 minutes) are also discarded. This of course 242 means that system clocks need to be reasonably synchronized for peers to 243 be able to communicate. Additionally, as the ephemeral key changes every 244 12 hours, a peer would not even be able to decrypt messages older than 245 12 hours. 246 247 .. _Type-maps: 248 249 Type maps 250 ^^^^^^^^^ 251 252 Once an encrypted connection has been established, peers begin to 253 exchange type maps. Type maps are used to allow the CORE service to 254 determine which (encrypted) connections should be shown to which 255 applications. A type map is an array of 65536 bits representing the 256 different types of messages understood by applications using the CORE 257 service. Each CORE service maintains this map, simply by setting the 258 respective bit for each message type supported by any of the 259 applications using the CORE service. Note that bits for message types 260 embedded in higher-level protocols (such as MESH) will not be included 261 in these type maps. 262 263 Typically, the type map of a peer will be sparse. Thus, the CORE service 264 attempts to compress its type map using ``gzip``-style compression 265 (\"deflate\") prior to transmission. However, if the compression fails 266 to compact the map, the map may also be transmitted without compression 267 (resulting in ``GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP`` or 268 ``GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP`` messages respectively). 269 Upon receiving a type map, the respective CORE service notifies 270 applications about the connection to the other peer if they support any 271 message type indicated in the type map (or no message type at all). If 272 the CORE service experience a connect or disconnect event from an 273 application, it updates its type map (setting or unsetting the 274 respective bits) and notifies its neighbours about the change. The CORE 275 services of the neighbours then in turn generate connect and disconnect 276 events for the peer that sent the type map for their respective 277 applications. As CORE messages may be lost, the CORE service confirms 278 receiving a type map by sending back a 279 ``GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP``. If such a confirmation 280 (with the correct hash of the type map) is not received, the sender will 281 retransmit the type map (with exponential back-off). 282 283