Esempio n. 1
0
result_t PKey::decrypt(Buffer_base *data, obj_ptr<Buffer_base> &retVal,
                       exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    result_t hr;
    bool priv;

    hr = isPrivate(priv);
    if (hr < 0)
        return hr;

    if (!priv)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    int ret;
    std::string str;
    std::string output;
    size_t olen;

    data->toString(str);
    output.resize(POLARSSL_PREMASTER_SIZE * 2);

    ret = pk_decrypt(&m_key, (const unsigned char *)str.c_str(), str.length(),
                     (unsigned char *)&output[0], &olen, output.length(),
                     ctr_drbg_random, &g_ssl.ctr_drbg);
    if (ret != 0)
        return CHECK_ERROR(_ssl::setError(ret));

    output.resize(olen);
    retVal = new Buffer(output);

    return 0;
}
Esempio n. 2
0
result_t db_base::openRedis(const char *connString,
                            obj_ptr<Redis_base> &retVal, exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    std::string host;
    int nPort = 6379;

    if (!qstrcmp(connString, "redis:", 6))
    {
        obj_ptr<Url> u = new Url();

        result_t hr = u->parse(connString);
        if (hr < 0)
            return hr;

        host = u->m_hostname;
        connString = host.c_str();

        if (u->m_port.length() > 0)
            nPort = atoi(u->m_port.c_str());
    }

    obj_ptr<Redis> conn = new Redis();
    retVal = conn;

    return conn->connect(connString, nPort, ac);
}
Esempio n. 3
0
result_t PKey::verify(Buffer_base *sign, Buffer_base *data, bool &retVal,
                      exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    int ret;
    std::string str;
    std::string strsign;

    data->toString(str);
    sign->toString(strsign);

    ret = pk_verify(&m_key, POLARSSL_MD_NONE,
                    (const unsigned char *)str.c_str(), str.length(),
                    (const unsigned char *)strsign.c_str(), strsign.length());
    if (ret == POLARSSL_ERR_ECP_VERIFY_FAILED ||
            ret == POLARSSL_ERR_RSA_VERIFY_FAILED)
    {
        retVal = false;
        return 0;
    }

    if (ret != 0)
        return CHECK_ERROR(_ssl::setError(ret));

    retVal = true;

    return 0;
}
Esempio n. 4
0
result_t PKey::genEcKey(const char *curve, exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    const ecp_curve_info *curve_info;
    curve_info = ecp_curve_info_from_name(curve);
    if (curve_info == NULL)
        return CHECK_ERROR(Runtime::setError("PKey: Unknown curve"));

    int ret;

    clear();

    ret = pk_init_ctx(&m_key, pk_info_from_type(POLARSSL_PK_ECKEY));
    if (ret != 0)
        return CHECK_ERROR(_ssl::setError(ret));

    ret = ecp_gen_key(curve_info->grp_id, pk_ec(m_key),
                      ctr_drbg_random, &g_ssl.ctr_drbg);

    if (ret != 0)
        return CHECK_ERROR(_ssl::setError(ret));

    return 0;
}
Esempio n. 5
0
File: zlib.cpp Progetto: CYCOK/fibjs
result_t zlib_base::gunzip(Buffer_base *data, obj_ptr<Buffer_base> &retVal,
                           exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    return gunz().process(data, retVal);
}
Esempio n. 6
0
File: zlib.cpp Progetto: CYCOK/fibjs
result_t zlib_base::deflate(Buffer_base *data, int32_t level,
                            obj_ptr<Buffer_base> &retVal, exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    return def(level).process(data, retVal);
}
Esempio n. 7
0
result_t SQLite::execute(const char *sql, obj_ptr<DBResult_base> &retVal, exlib::AsyncEvent *ac)
{
    if (!m_db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    return execute(sql, (int) qstrlen(sql), retVal);
}
Esempio n. 8
0
result_t File::readAll(obj_ptr<Buffer_base> &retVal, exlib::AsyncEvent *ac)
{
    if (m_fd == -1)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    std::string strBuf;

    int32_t bytes;
    int64_t p = _lseeki64(m_fd, 0, SEEK_CUR);
    if (p < 0)
        return CHECK_ERROR(LastError());

    int64_t sz = _lseeki64(m_fd, 0, SEEK_END);
    if (sz < 0)
        return CHECK_ERROR(LastError());

    if (_lseeki64(m_fd, p, SEEK_SET) < 0)
        return CHECK_ERROR(LastError());

    sz -= p;

    bytes = (int32_t) sz;

    if (bytes > 0)
    {
        strBuf.resize(bytes);
        int sz = bytes;
        char *p = &strBuf[0];

        while (sz)
        {
            int n = (int) ::_read(m_fd, p, sz > STREAM_BUFF_SIZE ? STREAM_BUFF_SIZE : sz);
            if (n < 0)
                return CHECK_ERROR(LastError());
            if (n == 0)
                break;

            sz -= n;
            p += n;
        }

        strBuf.resize(bytes - sz);
    }

    if (strBuf.length() == 0)
        return CALL_RETURN_NULL;

    retVal = new Buffer(strBuf);

    return 0;
}
Esempio n. 9
0
result_t SQLite::commit(exlib::AsyncEvent *ac)
{
    if (!m_db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    obj_ptr<DBResult_base> retVal;
    return execute("COMMIT", 6, retVal);
}
Esempio n. 10
0
result_t SQLite::rollback(exlib::AsyncEvent *ac)
{
    if (!m_db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    obj_ptr<DBResult_base> retVal;
    return execute("ROLLBACK", 8, retVal);
}
Esempio n. 11
0
result_t File::truncate(int64_t bytes, exlib::AsyncEvent *ac)
{
    if (m_fd == -1)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    if (ftruncate64(m_fd, bytes) < 0)
        return CHECK_ERROR(LastError());

    return 0;
}
Esempio n. 12
0
result_t File::write(Buffer_base *data, exlib::AsyncEvent *ac)
{
    if (m_fd == -1)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    std::string strBuf;
    data->toString(strBuf);

    return Write(strBuf.c_str(), (int) strBuf.length());
}
Esempio n. 13
0
result_t SQLite::close(exlib::AsyncEvent *ac)
{
    if (!m_db)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    sqlite3_close(m_db);
    m_db = NULL;

    return 0;
}
Esempio n. 14
0
result_t File::chmod(int32_t mode, exlib::AsyncEvent *ac)
{
#ifdef _WIN32
    return CHECK_ERROR(CALL_E_INVALID_CALL);
#else
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    if (::fchmod(m_fd, mode))
        return CHECK_ERROR(LastError());

    return 0;
#endif
}
Esempio n. 15
0
result_t File::flush(exlib::AsyncEvent *ac)
{
    if (m_fd == -1)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    //    fflush(m_file);

    //    if (ferror(m_file))
    //        return CHECK_ERROR(LastError());

    return 0;
}
Esempio n. 16
0
result_t File::stat(obj_ptr<Stat_base> &retVal, exlib::AsyncEvent *ac)
{
    if (m_fd == -1)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    struct stat64 st;
    fstat64(m_fd, &st);

    obj_ptr<Stat> pStat = new Stat();
    pStat->fill(name.c_str(), st);
    retVal = pStat;

    return 0;
}
Esempio n. 17
0
result_t File::close(exlib::AsyncEvent *ac)
{
    if (m_fd != -1)
    {
        if (switchToAsync(ac))
            return CHECK_ERROR(CALL_E_NOSYNC);

        if (m_pipe)
            pclose(m_pipe);
        else
            ::_close(m_fd);

        m_fd = -1;
        m_pipe = NULL;
    }

    return 0;
}
Esempio n. 18
0
result_t File::open(const char *fname, const char *flags, exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

#ifdef _WIN32
    int _flags = _O_BINARY;
#else
    int _flags = 0;
#endif

    if (!qstrcmp(flags, "r" ))
        _flags |= O_RDONLY;
    else if (!qstrcmp(flags, "r+" ))
        _flags |= O_RDWR;
    else if (!qstrcmp(flags, "w" ))
        _flags |= O_TRUNC | O_CREAT | O_WRONLY;
    else if (!qstrcmp(flags, "w+" ))
        _flags |= O_TRUNC | O_CREAT | O_RDWR;
    else if (!qstrcmp(flags, "a" ))
        _flags |= O_APPEND | O_CREAT | O_WRONLY;
    else if (!qstrcmp(flags, "a+" ))
        _flags |= O_APPEND | O_CREAT | O_RDWR;

    close(ac);

#ifdef _WIN32
    m_fd = _wopen(UTF8_W(fname), _flags, _S_IREAD | _S_IWRITE);
#else
    m_fd = ::open(fname, _flags, 0666);
#endif
    if (m_fd < 0)
        return CHECK_ERROR(LastError());

#ifndef _WIN32
    if (::fcntl(m_fd, F_SETFD, FD_CLOEXEC))
        return CHECK_ERROR(LastError());
#endif

    name = fname;

    return 0;
}
Esempio n. 19
0
result_t db_base::openSQLite(const char *connString,
                             obj_ptr<SQLite_base> &retVal, exlib::AsyncEvent *ac)
{
    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    result_t hr;

    if (!qstrcmp(connString, "sqlite:", 7))
        connString += 7;

    obj_ptr<SQLite> db = new SQLite();
    hr = db->open(connString);
    if (hr < 0)
        return hr;

    retVal = db;

    return 0;
}
Esempio n. 20
0
result_t PKey::genRsaKey(int32_t size, exlib::AsyncEvent *ac)
{
    if (size < 128 || size > 8192)
        return CHECK_ERROR(Runtime::setError("PKey: Invalid key size"));

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    int ret;

    clear();

    ret = pk_init_ctx(&m_key, pk_info_from_type(POLARSSL_PK_RSA));
    if (ret != 0)
        return CHECK_ERROR(_ssl::setError(ret));

    ret = rsa_gen_key(pk_rsa(m_key), ctr_drbg_random, &g_ssl.ctr_drbg,
                      size, 65537);

    if (ret != 0)
        return CHECK_ERROR(_ssl::setError(ret));

    return 0;
}
Esempio n. 21
0
result_t Redis::_command(std::string &req, Variant &retVal, exlib::AsyncEvent *ac)
{
    class asyncCommand: public asyncState
    {
    public:
        asyncCommand(Redis *pThis, std::string &req, Variant &retVal, exlib::AsyncEvent *ac) :
            asyncState(ac), m_pThis(pThis), m_req(req), m_retVal(retVal)
        {
            m_stmBuffered = pThis->m_stmBuffered;
            set(send);
        }

        static int send(asyncState *pState, int n)
        {
            asyncCommand *pThis = (asyncCommand *) pState;

            pThis->m_buffer = new Buffer(pThis->m_req);

            pThis->set(read);
            return pThis->m_stmBuffered->write(pThis->m_buffer, pThis);
        }

        static int read(asyncState *pState, int n)
        {
            asyncCommand *pThis = (asyncCommand *) pState;

            pThis->set(read_ok);
            return pThis->m_stmBuffered->readLine(REDIS_MAX_LINE, pThis->m_strLine, pThis);
        }

        int setResult(int hr = 0)
        {
            while (m_lists.size())
            {
                int32_t idx = (int32_t)m_lists.size() - 1;
                m_lists[idx]->push(m_val);
                m_counts[idx] --;

                if (m_counts[idx])
                {
                    m_val.clear();
                    set(read);
                    return 0;
                }

                m_val = m_lists[idx];
                m_lists.pop();
                m_counts.pop();

                hr = 0;
            }

            m_retVal = m_val;
            return done(hr);
        }

        static int read_ok(asyncState *pState, int n)
        {
            asyncCommand *pThis = (asyncCommand *) pState;
            char ch = pThis->m_strLine[0];

            if (ch == '+')
            {
                pThis->m_val = pThis->m_strLine.substr(1);
                return pThis->setResult();
            }

            if (ch == '-')
                return Runtime::setError(pThis->m_strLine.substr(1));

            if (ch == ':')
            {
                pThis->m_val = atoi(pThis->m_strLine.c_str() + 1);
                return pThis->setResult();
            }

            if (ch == '$')
            {
                int32_t sz = atoi(pThis->m_strLine.c_str() + 1);

                if (sz < 0)
                {
                    pThis->m_val.setNull();
                    return pThis->setResult(CALL_RETURN_NULL);
                }

                pThis->set(bulk_ok);
                pThis->m_buffer.Release();

                return pThis->m_stmBuffered->read(sz + 2, pThis->m_buffer, pThis);
            }

            if (ch == '*')
            {
                int32_t sz = atoi(pThis->m_strLine.c_str() + 1);

                if (sz < 0)
                {
                    pThis->m_val.setNull();
                    return pThis->setResult(CALL_RETURN_NULL);
                }

                if (sz == 0)
                {
                    pThis->m_val = new List();
                    return pThis->setResult();
                }

                pThis->m_lists.append(new List());
                pThis->m_counts.append(sz);

                pThis->set(read);
                return 0;
            }

            return Runtime::setError("Invalid response.");
        }

        static int bulk_ok(asyncState *pState, int n)
        {
            asyncCommand *pThis = (asyncCommand *) pState;

            if (n == CALL_RETURN_NULL)
                return pThis->setResult(CALL_RETURN_NULL);

            std::string str;

            pThis->m_buffer->toString(str);
            str.resize(str.length() - 2);
            pThis->m_val = str;
            return pThis->setResult();
        }

    protected:
        obj_ptr<Redis> m_pThis;
        std::string &m_req;
        Variant &m_retVal;
        Variant m_val;
        obj_ptr<BufferedStream_base> m_stmBuffered;
        obj_ptr<Buffer_base> m_buffer;
        QuickArray<obj_ptr<List_base> > m_lists;
        QuickArray<int32_t> m_counts;
        std::string m_strLine;
    };

    if (switchToAsync(ac))
        return CHECK_ERROR(CALL_E_NOSYNC);

    return (new asyncCommand(this, req, retVal, ac))->post(0);
}