| 1 |
|
| 2 |
|
| 3 |
package ntlm |
| 4 |
|
| 5 |
import ( |
| 6 |
rc4P "crypto/rc4" |
| 7 |
"encoding/binary" |
| 8 |
"encoding/hex" |
| 9 |
"fmt" |
| 10 |
) |
| 11 |
|
| 12 |
type NtlmsspMessageSignature struct { |
| 13 |
ByteData []byte |
| 14 |
|
| 15 |
Version []byte |
| 16 |
|
| 17 |
RandomPad []byte |
| 18 |
|
| 19 |
CheckSum []byte |
| 20 |
|
| 21 |
SeqNum []byte |
| 22 |
} |
| 23 |
|
| 24 |
func (n *NtlmsspMessageSignature) String() string { |
| 25 |
return fmt.Sprintf("NtlmsspMessageSignature: %s", hex.EncodeToString(n.Bytes())) |
| 26 |
} |
| 27 |
|
| 28 |
func (n *NtlmsspMessageSignature) Bytes() []byte { |
| 29 |
if n.ByteData != nil { |
| 30 |
return n.ByteData |
| 31 |
} else { |
| 32 |
return concat(n.Version, n.RandomPad, n.CheckSum, n.SeqNum) |
| 33 |
} |
| 34 |
return nil |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func seal(negFlags uint32, handle *rc4P.Cipher, signingKey []byte, seqNum uint32, message []byte) (sealedMessage []byte, sig *NtlmsspMessageSignature) { |
| 39 |
sealedMessage = rc4(handle, message) |
| 40 |
sig = mac(negFlags, handle, signingKey, uint32(seqNum), message) |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func sign(negFlags uint32, handle *rc4P.Cipher, signingKey []byte, seqNum uint32, message []byte) []byte { |
| 46 |
return concat(message, mac(negFlags, handle, signingKey, uint32(seqNum), message).Bytes()) |
| 47 |
} |
| 48 |
|
| 49 |
func mac(negFlags uint32, handle *rc4P.Cipher, signingKey []byte, seqNum uint32, message []byte) (result *NtlmsspMessageSignature) { |
| 50 |
if NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY.IsSet(negFlags) { |
| 51 |
result = macWithExtendedSessionSecurity(negFlags, handle, signingKey, seqNum, message) |
| 52 |
} else { |
| 53 |
result = macWithoutExtendedSessionSecurity(handle, seqNum, message) |
| 54 |
} |
| 55 |
return result |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func macWithoutExtendedSessionSecurity(handle *rc4P.Cipher, seqNum uint32, message []byte) *NtlmsspMessageSignature { |
| 73 |
sig := new(NtlmsspMessageSignature) |
| 74 |
|
| 75 |
seqNumBytes := make([]byte, 4) |
| 76 |
binary.LittleEndian.PutUint32(seqNumBytes, seqNum) |
| 77 |
|
| 78 |
sig.Version = []byte{0x01, 0x00, 0x00, 0x00} |
| 79 |
sig.CheckSum = make([]byte, 4) |
| 80 |
binary.LittleEndian.PutUint32(sig.CheckSum, crc32(message)) |
| 81 |
sig.RandomPad = rc4(handle, zeroBytes(4)) |
| 82 |
sig.CheckSum = rc4(handle, sig.CheckSum) |
| 83 |
sig.SeqNum = rc4(handle, zeroBytes(4)) |
| 84 |
for i := 0; i < 4; i++ { |
| 85 |
sig.SeqNum[i] = sig.SeqNum[i] ^ seqNumBytes[i] |
| 86 |
} |
| 87 |
sig.RandomPad = zeroBytes(4) |
| 88 |
return sig |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
func macWithExtendedSessionSecurity(negFlags uint32, handle *rc4P.Cipher, signingKey []byte, seqNum uint32, message []byte) *NtlmsspMessageSignature { |
| 102 |
sig := new(NtlmsspMessageSignature) |
| 103 |
sig.Version = []byte{0x01, 0x00, 0x00, 0x00} |
| 104 |
seqNumBytes := make([]byte, 4) |
| 105 |
binary.LittleEndian.PutUint32(seqNumBytes, seqNum) |
| 106 |
sig.CheckSum = hmacMd5(signingKey, concat(seqNumBytes, message))[0:8] |
| 107 |
if NTLMSSP_NEGOTIATE_KEY_EXCH.IsSet(negFlags) { |
| 108 |
sig.CheckSum = rc4(handle, sig.CheckSum) |
| 109 |
} |
| 110 |
sig.SeqNum = seqNumBytes |
| 111 |
return sig |
| 112 |
} |
| 113 |
|
| 114 |
func reinitSealingKey(key []byte, sequenceNumber int) (handle *rc4P.Cipher, err error) { |
| 115 |
seqNumBytes := make([]byte, 4) |
| 116 |
binary.LittleEndian.PutUint32(seqNumBytes, uint32(sequenceNumber)) |
| 117 |
newKey := md5(concat(key, seqNumBytes)) |
| 118 |
handle, err = rc4Init(newKey) |
| 119 |
return handle, err |
| 120 |
} |