Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secret key for messsage not found #41

Closed
knaidoo33 opened this issue Nov 6, 2019 · 1 comment
Closed

Secret key for messsage not found #41

knaidoo33 opened this issue Nov 6, 2019 · 1 comment

Comments

@knaidoo33
Copy link

Hi, I am trying to decrypt a Stream using an Azure function app in which the function is http triggered and the data to be decrypted is passed into req.body.
The passphrase and key are stored in a vault, I have verified that they work using gpg4win, and that the app can access them and they print out correctly.
Can you help me figure out why I am seeing the error "Secret Key for message cannot be found" when the key, passphrase, and data are being passed correctly?

using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using PgpCore;
using System.Threading.Tasks;
using Microsoft.Azure.KeyVault.Models;
using System.Net.Http;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Azure.KeyVault;
using System;
using System.Text;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;

namespace AzureFunctionsPGPDecrypt
{
public static class PGPDecrypt
{
private static readonly HttpClient client = new HttpClient();
private static ConcurrentDictionary<string, string> secrects = new ConcurrentDictionary<string, string>();

    [FunctionName(nameof(PGPDecrypt))]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
    HttpRequest req, ILogger log)
    {
        log.LogInformation($"C# HTTP trigger function {nameof(PGPDecrypt)} processed a request.");

    

        string privateKey =  Environment.GetEnvironmentVariable("secretkey", EnvironmentVariableTarget.Process);
        string passPhrase=  Environment.GetEnvironmentVariable("secretphrase", EnvironmentVariableTarget.Process);

        log.LogInformation($"SecretKey: {privateKey}");
        log.LogInformation($"SecretPassPhrase: {passPhrase}");

        if (privateKey == null)
        {
            return new BadRequestObjectResult("Please pass a private key secret identifier on the query string");
        }


        Stream outputStream = new MemoryStream();
        Stream privateKeyStream = GenerateStreamFromString(privateKey);

        var length = privateKeyStream.Length;
        var length2 = req.Body.Length;

        log.LogInformation($"Length private key stream: {length}");
        log.LogInformation($"Length body: {length2}");

//verifying that the streams were not empty

        using (PGP pgp = new PGP())
        {
            

            using (Stream inputStream = req.Body)
            using(outputStream)
            using (privateKeyStream)
            {
            
                pgp.DecryptStreamAsync(inputStream, outputStream, privateKeyStream, passPhrase);
                
                return new OkObjectResult(outputStream);
            }
        }

    }

    

    private static Stream GenerateStreamFromString(string s)
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(s);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }
}

}

@knaidoo33
Copy link
Author

I figured it out - I needed to find the beginning of the string

inputStream.Seek(0, SeekOrigin.Begin);
privateKeyStream.Seek(0, SeekOrigin.Begin);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant