Skip to content

Commit

Permalink
Merge pull request #17 from segor/dev
Browse files Browse the repository at this point in the history
Merge v2.0.0 changes to master
  • Loading branch information
segor committed Nov 19, 2023
2 parents a862ac7 + a15c72f commit 5c76ea8
Show file tree
Hide file tree
Showing 28 changed files with 796 additions and 707 deletions.
92 changes: 92 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Build and Test

on:
push:
branches: [ master, dev ]
tags: [ 'v*' ]
pull_request:
branches: [ master ]
workflow_dispatch:

env:
DOTNET_CLI_TELEMETRY_OPTOUT: true
DOTNET_GENERATE_ASPNET_CERTIFICATE: false
DOTNET_NOLOGO: true
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION: 1
NUGET_XMLDOC_MODE: skip

Dotnet_Sdk_Version: 8.0.x

jobs:

build-and-test:
name: Build and test

runs-on: windows-latest
env:
Solution_Path: src\SslCertBinding.Net.sln
Nuget_Artefact_Path: src\SslCertBinding.Net\bin\Release\*nupkg

steps:

- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.Dotnet_Sdk_Version }}

- name: Restore NuGet packages
run: dotnet restore "$env:Solution_Path"

- name: Build all targets
run: dotnet build "$env:Solution_Path" -c Release --no-restore

- name: Execute unit tests
run: dotnet test "$env:Solution_Path" -c Release --no-build --no-restore --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage

- name: Upload NuGet artifacts
uses: actions/upload-artifact@v3
with:
name: NuGet-packages
path: ${{ env.Nuget_Artefact_Path }}
if-no-files-found: error

- name: Upload code coverage artifacts
uses: actions/upload-artifact@v3
with:
name: Coverage-results
path: coverage/**/coverage.cobertura.xml
if-no-files-found: error

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

publish-nuget:
name: Publish NuGet packages to NuGet.org
needs: [ build-and-test]
runs-on: ubuntu-latest
if: |
github.event.repository.fork == false &&
startsWith(github.ref, 'refs/tags/')
steps:

- name: Download Nuget packages
uses: actions/download-artifact@v3
with:
name: NuGet-packages

- name: Install .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.Dotnet_Sdk_Version }}

- name: Push NuGet packages to NuGet.org
run: dotnet nuget push "*.nupkg" --api-key ${{ secrets.NUGET_TOKEN }} --skip-duplicate --source https://api.nuget.org/v3/index.json
71 changes: 71 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.

name: "CodeQL"

on:
push:
branches: [ "dev", "master" ]
schedule:
- cron: '22 1 * * 1'

jobs:
analyze:
name: Analyze
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners
# Consider using larger runners for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'csharp' ]


steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
# SslCertBinding.Net
SslCertBinding.Net is a library for .NET Framework and Windows and provides a simple API to add, remove or retrieve bindings between a https port and a SSL certificate.
SslCertBinding.Net is a library for .NET and Windows and provides a simple API to add, remove or retrieve bindings between a https port and a SSL certificate.

This library can be considered as a programmatic alternative to Windows command line tools `netsh http show|add|delete sslcert` or `httpcfg query|set|delete ssl`.

**Important: The library uses the Win32 API and works on the Windows platform only.**

| Branch | Package | Status|
|:--|:--|:--|
| main | [![NuGet](https://buildstats.info/nuget/SslCertBinding.Net?includePreReleases=fase)](https://www.nuget.org/packages/SslCertBinding.Net/ "Download from NuGet.org") | [![Build status - master](https://github.com/segor/SslCertBinding.Net/actions/workflows/build.yaml/badge.svg?branch=master&event=push)](https://github.com/segor/SslCertBinding.Net/actions/workflows/build.yaml?query=branch%3Amaster+event%3Apush) [![CodeQL](https://github.com/segor/SslCertBinding.Net/actions/workflows/codeql.yml/badge.svg?branch=master&event=push)](https://github.com/segor/SslCertBinding.Net/actions/workflows/codeql.yml) |
| dev | [![NuGet](https://buildstats.info/nuget/SslCertBinding.Net?includePreReleases=true)](https://www.nuget.org/packages/SslCertBinding.Net/ "Download from NuGet.org") | [![Build status - dev](https://github.com/segor/SslCertBinding.Net/actions/workflows/build.yaml/badge.svg?branch=dev&event=push)](https://github.com/segor/SslCertBinding.Net/actions/workflows/build.yaml?query=branch%3Adev+event%3Apush) [![CodeQL](https://github.com/segor/SslCertBinding.Net/actions/workflows/codeql.yml/badge.svg?branch=dev&event=push)](https://github.com/segor/SslCertBinding.Net/actions/workflows/codeql.yml) [![codecov](https://codecov.io/gh/segor/SslCertBinding.Net/graph/badge.svg?token=034FB4PVRL)](https://codecov.io/gh/segor/SslCertBinding.Net) |

## Installation
SslCertBinding.Net is available as a [NuGet package](http://www.nuget.org/packages/SslCertBinding.Net/).
```powershell
Install-Package SslCertBinding.Net
To get started, add the [SslCertBinding.Net](https://www.nuget.org/packages/SslCertBinding.Net/) NuGet package to your project by running the following command:

```sh
dotnet add package SslCertBinding.Net
```

## Usage
You can add, update or delete a SSL certificate binding by using the `CertificateBindingConfiguration` class as shown below:
```c#
ICertificateBindingConfiguration config = new CertificateBindingConfiguration();
var config = new CertificateBindingConfiguration();
var ipPort = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 443);
var certificateThumbprint = "372680E4AEC4A57CAE698307347C65D3CE38AF60";
var appId = Guid.Parse("214124cd-d05b-4309-9af9-9caa44b2b74a");

// add a new binding record
config.Bind( new CertificateBinding(
certificateThumbprint, StoreName.My, ipPort, appId)); //returns false
config.Bind( new CertificateBinding(certificateThumbprint, StoreName.My, ipPort, appId) );

// get a binding record
// get the binding record
var certificateBinding = config.Query(ipPort)[0];

// set an option and update the binding record
certificateBinding.Options.DoNotVerifyCertificateRevocation = true;
config.Bind(certificateBinding); //returns true
config.Bind(certificateBinding);

// remove the binding record
config.Delete(ipPort);
Expand All @@ -37,4 +45,4 @@ config.Delete(ipPort);
Cerificates configuration needs elevated permissions. Run Visual Studio as an Administrator before running unit tests.

### I am getting the error "A specified logon session does not exist. It may have already been terminated". How to fix it?
Make sure that you have installed your certificate properly, certificate has a privaite key, your private key store is not broken, etc. Try binding your certificate with `netsh` CLI tool. If you get the same error it should not be a bug in `SslCertBinding.Net`.
Make sure that you have installed your certificate properly, certificate has a private key, your private key store is not broken, etc. Try binding your certificate with `netsh` CLI tool. If you get the same error it should not be a bug in `SslCertBinding.Net`.
30 changes: 20 additions & 10 deletions src/SslCertBinding.Net.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@

namespace SslCertBinding.Net.Sample
{
class Program
#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
static class Program
{
private static void Main(string[] args) {
#if NET5_0_OR_GREATER
if (!OperatingSystem.IsWindows()){
Console.WriteLine("The current OS is not supported!");
return;
}
#endif

var configuration = new CertificateBindingConfiguration();

string command = args.Length > 0 ? args[0].ToLowerInvariant() : string.Empty;
Expand All @@ -34,14 +44,14 @@ class Program
var ipEndPoint = args.Length > 1 ? ParseIpEndPoint(args[1]) : null;
var certificateBindings = configuration.Query(ipEndPoint);
foreach (var info in certificateBindings){
X509Store store;
if (!stores.TryGetValue(info.StoreName, out store)){
store = new X509Store(info.StoreName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
stores.Add(info.StoreName, store);
}
if (!stores.TryGetValue(info.StoreName, out X509Store store))
{
store = new X509Store(info.StoreName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
stores.Add(info.StoreName, store);
}

var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, info.Thumbprint, false)[0];
var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, info.Thumbprint, false)[0];
string certStr = String.Format(
@" IP:port : {2}
Thumbprint : {0}
Expand Down Expand Up @@ -70,8 +80,8 @@ class Program

private static void Bind(string[] args, CertificateBindingConfiguration configuration){
var endPoint = ParseIpEndPoint(args[3]);
var updated = configuration.Bind(new CertificateBinding(args[1], args[2], endPoint, Guid.Parse(args[4])));
Console.WriteLine(updated ? "The binding record has been successfully updated." : "The binding record has been successfully added.");
configuration.Bind(new CertificateBinding(args[1], args[2], endPoint, Guid.Parse(args[4])));
Console.WriteLine("The binding record has been successfully applied.");
}

private static void Delete(string[] args, CertificateBindingConfiguration configuration){
Expand Down
36 changes: 0 additions & 36 deletions src/SslCertBinding.Net.Sample/Properties/AssemblyInfo.cs

This file was deleted.

8 changes: 8 additions & 0 deletions src/SslCertBinding.Net.Sample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"SslCertBinding.Net.Sample": {
"commandName": "Project",
"commandLineArgs": "show"
}
}
}
59 changes: 6 additions & 53 deletions src/SslCertBinding.Net.Sample/SslCertBinding.Net.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,61 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.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')" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5CA507D0-B18C-4758-BD53-6EDE46ABB509}</ProjectGuid>
<TargetFrameworks>net462;net6.0-windows;net6.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SslCertBinding.Net.Sample</RootNamespace>
<AssemblyName>SslCertBinding.Net.Sample</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<ProjectReference Include="..\SslCertBinding.Net\SslCertBinding.Net.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SslCertBinding.Net\SslCertBinding.Net.csproj">
<Project>{e1f46d9b-5b83-460a-a7b1-488cdcdb6633}</Project>
<Name>SslCertBinding.Net</Name>
</ProjectReference>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
</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>
6 changes: 6 additions & 0 deletions src/SslCertBinding.Net.Sample/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>
Loading

0 comments on commit 5c76ea8

Please sign in to comment.