2017-03-27 6 views
1

私はルアで証明書を要求しようとしています。lua https.request with certificate

最近、私はCOMODO SSLを手に入れました。

私はインターネット上で多くのチュートリアルを試しましたが、無駄です。

私はthis blog's proposalは非常に興味深いが見つかりました:

Iは、Linux/OpenWrtの/ Luaの5.1上の要求を実行することができ得ていないのです。

COMODOは、以下のファイルを提供してくれました

  1. AddTrustExternalCARoot.crt
  2. my_domain_com.crt
  3. COMODORSAAddTrustCA.crt
  4. COMODORSADomainValidationSecureServerCA.crt

そして、このブログで彼はこれらのファイルを述べています:

  1. キー= "/root/client.key"
  2. 証明書= "/ルート/ client.crt"、
  3. cafile = "/ルート/ ca.crt"

行う方法私はCOMODOの.crtファイルをブログで言及したものに変換しますか?

Obs:私はカールでダウンロードしようとしましたが、動作しませんでした。

+0

あなたはサーバーで自分自身を認証するために、クライアント側でこの証明書を使用しようとしていますか?これはサーバー証明書なので珍しいようです。誰が接続しようとしているサーバーを制御していますか?トポロジーに関する詳細を記入してください。 –

答えて

0

私はdetails in a blog postについて説明しました。あなたがファイルを.PEMする.CRTを変換する必要がある場合following SO answer may helpその後、

local params = { 
    mode = "client", 
    protocol = "tlsv1", 
    cafile = "/path/to/downloaded/cacert.pem", --<-- added cafile parameters 
    verify = "peer", --<-- changed "none" to "peer" 
    options = "all", 
} 

:基本的に、あなたはssl.wrap通話のためのモードと証明書ファイルを指定する必要があります。私は.crtで試したことはありませんが、私は.pemファイルで作業している例です。

0

私はthis codeとそれを解決する:

module("https", package.seeall) 

local socket = require "socket" 
local http = require "socket.http" 
local ssl = require "ssl" 
local ltn12 = require "ltn12" 

local try = socket.try 
local protect = socket.protect 

local DEFAULT_PROTOCOL = "sslv23" 
local DEFAULT_CAFILE = "/etc/ssl/certs/ca-certificates.crt" 
local DEFAULT_VERIFY = "peer" 
local DEFAULT_OPTIONS = "all" 
local DEFAULT_CIPHERS = "ADH-AES256-SHA:ADH-AES128-SHA:HIGH:MEDIUM" 
local DEFAULT_HTTPS_PORT = 443 

local https_mt = { 
    -- Create proxy functions for each call through the metatable 
    __index = function(tbl, key) 
     local f = function(prxy, ...) 
      local c = prxy.c 
      return c[key](c, ...) 
     end 
     tbl[key] = f -- Save new proxy function in cache for speed 
     return f 
    end 
} 

local function new_create(params) 
    return function() 
     local t = { c = try(socket.tcp()) } 
     function t:connect(host, port) 
      try(self.c:connect(host, port)) 
      self.c = try(ssl.wrap(self.c, params)) 
      try(self.c:dohandshake()) 
      return 1 
     end 
     return setmetatable(t, https_mt) 
    end 
end 

local function request_generic(args) 
    local sslparams = { 
     mode = "client", 
     protocol = args.protocol or DEFAULT_PROTOCOL, 
     cafile = args.cafile or DEFAULT_CAFILE, 
     verify = args.verify or DEFAULT_VERIFY, 
     options = args.options or DEFAULT_OPTIONS, 
     ciphers = args.ciphers or DEFAULT_CIPHERS 
    } 
    local req = { 
     url = args.url, 
     port = args.port or DEFAULT_HTTPS_PORT, 
     sink = args.sink, 
     method = args.method, 
     headers = args.headers, 
     source = args.source, 
     step = args.step, 
     proxy = args.proxy,  -- Buggy? 
     redirect = args.redirect, 
     create = new_create(sslparams) 
    } 
    return http.request(req) 
end 

local function request_simple(url, body) 
    local tbl = { } 
    local req = { 
     url = url, 
     sink = ltn12.sink.table(tbl) 
    } 
    if body then 
     req.method = "POST" 
     req.source = ltn12.source.string(body) 
     req.headers = { 
      ["Content-length"] = #body, 
      ["Content-type"] = "application/x-www-form-urlencoded" 
     } 
    end 
    local _, status, headers = request_generic(req) 
    return table.concat(tbl), status, headers 
end 


function request(req_or_url, body) 
    if type(req_or_url) == "string" then 
     return request_simple(req_or_url, body) 
    else 
     return request_generic(req_or_url) 
    end 
end