AWS Lambda(Linux内核版本 - 4.4.51-40.60.amzn1.x86_64)在Interop.Crypto.Pkcs12Parse

时间:2017-05-09 11:22:58

标签: amazon-web-services .net-core aws-lambda

当我从窗口运行控制台应用程序时,它可以正常工作

using System.Security.Cryptography.X509Certificates;    
public static void Main(string[] args)
{
    Console.WriteLine("File exists: " + File.Exists("key.p12"));
    var certificate = new X509Certificate2("key.p12", "notasecret");
    Console.WriteLine("Success");
    Console.ReadLine();
}

但是当我在AWS lambda中尝试抛出错误时

.ctor: MissingMethodException
at Interop.Crypto.Pkcs12Parse(SafePkcs12Handle p12, String pass, SafeEvpPKeyHandle& pkey, SafeX509Handle& cert, SafeX509StackHandle& ca)
at Internal.Cryptography.Pal.OpenSslPkcs12Reader.Decrypt(String password)
at Internal.Cryptography.Pal.PkcsFormatReader.TryReadPkcs12(OpenSslPkcs12Reader pfx, String password, Boolean single, ICertificatePal& readPal, List`1& readCerts)
at Internal.Cryptography.Pal.PkcsFormatReader.TryReadPkcs12(SafeBioHandle bio, String password, Boolean single, ICertificatePal& readPal, List`1& readCerts)
at Internal.Cryptography.Pal.CertificatePal.FromBio(SafeBioHandle bio, String password)
at Internal.Cryptography.Pal.CertificatePal.FromFile(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
at test_openssl_lambda.Functions.TriggerSync() in /tmp/src818283224/src/test-openssl-lambda/test_openssl_lambda/Function.cs:line 21
at lambda_method(Closure , Stream , Stream , ContextInfo )

下面是我的project.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": false
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },

    "Amazon.Lambda.Core": "1.0.0",
    "Amazon.Lambda.APIGatewayEvents": "1.1.0",
    "Amazon.Lambda.Serialization.Json": "1.1.0",
    "Amazon.Lambda.Tools": {
      "type": "build",
      "version": "1.5.0"
    },

    "System.Security.Cryptography.X509Certificates": "4.3.0"
  },

  "tools": {
    "Amazon.Lambda.Tools": "1.5.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

调用Function.cs(与上面的控制台应用程序相同)

using System.Security.Cryptography.X509Certificates;
using Amazon.Lambda.Core;

[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public void Trigger()
{
    Console.WriteLine("File exists: " + File.Exists("Key.p12"));
    var certificate = new X509Certificate2("Key.p12", "notasecret");
    Console.WriteLine("Success");
}

它抛出异常,因为它无法在AWS Lambda环境中执行DllImport System.Security.Cryptography.Native.OpenSsl(Linux内核版本 - 4.4.51-40.60.amzn1.x86_64) 有人可以帮我解决问题吗?即,帮助我在AWS Lambda中安装System.Security.Cryptography.Native.OpenSsl?

2 个答案:

答案 0 :(得分:2)

当底层机器没有安装所需的OpenSSL版本先决条件时会发生此异常,这似乎是AWS Lambda的情况。 见https://github.com/dotnet/corefx/issues/14356

答案 1 :(得分:0)

现在,我将我的项目更改为.Net Core 2.1及其工作方式。