pac_type.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package pac
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. "gopkg.in/jcmturner/gokrb5.v2/crypto"
  7. "gopkg.in/jcmturner/gokrb5.v2/iana/keyusage"
  8. "gopkg.in/jcmturner/gokrb5.v2/ndr"
  9. "gopkg.in/jcmturner/gokrb5.v2/types"
  10. )
  11. // PACType implements: https://msdn.microsoft.com/en-us/library/cc237950.aspx
  12. type PACType struct {
  13. CBuffers uint32
  14. Version uint32
  15. Buffers []InfoBuffer
  16. Data []byte
  17. KerbValidationInfo *KerbValidationInfo
  18. CredentialsInfo *CredentialsInfo
  19. ServerChecksum *SignatureData
  20. KDCChecksum *SignatureData
  21. ClientInfo *ClientInfo
  22. S4UDelegationInfo *S4UDelegationInfo
  23. UPNDNSInfo *UPNDNSInfo
  24. ClientClaimsInfo *ClientClaimsInfo
  25. DeviceInfo *DeviceInfo
  26. DeviceClaimsInfo *DeviceClaimsInfo
  27. ZeroSigData []byte
  28. }
  29. // Unmarshal bytes into the PACType struct
  30. func (pac *PACType) Unmarshal(b []byte) error {
  31. var p int
  32. var e binary.ByteOrder = binary.LittleEndian
  33. pac.Data = b
  34. zb := make([]byte, len(b), len(b))
  35. copy(zb, b)
  36. pac.ZeroSigData = zb
  37. pac.CBuffers = ndr.ReadUint32(&b, &p, &e)
  38. pac.Version = ndr.ReadUint32(&b, &p, &e)
  39. buf := make([]InfoBuffer, pac.CBuffers, pac.CBuffers)
  40. for i := range buf {
  41. buf[i] = ReadPACInfoBuffer(&b, &p, &e)
  42. }
  43. pac.Buffers = buf
  44. return nil
  45. }
  46. // ProcessPACInfoBuffers processes the PAC Info Buffers.
  47. // https://msdn.microsoft.com/en-us/library/cc237954.aspx
  48. func (pac *PACType) ProcessPACInfoBuffers(key types.EncryptionKey) error {
  49. for _, buf := range pac.Buffers {
  50. p := make([]byte, buf.CBBufferSize, buf.CBBufferSize)
  51. copy(p, pac.Data[int(buf.Offset):int(buf.Offset)+int(buf.CBBufferSize)])
  52. switch int(buf.ULType) {
  53. case ulTypeKerbValidationInfo:
  54. if pac.KerbValidationInfo != nil {
  55. //Must ignore subsequent buffers of this type
  56. continue
  57. }
  58. var k KerbValidationInfo
  59. err := k.Unmarshal(p)
  60. if err != nil {
  61. return fmt.Errorf("Error processing KerbValidationInfo: %v", err)
  62. }
  63. pac.KerbValidationInfo = &k
  64. case ulTypeCredentials:
  65. if pac.CredentialsInfo != nil {
  66. //Must ignore subsequent buffers of this type
  67. continue
  68. }
  69. var k CredentialsInfo
  70. err := k.Unmarshal(p, key)
  71. if err != nil {
  72. return fmt.Errorf("Error processing CredentialsInfo: %v", err)
  73. }
  74. pac.CredentialsInfo = &k
  75. case ulTypePACServerSignatureData:
  76. if pac.ServerChecksum != nil {
  77. //Must ignore subsequent buffers of this type
  78. continue
  79. }
  80. var k SignatureData
  81. zb, err := k.Unmarshal(p)
  82. copy(pac.ZeroSigData[int(buf.Offset):int(buf.Offset)+int(buf.CBBufferSize)], zb)
  83. if err != nil {
  84. return fmt.Errorf("Error processing ServerChecksum: %v", err)
  85. }
  86. pac.ServerChecksum = &k
  87. case ulTypePACKDCSignatureData:
  88. if pac.KDCChecksum != nil {
  89. //Must ignore subsequent buffers of this type
  90. continue
  91. }
  92. var k SignatureData
  93. zb, err := k.Unmarshal(p)
  94. copy(pac.ZeroSigData[int(buf.Offset):int(buf.Offset)+int(buf.CBBufferSize)], zb)
  95. if err != nil {
  96. return fmt.Errorf("Error processing KDCChecksum: %v", err)
  97. }
  98. pac.KDCChecksum = &k
  99. case ulTypePACClientInfo:
  100. if pac.ClientInfo != nil {
  101. //Must ignore subsequent buffers of this type
  102. continue
  103. }
  104. var k ClientInfo
  105. err := k.Unmarshal(p)
  106. if err != nil {
  107. return fmt.Errorf("Error processing ClientInfo: %v", err)
  108. }
  109. pac.ClientInfo = &k
  110. case ulTypeS4UDelegationInfo:
  111. if pac.S4UDelegationInfo != nil {
  112. //Must ignore subsequent buffers of this type
  113. continue
  114. }
  115. var k S4UDelegationInfo
  116. err := k.Unmarshal(p)
  117. if err != nil {
  118. return fmt.Errorf("Error processing S4U_DelegationInfo: %v", err)
  119. }
  120. pac.S4UDelegationInfo = &k
  121. case ulTypeUPNDNSInfo:
  122. if pac.UPNDNSInfo != nil {
  123. //Must ignore subsequent buffers of this type
  124. continue
  125. }
  126. var k UPNDNSInfo
  127. err := k.Unmarshal(p)
  128. if err != nil {
  129. return fmt.Errorf("Error processing UPN_DNSInfo: %v", err)
  130. }
  131. pac.UPNDNSInfo = &k
  132. case ulTypePACClientClaimsInfo:
  133. if pac.ClientClaimsInfo != nil {
  134. //Must ignore subsequent buffers of this type
  135. continue
  136. }
  137. var k ClientClaimsInfo
  138. err := k.Unmarshal(p)
  139. if err != nil {
  140. return fmt.Errorf("Error processing ClientClaimsInfo: %v", err)
  141. }
  142. pac.ClientClaimsInfo = &k
  143. case ulTypePACDeviceInfo:
  144. if pac.DeviceInfo != nil {
  145. //Must ignore subsequent buffers of this type
  146. continue
  147. }
  148. var k DeviceInfo
  149. err := k.Unmarshal(p)
  150. if err != nil {
  151. return fmt.Errorf("Error processing DeviceInfo: %v", err)
  152. }
  153. pac.DeviceInfo = &k
  154. case ulTypePACDeviceClaimsInfo:
  155. if pac.DeviceClaimsInfo != nil {
  156. //Must ignore subsequent buffers of this type
  157. continue
  158. }
  159. var k DeviceClaimsInfo
  160. err := k.Unmarshal(p)
  161. if err != nil {
  162. return fmt.Errorf("Error processing DeviceClaimsInfo: %v", err)
  163. }
  164. pac.DeviceClaimsInfo = &k
  165. }
  166. }
  167. if ok, err := pac.validate(key); !ok {
  168. return err
  169. }
  170. return nil
  171. }
  172. func (pac *PACType) validate(key types.EncryptionKey) (bool, error) {
  173. if pac.KerbValidationInfo == nil {
  174. return false, errors.New("PAC Info Buffers does not contain a KerbValidationInfo")
  175. }
  176. if pac.ServerChecksum == nil {
  177. return false, errors.New("PAC Info Buffers does not contain a ServerChecksum")
  178. }
  179. if pac.KDCChecksum == nil {
  180. return false, errors.New("PAC Info Buffers does not contain a KDCChecksum")
  181. }
  182. if pac.ClientInfo == nil {
  183. return false, errors.New("PAC Info Buffers does not contain a ClientInfo")
  184. }
  185. etype, err := crypto.GetChksumEtype(int(pac.ServerChecksum.SignatureType))
  186. if err != nil {
  187. return false, err
  188. }
  189. if ok := etype.VerifyChecksum(key.KeyValue,
  190. pac.ZeroSigData,
  191. pac.ServerChecksum.Signature,
  192. keyusage.KERB_NON_KERB_CKSUM_SALT); !ok {
  193. return false, errors.New("PAC service checksum verification failed")
  194. }
  195. return true, nil
  196. }