syncthing-device-ids.7 11 KB

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