Skip to content

Commit

Permalink
update v1 to connect to oracle 9
Browse files Browse the repository at this point in the history
  • Loading branch information
sijms committed Aug 2, 2022
1 parent ac4896e commit 705b3f5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
55 changes: 50 additions & 5 deletions auth_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,20 @@ func NewAuthObject(username string, password string, tcpNego *TCPNego, session *
padding := false
var err error
if ret.VerifierType == 0 {
ret.VerifierType = 2361
}
if ret.VerifierType == 2361 {
key, err = getKeyFromUserNameAndPassword(username, password)
if err != nil {
return nil, err
}
ret.ServerSessKey, err = decryptSessionKey2(key[:8], ret.EServerSessKey)
if err != nil {
return nil, err
}
ret.EPassword, err = encryptPassword2(password, ret.ServerSessKey)
if err != nil {
return nil, err
}
return ret, nil
} else if ret.VerifierType == 2361 {
key, err = getKeyFromUserNameAndPassword(username, password)
if err != nil {
return nil, err
Expand Down Expand Up @@ -160,7 +171,8 @@ func (obj *AuthObject) Write(connOption *network.ConnectionOption, mode LogonMod
session.PutUint(keyValSize, 4, true, true)
session.PutBytes(1, 1)
if len(connOption.UserID) > 0 {
session.PutBytes([]byte(connOption.UserID)...)
//session.PutBytes([]byte(connOption.UserID)...)
session.PutClr([]byte(connOption.UserID))
}
index := 0
if len(obj.EClientSessKey) > 0 {
Expand Down Expand Up @@ -330,6 +342,21 @@ func decryptSessionKey(padding bool, encKey []byte, sessionKey string) ([]byte,
return output[:len(output)-cutLen], nil
}

func decryptSessionKey2(encKey []byte, sessionKey string) ([]byte, error) {
result, err := hex.DecodeString(sessionKey)
if err != nil {
return nil, err
}
blk, err := des.NewCipher(encKey)
if err != nil {
return nil, err
}
enc := cipher.NewCBCDecrypter(blk, make([]byte, 8))
output := make([]byte, len(result))
enc.CryptBlocks(output, result)
return output, nil
}

func EncryptSessionKey(padding bool, encKey []byte, sessionKey []byte) (string, error) {
blk, err := aes.NewCipher(encKey)
if err != nil {
Expand All @@ -347,13 +374,31 @@ func EncryptSessionKey(padding bool, encKey []byte, sessionKey []byte) (string,
func EncryptPassword(password string, key []byte) (string, error) {
buff1 := make([]byte, 0x10)
_, err := rand.Read(buff1)
//buff_1 = []byte{109, 250, 127, 252, 157, 165, 29, 6, 165, 174, 50, 93, 165, 202, 192, 100}
if err != nil {
return "", nil
}
buffer := append(buff1, []byte(password)...)
return EncryptSessionKey(true, key, buffer)
}
func encryptPassword2(password string, key []byte) (string, error) {
padding := 0
temp := []byte(password)
if len(password)%8 > 0 {
padding = 8 - (len(password) % 8)
temp = append(temp, bytes.Repeat([]byte{0}, padding)...)
}
blk, err := des.NewCipher(key)
if err != nil {
return "", err
}
enc := cipher.NewCBCEncrypter(blk, make([]byte, 8))
output := make([]byte, len(temp))
enc.CryptBlocks(output, temp)
encPassword := hex.EncodeToString(output)
encPassword += strconv.Itoa(padding)
// [36, -90, -28, -115, -91, 95, -80, -2]
return strings.ToUpper(encPassword), nil
}

func CalculateKeysHash(verifierType int, key1 []byte, key2 []byte) ([]byte, error) {
hash := md5.New()
Expand Down
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (stmt *defaultStmt) basicWrite(exeOp int, parse, define bool) error {
session.PutBytes(0, 0, 0, 0, 0)
}
if parse {
session.PutBytes(stmt.connection.strConv.Encode(stmt.text)...)
session.PutClr(stmt.connection.strConv.Encode(stmt.text))
}
if define {
session.PutBytes(0)
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func (conn *Connection) doAuth() error {
return err
}

conn.authObject, err = NewAuthObject(conn.connOption.UserID, conn.connOption.Password, conn.tcpNego, conn.session)
conn.authObject, err = NewAuthObject(conn.connOption.UserID, conn.conStr.password, conn.tcpNego, conn.session)
if err != nil {
return err
}
Expand Down
17 changes: 9 additions & 8 deletions db_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ func GetDBVersion(session *network.Session) (*DBVersion, error) {
if msg != 8 {
return nil, errors.New(fmt.Sprintf("message code error: received code %d and expected code is 8", msg))
}
length, err := session.GetInt(2, true, true)
if err != nil {
return nil, err
}
info, err := session.GetBytes(int(length))
if err != nil {
return nil, err
}
info, err := session.GetDlc()
//length, err := session.GetInt(2, true, true)
//if err != nil {
// return nil, err
//}
//info, err := session.GetBytes(int(length))
//if err != nil {
// return nil, err
//}
number, err := session.GetInt(4, true, true)
if err != nil {
return nil, err
Expand Down

0 comments on commit 705b3f5

Please sign in to comment.