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/principal_name.rb
Views: 11766
# -*- coding: binary -*-12module Rex3module Proto4module Kerberos5module Model6# This class provides a representation of a principal, an asset (e.g., a7# workstation user or a network server) on a network.8class PrincipalName < Element910# @!attribute name_type11# @return [Integer] The type of name12attr_accessor :name_type13# @!attribute name_string14# @return [Array<String>] A sequence of strings that form a name.15attr_accessor :name_string1617def ==(other)18name_type == other.name_type &&19name_string == other.name_string20end2122# Decodes a Rex::Proto::Kerberos::Model::PrincipalName23#24# @param input [String, OpenSSL::ASN1::Sequence] the input to decode from25# @return [self] if decoding succeeds26# @raise [Rex::Proto::Kerberos::Model::Error::KerberosDecodingError] if decoding doesn't succeed27def decode(input)28case input29when String30decode_string(input)31when OpenSSL::ASN1::Sequence32decode_asn1(input)33else34raise ::Rex::Proto::Kerberos::Model::Error::KerberosDecodingError, 'Failed to decode Principal Name, invalid input'35end3637self38end3940# Encodes a Rex::Proto::Kerberos::Model::PrincipalName into an41# ASN.1 String42#43# @return [String]44def encode45integer_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_name_type], 0, :CONTEXT_SPECIFIC)46string_asn1 = OpenSSL::ASN1::ASN1Data.new([encode_name_string], 1, :CONTEXT_SPECIFIC)47seq = OpenSSL::ASN1::Sequence.new([integer_asn1, string_asn1])4849seq.to_der50end5152def to_s53name_string.join('/')54end5556private5758# Encodes the name_type59#60# @return [OpenSSL::ASN1::Integer]61def encode_name_type62int_bn = OpenSSL::BN.new(name_type.to_s)63int = OpenSSL::ASN1::Integer.new(int_bn)6465int66end6768# Encodes the name_string69#70# @return [OpenSSL::ASN1::Sequence]71def encode_name_string72strings = []73name_string.each do |s|74strings << OpenSSL::ASN1::GeneralString.new(s)75end76seq_string = OpenSSL::ASN1::Sequence.new(strings)7778seq_string79end8081# Decodes a Rex::Proto::Kerberos::Model::PrincipalName from an String82#83# @param input [String] the input to decode from84def decode_string(input)85asn1 = OpenSSL::ASN1.decode(input)8687decode_asn1(asn1)88end8990# Decodes a Rex::Proto::Kerberos::Model::PrincipalName from an91# OpenSSL::ASN1::Sequence92#93# @param input [OpenSSL::ASN1::Sequence] the input to decode from94def decode_asn1(input)95seq_values = input.value96self.name_type = decode_name_type(seq_values[0])97self.name_string = decode_name_string(seq_values[1])98end99100# Decodes the name_type from an OpenSSL::ASN1::ASN1Data101#102# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from103# @return [Integer]104def decode_name_type(input)105input.value[0].value.to_i106end107108# Decodes the name_string from an OpenSSL::ASN1::ASN1Data109#110# @param input [OpenSSL::ASN1::ASN1Data] the input to decode from111# @return [Array<String>]112def decode_name_string(input)113strings = []114input.value[0].value.each do |v|115strings << v.value116end117118strings119end120end121end122end123end124end125126127