コード例 #1
0
ファイル: cipher.c プロジェクト: world100/11111
static LUA_FUNCTION(openssl_evp_BytesToKey)
{
  EVP_CIPHER* c = CHECK_OBJECT(1, EVP_CIPHER, "openssl.evp_cipher");
  size_t lsalt, lk;
  const char* k = luaL_checklstring(L, 2, &lk);
  const char* salt = luaL_optlstring(L, 3, NULL, &lsalt);
  const EVP_MD* m = lua_isnoneornil(L, 4) ? EVP_get_digestbyname("sha1") : get_digest(L, 4);
  char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  int ret;
  if (salt != NULL && lsalt < PKCS5_SALT_LEN)
  {
    lua_pushfstring(L, "salt must not shorter than %d", PKCS5_SALT_LEN);
    luaL_argerror(L, 3, lua_tostring(L, -1));
  }

  ret = EVP_BytesToKey(c, m, (unsigned char*)salt, (unsigned char*)k, lk, 1, (unsigned char*)key, (unsigned char*)iv);
  if (ret > 1)
  {
    lua_pushlstring(L, key, EVP_CIPHER_key_length(c));
    lua_pushlstring(L, iv, EVP_CIPHER_iv_length(c));
    return 2;
  }
  return openssl_pushresult(L, ret);
}
コード例 #2
0
ファイル: lstrlib.c プロジェクト: derekmarcotte/freebsd
static int str_rep (lua_State *L) {
  size_t l, lsep;
  const char *s = luaL_checklstring(L, 1, &l);
  lua_Integer n = luaL_checkinteger(L, 2);
  const char *sep = luaL_optlstring(L, 3, "", &lsep);
  if (n <= 0) lua_pushliteral(L, "");
  else if (l + lsep < l || l + lsep > MAXSIZE / n)  /* may overflow? */
    return luaL_error(L, "resulting string too large");
  else {
    size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep;
    luaL_Buffer b;
    char *p = luaL_buffinitsize(L, &b, totallen);
    while (n-- > 1) {  /* first n-1 copies (followed by separator) */
      memcpy(p, s, l * sizeof(char)); p += l;
      if (lsep > 0) {  /* empty 'memcpy' is not that cheap */
        memcpy(p, sep, lsep * sizeof(char));
        p += lsep;
      }
    }
    memcpy(p, s, l * sizeof(char));  /* last copy (not followed by separator) */
    luaL_pushresultsize(&b, totallen);
  }
  return 1;
}
コード例 #3
0
const char* LuaState::OptString(int numArg, const char* def)
{
	return luaL_optlstring(m_state, numArg, def, NULL);
}
コード例 #4
0
const char* LuaState::OptLString(int numArg, const char *def, size_t* len)
{
	return luaL_optlstring(m_state, numArg, def, len);
}
コード例 #5
0
ファイル: pkey.c プロジェクト: houzhenggang/luajit-android
static LUA_FUNCTION(openssl_pkey_export)
{
  EVP_PKEY * key;
  int ispriv = 0;
  int exraw = 0;
  int expem = 1;
  size_t passphrase_len = 0;
  BIO * bio_out = NULL;
  int ret = 0;
  const EVP_CIPHER * cipher;
  const char * passphrase = NULL;

  key = CHECK_OBJECT(1, EVP_PKEY, "openssl.evp_pkey");
  ispriv = openssl_pkey_is_private(key);

  if (!lua_isnoneornil(L, 2))
    expem = lua_toboolean(L, 2);

  if (expem)
  {
    if (!lua_isnoneornil(L, 3))
      exraw = lua_toboolean(L, 3);
    passphrase = luaL_optlstring(L, 4, NULL, &passphrase_len);
  } else
  {
    passphrase = luaL_optlstring(L, 3, NULL, &passphrase_len);
  }

  if (passphrase)
  {
    cipher = (EVP_CIPHER *) EVP_des_ede3_cbc();
  }
  else
  {
    cipher = NULL;
  }

  bio_out = BIO_new(BIO_s_mem());
  if (expem)
  {
    if (exraw==0)
    {
      ret = ispriv ?
        PEM_write_bio_PrivateKey(bio_out, key, cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL) :
        PEM_write_bio_PUBKEY(bio_out, key);
    }
    else
    {
      /* export raw key format */
      switch (EVP_PKEY_type(key->type))
      {
      case EVP_PKEY_RSA:
      case EVP_PKEY_RSA2:
        ret = ispriv ? PEM_write_bio_RSAPrivateKey(bio_out, key->pkey.rsa, cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL)
          : PEM_write_bio_RSAPublicKey(bio_out, key->pkey.rsa);
      break;
      case EVP_PKEY_DSA:
      case EVP_PKEY_DSA2:
      case EVP_PKEY_DSA3:
      case EVP_PKEY_DSA4:
      {
        ret = ispriv ? PEM_write_bio_DSAPrivateKey(bio_out, key->pkey.dsa, cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL)
          : PEM_write_bio_DSA_PUBKEY(bio_out, key->pkey.dsa);
      }
      break;
      case EVP_PKEY_DH:
        ret = PEM_write_bio_DHparams(bio_out, key->pkey.dh);
      break;
#ifndef OPENSSL_NO_EC
      case EVP_PKEY_EC:
        ret = ispriv ? PEM_write_bio_ECPrivateKey(bio_out, key->pkey.ec, cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL)
        : PEM_write_bio_EC_PUBKEY(bio_out, key->pkey.ec);
      break;
#endif
      default:
      ret = 0;
      break;
      }
    }
  }
  else
  {
    if (ispriv)
    {
      if (passphrase == NULL)
      {
        ret = i2d_PrivateKey_bio(bio_out, key);
      } else
      {
        ret = i2d_PKCS8PrivateKey_bio(bio_out, key, cipher, (char *)passphrase, passphrase_len, NULL, NULL);
      }
    } else
    {
      int l;
      l = i2d_PublicKey(key, NULL);
      if (l > 0)
      {
        unsigned char* p = malloc(l);
        unsigned char* pp = p;
        l = i2d_PublicKey(key, &pp);
        if (l > 0)
        {
          BIO_write(bio_out, p, l);
          ret = 1;
        } else
          ret = 0;
        free(p);
      } else
        ret = 0;
    }
  }

  
  if (ret)
  {
    char * bio_mem_ptr;
    long bio_mem_len;

    bio_mem_len = BIO_get_mem_data(bio_out, &bio_mem_ptr);

    lua_pushlstring(L, bio_mem_ptr, bio_mem_len);
    ret  = 1;
  }

  if (bio_out)
  {
    BIO_free(bio_out);
  }
  return ret;
}
コード例 #6
0
ファイル: pkey.c プロジェクト: houzhenggang/luajit-android
static LUA_FUNCTION(openssl_pkey_new)
{
  EVP_PKEY *pkey = NULL;
  const char* alg = "rsa";

  if (lua_isnoneornil(L, 1) || lua_isstring(L, 1))
  {
    alg = luaL_optstring(L, 1, alg);

    if (strcasecmp(alg, "rsa") == 0)
    {
      int bits = luaL_optint(L, 2, 1024);
      int e = luaL_optint(L, 3, 65537);
      RSA* rsa = RSA_new();

      BIGNUM *E = BN_new();
      BN_set_word(E, e);
      if (RSA_generate_key_ex(rsa, bits, E, NULL))
      {
        pkey = EVP_PKEY_new();
        EVP_PKEY_assign_RSA(pkey, rsa);
      }
      else
        RSA_free(rsa);
      BN_free(E);
    }
    else if (strcasecmp(alg, "dsa") == 0)
    {
      int bits = luaL_optint(L, 2, 1024);
      size_t seed_len = 0;
      const char* seed = luaL_optlstring(L, 3, NULL, &seed_len);

      DSA *dsa = DSA_new();
      if (DSA_generate_parameters_ex(dsa, bits, (byte*)seed, seed_len, NULL, NULL, NULL)
          && DSA_generate_key(dsa))
      {
        pkey = EVP_PKEY_new();
        EVP_PKEY_assign_DSA(pkey, dsa);
      }
      else
        DSA_free(dsa);
    }
    else if (strcasecmp(alg, "dh") == 0)
    {
      int bits = luaL_optint(L, 2, 512);
      int generator = luaL_optint(L, 3, 2);

      DH* dh = DH_new();
      if (DH_generate_parameters_ex(dh, bits, generator, NULL))
      {
        if (DH_generate_key(dh))
        {
          pkey = EVP_PKEY_new();
          EVP_PKEY_assign_DH(pkey, dh);
        }
        else
          DH_free(dh);
      }
      else
        DH_free(dh);
    }
#ifndef OPENSSL_NO_EC
    else if (strcasecmp(alg, "ec") == 0)
    {
      EC_KEY *ec = NULL;
      EC_GROUP *group = openssl_get_ec_group(L, 2, 3, 4);
      if (!group)
        luaL_error(L, "failed to get ec_group object");
      ec = EC_KEY_new();
      if (ec)
      {
        EC_KEY_set_group(ec, group);
        EC_GROUP_free(group);
        if (EC_KEY_generate_key(ec))
        {
          pkey = EVP_PKEY_new();
          EVP_PKEY_assign_EC_KEY(pkey, ec);
        }
        else
          EC_KEY_free(ec);
      }
      else
        EC_GROUP_free(group);

    }
#endif
    else
    {
      luaL_error(L, "not support %s!!!!", alg);
    }
  }
  else if (lua_istable(L, 1))
  {
    lua_getfield(L, 1, "alg");
    alg = luaL_optstring(L, -1, alg);
    lua_pop(L, 1);
    if (strcasecmp(alg, "rsa") == 0)
    {
      pkey = EVP_PKEY_new();
      if (pkey)
      {
        RSA *rsa = RSA_new();
        if (rsa)
        {
          OPENSSL_PKEY_SET_BN(1, rsa, n);
          OPENSSL_PKEY_SET_BN(1, rsa, e);
          OPENSSL_PKEY_SET_BN(1, rsa, d);
          OPENSSL_PKEY_SET_BN(1, rsa, p);
          OPENSSL_PKEY_SET_BN(1, rsa, q);
          OPENSSL_PKEY_SET_BN(1, rsa, dmp1);
          OPENSSL_PKEY_SET_BN(1, rsa, dmq1);
          OPENSSL_PKEY_SET_BN(1, rsa, iqmp);
          if (rsa->n)
          {
            if (!EVP_PKEY_assign_RSA(pkey, rsa))
            {
              EVP_PKEY_free(pkey);
              pkey = NULL;
            }
          }
        }
      }
    }
    else if (strcasecmp(alg, "dsa") == 0)
    {
      pkey = EVP_PKEY_new();
      if (pkey)
      {
        DSA *dsa = DSA_new();
        if (dsa)
        {
          OPENSSL_PKEY_SET_BN(-1, dsa, p);
          OPENSSL_PKEY_SET_BN(-1, dsa, q);
          OPENSSL_PKEY_SET_BN(-1, dsa, g);
          OPENSSL_PKEY_SET_BN(-1, dsa, priv_key);
          OPENSSL_PKEY_SET_BN(-1, dsa, pub_key);
          if (dsa->p && dsa->q && dsa->g)
          {
            if (!dsa->priv_key && !dsa->pub_key)
            {
              DSA_generate_key(dsa);
            }
            if (!EVP_PKEY_assign_DSA(pkey, dsa))
            {
              EVP_PKEY_free(pkey);
              pkey = NULL;
            }
          }
        }
      }
    }
    else if (strcasecmp(alg, "dh") == 0)
    {

      pkey = EVP_PKEY_new();
      if (pkey)
      {
        DH *dh = DH_new();
        if (dh)
        {
          OPENSSL_PKEY_SET_BN(-1, dh, p);
          OPENSSL_PKEY_SET_BN(-1, dh, g);
          OPENSSL_PKEY_SET_BN(-1, dh, priv_key);
          OPENSSL_PKEY_SET_BN(-1, dh, pub_key);
          if (dh->p && dh->g)
          {
            if (!dh->pub_key)
            {
              DH_generate_key(dh);
            }
            if (!EVP_PKEY_assign_DH(pkey, dh))
            {
              EVP_PKEY_free(pkey);
              pkey = NULL;
            }
          }
        }
      }
    }
    else if (strcasecmp(alg, "ec") == 0)
    {
      BIGNUM *d = NULL;
      BIGNUM *x = NULL;
      BIGNUM *y = NULL;
      BIGNUM *z = NULL;
      EC_GROUP *group = NULL;

      lua_getfield(L, -1, "ec_name");
      lua_getfield(L, -2, "param_enc");
      lua_getfield(L, -3, "conv_form");
      group = openssl_get_ec_group(L, -3, -2, -1);
      lua_pop(L, 3);
      if (!group)
      {
        luaL_error(L, "get openssl.ec_group fail");
      }

      EC_GET_FIELD(d);
      EC_GET_FIELD(x);
      EC_GET_FIELD(y);
      EC_GET_FIELD(z);


      pkey = EVP_PKEY_new();
      if (pkey)
      {
        EC_KEY *ec = EC_KEY_new();
        if (ec)
        {
          EC_KEY_set_group(ec, group);
          if (d)
            EC_KEY_set_private_key(ec, d);
          if (x != NULL && y != NULL)
          {
            EC_POINT *pnt = EC_POINT_new(group);
            if (z == NULL)
              EC_POINT_set_affine_coordinates_GFp(group, pnt, x, y, NULL);
            else
              EC_POINT_set_Jprojective_coordinates_GFp(group, pnt, x, y, z, NULL);

            EC_KEY_set_public_key(ec, pnt);
          }

          if (!EVP_PKEY_assign_EC_KEY(pkey, ec))
          {
            EC_KEY_free(ec);
            EVP_PKEY_free(pkey);
            pkey = NULL;
          }
          if (d && !EC_KEY_check_key(ec))
          {
            EC_KEY_generate_key_part(ec);
          }
        }
      }
    }
  }

  if (pkey)
  {
    PUSH_OBJECT(pkey, "openssl.evp_pkey");
    return 1;
  }
  return 0;

}
コード例 #7
0
ファイル: bind.c プロジェクト: BeanGu/LuCI
/**
 * connect()/bind() shortcut
 */
static int nixio__bind_connect(lua_State *L, int do_bind) {
	const char *host = NULL;
	if (!lua_isnoneornil(L, 1)) {
		host = luaL_checklstring(L, 1, NULL);
	}
	const char *port = luaL_checklstring(L, 2, NULL);
	const char *family = luaL_optlstring(L, 3, "any", NULL);
	const char *socktype = luaL_optlstring(L, 4, "stream", NULL);

	struct addrinfo hints, *result, *rp;
	memset(&hints, 0, sizeof(hints));

	if (!strcmp(family, "any")) {
		hints.ai_family = AF_UNSPEC;
	} else if (!strcmp(family, "inet")) {
		hints.ai_family = AF_INET;
	} else if (!strcmp(family, "inet6")) {
		hints.ai_family = AF_INET6;
	} else {
		return luaL_argerror(L, 3, "supported values: any, inet, inet6");
	}

	if (!strcmp(socktype, "any")) {
		hints.ai_socktype = 0;
	} else if (!strcmp(socktype, "stream")) {
		hints.ai_socktype = SOCK_STREAM;
	} else if (!strcmp(socktype, "dgram")) {
		hints.ai_socktype = SOCK_DGRAM;
	} else {
		return luaL_argerror(L, 4, "supported values: any, stream, dgram");
	}

	if (do_bind) {
		hints.ai_flags |= AI_PASSIVE;
	}

	hints.ai_protocol = 0;

	int aistat = getaddrinfo(host, port, &hints, &result);
	if (aistat) {
		lua_pushnil(L);
		lua_pushinteger(L, aistat);
		lua_pushstring(L, gai_strerror(aistat));
		return 3;
	}

	/* create socket object */
	nixio_sock *sock = lua_newuserdata(L, sizeof(nixio_sock));
	int status = -1, clstat;

	for (rp = result; rp != NULL; rp = rp->ai_next) {
		sock->fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
		if (sock->fd == -1) {
			continue;
		}

		if (do_bind) {
			int one = 1;
			setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR,
			 (char*)&one, sizeof(one));
			status = bind(sock->fd, rp->ai_addr, rp->ai_addrlen);
		} else {
			do {
				status = connect(sock->fd, rp->ai_addr, rp->ai_addrlen);
			} while (status == -1 && errno == EINTR);
		}

		/* on success */
		if (!status) {
			sock->domain = rp->ai_family;
			sock->type = rp->ai_socktype;
			sock->protocol = rp->ai_protocol;
			break;
		}

		do {
#ifndef __WINNT__
			clstat = close(sock->fd);
#else
			clstat = closesocket(sock->fd);
#endif
		} while (clstat == -1 && errno == EINTR);
	}

	freeaddrinfo(result);

	/* on failure */
	if (status) {
		return nixio__perror_s(L);
	}

	luaL_getmetatable(L, NIXIO_META);
	lua_setmetatable(L, -2);

	return 1;
}
コード例 #8
0
ファイル: menu.c プロジェクト: mdombroski/libui-lua
static int new_menu( lua_State* L )
{
	void* m = uiNewMenu( luaL_optlstring( L, 1, "Menu", NULL ) );
	object_create( L, m, uiMenuSignature, _menu_functions, 0 );
	return 1;
}
コード例 #9
0
ファイル: ngx_lua_http.c プロジェクト: alemic/ngx_lua_module
static ngx_int_t
ngx_lua_http_parse_args(lua_State *l, ngx_lua_thread_t *thr,
    ngx_lua_http_ctx_t *ctx)
{
    int            top;
    char          *errstr;
    u_char        *p, *last;
    ngx_str_t      str;
    ngx_keyval_t  *header;

    ngx_log_debug0(NGX_LOG_DEBUG_CORE, thr->log, 0, "lua http parse args");

    if (!lua_istable(l, 1)) {
        return luaL_error(l, "invalid the first argument, must be a table");
    }

    top = lua_gettop(l);

    lua_getfield(l, 1, "method");
    str.data = (u_char *) luaL_optlstring(l, -1, "GET", &str.len);

    ctx->method.len = str.len;
    ctx->method.data = ngx_pstrdup(ctx->pool, &str);
    if (ctx->method.data == NULL) {
        errstr = "ngx_pstrdup() failed";
        goto error;
    }

    lua_getfield(l, 1, "version");
    str.data = (u_char *) luaL_optlstring(l, -1, "1.1", &str.len);

    ctx->version.len = str.len;
    ctx->version.data = ngx_pstrdup(ctx->pool, &str);
    if (ctx->version.data == NULL) {
        errstr = "ngx_pstrdup() failed";
        goto error;
    }

    lua_getfield(l, 1, "url");
    str.data = (u_char *) luaL_checklstring(l, -1, &str.len);

    ctx->u.url.len = str.len;
    ctx->u.url.data = ngx_pstrdup(ctx->pool, &str);
    if (ctx->u.url.data == NULL) {
        errstr = "ngx_pstrdup() failed";
        goto error;
    }

    lua_getfield(l, 1, "headers");
    if (!lua_isnil(l, -1)) {
        if (!lua_istable(l, -1)) {
            return luaL_error(l,
                              "invalid value of the argument \"headers\""
                              ", must be a table");
        }

        lua_pushnil(l);

        while (lua_next(l, -2)) {
            header = ngx_array_push(&ctx->headers);
            if (header == NULL) {
                errstr = "ngx_array_push() failed";
                goto error;
            }

            str.data = (u_char *) luaL_checklstring(l, -2, &str.len);

            header->key.len = str.len;
            header->key.data = ngx_pstrdup(ctx->pool, &str);

            header->key.data[0] = ngx_toupper(header->key.data[0]);

            for (p = header->key.data, last = p + header->key.len;
                 p < last - 1;
                 p++)
            {
                if (*p == '_') {
                    *p = '-';

                    p[1] = ngx_toupper(p[1]);
                }
            }

            str.data = (u_char *) luaL_checklstring(l, -1, &str.len);

            header->value.len = str.len;
            header->value.data = ngx_pstrdup(ctx->pool, &str);

            lua_pop(l, 1);
        }
    }

    lua_getfield(l, 1, "body");
    str.data = (u_char *) luaL_optlstring(l, -1, "", &str.len);

    ctx->body.len = str.len;
    ctx->body.data = ngx_pstrdup(ctx->pool, &str);
    if (ctx->body.data == NULL) {
        errstr = "ngx_pstrdup() failed";
        goto error;
    }

    lua_settop(l, top);

    ctx->connect_timeout = (ngx_msec_t) luaL_optnumber(l, 2, 60000);
    ctx->send_timeout = (ngx_msec_t) luaL_optnumber(l, 3, 60000);
    ctx->read_timeout = (ngx_msec_t) luaL_optnumber(l, 4, 60000);

    return NGX_OK;

error:

    lua_settop(l, top);
    lua_pushboolean(l, 0);
    lua_pushstring(l, errstr);

    return NGX_ERROR;
}
コード例 #10
0
ファイル: controls.c プロジェクト: mdombroski/libui-lua
static int new_group( lua_State* L )
{
	uiGroup* g = uiNewGroup( luaL_optlstring( L, 1, "Label", NULL ) );
	object_create( L, g, uiGroupSignature, control_common, group_functions, 0 );
	return 1;
}
コード例 #11
0
ファイル: controls.c プロジェクト: mdombroski/libui-lua
static int new_label( lua_State* L )
{
	uiLabel* l = uiNewLabel( luaL_optlstring( L, 1, "Label", NULL ) );
	object_create( L, l, uiLabelSignature, control_common, label_functions, 0 );
	return 1;
}
コード例 #12
0
ファイル: controls.c プロジェクト: mdombroski/libui-lua
static int new_checkbox( lua_State* L )
{
	uiCheckbox* c = uiNewCheckbox( luaL_optlstring( L, 1, "Checkbox", NULL ) );
	object_create( L, c, uiCheckboxSignature, control_common, checkbox_functions, 0 );
	return 1;
}
コード例 #13
0
ファイル: cipher.c プロジェクト: world100/11111
static LUA_FUNCTION(openssl_evp_encrypt)
{
  const EVP_CIPHER* cipher = NULL;
  if (lua_istable(L, 1))
  {
    if (lua_getmetatable(L, 1) && lua_equal(L, 1, -1))
    {
      lua_pop(L, 1);
      lua_remove(L, 1);
    }
    else
      luaL_error(L, "call function with invalid state");
  }

  cipher = get_cipher(L, 1, NULL);
  if (cipher)
  {
    size_t input_len = 0;
    const char *input = luaL_checklstring(L, 2, &input_len);
    size_t key_len = 0;
    const char *key = luaL_optlstring(L, 3, NULL, &key_len); /* can be NULL */
    size_t iv_len = 0;
    const char *iv = luaL_optlstring(L, 4, NULL, &iv_len);   /* can be NULL */
    int pad = lua_isnoneornil(L, 5) ? 1 : lua_toboolean(L, 5);
    ENGINE *e = lua_isnoneornil(L, 6) ? NULL : CHECK_OBJECT(6, ENGINE, "openssl.engine");

    EVP_CIPHER_CTX *c = EVP_CIPHER_CTX_new();

    int output_len = 0;
    int len = 0;
    char *buffer = NULL;
    char evp_key[EVP_MAX_KEY_LENGTH] = {0};
    char evp_iv[EVP_MAX_IV_LENGTH] = {0};
    int ret = 0;

    if (key)
    {
      key_len = EVP_MAX_KEY_LENGTH > key_len ? key_len : EVP_MAX_KEY_LENGTH;
      memcpy(evp_key, key, key_len);
    }
    if (iv_len > 0 && iv)
    {
      iv_len = EVP_MAX_IV_LENGTH > iv_len ? iv_len : EVP_MAX_IV_LENGTH;
      memcpy(evp_iv, iv, iv_len);
    }

    EVP_CIPHER_CTX_init(c);
    ret = EVP_EncryptInit_ex(c, cipher, e, (const byte*)evp_key, iv_len > 0 ? (const byte*)evp_iv : NULL);
    if (ret == 1)
    {
      ret = EVP_CIPHER_CTX_set_padding(c, pad);
      if (ret == 1)
      {
        buffer = OPENSSL_malloc(input_len + EVP_CIPHER_CTX_block_size(c));
        ret = EVP_EncryptUpdate(c, (byte*) buffer, &len, (const byte*)input, input_len);
        if ( ret == 1 )
        {
          output_len += len;
          ret = EVP_EncryptFinal(c, (byte*)buffer + len, &len);
          if (ret == 1)
          {
            output_len += len;
            lua_pushlstring(L,  buffer, output_len);
          }
        }
        OPENSSL_free(buffer);
      }
    }
    EVP_CIPHER_CTX_cleanup(c);
    EVP_CIPHER_CTX_free(c);
    return (ret == 1) ? ret : openssl_pushresult(L, ret);
  }
  else
    luaL_error(L, "argument #1 is not a valid cipher algorithm or openssl.evp_cipher object");
  return 0;
}
コード例 #14
0
ファイル: luautil.c プロジェクト: Aerobota/chdkptp
const char *lu_table_optlstring(lua_State *L, int narg, const char *fname, const char *d, size_t *l) {
	lua_getfield(L, narg, fname);
	const char *r = luaL_optlstring(L,-1,d,l);
	lua_pop(L,1);
	return r;
}
コード例 #15
0
ファイル: Lua.hpp プロジェクト: fu7mu4/entonetics
 const std::string L_optstring( int index, const std::string& def )
 {
    return luaL_optlstring( m_lua.get(), index, def.c_str(), NULL );
 }
コード例 #16
0
	const char* LuaState::CheckString(int index, const char* defValue, std::size_t* length) const
	{
		return luaL_optlstring(m_state, index, defValue, length);
	}
コード例 #17
0
ファイル: sockopt.c プロジェクト: 1558204459/luci
/**
 * get/setsockopt() helper
 */
static int nixio__getsetsockopt(lua_State *L, int set) {
	nixio_sock *sock = nixio__checksock(L);
	const char *level = luaL_optlstring(L, 2, "", NULL);
	const char *option = luaL_optlstring(L, 3, "", NULL);
	set = (set) ? 4 : 0;

	if (!strcmp(level, "socket")) {
		if (!strcmp(option, "keepalive")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_KEEPALIVE, set);
		} else if (!strcmp(option, "reuseaddr")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_REUSEADDR, set);
		} else if (!strcmp(option, "rcvbuf")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_RCVBUF, set);
		} else if (!strcmp(option, "sndbuf")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_SNDBUF, set);
		} else if (!strcmp(option, "priority")) {
#ifdef SO_PRIORITY
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_PRIORITY, set);
#else
			return nixio__pstatus(L, !(errno = ENOPROTOOPT));
#endif
		} else if (!strcmp(option, "broadcast")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_BROADCAST, set);
		} else if (!strcmp(option, "dontroute")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_DONTROUTE, set);
		} else if (!strcmp(option, "error")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_ERROR, set);
		} else if (!strcmp(option, "oobinline")) {
			return nixio__gso_int(L, sock->fd, SOL_SOCKET, SO_OOBINLINE, set);
		} else if (!strcmp(option, "linger")) {
			return nixio__gso_ling(L, sock->fd, SOL_SOCKET, SO_LINGER, set);
		} else if (!strcmp(option, "sndtimeo")) {
			return nixio__gso_timev(L, sock->fd, SOL_SOCKET, SO_SNDTIMEO, set);
		} else if (!strcmp(option, "rcvtimeo")) {
			return nixio__gso_timev(L, sock->fd, SOL_SOCKET, SO_RCVTIMEO, set);
		} else if (!strcmp(option, "bindtodevice")) {
#ifdef SO_BINDTODEVICE
			return nixio__gso_b(L, sock->fd, SOL_SOCKET, SO_BINDTODEVICE, set);
#else
			return nixio__pstatus(L, !(errno = ENOPROTOOPT));
#endif
		} else {
			return luaL_argerror(L, 3, "supported values: keepalive, reuseaddr,"
			 " sndbuf, rcvbuf, priority, broadcast, linger, sndtimeo, rcvtimeo,"
			 " dontroute, bindtodevice, error, oobinline"
			);
		}
	} else if (!strcmp(level, "tcp")) {
		if (!strcmp(option, "cork")) {
#ifdef TCP_CORK
			return nixio__gso_int(L, sock->fd, IPPROTO_TCP, TCP_CORK, set);
#else
			return nixio__pstatus(L, !(errno = ENOPROTOOPT));
#endif
		} else if (!strcmp(option, "nodelay")) {
			return nixio__gso_int(L, sock->fd, IPPROTO_TCP, TCP_NODELAY, set);
		} else {
			return luaL_argerror(L, 3, "supported values: cork, nodelay");
		}
	} else if (!strcmp(level, "ip")) {
		if (!strcmp(option, "mtu")) {
#ifdef IP_MTU
			return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_MTU, set);
#else
			return nixio__pstatus(L, !(errno = ENOPROTOOPT));
#endif
		} else if (!strcmp(option, "hdrincl")) {
			return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_HDRINCL,
					set);
		} else if (!strcmp(option, "multicast_loop")) {
			return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
					set);
		} else if (!strcmp(option, "multicast_ttl")) {
			return nixio__gso_int(L, sock->fd, IPPROTO_IP, IP_MULTICAST_TTL,
					set);
		} else if (!strcmp(option, "multicast_if")) {
			return nixio__gso_mreq4(L, sock->fd, IPPROTO_IP, IP_MULTICAST_IF,
					set);
		} else if (!strcmp(option, "add_membership")) {
			return nixio__gso_mreq4(L, sock->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
					set);
		} else if (!strcmp(option, "drop_membership")) {
			return nixio__gso_mreq4(L, sock->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
					set);
		} else {
			return luaL_argerror(L, 3,
				"supported values: hdrincl, mtu, multicast_loop, "
				"multicast_ttl, multicast_if, add_membership, drop_membership");
		}
	} else if (!strcmp(level, "ipv6")) {
		if (!strcmp(option, "mtu")) {
#ifdef IPV6_MTU
			return nixio__gso_int(L, sock->fd, IPPROTO_IPV6, IPV6_MTU, set);
#else
			return nixio__pstatus(L, !(errno = ENOPROTOOPT));
#endif
		} else if (!strcmp(option, "v6only")) {
#ifdef IPV6_V6ONLY
			return nixio__gso_int(L, sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, set);
#else
			return nixio__pstatus(L, !(errno = ENOPROTOOPT));
#endif
		} else if (!strcmp(option, "multicast_loop")) {
			return nixio__gso_int(L, sock->fd, IPPROTO_IPV6,
					IPV6_MULTICAST_LOOP, set);
		} else if (!strcmp(option, "multicast_hops")) {
			return nixio__gso_int(L, sock->fd, IPPROTO_IPV6,
					IPV6_MULTICAST_HOPS, set);
		} else if (!strcmp(option, "multicast_if")) {
			return nixio__gso_mreq6(L, sock->fd, IPPROTO_IPV6,
					IPV6_MULTICAST_IF, set);
		} else if (!strcmp(option, "add_membership")) {
			return nixio__gso_mreq6(L, sock->fd, IPPROTO_IPV6,
					IPV6_ADD_MEMBERSHIP, set);
		} else if (!strcmp(option, "drop_membership")) {
			return nixio__gso_mreq6(L, sock->fd, IPPROTO_IPV6,
					IPV6_DROP_MEMBERSHIP, set);
		} else {
			return luaL_argerror(L, 3,
				"supported values: v6only, mtu, multicast_loop, multicast_hops,"
				" multicast_if, add_membership, drop_membership");
		}
	} else {
		return luaL_argerror(L, 2, "supported values: socket, tcp, ip, ipv6");
	}
}