Path: blob/master/lib/rex/proto/kerberos/model/krb_error.rb
19715 views
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of a Kerberos KRB-ERROR (response error)7# message definition.8class KrbError < Element9# @!attribute pvno10# @return [Integer] The protocol version number11attr_accessor :pvno12# @!attribute msg_type13# @return [Integer] The type of a protocol message14attr_accessor :msg_type15# @!attribute ctime16# @return [Time] The current time of the client's host17attr_accessor :ctime18# @!attribute cusec19# @return [Integer] The microseconds part of the client timestamp20attr_accessor :cusec21# @!attribute stime22# @return [Time] The current time of the server23attr_accessor :stime24# @!attribute susec25# @return [Integer] The microseconds part of the server timestamp26attr_accessor :susec27# @!attribute error_code28# @return [Rex::Proto::Kerberos::Model::Error::ErrorCode] The error request returned by kerberos or the server when a request fails29attr_accessor :error_code30# @!attribute crealm31# @return [String] The realm part of the client's principal identifier32attr_accessor :crealm33# @!attribute cname34# @return [Rex::Proto::Kerberos::Model::PrincipalName] The name part of the client's principal identifier35attr_accessor :cname36# @!attribute realm37# @return [String] The realm part of the server's principal identifier38attr_accessor :realm39# @!attribute sname40# @return [Rex::Proto::Kerberos::Model::PrincipalName] The name part of the server's identity41attr_accessor :sname42# @!attribute etext43# @return [String] Additional text to help explain the error code44attr_accessor :etext45# @!attribute e_data46# @return [String] additional data about the error (ASN.1 encoded data)47attr_accessor :e_data4849# Decodes the Rex::Proto::Kerberos::Model::KrbError from an input50#51# @param input [String, OpenSSL::ASN1::ASN1Data] the input to decode from52# @return [self] if decoding succeeds53# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed54def decode(input)55case input56when String57decode_string(input)58when OpenSSL::ASN1::ASN1Data59decode_asn1(input)60else61raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode KrbError, invalid input'62end6364self65end6667# Rex::Proto::Kerberos::Model::KrbError encoding isn't supported68#69# @raise [NotImplementedError]70def encode71raise ::NotImplementedError, 'KrbError encoding not supported'72end7374# Decodes the e_data field as an Array<PreAuthDataEntry>.75#76# @return [Array<Rex::Proto::Kerberos::Model::PreAuthDataEntry>]77def e_data_as_pa_data78return [] unless self.e_data7980pre_auth = []81decoded = OpenSSL::ASN1.decode(self.e_data)8283if decoded.first.tag_class == :UNIVERSAL && decoded.first.tag == 1684decoded.each do |pre_auth_data|85pre_auth << Rex::Proto::Kerberos::Model::PreAuthDataEntry.decode(pre_auth_data)86end87else88pre_auth << Rex::Proto::Kerberos::Model::PreAuthDataEntry.decode(decoded)89end9091pre_auth92end9394private9596# Decodes a Rex::Proto::Kerberos::Model::KrbError from an String97#98# @param input [String] the input to decode from99def decode_string(input)100asn1 = OpenSSL::ASN1.decode(input)101102decode_asn1(asn1)103end104105# Decodes a Rex::Proto::Kerberos::Model::KrbError106#107# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from108# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed109def decode_asn1(input)110input.value[0].value.each do |val|111case val.tag112when 0113self.pvno = decode_pvno(val)114when 1115self.msg_type = decode_msg_type(val)116when 2117self.ctime = decode_ctime(val)118when 3119self.cusec = decode_cusec(val)120when 4121self.stime = decode_stime(val)122when 5123self.susec = decode_susec(val)124when 6125self.error_code = decode_error_code(val)126when 7127self.crealm = decode_crealm(val)128when 8129self.cname = decode_cname(val)130when 9131self.realm = decode_realm(val)132when 10133self.sname = decode_sname(val)134when 11135self.etext = decode_etext(val)136when 12137self.e_data = decode_e_data(val)138else139raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, "Failed to decode KRB-ERROR SEQUENCE (#{val.tag})"140end141end142end143144# Decodes the pvno from an OpenSSL::ASN1::ASN1Data145#146# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from147# @return [Integer]148def decode_pvno(input)149input.value[0].value.to_i150end151152# Decodes the msg_type from an OpenSSL::ASN1::ASN1Data153#154# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from155# @return [Integer]156def decode_msg_type(input)157input.value[0].value.to_i158end159160# Decodes the ctime field161#162# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from163# @return [Time]164def decode_ctime(input)165input.value[0].value166end167168# Decodes the cusec field169#170# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from171# @return [Integer]172def decode_cusec(input)173input.value[0].value174end175176# Decodes the stime field177#178# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from179# @return [Time]180def decode_stime(input)181input.value[0].value182end183184# Decodes the susec field185#186# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from187# @return [Integer]188def decode_susec(input)189input.value[0].value.to_i190end191192# Decodes the error_code field193#194# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from195# @return [Rex::Proto::Kerberos::Model::Error::ErrorCode]196def decode_error_code(input)197value = input.value[0].value.to_i198199Error::ErrorCodes::ERROR_MAP[value] || Error::ErrorCode.new('UNKNOWN', value, 'Unknown error')200end201202# Decodes the crealm field203#204# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from205# @return [String]206def decode_crealm(input)207input.value[0].value208end209210# Decodes the cname field211#212# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from213# @return [Rex::Proto::Kerberos::Model::PrincipalName]214def decode_cname(input)215Rex::Proto::Kerberos::Model::PrincipalName.decode(input.value[0])216end217218# Decodes the realm field219#220# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from221# @return [String]222def decode_realm(input)223input.value[0].value224end225226# Decodes the sname field227#228# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from229# @return [Rex::Proto::Kerberos::Model::PrincipalName]230def decode_sname(input)231Rex::Proto::Kerberos::Model::PrincipalName.decode(input.value[0])232end233234# Decodes the e-text field235#236# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from237# @return [String]238def decode_etext(input)239input.value[0].value240end241242# Decodes the e_data from an OpenSSL::ASN1::ASN1Data243#244# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from245# @return [String]246def decode_e_data(input)247input.value[0].value248end249end250end251end252end253end254255256