我有一台小型的嵌入式Linux设备,具有128 MB闪存,可以用作暂存器。该设备运行NGINX Web服务器。为了进行固件更新-系统通过NGINX接收到加密的二进制文件作为HTTPS POST到暂存器。然后,系统解密文件并刷新另一个QSPI闪存设备以完成更新。

固件二进制文件在设备外部加密,如下所示:

openssl smime -encrypt -binary -aes-256-cbc -in plainfile.zip -out encrypted.zip.enc -outform DER yourSslCertificate.pem


通过NGINX接收到固件二进制文件后,在设备上将其解密,如下所示:

openssl smime -decrypt -binary -in encrypted.zip.enc -inform DER -out decrypted.zip -inkey private.key -passin pass:your_password


我真的很想解密通过NGINX(即时)收到的二进制文件,以便它以解密形式出现在Flash暂存器中。

我一直无法在Google上找到任何可以执行此操作的NGINX模块。我该怎么做?谢谢。

最佳答案

首先,您需要了解一件事。虽然nginx将解密文件-所有其他请求将被阻止。因此,nginx不支持CGI,仅支持FastCGI。

如果可以(例如,nginx仅用于更新目的),则可以使用perl或lua扩展名:http://nginx.org/en/docs/http/ngx_http_perl_module.htmlhttps://github.com/openresty/lua-nginx-module

使用此模块,您可以执行外壳程序。要访问上传的文件,需要设置client_body_in_file_only指令-https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_in_file_only

perl模块示例(未试用):

location /upload {
  client_body_in_file_only clean;
  perl 'sub {
    my $r = shift;
    if ($r->request_body_file) {
       system("openssl smime -decrypt -binary -in ".$r->request_body_file." -inform DER -out /tmp/decrypted.zip -inkey private.key -passin pass:your_password");
    }
  }';
}


但是使用fastcgi更好。您可以使用轻便的fastcgi包装器,例如fcgiwrap https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/

10-08 02:41