syncthing-device-ids.7 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. .\" Man page generated from reStructuredText.
  2. .
  3. .
  4. .nr rst2man-indent-level 0
  5. .
  6. .de1 rstReportMargin
  7. \\$1 \\n[an-margin]
  8. level \\n[rst2man-indent-level]
  9. level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
  10. -
  11. \\n[rst2man-indent0]
  12. \\n[rst2man-indent1]
  13. \\n[rst2man-indent2]
  14. ..
  15. .de1 INDENT
  16. .\" .rstReportMargin pre:
  17. . RS \\$1
  18. . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
  19. . nr rst2man-indent-level +1
  20. .\" .rstReportMargin post:
  21. ..
  22. .de UNINDENT
  23. . RE
  24. .\" indent \\n[an-margin]
  25. .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
  26. .nr rst2man-indent-level -1
  27. .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
  28. .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
  29. ..
  30. .TH "SYNCTHING-DEVICE-IDS" "7" "Nov 08, 2023" "v1.26.0" "Syncthing"
  31. .SH NAME
  32. syncthing-device-ids \- Understanding Device IDs
  33. .sp
  34. Every device is identified by a device ID. The device ID is used for address
  35. resolution, authentication and authorization. The term “device ID” could
  36. interchangeably have been “key ID” since the device ID is a direct property of
  37. the public key in use.
  38. .SH KEYS
  39. .sp
  40. To understand device IDs we need to look at the underlying mechanisms. At first
  41. startup, Syncthing will create a public/private keypair.
  42. .sp
  43. Currently this is a 384 bit ECDSA key (3072 bit RSA prior to v0.12.5,
  44. which is what is used as an example in this article). The keys are saved in
  45. the form of the private key (\fBkey.pem\fP) and a self signed certificate
  46. (\fBcert.pem\fP). The self signing part doesn’t actually add any security or
  47. functionality as far as Syncthing is concerned but it enables the use of the
  48. keys in a standard TLS exchange.
  49. .sp
  50. The typical certificate will look something like this, inspected with
  51. \fBopenssl x509\fP:
  52. .INDENT 0.0
  53. .INDENT 3.5
  54. .sp
  55. .nf
  56. .ft C
  57. Certificate:
  58. Data:
  59. Version: 3 (0x2)
  60. Serial Number: 0 (0x0)
  61. Signature Algorithm: sha1WithRSAEncryption
  62. Issuer: CN=syncthing
  63. Validity
  64. Not Before: Mar 30 21:10:52 2014 GMT
  65. Not After : Dec 31 23:59:59 2049 GMT
  66. Subject: CN=syncthing
  67. Subject Public Key Info:
  68. Public Key Algorithm: rsaEncryption
  69. RSA Public Key: (3072 bit)
  70. Modulus (3072 bit):
  71. 00:da:83:8a:c0:95:af:0a:42:af:43:74:65:29:f2:
  72. 30:e3:b9:12:d2:6b:70:93:da:0b:7b:8a:1e:e5:79:
  73. ...
  74. 99:09:4c:a9:7b:ba:4a:6a:8b:3b:e6:e7:c7:2c:00:
  75. 90:aa:bc:ad:94:e7:80:95:d2:1b
  76. Exponent: 65537 (0x10001)
  77. X509v3 extensions:
  78. X509v3 Key Usage: critical
  79. Digital Signature, Key Encipherment
  80. X509v3 Extended Key Usage:
  81. TLS Web Server Authentication, TLS Web Client Authentication
  82. X509v3 Basic Constraints: critical
  83. CA:FALSE
  84. Signature Algorithm: sha1WithRSAEncryption
  85. 68:72:43:8b:83:61:09:68:f0:ef:f0:43:b7:30:a6:73:1e:a8:
  86. d9:24:6c:2d:b4:bc:c9:e8:3e:0b:1e:3c:cc:7a:b2:c8:f1:1d:
  87. ...
  88. 88:7e:e2:61:aa:4c:02:e3:64:b0:da:70:3a:cd:1c:3d:86:db:
  89. df:54:b9:4e:be:1b
  90. .ft P
  91. .fi
  92. .UNINDENT
  93. .UNINDENT
  94. .sp
  95. We can see here that the certificate is little more than a container for the
  96. public key; the serial number is zero and the Issuer and Subject are both
  97. “syncthing” where a qualified name might otherwise be expected.
  98. .sp
  99. An advanced user could replace the \fBkey.pem\fP and \fBcert.pem\fP files with a
  100. keypair generated directly by the \fBopenssl\fP utility or other mechanism.
  101. .SH DEVICE IDS
  102. .sp
  103. To form a device ID the SHA\-256 hash of the certificate data in DER form is
  104. calculated. This means the hash covers all information under the
  105. \fBCertificate:\fP section above.
  106. .sp
  107. The hashing results in a 256 bit hash which we encode using base32. Base32
  108. encodes five bits per character so we need 256 / 5 = 51.2 characters to encode
  109. the device ID. This becomes 52 characters in practice, but 52 characters of
  110. base32 would decode to 260 bits which is not a whole number of bytes. The
  111. base32 encoding adds padding to 280 bits (the next multiple of both 5 and 8
  112. bits) so the resulting ID looks something like:
  113. .INDENT 0.0
  114. .INDENT 3.5
  115. .sp
  116. .nf
  117. .ft C
  118. MFZWI3DBONSGYYLTMRWGC43ENRQXGZDMMFZWI3DBONSGYYLTMRWA====
  119. .ft P
  120. .fi
  121. .UNINDENT
  122. .UNINDENT
  123. .sp
  124. The padding (\fB====\fP) is stripped away, the device ID split into four
  125. groups, and \fI\%check
  126. digits\fP <\fBhttps://forum.syncthing.net/t/v0-9-0-new-node-id-format/478\fP>
  127. are added for each group. For presentation purposes the device ID is
  128. grouped with dashes, resulting in the final value:
  129. .INDENT 0.0
  130. .INDENT 3.5
  131. .sp
  132. .nf
  133. .ft C
  134. MFZWI3D\-BONSGYC\-YLTMRWG\-C43ENR5\-QXGZDMM\-FZWI3DP\-BONSGYY\-LTMRWAD
  135. .ft P
  136. .fi
  137. .UNINDENT
  138. .UNINDENT
  139. .SS Connection Establishment
  140. .sp
  141. Now we know what device IDs are, here’s how they are used in Syncthing. When
  142. you add a device ID to the configuration, Syncthing will attempt to
  143. connect to that device. The first thing we need to do is figure out the IP and
  144. port to connect to. There are three possibilities here:
  145. .INDENT 0.0
  146. .IP \(bu 2
  147. The IP and port can be set statically in the configuration. The IP
  148. can equally well be a host name, so if you have a static IP or a
  149. dynamic DNS setup this might be a good option.
  150. .IP \(bu 2
  151. Using local discovery, if enabled. Every Syncthing instance on a LAN
  152. periodically broadcasts information about itself (device ID, address,
  153. port number). If we’ve seen one of these broadcasts for a given
  154. device ID that’s where we try to connect.
  155. .IP \(bu 2
  156. Using global discovery, if enabled. Every Syncthing instance
  157. announces itself to the global discovery service (device ID and
  158. external port number \- the internal address is not announced to the
  159. global server). If we don’t have a static address and haven’t seen
  160. any local announcements the global discovery server will be queried
  161. for an address.
  162. .UNINDENT
  163. .sp
  164. Once we have an address and port a TCP connection is established and a TLS
  165. handshake performed. As part of the handshake both devices present their
  166. certificates. Once the handshake has completed and the peer certificate is
  167. known, the following steps are performed:
  168. .INDENT 0.0
  169. .IP 1. 3
  170. Calculate the remote device ID by processing the received certificate as above.
  171. .IP 2. 3
  172. Weed out a few possible misconfigurations \- i.e. if the device ID is
  173. that of the local device or of a device we already have an active
  174. connection to. Drop the connection in these cases.
  175. .IP 3. 3
  176. Verify the remote device ID against the configuration. If it is not a
  177. device ID we are expecting to talk to, drop the connection.
  178. .IP 4. 3
  179. Verify the certificate \fBCommonName\fP against the configuration. By
  180. default, we expect it to be \fBsyncthing\fP, but when using custom
  181. certificates this can be changed.
  182. .IP 5. 3
  183. If everything checks out so far, accept the connection.
  184. .UNINDENT
  185. .SH AN ASIDE ABOUT COLLISIONS
  186. .sp
  187. The SHA\-256 hash is cryptographically collision resistant. This means
  188. that there is no way that we know of to create two different messages
  189. with the same hash.
  190. .sp
  191. You can argue that of course there are collisions \- there’s an infinite
  192. amount of inputs and a finite amount of outputs \- so by definition there
  193. are infinitely many messages that result in the same hash.
  194. .sp
  195. I’m going to quote \fI\%stack
  196. overflow\fP <\fBhttps://stackoverflow.com/questions/4014090/is-it-safe-to-ignore-the-possibility-of-sha-collisions-in-practice\fP>
  197. here:
  198. .INDENT 0.0
  199. .INDENT 3.5
  200. The usual answer goes thus: what is the probability that a rogue
  201. asteroid crashes on Earth within the next second, obliterating
  202. civilization\-as\-we\- know\-it, and killing off a few billion people ?
  203. It can be argued that any unlucky event with a probability lower
  204. than that is not actually very important.
  205. .sp
  206. If we have a “perfect” hash function with output size n, and we have
  207. p messages to hash (individual message length is not important),
  208. then probability of collision is about p2/2n+1 (this is an
  209. approximation which is valid for “small” p, i.e. substantially
  210. smaller than 2n/2). For instance, with SHA\-256 (n=256) and one
  211. billion messages (p=10^9) then the probability is about 4.3*10^\-60.
  212. .sp
  213. A mass\-murderer space rock happens about once every 30 million years
  214. on average. This leads to a probability of such an event occurring
  215. in the next second to about 10^\-15. That’s 45 orders of magnitude
  216. more probable than the SHA\-256 collision. Briefly stated, if you
  217. find SHA\-256 collisions scary then your priorities are wrong.
  218. .UNINDENT
  219. .UNINDENT
  220. .sp
  221. It’s also worth noting that the property of SHA\-256 that we are using is not
  222. simply collision resistance but resistance to a preimage attack, i.e. even if
  223. you can find two messages that result in a hash collision that doesn’t help you
  224. attack Syncthing (or TLS in general). You need to create a message that hashes
  225. to exactly the hash that my certificate already has or you won’t get in.
  226. .sp
  227. Note also that it’s not good enough to find a random blob of bits that happen to
  228. have the same hash as my certificate. You need to create a valid DER\-encoded,
  229. signed certificate that has the same hash as mine. The difficulty of this is
  230. staggeringly far beyond the already staggering difficulty of finding a SHA\-256
  231. collision.
  232. .SH PROBLEMS AND VULNERABILITIES
  233. .sp
  234. As far as I know, these are the issues or potential issues with the
  235. above mechanism.
  236. .SS Discovery Spoofing
  237. .sp
  238. Currently, the local discovery mechanism isn’t protected by crypto. This
  239. means that any device can in theory announce itself for any device ID and
  240. potentially receive connections for that device from the local network.
  241. .SS Long Device IDs are Painful
  242. .sp
  243. It’s a mouthful to read over the phone, annoying to type into an SMS or even
  244. into a computer. And it needs to be done twice, once for each side.
  245. .sp
  246. This isn’t a vulnerability as such, but a user experience problem. There are
  247. various possible solutions:
  248. .INDENT 0.0
  249. .IP \(bu 2
  250. Use shorter device IDs with verification based on the full ID (“You
  251. entered MFZWI3; I found and connected to a device with the ID
  252. MFZWI3\-DBONSG\-YYLTMR\-WGC43E\-NRQXGZ\-DMMFZW\-I3DBON\-SGYYLT\-MRWA, please
  253. confirm that this is correct”).
  254. .IP \(bu 2
  255. Use shorter device IDs with an out of band authentication, a la
  256. Bluetooth pairing. You enter a one time PIN into Syncthing and give
  257. that PIN plus a short device ID to another user. On initial connect,
  258. both sides verify that the other knows the correct PIN before
  259. accepting the connection.
  260. .UNINDENT
  261. .SH AUTHOR
  262. The Syncthing Authors
  263. .SH COPYRIGHT
  264. 2014-2019, The Syncthing Authors
  265. .\" Generated by docutils manpage writer.
  266. .