Method: NETSNMP::Message#encode

Defined in:
lib/netsnmp/message.rb

#encode(pdu, security_parameters:, engine_boots: 0, engine_time: 0) ⇒ String



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/netsnmp/message.rb', line 81

def encode(pdu, security_parameters:, engine_boots: 0, engine_time: 0)
  log(level: 2) { pdu.to_hex }
  log { "encoding PDU in V3 message..." }
  scoped_pdu, salt_param = security_parameters.encode(pdu, salt: PRIVNONE,
                                                           engine_boots: engine_boots,
                                                           engine_time: engine_time)

  sec_params = OpenSSL::ASN1::Sequence.new([
                                             OpenSSL::ASN1::OctetString.new(security_parameters.engine_id).with_label(:engine_id),
                                             OpenSSL::ASN1::Integer.new(engine_boots).with_label(:engine_boots),
                                             OpenSSL::ASN1::Integer.new(engine_time).with_label(:engine_time),
                                             OpenSSL::ASN1::OctetString.new(security_parameters.username).with_label(:username),
                                             authnone(security_parameters.auth_protocol),
                                             salt_param
                                           ]).with_label(:security_params)
  log(level: 2) { sec_params.to_hex }

  message_flags = MSG_REPORTABLE | security_parameters.security_level
  message_id    = OpenSSL::ASN1::Integer.new(SecureRandom.random_number(2147483647)).with_label(:message_id)
  headers = OpenSSL::ASN1::Sequence.new([
                                          message_id,
                                          MSG_MAX_SIZE,
                                          OpenSSL::ASN1::OctetString.new([String(message_flags)].pack("h*")).with_label(:message_flags),
                                          MSG_SECURITY_MODEL
                                        ]).with_label(:headers)

  encoded = OpenSSL::ASN1::Sequence([
                                      MSG_VERSION,
                                      headers,
                                      OpenSSL::ASN1::OctetString.new(sec_params.to_der).with_label(:security_params),
                                      scoped_pdu
                                    ]).with_label(:v3_message)
  log(level: 2) { encoded.to_hex }

  encoded = encoded.to_der
  log { Hexdump.dump(encoded) }
  signature = security_parameters.sign(encoded)
  if signature
    log { "signing V3 message..." }
    auth_salt = OpenSSL::ASN1::OctetString.new(signature).with_label(:auth)
    log(level: 2) { auth_salt.to_hex }
    none_der = authnone(security_parameters.auth_protocol).to_der
    encoded[encoded.index(none_der), none_der.size] = auth_salt.to_der
    log { Hexdump.dump(encoded) }
  end
  encoded
end