-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding functionality to generate self-signed X509 certificates for fi…
…le encryption. Other fixes related to file encryption UI.
- Loading branch information
1 parent
9ef68f9
commit f6ed76b
Showing
15 changed files
with
334 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
namespace SyncPro.Certificates | ||
{ | ||
using System; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
using CERTENROLLLib; | ||
|
||
public static class CertificateHelper | ||
{ | ||
public static X509Certificate2 CreateSelfSignedCertificate(string subjectName) | ||
{ | ||
var distinguishedName = new CX500DistinguishedName(); | ||
distinguishedName.Encode( | ||
"CN=" + subjectName, | ||
X500NameFlags.XCN_CERT_NAME_STR_NONE); | ||
|
||
CCspInformations objCSPs = new CCspInformations(); | ||
CCspInformation objCSP = new CCspInformation(); | ||
|
||
objCSP.InitializeFromName( | ||
"Microsoft Enhanced RSA and AES Cryptographic Provider"); | ||
|
||
objCSPs.Add(objCSP); | ||
|
||
// Build the private key | ||
CX509PrivateKey privateKey = new CX509PrivateKey(); | ||
|
||
privateKey.MachineContext = false; | ||
privateKey.Length = 2048; | ||
privateKey.CspInformations = objCSPs; | ||
privateKey.KeySpec = X509KeySpec.XCN_AT_KEYEXCHANGE; | ||
privateKey.KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_ALL_USAGES; | ||
privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG; | ||
|
||
// Create the private key in the CSP's protected storage | ||
privateKey.Create(); | ||
|
||
// Build the algorithm identifier | ||
var hashobj = new CObjectId(); | ||
hashobj.InitializeFromAlgorithmName( | ||
ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID, | ||
ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY, | ||
AlgorithmFlags.AlgorithmFlagsNone, | ||
"SHA256"); | ||
|
||
// Create the self-signing request from the private key | ||
var certificateRequest = new CX509CertificateRequestCertificate(); | ||
certificateRequest.InitializeFromPrivateKey( | ||
X509CertificateEnrollmentContext.ContextUser, | ||
privateKey, | ||
string.Empty); | ||
|
||
certificateRequest.Subject = distinguishedName; | ||
certificateRequest.Issuer = distinguishedName; | ||
certificateRequest.NotBefore = DateTime.Now.AddDays(-1); | ||
certificateRequest.NotAfter = DateTime.Now.AddYears(100); | ||
certificateRequest.HashAlgorithm = hashobj; | ||
|
||
certificateRequest.Encode(); | ||
|
||
var enrollment = new CX509Enrollment(); | ||
|
||
// Load the certificate request | ||
enrollment.InitializeFromRequest(certificateRequest); | ||
enrollment.CertificateFriendlyName = subjectName; | ||
|
||
// Output the request in base64 and install it back as the response | ||
string csr = enrollment.CreateRequest(); | ||
|
||
// Install the response | ||
enrollment.InstallResponse( | ||
InstallResponseRestrictionFlags.AllowUntrustedCertificate, | ||
csr, | ||
EncodingType.XCN_CRYPT_STRING_BASE64, | ||
string.Empty); | ||
|
||
// Get the new certificate without the private key | ||
byte[] certificateData = Convert.FromBase64String(enrollment.Certificate); | ||
|
||
return new X509Certificate2(certificateData); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("SyncPro.Certificates")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("SyncPro.Certificates")] | ||
[assembly: AssemblyCopyright("Copyright © 2018")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("11febc64-ee05-4095-bd49-9fc7fabcc1df")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{11FEBC64-EE05-4095-BD49-9FC7FABCC1DF}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>SyncPro.Certificates</RootNamespace> | ||
<AssemblyName>SyncPro.Certificates</AssemblyName> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="CertificateHelper.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<COMReference Include="CERTENROLLLib"> | ||
<Guid>{728AB348-217D-11DA-B2A4-000E7BBB2B09}</Guid> | ||
<VersionMajor>1</VersionMajor> | ||
<VersionMinor>0</VersionMinor> | ||
<Lcid>0</Lcid> | ||
<WrapperTool>tlbimp</WrapperTool> | ||
<Isolated>False</Isolated> | ||
<EmbedInteropTypes>True</EmbedInteropTypes> | ||
</COMReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.