Skip to content

Commit

Permalink
commit - set up gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Grohme committed Nov 23, 2024
1 parent bcb3d54 commit ce7994d
Show file tree
Hide file tree
Showing 20 changed files with 1,030 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

/kms_signer/bin
/kms_signer/obj
*.exe
/lambda_c2pasign/obj
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# c2pa aws lambda sign demo with aws kms

37 changes: 37 additions & 0 deletions kms_signer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Amazon.KeyManagementService;

MemoryStream input = new MemoryStream();

try
{
using (Stream stdin = Console.OpenStandardInput())
{
byte[] buffer = new byte[2048];
int bytes;
while ((bytes = stdin.Read(buffer, 0, buffer.Length)) > 0)
{
input.Write(buffer, 0, bytes);
}
}
var client = new AmazonKeyManagementServiceClient();

var signResponse = await client.SignAsync(new Amazon.KeyManagementService.Model.SignRequest()
{
KeyId = "<put in here your KMS KeyId>",
MessageType = MessageType.RAW,
SigningAlgorithm = SigningAlgorithmSpec.ECDSA_SHA_256,
Message = input
});

MemoryStream output = new System.IO.MemoryStream();

signResponse.Signature.CopyTo(output);

output.Position = 0;

output.CopyTo(Console.OpenStandardOutput());


}
catch (System.Exception e)
{ Console.WriteLine(e.Message); }
19 changes: 19 additions & 0 deletions kms_signer/kms_signer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.KeyManagementService" Version="3.7.400.53" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /y &quot;$(TargetDir)*.*&quot; &quot;$(SolutionDir)\lambda_c2pasign\c2pa\&quot;" />
</Target>

</Project>
31 changes: 31 additions & 0 deletions lambda_c2pasign.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34616.47
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "lambda_c2pasign", "lambda_c2pasign\lambda_c2pasign.csproj", "{4756DFF9-F3AD-47DC-9251-C9D72594B232}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "kms_signer", "kms_signer\kms_signer.csproj", "{6945C589-A61A-4758-A0FD-4FA247D26129}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4756DFF9-F3AD-47DC-9251-C9D72594B232}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4756DFF9-F3AD-47DC-9251-C9D72594B232}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4756DFF9-F3AD-47DC-9251-C9D72594B232}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4756DFF9-F3AD-47DC-9251-C9D72594B232}.Release|Any CPU.Build.0 = Release|Any CPU
{6945C589-A61A-4758-A0FD-4FA247D26129}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6945C589-A61A-4758-A0FD-4FA247D26129}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6945C589-A61A-4758-A0FD-4FA247D26129}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6945C589-A61A-4758-A0FD-4FA247D26129}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4101F73E-8132-4AC8-ABB9-79F156DA5D9B}
EndGlobalSection
EndGlobal
147 changes: 147 additions & 0 deletions lambda_c2pasign/Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using Amazon.Lambda.Core;
using Amazon.Lambda.S3Events;
using Amazon.S3.Model;
using Amazon.S3;
using System.Net;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Amazon.Lambda.Serialization;
using ThirdParty.Json.LitJson;
using Amazon.Runtime.Internal;
using Amazon.Runtime.Internal.Util;
using System.Web;



using Amazon.S3.Util;
using System.Text.Json;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace c2panalyze2;

public class Function
{
private readonly IAmazonS3 _s3Client;

public Function()

{

_s3Client = new AmazonS3Client();

}

public async Task<string> FunctionHandlerSign(S3Event evnt, ILambdaContext context)

{

var s3Event = evnt.Records?.FirstOrDefault();

if (s3Event == null)
{
return "No S3 event detected.";
}

string s3BucketPathSigned = "data_sign";

try
{
s3BucketPathSigned = Environment.GetEnvironmentVariable("s3BucketPathSigned").TrimStart('/');
}
catch
{
}

string s3BucketPath = "data";

try
{
s3BucketPath = Environment.GetEnvironmentVariable("s3BucketPath").TrimStart('/');
}
catch
{
}



string bucketName = s3Event.S3.Bucket.Name;

string fileName = s3Event.S3.Object.Key;

Console.WriteLine("s3BucketPath " + s3BucketPath);
Console.WriteLine("s3BucketPathSigned " + s3BucketPathSigned);
Console.WriteLine("bucketName " + bucketName);
Console.WriteLine("fileName " + fileName);

string extension = System.IO.Path.GetExtension(fileName);

string _outputDirectory = "/tmp/" + fileName.Replace(extension, "");

string _tmpFilename = "/tmp/" + fileName;

string _tmpFilenameSigned = "/tmp/" + fileName.Replace(extension, "") + "_signed" + extension;

Console.WriteLine("_tmpFilename " + _tmpFilename);

Console.WriteLine("_tmpFilenameSigned " + _tmpFilenameSigned);

Console.WriteLine("_outputDirectory " + _outputDirectory);


try

{

Console.WriteLine("get file");
var getRequest = new GetObjectRequest

{
BucketName = bucketName,
Key = fileName

};
var response = _s3Client.GetObjectAsync(getRequest).GetAwaiter().GetResult();
response.WriteResponseStreamToFileAsync(_tmpFilename, false, new CancellationTokenSource().Token).GetAwaiter().GetResult();

}
catch (Exception e)
{
Console.WriteLine("get File failed " + e.Message + "@" + e.StackTrace);
}


try
{

processC2PA run3 = new processC2PA(_tmpFilename, _outputDirectory);

string result3 = run3.runSign(_tmpFilenameSigned);

Console.WriteLine("runSign Result " + result3);

s3Load s3Loader1 = new s3Load("", "", "eu-central-1");

List<string> _ingredientFiles1 = new List<string>();
_ingredientFiles1.Add(_tmpFilenameSigned);

Console.WriteLine("Upload file Sign " + _tmpFilenameSigned);

string s3result1 = s3Loader1.putS3Files(_ingredientFiles1, bucketName, s3BucketPathSigned).GetAwaiter().GetResult();

Console.WriteLine("putS3Files Result " + s3result1);

File.Delete(_tmpFilenameSigned);
File.Delete(_tmpFilename);
}
catch (Exception e)
{
Console.WriteLine("RunSign or Upload failed Error " + e.Message + "@" + e.StackTrace);
}


return "ok";
}


}
10 changes: 10 additions & 0 deletions lambda_c2pasign/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Mock Lambda Test Tool": {
"commandName": "Executable",
"commandLineArgs": "--port 5050",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-8.0.exe"
}
}
}
Binary file added lambda_c2pasign/c2pa/AWSSDK.Core.dll
Binary file not shown.
Binary file not shown.
Binary file added lambda_c2pasign/c2pa/c2patool
Binary file not shown.
Binary file added lambda_c2pasign/c2pa/kms_signer
Binary file not shown.
59 changes: 59 additions & 0 deletions lambda_c2pasign/c2pa/kms_signer.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"kms_signer/1.0.0": {
"dependencies": {
"AWSSDK.KeyManagementService": "3.7.400.53"
},
"runtime": {
"kms_signer.dll": {}
}
},
"AWSSDK.Core/3.7.400.53": {
"runtime": {
"lib/net8.0/AWSSDK.Core.dll": {
"assemblyVersion": "3.3.0.0",
"fileVersion": "3.7.400.53"
}
}
},
"AWSSDK.KeyManagementService/3.7.400.53": {
"dependencies": {
"AWSSDK.Core": "3.7.400.53"
},
"runtime": {
"lib/net8.0/AWSSDK.KeyManagementService.dll": {
"assemblyVersion": "3.3.0.0",
"fileVersion": "3.7.400.53"
}
}
}
}
},
"libraries": {
"kms_signer/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"AWSSDK.Core/3.7.400.53": {
"type": "package",
"serviceable": true,
"sha512": "sha512-OwJTHfD3tXKLPiB/UUr6SSigFycQm6JNTReh5j2kYJP0CgcZmV+qeePEpIptxCVbq28esef/8hdzezkzNhN+fA==",
"path": "awssdk.core/3.7.400.53",
"hashPath": "awssdk.core.3.7.400.53.nupkg.sha512"
},
"AWSSDK.KeyManagementService/3.7.400.53": {
"type": "package",
"serviceable": true,
"sha512": "sha512-RyBv5ZgUAzvLkF685BIx/ey+0KNY7IrFNImQdggqYVm0zyS0nOLu+2sV7Kg3bF6QT7X1tybVNKYhnz07ET5DVQ==",
"path": "awssdk.keymanagementservice/3.7.400.53",
"hashPath": "awssdk.keymanagementservice.3.7.400.53.nupkg.sha512"
}
}
}
Binary file added lambda_c2pasign/c2pa/kms_signer.dll
Binary file not shown.
Binary file added lambda_c2pasign/c2pa/kms_signer.pdb
Binary file not shown.
12 changes: 12 additions & 0 deletions lambda_c2pasign/c2pa/kms_signer.runtimeconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
31 changes: 31 additions & 0 deletions lambda_c2pasign/certs/es256_certs.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIChzCCAi6gAwIBAgIUcCTmJHYF8dZfG0d1UdT6/LXtkeYwCgYIKoZIzj0EAwIw
gYwxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTESMBAGA1UEBwwJU29tZXdoZXJl
MScwJQYDVQQKDB5DMlBBIFRlc3QgSW50ZXJtZWRpYXRlIFJvb3QgQ0ExGTAXBgNV
BAsMEEZPUiBURVNUSU5HX09OTFkxGDAWBgNVBAMMD0ludGVybWVkaWF0ZSBDQTAe
Fw0yMjA2MTAxODQ2NDBaFw0zMDA4MjYxODQ2NDBaMIGAMQswCQYDVQQGEwJVUzEL
MAkGA1UECAwCQ0ExEjAQBgNVBAcMCVNvbWV3aGVyZTEfMB0GA1UECgwWQzJQQSBU
ZXN0IFNpZ25pbmcgQ2VydDEZMBcGA1UECwwQRk9SIFRFU1RJTkdfT05MWTEUMBIG
A1UEAwwLQzJQQSBTaWduZXIwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQPaL6R
kAkYkKU4+IryBSYxJM3h77sFiMrbvbI8fG7w2Bbl9otNG/cch3DAw5rGAPV7NWky
l3QGuV/wt0MrAPDoo3gwdjAMBgNVHRMBAf8EAjAAMBYGA1UdJQEB/wQMMAoGCCsG
AQUFBwMEMA4GA1UdDwEB/wQEAwIGwDAdBgNVHQ4EFgQUFznP0y83joiNOCedQkxT
tAMyNcowHwYDVR0jBBgwFoAUDnyNcma/osnlAJTvtW6A4rYOL2swCgYIKoZIzj0E
AwIDRwAwRAIgOY/2szXjslg/MyJFZ2y7OH8giPYTsvS7UPRP9GI9NgICIDQPMKrE
LQUJEtipZ0TqvI/4mieoyRCeIiQtyuS0LACz
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIICajCCAg+gAwIBAgIUfXDXHH+6GtA2QEBX2IvJ2YnGMnUwCgYIKoZIzj0EAwIw
dzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRIwEAYDVQQHDAlTb21ld2hlcmUx
GjAYBgNVBAoMEUMyUEEgVGVzdCBSb290IENBMRkwFwYDVQQLDBBGT1IgVEVTVElO
R19PTkxZMRAwDgYDVQQDDAdSb290IENBMB4XDTIyMDYxMDE4NDY0MFoXDTMwMDgy
NzE4NDY0MFowgYwxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTESMBAGA1UEBwwJ
U29tZXdoZXJlMScwJQYDVQQKDB5DMlBBIFRlc3QgSW50ZXJtZWRpYXRlIFJvb3Qg
Q0ExGTAXBgNVBAsMEEZPUiBURVNUSU5HX09OTFkxGDAWBgNVBAMMD0ludGVybWVk
aWF0ZSBDQTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHllI4O7a0EkpTYAWfPM
D6Rnfk9iqhEmCQKMOR6J47Rvh2GGjUw4CS+aLT89ySukPTnzGsMQ4jK9d3V4Aq4Q
LsOjYzBhMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgGGMB0GA1UdDgQW
BBQOfI1yZr+iyeUAlO+1boDitg4vazAfBgNVHSMEGDAWgBRembiG4Xgb2VcVWnUA
UrYpDsuojDAKBggqhkjOPQQDAgNJADBGAiEAtdZ3+05CzFo90fWeZ4woeJcNQC4B
84Ill3YeZVvR8ZECIQDVRdha1xEDKuNTAManY0zthSosfXcvLnZui1A/y/DYeg==
-----END CERTIFICATE-----
Loading

0 comments on commit ce7994d

Please sign in to comment.