passwd.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Package kadmin provides Kerberos administration capabilities.
  2. package kadmin
  3. import (
  4. "github.com/jcmturner/gokrb5/v8/crypto"
  5. "github.com/jcmturner/gokrb5/v8/krberror"
  6. "github.com/jcmturner/gokrb5/v8/messages"
  7. "github.com/jcmturner/gokrb5/v8/types"
  8. )
  9. // ChangePasswdMsg generate a change password request and also return the key needed to decrypt the reply.
  10. func ChangePasswdMsg(cname types.PrincipalName, realm, password string, tkt messages.Ticket, sessionKey types.EncryptionKey) (r Request, k types.EncryptionKey, err error) {
  11. // Create change password data struct and marshal to bytes
  12. chgpasswd := ChangePasswdData{
  13. NewPasswd: []byte(password),
  14. TargName: cname,
  15. TargRealm: realm,
  16. }
  17. chpwdb, err := chgpasswd.Marshal()
  18. if err != nil {
  19. err = krberror.Errorf(err, krberror.KRBMsgError, "error marshaling change passwd data")
  20. return
  21. }
  22. // Generate authenticator
  23. auth, err := types.NewAuthenticator(realm, cname)
  24. if err != nil {
  25. err = krberror.Errorf(err, krberror.KRBMsgError, "error generating new authenticator")
  26. return
  27. }
  28. etype, err := crypto.GetEtype(sessionKey.KeyType)
  29. if err != nil {
  30. err = krberror.Errorf(err, krberror.KRBMsgError, "error generating subkey etype")
  31. return
  32. }
  33. err = auth.GenerateSeqNumberAndSubKey(etype.GetETypeID(), etype.GetKeyByteSize())
  34. if err != nil {
  35. err = krberror.Errorf(err, krberror.KRBMsgError, "error generating subkey")
  36. return
  37. }
  38. k = auth.SubKey
  39. // Generate AP_REQ
  40. APreq, err := messages.NewAPReq(tkt, sessionKey, auth)
  41. if err != nil {
  42. return
  43. }
  44. // Form the KRBPriv encpart data
  45. kp := messages.EncKrbPrivPart{
  46. UserData: chpwdb,
  47. Timestamp: auth.CTime,
  48. Usec: auth.Cusec,
  49. SequenceNumber: auth.SeqNumber,
  50. }
  51. kpriv := messages.NewKRBPriv(kp)
  52. err = kpriv.EncryptEncPart(k)
  53. if err != nil {
  54. err = krberror.Errorf(err, krberror.EncryptingError, "error encrypting change passwd data")
  55. return
  56. }
  57. r = Request{
  58. APREQ: APreq,
  59. KRBPriv: kpriv,
  60. }
  61. return
  62. }