MCPcopy Create free account
hub / github.com/XTLS/Go / decryptTicket

Method decryptTicket

ticket.go:147–185  ·  view source on GitHub ↗
(encrypted []byte)

Source from the content-addressed store, hash-verified

145}
146
147func (c *Conn) decryptTicket(encrypted []byte) (plaintext []byte, usedOldKey bool) {
148 if len(encrypted) < ticketKeyNameLen+aes.BlockSize+sha256.Size {
149 return nil, false
150 }
151
152 keyName := encrypted[:ticketKeyNameLen]
153 iv := encrypted[ticketKeyNameLen : ticketKeyNameLen+aes.BlockSize]
154 macBytes := encrypted[len(encrypted)-sha256.Size:]
155 ciphertext := encrypted[ticketKeyNameLen+aes.BlockSize : len(encrypted)-sha256.Size]
156
157 keyIndex := -1
158 for i, candidateKey := range c.ticketKeys {
159 if bytes.Equal(keyName, candidateKey.keyName[:]) {
160 keyIndex = i
161 break
162 }
163 }
164 if keyIndex == -1 {
165 return nil, false
166 }
167 key := &c.ticketKeys[keyIndex]
168
169 mac := hmac.New(sha256.New, key.hmacKey[:])
170 mac.Write(encrypted[:len(encrypted)-sha256.Size])
171 expected := mac.Sum(nil)
172
173 if subtle.ConstantTimeCompare(macBytes, expected) != 1 {
174 return nil, false
175 }
176
177 block, err := aes.NewCipher(key.aesKey[:])
178 if err != nil {
179 return nil, false
180 }
181 plaintext = make([]byte, len(ciphertext))
182 cipher.NewCTR(block, iv).XORKeyStream(plaintext, ciphertext)
183
184 return plaintext, keyIndex > 0
185}

Callers 2

checkForResumptionMethod · 0.80
checkForResumptionMethod · 0.80

Calls 2

WriteMethod · 0.45
SumMethod · 0.45

Tested by

no test coverage detected