Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/kerberos/model/ticket.rb
Views: 11766
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of a Kerberos ticket that helps7# a client authenticate to a service.8class Ticket < Element9# @!attribute tkt_vno10# @return [Integer] The ticket version number11attr_accessor :tkt_vno12# @!attribute realm13# @return [String] The realm that issued the ticket14attr_accessor :realm15# @!attribute sname16# @return [Rex::Proto::Kerberos::Model::PrincipalName] The name part of the server's identity17attr_accessor :sname18# @!attribute enc_part19# @return [Rex::Proto::Kerberos::Model::EncryptedData] The encrypted part of the ticket20attr_accessor :enc_part2122def ==(other)23tkt_vno == other.tkt_vno &&24realm == other.realm &&25sname == other.sname &&26enc_part == other.enc_part27end2829# Decodes the Rex::Proto::Kerberos::Model::KrbError from an input30#31# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from32# @return [self] if decoding succeeds33# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed34def decode(input)35case input36when String37decode_string(input)38when OpenSSL::ASN1::ASN1Data39decode_asn1(input)40else41raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode Ticket, invalid input'42end4344self45end4647def encode48elems = []49elems << OpenSSL::ASN1::ASN1Data.new([encode_tkt_vno], 0, :CONTEXT_SPECIFIC)50elems << OpenSSL::ASN1::ASN1Data.new([encode_realm], 1, :CONTEXT_SPECIFIC)51elems << OpenSSL::ASN1::ASN1Data.new([encode_sname], 2, :CONTEXT_SPECIFIC)52elems << OpenSSL::ASN1::ASN1Data.new([encode_enc_part], 3, :CONTEXT_SPECIFIC)53seq = OpenSSL::ASN1::Sequence.new(elems)5455seq_asn1 = OpenSSL::ASN1::ASN1Data.new([seq], TICKET, :APPLICATION)5657seq_asn1.to_der58end5960private6162# Encodes the tkt_vno field63#64# @return [OpenSSL::ASN1::Integer]65def encode_tkt_vno66bn = OpenSSL::BN.new(tkt_vno.to_s)67int = OpenSSL::ASN1::Integer.new(bn)6869int70end7172# Encodes the realm field73#74# @return [OpenSSL::ASN1::GeneralString]75def encode_realm76OpenSSL::ASN1::GeneralString.new(realm)77end7879# Encodes the sname field80#81# @return [String]82def encode_sname83sname.encode84end8586# Encodes the enc_part field87#88# @return [String]89def encode_enc_part90enc_part.encode91end9293# Decodes a Rex::Proto::Kerberos::Model::Ticket from an String94#95# @param input [String] the input to decode from96def decode_string(input)97asn1 = OpenSSL::ASN1.decode(input)9899decode_asn1(asn1)100end101102# Decodes a Rex::Proto::Kerberos::Model::Ticket103#104# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from105# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed106def decode_asn1(input)107input.value[0].value.each do |val|108case val.tag109when 0110self.tkt_vno = decode_tkt_vno(val)111when 1112self.realm = decode_realm(val)113when 2114self.sname = decode_sname(val)115when 3116self.enc_part = decode_enc_part(val)117else118raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode Ticket SEQUENCE'119end120end121end122123# Decodes the tkt_vno from an OpenSSL::ASN1::ASN1Data124#125# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from126# @return [Integer]127def decode_tkt_vno(input)128input.value[0].value.to_i129end130131#132# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from133# @return [String]134def decode_realm(input)135input.value[0].value136end137138# Decodes the sname field139#140# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from141# @return [Rex::Proto::Kerberos::Model::PrincipalName]142def decode_sname(input)143Rex::Proto::Kerberos::Model::PrincipalName.decode(input.value[0])144end145146# Decodes the enc_part from an OpenSSL::ASN1::ASN1Data147#148# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from149# @return [Rex::Proto::Kerberos::Model::EncryptedData]150def decode_enc_part(input)151Rex::Proto::Kerberos::Model::EncryptedData.decode(input.value[0])152end153end154end155end156end157end158159160