MCPcopy Create free account
hub / github.com/PrismarineJS/node-minecraft-protocol / gotSharedSecret

Function gotSharedSecret

src/client/encrypt.js:15–75  ·  view source on GitHub ↗
(err, sharedSecret)

Source from the content-addressed store, hash-verified

13 crypto.randomBytes(16, gotSharedSecret)
14
15 function gotSharedSecret (err, sharedSecret) {
16 if (err) {
17 debug(err)
18 client.emit('error', err)
19 client.end('encryptionSecretError')
20 return
21 }
22 if (options.haveCredentials) {
23 joinServerRequest(onJoinServerResponse)
24 } else {
25 if (packet.serverId !== '-') {
26 debug('This server appears to be an online server and you are providing no password, the authentication will probably fail')
27 }
28 sendEncryptionKeyResponse()
29 }
30
31 function onJoinServerResponse (err) {
32 if (err) {
33 client.emit('error', err)
34 client.end('encryptionLoginError')
35 } else {
36 sendEncryptionKeyResponse()
37 }
38 }
39
40 function joinServerRequest (cb) {
41 yggdrasilServer.join(options.accessToken, client.session.selectedProfile.id,
42 packet.serverId, sharedSecret, packet.publicKey, cb)
43 }
44
45 function sendEncryptionKeyResponse () {
46 const mcData = require('minecraft-data')(client.version)
47
48 const pubKey = mcPubKeyToPem(packet.publicKey)
49 const encryptedSharedSecretBuffer = crypto.publicEncrypt({ key: pubKey, padding: crypto.constants.RSA_PKCS1_PADDING }, sharedSecret)
50 const encryptedVerifyTokenBuffer = crypto.publicEncrypt({ key: pubKey, padding: crypto.constants.RSA_PKCS1_PADDING }, packet.verifyToken)
51
52 if (mcData.supportFeature('signatureEncryption')) {
53 const salt = BigInt(Date.now())
54 client.write('encryption_begin', {
55 sharedSecret: encryptedSharedSecretBuffer,
56 hasVerifyToken: client.profileKeys == null,
57 crypto: client.profileKeys
58 ? {
59 salt,
60 messageSignature: crypto.sign('sha256WithRSAEncryption',
61 concat('buffer', packet.verifyToken, 'i64', salt), client.profileKeys.private)
62 }
63 : {
64 verifyToken: encryptedVerifyTokenBuffer
65 }
66 })
67 } else {
68 client.write('encryption_begin', {
69 sharedSecret: encryptedSharedSecretBuffer,
70 verifyToken: encryptedVerifyTokenBuffer
71 })
72 }

Callers

nothing calls this directly

Calls 3

joinServerRequestFunction · 0.85
endMethod · 0.80

Tested by

no test coverage detected