Exemplo n.º 1
0
int sha_test()
{
    SHA  sha;
    byte hash[SHA::DIGEST_SIZE];

    testVector test_sha[] =
    {
        testVector("abc", 
                 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
                 "\x6C\x9C\xD0\xD8\x9D"),
        testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
                 "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
                 "\xE5\xE5\x46\x70\xF1"),
        testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                 "aaaaaa", 
                 "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
                 "\x2A\x25\xEC\x64\x4D"),
        testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                 "aaaaaaaaaa",
                 "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
                 "\x53\x99\x5E\x26\xA0")  
    };

    int times( sizeof(test_sha) / sizeof(testVector) );
    for (int i = 0; i < times; ++i) {
        sha.Update(test_sha[i].input_, test_sha[i].inLen_);
        sha.Final(hash);

        if (memcmp(hash, test_sha[i].output_, SHA::DIGEST_SIZE) != 0)
            return -1 - i;
    }

    return 0;
}
Exemplo n.º 2
0
  bool UniqueMUCRoom::handleIqID( Stanza *stanza, int context )
  {
    switch( stanza->subtype() )
    {
      case StanzaIqResult:
        if( context == RequestUniqueName )
        {
          Tag *u = stanza->findChild( "unique", XMLNS_MUC_UNIQUE );
          if( u )
          {
            const std::string& name = u->cdata();
            if( !name.empty() )
              setName( name );
          }
        }
        break;
      case StanzaIqError:
        if( context == RequestUniqueName )
        {
          SHA s;
          s.feed( m_parent->jid().full() );
          s.feed( m_parent->getID() );
          setName( s.hex() );
        }
        break;
      default:
        break;
    }

    MUCRoom::join();

    return false;
  }
Exemplo n.º 3
0
Arquivo: dsa.cpp Projeto: acat/emule
bool DSA::GeneratePrimes(const byte *seedIn, unsigned int g, int &counter,
						  Integer &p, unsigned int L, Integer &q, bool useInputCounterValue)
{
	assert(g%8 == 0);

	SHA sha;
	SecByteBlock seed(seedIn, g/8);
	SecByteBlock U(SHA::DIGESTSIZE);
	SecByteBlock temp(SHA::DIGESTSIZE);
	SecByteBlock W(((L-1)/160+1) * SHA::DIGESTSIZE);
	const int n = (L-1) / 160;
	const int b = (L-1) % 160;
	Integer X;

	sha.CalculateDigest(U, seed, g/8);

	for (int i=g/8-1, carry=true; i>=0 && carry; i--)
		carry=!++seed[i];

	sha.CalculateDigest(temp, seed, g/8);
	xorbuf(U, temp, SHA::DIGESTSIZE);

	U[0] |= 0x80;
	U[SHA::DIGESTSIZE-1] |= 1;
	q.Decode(U, SHA::DIGESTSIZE);

	if (!IsPrime(q))
		return false;

	int counterEnd = useInputCounterValue ? counter+1 : 4096;

	for (int c = 0; c < counterEnd; c++)
	{
		for (int k=0; k<=n; k++)
		{
			for (int i=g/8-1, carry=true; i>=0 && carry; i--)
				carry=!++seed[i];
			if (!useInputCounterValue || c == counter)
				sha.CalculateDigest(W+(n-k)*SHA::DIGESTSIZE, seed, g/8);
		}
		if (!useInputCounterValue || c == counter)
		{
			W[SHA::DIGESTSIZE - 1 - b/8] |= 0x80;
			X.Decode(W + SHA::DIGESTSIZE - 1 - b/8, L/8);
			p = X-((X % (2*q))-1);

			if (p.GetBit(L-1) && IsPrime(p))
			{
				counter = c;
				return true;
			}
		}
	}
	return false;
}
  bool SOCKS5BytestreamManager::requestSOCKS5Bytestream( const JID& to, S5BMode /*mode*/,
                                                         const std::string& sid )
  {
    if( !m_parent )
      return false;

    if( m_hosts.empty() )
    {
      m_parent->logInstance().log( LogLevelWarning, LogAreaClassS5BManager,
                                   "No stream hosts set, cannot request bytestream." );
      return false;
    }

    const std::string& msid = sid.empty() ? m_parent->getID() : sid;
    const std::string& id = m_parent->getID();
    Tag *iq = new Tag( "iq" );
    iq->addAttribute( "type", "set" );
    iq->addAttribute( "to", to.full() );
    iq->addAttribute( "id", id );
    Tag *q = new Tag( iq, "query", "xmlns", XMLNS_BYTESTREAMS );
    q->addAttribute( "sid", msid );
    q->addAttribute( "mode", /*( mode == S5BTCP ) ?*/ "tcp" /*: "udp"*/ );

    StreamHostList::const_iterator it = m_hosts.begin();
    for( ; it != m_hosts.end(); ++it )
    {
      Tag* s = new Tag( q, "streamhost", "jid", (*it).jid.full() );
      s->addAttribute( "host", (*it).host );
      s->addAttribute( "port", (*it).port );
    }

    if( m_server )
    {
      SHA sha;
      sha.feed( msid );
      sha.feed( m_parent->jid().full() );
      sha.feed( to.full() );
      m_server->registerHash( sha.hex() );
    }

    AsyncS5BItem asi;
    asi.sHosts = m_hosts;
    asi.id = id;
    asi.from = to;
    asi.incoming = false;
    m_asyncTrackMap[msid] = asi;

    m_trackMap[id] = msid;
    m_parent->trackID( this, id, S5BOpenStream );
    m_parent->send( iq );

    return true;
  }
Exemplo n.º 5
0
  const std::string Capabilities::ver() const
  {
    if( !m_disco )
      return m_ver;

    SHA sha;
    sha.feed( generate( m_disco->identities(), m_disco->features( true ), m_disco->form() ) );
    const std::string& hash = Base64::encode64( sha.binary() );
    m_disco->removeNodeHandlers( const_cast<Capabilities*>( this ) );
    m_disco->registerNodeHandler( const_cast<Capabilities*>( this ), m_node + '#' + hash );
    return hash;
  }
Exemplo n.º 6
0
  void SOCKS5Bytestream::setConnectionImpl( ConnectionBase* connection )
  {
    if( m_socks5 )
      delete m_socks5; // deletes m_connection as well

    m_connection = connection;

    SHA sha;
    sha.feed( m_sid );
    sha.feed( m_initiator.full() );
    sha.feed( m_target.full() );
    m_socks5 = new ConnectionSOCKS5Proxy( this, connection, m_logInstance, sha.hex(), 0 );
  }
Exemplo n.º 7
0
void jAccount::s_saveVCard(VCard *vcard)
{
	QString hex = "";
	const VCard::Photo &photo = vcard->photo();
	if(!photo.binval.empty())
	{
		QByteArray data(photo.binval.c_str(),photo.binval.length());
		SHA sha;
		sha.feed(photo.binval);
		sha.finalize();
		hex = jProtocol::fromStd(sha.hex());
	}
	m_jabber_protocol->updateAvatarPresence(hex);
	m_jabber_protocol->storeVCard(vcard);
}
Exemplo n.º 8
0
  NonSaslAuth::Query* NonSaslAuth::Query::newInstance( const std::string& user,
                                                       const std::string& sid,
                                                       const std::string& pwd,
                                                       const std::string& resource ) const
  {
    Query* q = new Query( user );
    if( m_digest && !sid.empty() )
    {
      SHA sha;
      sha.feed( sid );
      sha.feed( pwd );
      q->m_pwd = sha.hex();
    }
    else
      q->m_pwd = pwd;

    q->m_resource = resource;
    q->m_digest = m_digest;
    return q;
  }
Exemplo n.º 9
0
int dsa_test()
{
    Source source;
    FileSource("../certs/dsa512.der", source);
    if (source.size() == 0) {
        FileSource("../../certs/dsa512.der", source);  // for testsuite
        if (source.size() == 0) {
            FileSource("../../../certs/dsa512.der", source); // win32 Debug dir
            if (source.size() == 0)
                err_sys("where's your certs dir?", -89);
        }
    }

    const char msg[] = "this is the message";
    byte signature[40];

    DSA_PrivateKey priv(source);
    DSA_Signer signer(priv);

    SHA sha;
    byte digest[SHA::DIGEST_SIZE];
    sha.Update((byte*)msg, sizeof(msg));
    sha.Final(digest);

    signer.Sign(digest, signature, rng);

    byte encoded[sizeof(signature) + 6];
    byte decoded[40];

    word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded);
    DecodeDSA_Signature(decoded, encoded, encSz);

    DSA_PublicKey pub(priv);
    DSA_Verifier verifier(pub);

    if (!verifier.Verify(digest, decoded))
        return -90;

    return 0;
}
Exemplo n.º 10
0
bool SOCKS5BytestreamManager::requestSOCKS5Bytestream( const JID& to, S5BMode mode,
        const std::string& sid,
        const JID& from )
{
    if( !m_parent )
    {
        m_parent->logInstance().warn( LogAreaClassS5BManager,
                                      "No parent (ClientBase) set, cannot request bytestream." );
        return false;
    }

    if( m_hosts.empty() )
    {
        m_parent->logInstance().warn( LogAreaClassS5BManager,
                                      "No stream hosts set, cannot request bytestream." );
        return false;
    }

    const std::string& msid = sid.empty() ? m_parent->getID() : sid;
    const std::string& id = m_parent->getID();
    IQ iq( IQ::Set, to, id );
    iq.addExtension( new Query( msid, mode, m_hosts ) );
    if( from )
        iq.setFrom( from );

    if( m_server )
    {
        SHA sha;
        sha.feed( msid );
        if( from )
            sha.feed( from.full() );
        else
            sha.feed( m_parent->jid().full() );
        sha.feed( to.full() );
        m_server->registerHash( sha.hex() );
    }

    AsyncS5BItem asi;
    asi.sHosts = m_hosts;
    asi.id = id;
    asi.from = to;
    asi.to = from ? from : m_parent->jid();
    asi.incoming = false;
    m_asyncTrackMap[msid] = asi;

    m_trackMap[id] = msid;
    m_parent->send( iq, this, S5BOpenStream );

    return true;
}
Exemplo n.º 11
0
inline void swap(SHA& a, SHA& b)
{
    a.Swap(b);
}
// process NAME, either issuer or subject
void CertDecoder::GetName(NameType nt)
{
    if (source_.GetError().What()) return;

    SHA    sha;
    word32 length = GetSequence();  // length of all distinguished names

    if (length >= ASN_NAME_MAX)
        goto err;
    length += source_.get_index();

    char *ptr, *buf_end;

    if (nt == ISSUER) {
        ptr= issuer_;
        buf_end= ptr + sizeof(issuer_) - 1;  // 1 byte for trailing 0
    }
    else {
        ptr= subject_;
        buf_end= ptr + sizeof(subject_) - 1;  // 1 byte for trailing 0
    }

    while (source_.get_index() < length) {
        GetSet();
        GetSequence();

        byte b = source_.next();
        if (b != OBJECT_IDENTIFIER) {
            source_.SetError(OBJECT_ID_E);
            return;
        }

        word32 oidSz = GetLength(source_);
        byte joint[2];
        memcpy(joint, source_.get_current(), sizeof(joint));

        // v1 name types
        if (joint[0] == 0x55 && joint[1] == 0x04) {
            source_.advance(2);
            byte   id      = source_.next();  
            b              = source_.next();    // strType
            word32 strLen  = GetLength(source_);

            switch (id) {
            case COMMON_NAME:
                if (!(ptr= AddTag(ptr, buf_end, "/CN=", 4, strLen)))
                  goto err;
                break;
            case SUR_NAME:
                if (!(ptr= AddTag(ptr, buf_end, "/SN=", 4, strLen)))
                  goto err;
                break;
            case COUNTRY_NAME:
                if (!(ptr= AddTag(ptr, buf_end, "/C=", 3, strLen)))
                  goto err;
                break;
            case LOCALITY_NAME:
                if (!(ptr= AddTag(ptr, buf_end, "/L=", 3, strLen)))
                  goto err;
                break;
            case STATE_NAME:
                if (!(ptr= AddTag(ptr, buf_end, "/ST=", 4, strLen)))
                  goto err;
                break;
            case ORG_NAME:
                if (!(ptr= AddTag(ptr, buf_end, "/O=", 3, strLen)))
                  goto err;
                break;
            case ORGUNIT_NAME:
                if (!(ptr= AddTag(ptr, buf_end, "/OU=", 4, strLen)))
                  goto err;
                break;
            }

            sha.Update(source_.get_current(), strLen);
            source_.advance(strLen);
        }
        else {
            bool email = false;
            if (joint[0] == 0x2a && joint[1] == 0x86)  // email id hdr
                email = true;

            source_.advance(oidSz + 1);
            word32 length = GetLength(source_);

            if (email && !(ptr= AddTag(ptr, buf_end, "/emailAddress=", 14, length)))
                goto err;

            source_.advance(length);
        }
    }
    *ptr= 0;

    sha.Final(nt == ISSUER ? issuerHash_ : subjectHash_);
        
    return;
    
err:
    source_.SetError(CONTENT_E);
}
Exemplo n.º 13
0
TEST(SHATest, sha256Text_a_1m) {
	SHA sha;
	std::string hash = sha.sha256(boost::filesystem::path("src/test/hash/a_1m.dat"));
	ASSERT_EQ("cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0",hash);
}
Exemplo n.º 14
0
TEST(SHATest, sha256Text_abcdbcde) {
	SHA sha;
	std::string hash = sha.sha256(boost::filesystem::path("src/test/hash/abcdbcde.dat"));
	ASSERT_EQ("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",hash);
}
Exemplo n.º 15
0
TEST(SHATest, sha256Text_abc) {
	SHA sha;
	std::string hash = sha.sha256(boost::filesystem::path("src/test/hash/abc.dat"));
	ASSERT_EQ("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",hash);
}
Exemplo n.º 16
0
TEST(SHATest, sha256Text_empty) {
	SHA sha;
	std::string hash = sha.sha256(boost::filesystem::path("src/test/hash/empty"));
	ASSERT_EQ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",hash);
}
Exemplo n.º 17
0
TEST(SHATest, sha256Binary_Image) {
	SHA sha;
	std::string hash = sha.sha256(boost::filesystem::path("src/test/hash/testImage.jpg"));
	ASSERT_EQ("3100fc5145acab142a5935b6b9436e53cde05e7382f36dc1e67360b9ff96539b",hash);
}
void NonSaslAuthTest::send( const IQ& iq, IqHandler* ih, int ctx )
{
  if( m_test == 1 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      m_result++;
      m_test = 2;
      IQ re( IQ::Result, iq.from(), iq.id() );
      Tag* d = new Tag( "query" );
      d->setXmlns( XMLNS_AUTH );
      new Tag( d, "username" );
      new Tag( d, "password" );
      new Tag( d, "resource" );
      re.addExtension( new NonSaslAuth::Query( d ) );
      ih->handleIqID( re, ctx );
      delete d;
    }
  }
  else if( m_test == 2 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      Tag* d = q->tag();
      if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
                      "<username>user</username>"
                      "<password>pwd</password>"
                      "<resource>resource</resource>"
                      "</query>" )
      {
        m_result++;
        IQ re( IQ::Result, iq.from(), iq.id() );
        ih->handleIqID( re, ctx );
      }
      delete d;
    }
  }
  else if( m_test == 3 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      m_result++;
      m_test = 4;
      IQ re( IQ::Result, iq.from(), iq.id() );
      Tag* d = new Tag( "query" );
      d->setXmlns( XMLNS_AUTH );
      new Tag( d, "username" );
      new Tag( d, "digest" );
      new Tag( d, "password" );
      new Tag( d, "resource" );
      re.addExtension( new NonSaslAuth::Query( d ) );
      ih->handleIqID( re, ctx );
      delete d;
    }
  }
  else if( m_test == 4 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      Tag* d = q->tag();
      SHA sha;
      sha.feed( "sid2" );
      sha.feed( "pwd" );
      if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
          "<username>user</username>"
          "<digest>" + sha.hex() + "</digest>"
          "<resource>resource</resource>"
          "</query>" )
      {
        m_result += 2;
        IQ re( IQ::Result, iq.from(), iq.id() );
        ih->handleIqID( re, ctx );
      }
      delete d;
    }
  }
}
Exemplo n.º 19
0
  bool NonSaslAuth::handleIqID( Stanza *stanza, int context )
  {
    switch( stanza->subtype() )
    {
      case StanzaIqError:
      {
        m_parent->setAuthed( false );
        m_parent->disconnect( ConnAuthenticationFailed );

        Tag *t = stanza->findChild( "error" );
        if( t )
        {
          if( t->hasChild( "conflict" ) || t->hasAttribute( "code", "409" ) )
            m_parent->setAuthFailure( NonSaslConflict );
          else if( t->hasChild( "not-acceptable" ) || t->hasAttribute( "code", "406" ) )
            m_parent->setAuthFailure( NonSaslNotAcceptable );
          else if( t->hasChild( "not-authorized" ) || t->hasAttribute( "code", "401" ) )
            m_parent->setAuthFailure( NonSaslNotAuthorized );
        }
        break;
      }
      case StanzaIqResult:
        switch( context )
        {
          case TRACK_REQUEST_AUTH_FIELDS:
          {
            const std::string& id = m_parent->getID();

            Tag *iq = new Tag( "iq" );
            iq->addAttribute( "id", id );
            iq->addAttribute( "type", "set" );
            Tag *query = new Tag( iq, "query" );
            query->addAttribute( "xmlns", XMLNS_AUTH );
            new Tag( query, "username", m_parent->jid().username() );
            new Tag( query, "resource", m_parent->jid().resource() );

            Tag *q = stanza->findChild( "query" );
            if( ( q->hasChild( "digest" ) ) && !m_sid.empty() )
            {
              SHA sha;
              sha.feed( m_sid );
              sha.feed( m_parent->password() );
              sha.finalize();
              new Tag( query, "digest", sha.hex() );
            }
            else
            {
              new Tag( query, "password", m_parent->password() );
            }

            m_parent->trackID( this, id, TRACK_SEND_AUTH );
            m_parent->send( iq );
            break;
          }
          case TRACK_SEND_AUTH:
            m_parent->setAuthed( true );
            m_parent->connected();
            break;
        }
        break;

      default:
        break;
    }
    return false;
  }
  bool SOCKS5BytestreamManager::handleIqID( Stanza *stanza, int context )
  {
    StringMap::iterator it = m_trackMap.find( stanza->id() );
    if( it == m_trackMap.end() )
      return false;

    switch( context )
    {
      case S5BOpenStream:
      {
        switch( stanza->subtype() )
        {
          case StanzaIqResult:
          {
            Tag* q = stanza->findChild( "query", "xmlns", XMLNS_BYTESTREAMS );
            if( !q || !m_socks5BytestreamHandler )
              return false;

            Tag* s = q->findChild( "streamhost-used" );
            if( !s || !s->hasAttribute( "jid" ) )
              return false;

            const std::string & proxy = s->findAttribute( "jid" );
            const StreamHost* sh = findProxy( stanza->from(), proxy, (*it).second );
            if( sh )
            {
              SOCKS5Bytestream* s5b = 0;
              bool selfProxy = ( proxy == m_parent->jid().full() && m_server );
              if( selfProxy )
              {
                SHA sha;
                sha.feed( (*it).second );
                sha.feed( m_parent->jid().full() );
                sha.feed( stanza->from().full() );
                s5b = new SOCKS5Bytestream( this, m_server->getConnection( sha.hex() ),
                                            m_parent->logInstance(),
                                            m_parent->jid(), stanza->from(),
                                            (*it).second );
              }
              else
              {
                s5b = new SOCKS5Bytestream( this, m_parent->connectionImpl()->newInstance(),
                                            m_parent->logInstance(),
                                            m_parent->jid(), stanza->from(),
                                            (*it).second );
                StreamHostList shl;
                shl.push_back( *sh );
                s5b->setStreamHosts( shl );
              }
              m_s5bMap[(*it).second] = s5b;
              m_socks5BytestreamHandler->handleOutgoingSOCKS5Bytestream( s5b );
              if( selfProxy )
                s5b->activate();
            }
            break;
          }
          case StanzaIqError:
            m_socks5BytestreamHandler->handleSOCKS5BytestreamError( stanza, (*it).second );
            break;
          default:
            break;
        }
        break;
      }
      case S5BActivateStream:
      {
        switch( stanza->subtype() )
        {
          case StanzaIqResult:
          {
            S5BMap::const_iterator it5 = m_s5bMap.find( (*it).second );
            if( it5 != m_s5bMap.end() )
              (*it5).second->activate();
            break;
          }
          case StanzaIqError:
            m_socks5BytestreamHandler->handleSOCKS5BytestreamError( stanza, (*it).second );
            break;
          default:
            break;
        }
        break;
      }
      default:
        break;
    }
    m_trackMap.erase( it );

    return false;
  }
Exemplo n.º 21
0
int main_test1( int /*argc*/, char** /*argv*/ )
{
  int fail = 0;
  std::string name;
  Tag* t = 0;


  // -------
  {
    name = "field request";
    NonSaslAuth::Query q( "user" );
    t = q.tag();
    if( !t || t->xml() != "<query xmlns='" + XMLNS_AUTH + "'>"
                          "<username>user</username></query>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed\n", name.c_str() );
    }
    delete t;
    t = 0;
  }

  // -------
  {
    name = "reply w/ pwd";
    Tag* q = new Tag( "query" );
    q->setXmlns( XMLNS_AUTH );
    new Tag( q, "username" );
    new Tag( q, "password" );
    new Tag( q, "resource" );
    NonSaslAuth::Query n( q );
    NonSaslAuth::Query* nq = n.newInstance( "user", "sid", "pwd", "resource" );
    t = nq->tag();
    if( !t || t->xml() != "<query xmlns='" + XMLNS_AUTH + "'>"
                          "<username>user</username>"
                          "<password>pwd</password>"
                          "<resource>resource</resource>"
                          "</query>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed\n", name.c_str() );
    }
    delete nq;
    delete q;
    delete t;
    t = 0;
  }

  // -------
  {
    name = "reply w/ digest";
    Tag* q = new Tag( "query" );
    q->setXmlns( XMLNS_AUTH );
    new Tag( q, "username" );
    new Tag( q, "password" );
    new Tag( q, "digest" );
    new Tag( q, "resource" );
    NonSaslAuth::Query n( q );
    NonSaslAuth::Query* nq = n.newInstance( "user", "sid", "pwd", "resource" );
    SHA sha;
    sha.feed( "sid" );
    sha.feed( "pwd" );
    t = nq->tag();
    if( !t || t->xml() != "<query xmlns='" + XMLNS_AUTH + "'>"
         "<username>user</username>"
         "<digest>" + sha.hex() + "</digest>"
         "<resource>resource</resource>"
         "</query>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
    }
    delete nq;
    delete q;
    delete t;
    t = 0;
  }

  // -------
  {
    StanzaExtensionFactory sef;
    sef.registerExtension( new NonSaslAuth::Query() );
    name = "NonSaslAuth::Query/SEFactory test";
    Tag* f = new Tag( "iq" );
    new Tag( f, "query", "xmlns", XMLNS_AUTH );
    IQ iq( IQ::Set, JID(), "" );
    sef.addExtensions( iq, f );
    const NonSaslAuth::Query* se = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( se == 0 )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed\n", name.c_str() );
    }
    delete f;
  }


  if( fail == 0 )
  {
    printf( "NonSaslAuth::Query: OK\n" );
    return 0;
  }
  else
  {
    fprintf( stderr, "NonSaslAuth::Query: %d test(s) failed\n", fail );
    return 1;
  }

}
Exemplo n.º 22
0
void SOCKS5BytestreamManager::handleIqID( const IQ& iq, int context )
{
    StringMap::iterator it = m_trackMap.find( iq.id() );
    if( it == m_trackMap.end() )
        return;

    switch( context )
    {
    case S5BOpenStream:
    {
        switch( iq.subtype() )
        {
        case IQ::Result:
        {
            const Query* q = iq.findExtension<Query>( ExtS5BQuery );
            if( q && m_socks5BytestreamHandler )
            {
                const std::string& proxy = q->jid().full();
                const StreamHost* sh = findProxy( iq.from(), proxy, (*it).second );
                if( sh )
                {
                    SOCKS5Bytestream* s5b = 0;
                    bool selfProxy = ( proxy == m_parent->jid().full() && m_server );
                    if( selfProxy )
                    {
                        SHA sha;
                        sha.feed( (*it).second );
                        sha.feed( iq.to().full() );
                        sha.feed( iq.from().full() );
                        s5b = new SOCKS5Bytestream( this, m_server->getConnection( sha.hex() ),
                                                    m_parent->logInstance(),
                                                    iq.to(), iq.from(),
                                                    (*it).second );
                    }
                    else
                    {
                        s5b = new SOCKS5Bytestream( this, m_parent->connectionImpl()->newInstance(),
                                                    m_parent->logInstance(),
                                                    iq.to(), iq.from(),
                                                    (*it).second );
                        s5b->setStreamHosts( StreamHostList( 1, *sh ) );
                    }
                    m_s5bMap[(*it).second] = s5b;
                    m_socks5BytestreamHandler->handleOutgoingBytestream( s5b );
                    if( selfProxy )
                        s5b->activate();
                }
            }
            break;
        }
        case IQ::Error:
            m_socks5BytestreamHandler->handleBytestreamError( iq, (*it).second );
            break;
        default:
            break;
        }
        break;
    }
    case S5BActivateStream:
    {
        switch( iq.subtype() )
        {
        case IQ::Result:
        {
            S5BMap::const_iterator it5 = m_s5bMap.find( (*it).second );
            if( it5 != m_s5bMap.end() )
                (*it5).second->activate();
            break;
        }
        case IQ::Error:
            m_socks5BytestreamHandler->handleBytestreamError( iq, (*it).second );
            break;
        default:
            break;
        }
        break;
    }
    default:
        break;
    }
    m_trackMap.erase( it );
}
Exemplo n.º 23
0
int main( int /*argc*/, char** /*argv*/ )
{
  int fail = 0;
  std::string name;
  SHA sha;



  // -------
  name = "empty string";
  sha.feed( "" );
  sha.finalize();
  if( sha.hex() != "da39a3ee5e6b4b0d3255bfef95601890afd80709" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  sha.reset();

  // -------
  name = "The quick brown fox jumps over the lazy dog";
  sha.feed( name );
  sha.finalize();
  if( sha.hex() != "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "The quick brown fox jumps over the lazy cog";
  sha.feed( name );
  sha.finalize();
  if( sha.hex() != "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "two-step";
  sha.feed( "The quick brown fox " );
  sha.feed( "jumps over the lazy dog" );
  sha.finalize();
  if( sha.hex() != "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "54byte string";
  sha.feed( std::string( 54, 'x' ) );
  sha.finalize();
  if( sha.hex() != "31045e7bb077ff8d188a776b196b980388735dbb" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "55byte string";
  sha.feed( std::string( 55, 'x' ) );
  sha.finalize();
  if( sha.hex() != "cef734ba81a024479e09eb5a75b6ddae62e6abf1" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "56byte string";
  sha.feed( std::string( 56, 'x' ) );
  sha.finalize();
  if( sha.hex() != "901305367c259952f4e7af8323f480d59f81335b" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "57byte string";
  sha.feed( std::string( 57, 'x' ) );
  sha.finalize();
  if( sha.hex() != "025ecbd5d70f8fb3c5457cd96bab13fda305dc59" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();

  // -------
  name = "many-step";
  sha.feed( "The" );
  sha.feed( " quick bro" );
  sha.feed( "" );
  sha.feed( "wn fox " );
  sha.feed( "jumps over the lazy dog" );
  sha.finalize();
  if( sha.hex() != "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), sha.hex().c_str() );
  }
  sha.reset();



  if( fail == 0 )
  {
    printf( "SHA: OK\n" );
    return 0;
  }
  else
  {
    fprintf( stderr, "SHA: %d test(s) failed\n", fail );
    return 1;
  }


}
Exemplo n.º 24
0
// process NAME, either issuer or subject
void CertDecoder::GetName(NameType nt)
{
    if (source_.GetError().What()) return;

    SHA    sha;
    word32 length = GetSequence();  // length of all distinguished names

    if (length >= ASN_NAME_MAX)
        return;
    if (source_.IsLeft(length) == false) return;
    length += source_.get_index();
    
    char* ptr;
    char* buf_end;

    if (nt == ISSUER) {
        ptr = issuer_;
        buf_end = ptr + sizeof(issuer_) - 1;   // 1 byte for trailing 0
    }
    else {
        ptr = subject_;
        buf_end = ptr + sizeof(subject_) - 1;  // 1 byte for trailing 0
    }

    while (source_.get_index() < length) {
        GetSet();
        if (source_.GetError().What() == SET_E) {
            source_.SetError(NO_ERROR_E);  // extensions may only have sequence 
            source_.prev();
        }
        GetSequence();

        byte b = source_.next();
        if (b != OBJECT_IDENTIFIER) {
            source_.SetError(OBJECT_ID_E);
            return;
        }

        word32 oidSz = GetLength(source_);
        if (source_.IsLeft(oidSz) == false) return;

        byte joint[2];
        if (source_.IsLeft(sizeof(joint)) == false) return;
        memcpy(joint, source_.get_current(), sizeof(joint));

        // v1 name types
        if (joint[0] == 0x55 && joint[1] == 0x04) {
            source_.advance(2);
            byte   id      = source_.next();  
            b              = source_.next();    // strType
            word32 strLen  = GetLength(source_);

            if (source_.IsLeft(strLen) == false) return;

            switch (id) {
            case COMMON_NAME:
                if (!(ptr = AddTag(ptr, buf_end, "/CN=", 4, strLen)))
                    return;
                if (nt == ISSUER) {
                    issCnPos_ = (int)(ptr - strLen - issuer_);
                    issCnLen_ = (int)strLen;
                } else {
                    subCnPos_ = (int)(ptr - strLen - subject_);
                    subCnLen_ = (int)strLen;
                }
                break;
            case SUR_NAME:
                if (!(ptr = AddTag(ptr, buf_end, "/SN=", 4, strLen)))
                    return;
                break;
            case COUNTRY_NAME:
                if (!(ptr = AddTag(ptr, buf_end, "/C=", 3, strLen)))
                    return;
                break;
            case LOCALITY_NAME:
                if (!(ptr = AddTag(ptr, buf_end, "/L=", 3, strLen)))
                    return;
                break;
            case STATE_NAME:
                if (!(ptr = AddTag(ptr, buf_end, "/ST=", 4, strLen)))
                    return;
                break;
            case ORG_NAME:
                if (!(ptr = AddTag(ptr, buf_end, "/O=", 3, strLen)))
                    return;
                break;
            case ORGUNIT_NAME:
                if (!(ptr = AddTag(ptr, buf_end, "/OU=", 4, strLen)))
                    return;
                break;
            }

            sha.Update(source_.get_current(), strLen);
            source_.advance(strLen);
        }
        else { 
            bool email = false;
            if (joint[0] == 0x2a && joint[1] == 0x86)  // email id hdr
                email = true;

            source_.advance(oidSz + 1);
            word32 length = GetLength(source_);
            if (source_.IsLeft(length) == false) return;

            if (email) {
                if (!(ptr = AddTag(ptr, buf_end, "/emailAddress=", 14, length)))
                    return; 
            }

            source_.advance(length);
        }
    }

    *ptr = 0;

    if (nt == ISSUER)
        sha.Final(issuerHash_);
    else
        sha.Final(subjectHash_);
}