/*** add attribute to x509_req object @function attribute @tparam x509_attribute attribute attribute to add @treturn boolean result */ static LUA_FUNCTION(openssl_csr_attribute) { X509_REQ *csr = CHECK_OBJECT(1, X509_REQ, "openssl.x509_req"); if (auxiliar_getclassudata(L, "openssl.x509_attribute", 2)) { X509_ATTRIBUTE *attr = CHECK_OBJECT(2, X509_ATTRIBUTE, "openssl.x509_attribute"); int ret = X509_REQ_add1_attr(csr, attr); return openssl_pushresult(L, ret); } else if (lua_isnumber(L, 2)) { int loc = luaL_checkint(L, 2); X509_ATTRIBUTE *attr = NULL; if (lua_isnone(L, 3)) { attr = X509_REQ_get_attr(csr, loc); attr = X509_ATTRIBUTE_dup(attr); } else if (lua_isnil(L, 3)) { attr = X509_REQ_delete_attr(csr, loc); } if (attr) { PUSH_OBJECT(attr, "openssl.x509_attribute"); } else lua_pushnil(L); return 1; } else if (lua_istable(L, 2)) { int i; int ret = 1; int n = lua_rawlen(L, 2); for (i = 1; ret == 1 && i <= n; i++) { X509_ATTRIBUTE *attr; lua_rawgeti(L, 2, i); attr = NULL; if (lua_istable(L, -1)) { attr = openssl_new_xattribute(L, &attr, -1, NULL); ret = X509_REQ_add1_attr(csr, attr); X509_ATTRIBUTE_free(attr); } else { attr = CHECK_OBJECT(-1, X509_ATTRIBUTE, "openssl.x509_attribute"); ret = X509_REQ_add1_attr(csr, attr); } lua_pop(L, 1); } openssl_pushresult(L, ret); return 1; } return 0; }
static LUA_FUNCTION(openssl_csr_attribute) { X509_REQ *csr = CHECK_OBJECT(1, X509_REQ, "openssl.x509_req"); if (auxiliar_isclass(L, "openssl.x509_attribute", 2)) { X509_ATTRIBUTE *attr = CHECK_OBJECT(2, X509_ATTRIBUTE, "openssl.x509_attribute"); int ret = X509_REQ_add1_attr(csr, attr); return openssl_pushresult(L, ret); } else if (lua_isnumber(L, 2)) { int loc = luaL_checkint(L, 2); X509_ATTRIBUTE *attr = NULL; if (lua_isnone(L, 3)) { attr = X509_REQ_get_attr(csr, loc); attr = X509_ATTRIBUTE_dup(attr); } else if (lua_isnil(L, 3)) { attr = X509_REQ_delete_attr(csr, loc); } if (attr) { PUSH_OBJECT(attr, "openssl.x509_attribute"); } else lua_pushnil(L); return 1; } return 0; }
static LUA_FUNCTION(openssl_csr_new) { X509_REQ *csr = X509_REQ_new(); int i; int n = lua_gettop(L); int ret = X509_REQ_set_version(csr, 0L); for (i = 1; ret == 1 && i <= n; i++) { luaL_argcheck(L, auxiliar_isclass(L, "openssl.stack_of_x509_extension", i) || auxiliar_isclass(L, "openssl.stack_of_x509_attribute", i) || auxiliar_isclass(L, "openssl.x509_name", i) || auxiliar_isclass(L, "openssl.evp_pkey", i), i, "must be x509_name, stack_of_x509_extension or stack_of_x509_attribute"); if (auxiliar_isclass(L, "openssl.x509_name", i)) { X509_NAME * subject = CHECK_OBJECT(i, X509_NAME, "openssl.x509_name"); ret = X509_REQ_set_subject_name(csr, subject); } if (auxiliar_isclass(L, "openssl.stack_of_x509_attribute", i)) { int j, m; STACK_OF(X509_ATTRIBUTE) *attrs = CHECK_OBJECT(i, STACK_OF(X509_ATTRIBUTE), "openssl.stack_of_x509_attribute"); m = sk_X509_ATTRIBUTE_num(attrs); for (j = 0; ret == 1 && j < m; j++) { ret = X509_REQ_add1_attr(csr, sk_X509_ATTRIBUTE_value(attrs, j)); } } if (auxiliar_isclass(L, "openssl.stack_of_x509_extension", i)) { STACK_OF(X509_EXTENSION) *exts = CHECK_OBJECT(i, STACK_OF(X509_EXTENSION), "openssl.stack_of_x509_extension"); ret = X509_REQ_add_extensions(csr, exts); } if (auxiliar_isclass(L, "openssl.evp_pkey", i)) { EVP_PKEY *pkey; const EVP_MD *md; luaL_argcheck(L, i == n || i == n - 1, i, "must is evp_pkey object"); pkey = CHECK_OBJECT(i, EVP_PKEY, "openssl.evp_pkey"); luaL_argcheck(L, openssl_pkey_is_private(pkey), i, "must be private key"); if (i == n - 1) md = get_digest(L, n); else md = EVP_get_digestbyname("sha1"); ret = X509_REQ_set_pubkey(csr, pkey); if (ret == 1) { ret = X509_REQ_sign(csr, pkey, md); if (ret > 0) ret = 1; } break; } }; if (ret == 1) PUSH_OBJECT(csr, "openssl.x509_req"); else { X509_REQ_free(csr); return openssl_pushresult(L, ret); } return 1; }