# # # patch "ssh_agent.cc" # from [74b1a161ad150ea6a9f1292d41cfc7bed99d3997] # to [7b5574ecaf02c27720e9bdeee23a038ac94c8a85] # # patch "ssh_agent.hh" # from [b7dbc9fbcbb4189b1beaa5150d43fada0c3fb9f3] # to [e75bcc316cab986925e7c776bea9422fa73179d0] # ============================================================ --- ssh_agent.cc 74b1a161ad150ea6a9f1292d41cfc7bed99d3997 +++ ssh_agent.cc 7b5574ecaf02c27720e9bdeee23a038ac94c8a85 @@ -9,7 +9,6 @@ #include "botan/bigint.h" #include "netio.hh" -using Netxx::Stream; using Botan::BigInt; ssh_agent::ssh_agent() { @@ -52,32 +51,36 @@ unsigned long } unsigned long -ssh_agent::get_long(char const buf[4]) { - return ((unsigned long)(unsigned char)(buf)[0] << 24) - | ((unsigned long)(unsigned char)(buf)[1] << 16) - | ((unsigned long)(unsigned char)(buf)[2] << 8) - | ((unsigned long)(unsigned char)(buf)[3]); +ssh_agent::get_long(char const buf[4]) +{ + L(FL("agent: get_long: %u %u %u %u") + % (unsigned long)((unsigned char)(buf)[0]) + % (unsigned long)((unsigned char)(buf)[1]) + % (unsigned long)((unsigned char)(buf)[2]) + % (unsigned long)((unsigned char)(buf)[3])); + return ((unsigned long)((unsigned char)(buf)[0]) << 24) + | ((unsigned long)((unsigned char)(buf)[1]) << 16) + | ((unsigned long)((unsigned char)(buf)[2]) << 8) + | ((unsigned long)((unsigned char)(buf)[3])); } -void -ssh_agent::get_string_from_buf(std::string buf, size_t &loc, size_t &len, std::string &out) { - len = get_long(buf.c_str() + loc); +unsigned long +ssh_agent::get_long_from_buf(string const buf, unsigned long &loc) +{ + get_long(buf.c_str() + loc); loc += 4; - //L(FL("agent: get_string_from_buf %s, %i, %i" ) % buf % loc % len); - //E(loc + 4 + len <= buf.length(), F("agent: length (%i) of string (%s) less than loc (%i) + len (%i)") % buf.length() % buf % loc % len); - //char * buf = new char[len + 1]; - //buf.read(buf, len); +} + +void +ssh_agent::get_string_from_buf(string const buf, unsigned long &loc, unsigned long &len, string &out) +{ + L(FL("agent: get_string_from_buf: buf length: %u, loc: %u" ) % buf.length() % loc); + len = get_long_from_buf(buf, loc); + L(FL("agent: get_string_from_buf: len: %u" ) % len); + E(loc + len <= buf.length(), F("agent: length (%i) of buf less than loc (%u) + len (%u)") % buf.length() % loc % len); out = buf.substr(loc, len); - //L(FL("agent: len: %i") % len); - //ret = new unsigned char[len + 1]; - //ret[len] = 0; - /* - for (unsigned int i = 0; i < len; ++i) { - ret[i] = buf[loc + i + 4]; - } - */ + L(FL("agent: get_string_from_buf: out length: %u") % out.length()); loc += len; - //return ret; } void @@ -100,116 +103,88 @@ ssh_agent::get_keys() { ret = stream->read(buf, 4); len = get_long(buf); - L(FL("agent: len %i") % len); + L(FL("agent: len %u") % len); + string packet; + char * read_buf = new char[len]; + long get = len; + while (get > 0) { + ret = stream->read(read_buf, get); + //L(FL("agent: ----ret: %i") % ret); + packet.append(read_buf, ret); + get -= ret; + } + L(FL("agent: get: %u") % get); + delete read_buf; + L(FL("agent: packet length %u") % packet.length()); + //L(FL("agent: ----ret: %i, len: %u, buf: %u %u %u %u") % ret % len % buf[0] % buf[1] % buf[2] % buf[3]); - ret = stream->read(buf, 1); + //ret = stream->read(buf, 1); //L(FL("agent: ----ret: %i, buf: %u") % ret % buf[0]); - E(buf[0] == 12, F("agent: !!!!return type != 12")); - ret = stream->read(buf, 4); - unsigned long num_keys = get_long(buf); - size_t slen; + //first byte is packet type + unsigned long packet_loc = 0; + E(packet.at(0) == 12, F("agent: packet type != 12")); + packet_loc += 1; + + unsigned long num_keys = get_long_from_buf(packet, packet_loc); //L(FL("agent: ----ret: ret %i, num_keys: %u") % ret % num_keys); for (unsigned long key_num = 0; key_num < num_keys; ++key_num) { - L(FL("agent: getting key # %i") % key_num); + L(FL("agent: getting key # %u") % key_num); - ret = stream->read(buf, 4); - unsigned long key_len = get_long(buf); //L(FL("agent: ----ret: ret %i, key_len: %u") % ret % key_len); - //unsigned char * key = new unsigned char[key_len + 1]; - std::string key; - char * read_buf = new char[key_len]; + unsigned long key_len; + string key; + get_string_from_buf(packet, packet_loc, key_len, key); - //string key = string(); - long get = key_len; - //buf[1] = 0; - while (get > 0) { - //ret = stream->read(buf, 1); - //L(FL("agent: ----ret: %i, buf: %u") % ret % buf[0]); + unsigned long key_loc = 0, slen; + string type; + get_string_from_buf(key, key_loc, slen, type); - ret = stream->read(read_buf, get); - L(FL("agent: ----ret: %i") % ret);//, buf: %u", ret, buf[0]); - key.append(read_buf, ret); - /* - for (int i = 0; i < ret; ++i) { - key[key_len - get + i] = read_buf[i]; - } - */ - get -= ret; - } - //key[key_len] = 0; - //L(FL("agent: get: %i") % get); - - delete read_buf; - - size_t loc = 0; - std::string type; - get_string_from_buf(key, loc, slen, type); - L(FL("agent: type: %s") % type); if (type.compare("ssh-rsa") == 0) { L(FL("agent: RSA")); - std::string e; - get_string_from_buf(key, loc, slen, e); + string e; + get_string_from_buf(key, key_loc, slen, e); BigInt eb = BigInt::decode((unsigned char *)(e.c_str()), slen, BigInt::Binary); L(FL("agent: e: %s, len %u") % eb % slen); - std::string n; - get_string_from_buf(key, loc, slen, n); + string n; + get_string_from_buf(key, key_loc, slen, n); BigInt nb = BigInt::decode((unsigned char *)(n.c_str()), slen, BigInt::Binary); L(FL("agent: n: %s, len %u") % nb % slen); } else if (type.compare("ssh-dss") == 0) { L(FL("agent: DSA")); - std::string p; - get_string_from_buf(key, loc, slen, p); + string p; + get_string_from_buf(key, key_loc, slen, p); BigInt pb = BigInt::decode((unsigned char *)(p.c_str()), slen, BigInt::Binary); L(FL("agent: p: %s, len %u") % pb % slen); - std::string q; - get_string_from_buf(key, loc, slen, q); + string q; + get_string_from_buf(key, key_loc, slen, q); BigInt qb = BigInt::decode((unsigned char *)(q.c_str()), slen, BigInt::Binary); L(FL("agent: q: %s, len %u") % qb % slen); - std::string g; - get_string_from_buf(key, loc, slen, g); + string g; + get_string_from_buf(key, key_loc, slen, g); BigInt gb = BigInt::decode((unsigned char *)(g.c_str()), slen, BigInt::Binary); L(FL("agent: g: %s, len %u") % gb % slen); - std::string pub_key; - get_string_from_buf(key, loc, slen, pub_key); + string pub_key; + get_string_from_buf(key, key_loc, slen, pub_key); BigInt pkb = BigInt::decode((unsigned char *)(pub_key.c_str()), slen, BigInt::Binary); L(FL("agent: pub_key: %s, len %u") % pkb % slen); } - ret = stream->read(buf, 4); - key_len = get_long(buf); - //L(FL("agent: ----ret: ret %i, key_len: %u") % ret % key_len); + L(FL("agent: packet length %u, packet loc %u, key length %u, key loc, %u") + % packet.length() + % packet_loc + % key.length() + % key_loc); - key = new char[key_len + 1]; - read_buf = new char[key_len]; - - //string key = string(); - get = key_len; - //buf[1] = 0; - while (get > 0) { - //ret = stream->read(buf, 1); - //L(FL("agent: ----ret: %i, buf: %u") % ret % buf[0]); - - ret = stream->read(read_buf, get); - //L(FL("agent: ----ret: %i") % ret);//, buf: %u", ret, buf[0]); - for (long i = 0; i < ret; ++i) { - key[key_len - get + i] = read_buf[i]; - } - /* - if (!buf[0]) { - break; - } - */ - //key.append(buf); - get -= ret; - } - key[key_len] = 0; - L(FL("agent: get: %i, comment: %s") % get % key); + string comment; + unsigned long comment_len; + get_string_from_buf(packet, packet_loc, comment_len, comment); + L(FL("agent: comment_len: %u, comment: %s") % comment_len % comment); //L(FL("agent: \n\nkey:\n%s") % key.c_str()); //L(FL("agent: %i left") % len - i); /* ============================================================ --- ssh_agent.hh b7dbc9fbcbb4189b1beaa5150d43fada0c3fb9f3 +++ ssh_agent.hh e75bcc316cab986925e7c776bea9422fa73179d0 @@ -6,6 +6,7 @@ using boost::shared_ptr; using Netxx::Stream; using boost::shared_ptr; +using std::string; class ssh_agent { @@ -15,7 +16,8 @@ public: void get_keys(); unsigned long get_long(char const buf[4]); - void get_string_from_buf(std::string buf, size_t &loc, size_t &len, std::string &out); + unsigned long get_long_from_buf(string const buf, unsigned long &loc); + void get_string_from_buf(string const buf, unsigned long &loc, unsigned long &len, string &out); private: shared_ptr stream;