Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions source/Octostache.Tests/UsageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Octostache.Templates;
using Xunit;

Expand Down Expand Up @@ -581,5 +583,37 @@ public void CanGetIndexes()

absentIndexes.Should().HaveCount(0);
}

[Fact]
public void EncryptText()
{
var certificate = MakeCert();
if (certificate != null)
{
var variableDictionary = new VariableDictionary
{
//TODO: Verify this is how Octopus certificate variables create certificate base64 strings. #{CertificateVariable.Certificate}
["CertificateBase64String"] = Convert.ToBase64String(certificate.RawData),
["Data"] = "Test",
["EncryptedData"] = "#{Data | RSAEncrypt #{CertificateBase64String}}"
};

var encryptedString = variableDictionary.Evaluate("#{EncryptedData}");
encryptedString.Should().NotBeNullOrWhiteSpace();
}
}

static X509Certificate2 MakeCert()
{
#if NETCOREAPP
var rsa = RSA.Create();
var req = new CertificateRequest("cn=test", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return req.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(1));
#else
//TODO: could potentially manually create certificate and include it as an embedded resource to use here.
return null;
#endif

}
}
}
3 changes: 2 additions & 1 deletion source/Octostache/Templates/BuiltInFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ static class BuiltInFunctions
{"substring", TextSubstringFunction.Substring},
{"truncate", TextManipulationFunction.Truncate},
{"trim", TextManipulationFunction.Trim},
{"uripart", TextManipulationFunction.UriPart}
{"uripart", TextManipulationFunction.UriPart},
{"rsaencrypt", TextEncryptFunction.RSAEncrypt }
};

// Configuration should be done at startup, this isn't thread-safe.
Expand Down
55 changes: 55 additions & 0 deletions source/Octostache/Templates/Functions/TextEncryptFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Octostache.Templates.Functions
{
internal class TextEncryptFunction
{
public static string RSAEncrypt(string data, string[] arguments)
{
if (arguments == null || arguments.Length == 0 || arguments.Length > 2)
{
return null;
}

X509Certificate2 certificate = null;

try
{

certificate = new X509Certificate2(Convert.FromBase64String(arguments[0]));
#if NET40
var publicKey = (RSACryptoServiceProvider)certificate.PublicKey.Key;
#else
var publicKey = certificate.GetRSAPublicKey();
#endif
using (publicKey)
{
var encoding = Encoding.GetEncoding(arguments.Length > 1 ? arguments[1] : "utf-8");
var byteData = encoding.GetBytes(data);
#if NET40
var encryptedBytes = publicKey.Encrypt(byteData, true);
#else
var encryptedBytes = publicKey.Encrypt(byteData, RSAEncryptionPadding.OaepSHA1);
#endif
return Convert.ToBase64String(encryptedBytes);
}
}
catch (Exception)
{
}
finally
{
// IDisposable was only added to certificate after.Net 4.6
if (certificate != null && certificate is IDisposable disposableCertificate)
{
disposableCertificate.Dispose();
}
}

return null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be inside the catch statement

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just using the FormatFunction as an example for this. Would it be preferable for me to just allow the exception to bubble up?

}
}
}