Skip to content

Commit

Permalink
Upgrade examples to use ServerCertificateContext - Fix #3981
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone committed Sep 10, 2024
1 parent b1be545 commit a788d27
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 18 deletions.
11 changes: 7 additions & 4 deletions examples/protobuf/GenericHost/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
.Bind(hostContext.Configuration.GetSection("Server"))
.Configure(options =>
{
string certificatePath = Path.Combine(
hostContext.HostingEnvironment.ContentRootPath,
hostContext.Configuration.GetValue<string>("Certificate:File")!);
options.ServerAuthenticationOptions = new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2(
Path.Combine(
hostContext.HostingEnvironment.ContentRootPath,
hostContext.Configuration.GetValue<string>("Certificate:File")!))
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
};
});
Expand Down
6 changes: 6 additions & 0 deletions examples/protobuf/GenericHost/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<ProtoFile Include="../proto/greeter.proto" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.*" />
Expand Down
5 changes: 4 additions & 1 deletion examples/protobuf/Quic/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
using System.Security.Cryptography.X509Certificates;

// Create a server that uses the test server certificate, and the QUIC multiplexed transport.
string certificatePath = "../../../../certs/server.p12";
await using var server = new Server(
new Chatbot(),
new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
},
multiplexedServerTransport: new QuicServerTransport());

Expand Down
6 changes: 6 additions & 0 deletions examples/protobuf/Quic/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<!-- Enable preview features to use the QUIC transport -->
<EnablePreviewFeatures>True</EnablePreviewFeatures>
</PropertyGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<ProtoFile Include="../proto/greeter.proto" />
<Compile Include="../../../common/Program.CancelKeyPressed.cs" Link="Program.CancelKeyPressed.cs" />
Expand Down
5 changes: 4 additions & 1 deletion examples/protobuf/Secure/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
using System.Security.Cryptography.X509Certificates;

// Create the authentication options using the test server certificate.
string certificatePath = "../../../../certs/server.p12";
var serverAuthenticationOptions = new SslServerAuthenticationOptions()
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
};

await using var server = new Server(new Chatbot(), serverAuthenticationOptions);
Expand Down
6 changes: 6 additions & 0 deletions examples/protobuf/Secure/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<!-- Copy the PDBs from the NuGet packages to get file names and line numbers in stack traces. -->
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
</PropertyGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<ProtoFile Include="../proto/greeter.proto" />
<PackageReference Include="IceRpc.Protobuf.Tools" Version="$(IceRpcVersion)" PrivateAssets="All" />
Expand Down
9 changes: 7 additions & 2 deletions examples/protobuf/TcpFallback/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
.Map<IGreeterService>(new Chatbot());

// Create two servers that share the same dispatch pipeline.
string certificatePath = "../../../../certs/server.p12";
await using var quicServer = new Server(
router,
new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
},
multiplexedServerTransport: new QuicServerTransport(),
logger: loggerFactory.CreateLogger<Server>());
Expand All @@ -33,7 +36,9 @@
router,
new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
},
logger: loggerFactory.CreateLogger<Server>());

Expand Down
6 changes: 6 additions & 0 deletions examples/protobuf/TcpFallback/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<!-- Enable preview features to use the QUIC transport -->
<EnablePreviewFeatures>True</EnablePreviewFeatures>
</PropertyGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<ProtoFile Include="../proto/greeter.proto" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.*" />
Expand Down
11 changes: 7 additions & 4 deletions examples/slice/GenericHost/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
.Bind(hostContext.Configuration.GetSection("Server"))
.Configure(options =>
{
string certificatePath = Path.Combine(
hostContext.HostingEnvironment.ContentRootPath,
hostContext.Configuration.GetValue<string>("Certificate:File")!);
options.ServerAuthenticationOptions = new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2(
Path.Combine(
hostContext.HostingEnvironment.ContentRootPath,
hostContext.Configuration.GetValue<string>("Certificate:File")!))
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
};
});
Expand Down
6 changes: 6 additions & 0 deletions examples/slice/GenericHost/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<SliceFile Include="../slice/Greeter.slice" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.*" />
Expand Down
8 changes: 5 additions & 3 deletions examples/slice/Quic/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
using System.Security.Cryptography.X509Certificates;

// Create a server that uses the test server certificate, and the QUIC multiplexed transport.
string certificatePath = "../../../../certs/server.p12";
await using var server = new Server(
new Chatbot(),
new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
},
multiplexedServerTransport: new QuicServerTransport());
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
});

server.Listen();

Expand Down
6 changes: 6 additions & 0 deletions examples/slice/Quic/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<!-- Enable preview features to use the QUIC transport -->
<EnablePreviewFeatures>True</EnablePreviewFeatures>
</PropertyGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<SliceFile Include="../slice/Greeter.slice" />
<Compile Include="../../../common/Program.CancelKeyPressed.cs" Link="Program.CancelKeyPressed.cs" />
Expand Down
5 changes: 4 additions & 1 deletion examples/slice/Secure/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
using System.Security.Cryptography.X509Certificates;

// Create the authentication options using the test server certificate.
string certificatePath = "../../../../certs/server.p12";
var serverAuthenticationOptions = new SslServerAuthenticationOptions()
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
};

await using var server = new Server(new Chatbot(), serverAuthenticationOptions);
Expand Down
6 changes: 6 additions & 0 deletions examples/slice/Secure/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<!-- Copy the PDBs from the NuGet packages to get file names and line numbers in stack traces. -->
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
</PropertyGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<SliceFile Include="../slice/Greeter.slice" />
<PackageReference Include="IceRpc.Slice.Tools" Version="$(IceRpcVersion)" PrivateAssets="All" />
Expand Down
9 changes: 7 additions & 2 deletions examples/slice/TcpFallback/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
.Map<IGreeterService>(new Chatbot());

// Create two servers that share the same dispatch pipeline.
string certificatePath = "../../../../certs/server.p12";
await using var quicServer = new Server(
router,
new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
},
multiplexedServerTransport: new QuicServerTransport(),
logger: loggerFactory.CreateLogger<Server>());
Expand All @@ -33,7 +36,9 @@
router,
new SslServerAuthenticationOptions
{
ServerCertificate = new X509Certificate2("../../../../certs/server.p12")
ServerCertificateContext = SslStreamCertificateContext.Create(
X509CertificateLoader.LoadPkcs12FromFile(certificatePath, password: null),
X509CertificateLoader.LoadPkcs12CollectionFromFile(certificatePath, password: null))
},
logger: loggerFactory.CreateLogger<Server>());

Expand Down
6 changes: 6 additions & 0 deletions examples/slice/TcpFallback/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<!-- Enable preview features to use the QUIC transport -->
<EnablePreviewFeatures>True</EnablePreviewFeatures>
</PropertyGroup>

<!-- Required for X509CertificateLoader with .NET 8-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Bcl.Cryptography" Version="9.0.0-preview.*" />
</ItemGroup>

<ItemGroup>
<SliceFile Include="../slice/Greeter.slice" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.*" />
Expand Down

0 comments on commit a788d27

Please sign in to comment.