2021-11-16 16:44:29 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-16 18:02:48 -05:00
|
|
|
"bytes"
|
2021-12-16 17:41:17 -05:00
|
|
|
"context"
|
2021-12-16 18:02:48 -05:00
|
|
|
"crypto/md5"
|
2021-12-16 17:41:17 -05:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base32"
|
2021-12-17 19:01:22 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-12-16 18:02:48 -05:00
|
|
|
"io"
|
2021-12-16 17:41:17 -05:00
|
|
|
|
2021-12-16 19:45:32 -05:00
|
|
|
"aim-oscar/models"
|
|
|
|
"aim-oscar/oscar"
|
2021-12-17 19:01:22 -05:00
|
|
|
"aim-oscar/util"
|
2021-12-16 19:45:32 -05:00
|
|
|
|
2021-12-16 17:41:17 -05:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/uptrace/bun"
|
2021-11-16 16:44:29 -05:00
|
|
|
)
|
|
|
|
|
2021-12-16 17:41:17 -05:00
|
|
|
const CIPHER_LENGTH = 64
|
2021-12-16 18:02:48 -05:00
|
|
|
const AIM_MD5_STRING = "AOL Instant Messenger (SM)"
|
2021-12-16 17:41:17 -05:00
|
|
|
|
2021-12-18 16:48:13 -05:00
|
|
|
type AuthorizationCookie struct {
|
2021-12-18 21:02:47 -05:00
|
|
|
UIN int64
|
2021-12-18 16:48:13 -05:00
|
|
|
X string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthorizationRegistrationService struct{}
|
|
|
|
|
|
|
|
func AuthenticateFLAPCookie(ctx context.Context, db *bun.DB, flap *oscar.FLAP) (*models.User, error) {
|
|
|
|
// Otherwise this is a protocol negotiation from the client. They're likely trying to connect
|
|
|
|
// and sending a cookie to verify who they are.
|
|
|
|
tlvs, err := oscar.UnmarshalTLVs(flap.Data.Bytes()[4:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "authentication request missing TLVs")
|
|
|
|
}
|
|
|
|
|
|
|
|
cookieTLV := oscar.FindTLV(tlvs, 0x6)
|
|
|
|
if cookieTLV == nil {
|
|
|
|
return nil, errors.New("authentication request missing Cookie TLV 0x6")
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := AuthorizationCookie{}
|
|
|
|
if err := json.Unmarshal(cookieTLV.Data, &auth); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not unmarshal cookie")
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := models.UserByUIN(ctx, db, auth.UIN)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get User by UIN")
|
|
|
|
}
|
|
|
|
|
|
|
|
h := md5.New()
|
|
|
|
io.WriteString(h, user.Cipher)
|
|
|
|
io.WriteString(h, user.Password)
|
|
|
|
io.WriteString(h, AIM_MD5_STRING)
|
|
|
|
expectedPasswordHash := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
|
|
|
|
// Make sure the hash passed in matches the one from the DB
|
|
|
|
if expectedPasswordHash != auth.X {
|
|
|
|
return nil, errors.New("unexpected cookie hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
2021-12-16 19:45:32 -05:00
|
|
|
}
|
2021-11-16 16:44:29 -05:00
|
|
|
|
2021-12-16 17:41:17 -05:00
|
|
|
func (a *AuthorizationRegistrationService) GenerateCipher() string {
|
|
|
|
randomBytes := make([]byte, 64)
|
|
|
|
_, err := rand.Read(randomBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return base32.StdEncoding.EncodeToString(randomBytes)[:CIPHER_LENGTH]
|
|
|
|
}
|
|
|
|
|
2021-12-19 01:14:25 -05:00
|
|
|
func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *bun.DB, snac *oscar.SNAC) (context.Context, error) {
|
2021-12-18 16:48:13 -05:00
|
|
|
session, err := oscar.SessionFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
util.PanicIfError(err)
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:55:43 -05:00
|
|
|
switch snac.Header.Subtype {
|
2021-11-16 16:44:29 -05:00
|
|
|
// Request MD5 Auth Key
|
2021-12-15 19:55:43 -05:00
|
|
|
case 0x06:
|
2021-12-16 19:45:32 -05:00
|
|
|
tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes())
|
2021-12-17 19:01:22 -05:00
|
|
|
util.PanicIfError(err)
|
2021-12-15 19:55:43 -05:00
|
|
|
|
2021-12-16 19:45:32 -05:00
|
|
|
usernameTLV := oscar.FindTLV(tlvs, 1)
|
2021-12-15 19:55:43 -05:00
|
|
|
if usernameTLV == nil {
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, errors.New("missing username TLV")
|
2021-12-15 19:55:43 -05:00
|
|
|
}
|
2021-11-16 16:44:29 -05:00
|
|
|
|
2021-12-16 17:41:17 -05:00
|
|
|
// Fetch the user
|
2021-12-17 19:01:22 -05:00
|
|
|
user, err := models.UserByUsername(ctx, db, string(usernameTLV.Data))
|
2021-12-16 17:41:17 -05:00
|
|
|
if err != nil {
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, err
|
2021-12-16 17:41:17 -05:00
|
|
|
}
|
|
|
|
if user == nil {
|
2021-12-16 19:45:32 -05:00
|
|
|
snac := oscar.NewSNAC(0x17, 0x03)
|
2021-12-16 17:41:17 -05:00
|
|
|
snac.Data.WriteBinary(usernameTLV)
|
2021-12-16 19:45:32 -05:00
|
|
|
snac.Data.WriteBinary(oscar.NewTLV(0x08, []byte{0, 4}))
|
|
|
|
resp := oscar.NewFLAP(2)
|
2021-12-16 17:41:17 -05:00
|
|
|
resp.Data.WriteBinary(snac)
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, session.Send(resp)
|
2021-12-16 17:41:17 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:55:43 -05:00
|
|
|
// Create cipher for this user
|
2021-12-16 17:41:17 -05:00
|
|
|
user.Cipher = a.GenerateCipher()
|
2021-12-17 19:01:22 -05:00
|
|
|
if err = user.Update(ctx, db); err != nil {
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, err
|
2021-12-16 17:41:17 -05:00
|
|
|
}
|
|
|
|
|
2021-12-16 19:45:32 -05:00
|
|
|
snac := oscar.NewSNAC(0x17, 0x07)
|
2021-12-16 17:41:17 -05:00
|
|
|
snac.Data.WriteUint16(uint16(len(user.Cipher)))
|
|
|
|
snac.Data.WriteString(user.Cipher)
|
2021-11-16 16:44:29 -05:00
|
|
|
|
2021-12-16 19:45:32 -05:00
|
|
|
resp := oscar.NewFLAP(2)
|
2021-12-16 17:41:17 -05:00
|
|
|
resp.Data.WriteBinary(snac)
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, session.Send(resp)
|
2021-12-16 17:41:17 -05:00
|
|
|
|
|
|
|
// Client Authorization Request
|
|
|
|
case 0x02:
|
2021-12-16 19:45:32 -05:00
|
|
|
tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes())
|
2021-12-17 19:01:22 -05:00
|
|
|
util.PanicIfError(err)
|
2021-12-15 19:55:43 -05:00
|
|
|
|
2021-12-16 19:45:32 -05:00
|
|
|
usernameTLV := oscar.FindTLV(tlvs, 1)
|
2021-12-16 17:41:17 -05:00
|
|
|
if usernameTLV == nil {
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, errors.New("missing username TLV 0x1")
|
2021-12-16 17:41:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
username := string(usernameTLV.Data)
|
|
|
|
ctx := context.Background()
|
2021-12-17 19:01:22 -05:00
|
|
|
user, err := models.UserByUsername(ctx, db, username)
|
2021-12-16 17:41:17 -05:00
|
|
|
if err != nil {
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, err
|
2021-12-16 17:41:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2021-12-16 19:45:32 -05:00
|
|
|
snac := oscar.NewSNAC(0x17, 0x03)
|
2021-12-16 17:41:17 -05:00
|
|
|
snac.Data.WriteBinary(usernameTLV)
|
2021-12-16 19:45:32 -05:00
|
|
|
snac.Data.WriteBinary(oscar.NewTLV(0x08, []byte{0, 4}))
|
|
|
|
resp := oscar.NewFLAP(2)
|
2021-12-16 17:41:17 -05:00
|
|
|
resp.Data.WriteBinary(snac)
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, session.Send(resp)
|
2021-12-16 17:41:17 -05:00
|
|
|
}
|
|
|
|
|
2021-12-16 19:45:32 -05:00
|
|
|
passwordHashTLV := oscar.FindTLV(tlvs, 0x25)
|
2021-12-16 18:02:48 -05:00
|
|
|
if passwordHashTLV == nil {
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, errors.New("missing password hash TLV 0x25")
|
2021-12-16 18:02:48 -05:00
|
|
|
}
|
|
|
|
|
2021-12-17 19:01:22 -05:00
|
|
|
// Compute password has that we expect the client to send back if the password was right
|
2021-12-16 18:02:48 -05:00
|
|
|
h := md5.New()
|
|
|
|
io.WriteString(h, user.Cipher)
|
|
|
|
io.WriteString(h, user.Password)
|
|
|
|
io.WriteString(h, AIM_MD5_STRING)
|
|
|
|
expectedPasswordHash := h.Sum(nil)
|
|
|
|
|
|
|
|
if !bytes.Equal(expectedPasswordHash, passwordHashTLV.Data) {
|
|
|
|
// Tell the client this was a bad password
|
2021-12-16 19:45:32 -05:00
|
|
|
badPasswordSnac := oscar.NewSNAC(0x17, 0x03)
|
2021-12-16 18:02:48 -05:00
|
|
|
badPasswordSnac.Data.WriteBinary(usernameTLV)
|
2021-12-16 19:45:32 -05:00
|
|
|
badPasswordSnac.Data.WriteBinary(oscar.NewTLV(0x08, []byte{0, 4}))
|
|
|
|
badPasswordFlap := oscar.NewFLAP(2)
|
2021-12-16 18:02:48 -05:00
|
|
|
badPasswordFlap.Data.WriteBinary(badPasswordSnac)
|
|
|
|
session.Send(badPasswordFlap)
|
|
|
|
|
2021-12-17 19:01:22 -05:00
|
|
|
// Tell them to leave
|
2021-12-16 19:45:32 -05:00
|
|
|
discoFlap := oscar.NewFLAP(4)
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, session.Send(discoFlap)
|
2021-12-16 18:02:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send BOS response + cookie
|
2021-12-16 19:45:32 -05:00
|
|
|
authSnac := oscar.NewSNAC(0x17, 0x3)
|
2021-12-16 18:02:48 -05:00
|
|
|
authSnac.Data.WriteBinary(usernameTLV)
|
2021-12-17 19:01:22 -05:00
|
|
|
authSnac.Data.WriteBinary(oscar.NewTLV(0x5, []byte(SRV_ADDRESS)))
|
|
|
|
|
2021-12-18 16:48:13 -05:00
|
|
|
cookie, err := json.Marshal(AuthorizationCookie{
|
2021-12-17 19:01:22 -05:00
|
|
|
UIN: user.UIN,
|
|
|
|
X: fmt.Sprintf("%x", expectedPasswordHash),
|
|
|
|
})
|
|
|
|
util.PanicIfError(err)
|
|
|
|
|
|
|
|
authSnac.Data.WriteBinary(oscar.NewTLV(0x6, cookie))
|
|
|
|
authSnac.Data.WriteBinary(oscar.NewTLV(0x11, []byte(user.Email)))
|
2021-12-16 19:45:32 -05:00
|
|
|
authFlap := oscar.NewFLAP(2)
|
2021-12-16 18:02:48 -05:00
|
|
|
authFlap.Data.WriteBinary(authSnac)
|
|
|
|
session.Send(authFlap)
|
|
|
|
|
2021-12-17 19:01:22 -05:00
|
|
|
// Tell them to leave
|
2021-12-16 19:45:32 -05:00
|
|
|
discoFlap := oscar.NewFLAP(4)
|
2021-12-18 16:48:13 -05:00
|
|
|
session.Send(discoFlap)
|
|
|
|
return ctx, session.Disconnect()
|
2021-11-16 16:44:29 -05:00
|
|
|
}
|
2021-12-15 19:55:43 -05:00
|
|
|
|
2021-12-18 16:48:13 -05:00
|
|
|
return ctx, nil
|
2021-11-16 16:44:29 -05:00
|
|
|
}
|