Skip to content

Commit aa0a9ac

Browse files
committed
new draft
1 parent 6db4a56 commit aa0a9ac

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

Diff for: src/Agent.Listener/Configuration.Windows/RSAEncryptedFileKeyManager.cs

+21-10
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ public void DeleteKey()
132132
}
133133
}
134134

135-
public RSA GetKey()
135+
public RSA GetKey(bool useLegacyRsaImpl)
136136
{
137-
return GetKeyFromFile();
137+
return GetKeyFromFile(useLegacyRsaImpl);
138138
}
139139

140-
private RSA GetKeyFromNamedContainer()
140+
private RSA GetKeyFromNamedContainer(bool useLegacyRsaImpl)
141141
{
142142
if (!File.Exists(_keyFile))
143143
{
@@ -151,7 +151,7 @@ private RSA GetKeyFromNamedContainer()
151151
if (string.IsNullOrEmpty(result.containerName))
152152
{
153153
// we should not get here. GetKeyFromNamedContainer is only called from GetKeyFromFile when result.containerName is not empty
154-
return GetKeyFromFile();
154+
return GetKeyFromFile(useLegacyRsaImpl);
155155
}
156156

157157
if (result.useCng)
@@ -170,13 +170,24 @@ private RSA GetKeyFromNamedContainer()
170170
Trace.Info("Using RSACryptoServiceProvider");
171171
CspParameters Params = new CspParameters();
172172
Params.KeyContainerName = result.containerName;
173-
Params.Flags |= CspProviderFlags.UseNonExportableKey | CspProviderFlags.UseMachineKeyStore;
174-
var rsa = new RSACryptoServiceProvider(Params);
175-
return rsa;
173+
if (useLegacyRsaImpl)
174+
{
175+
Params.Flags |= CspProviderFlags.UseNonExportableKey | CspProviderFlags.UseMachineKeyStore;
176+
var rsa = new RSACryptoServiceProvider(Params);
177+
return rsa;
178+
}
179+
else
180+
{
181+
Params.Flags |= CspProviderFlags.UseMachineKeyStore;
182+
using (var csp = new RSACryptoServiceProvider(Params))
183+
{
184+
return RSA.Create(csp.ExportParameters(includePrivateParameters: true));
185+
}
186+
}
176187
}
177188
}
178189

179-
private RSA GetKeyFromFile()
190+
private RSA GetKeyFromFile(bool useLegacyRsaImpl)
180191
{
181192
if (!File.Exists(_keyFile))
182193
{
@@ -190,10 +201,10 @@ private RSA GetKeyFromFile()
190201
if(!string.IsNullOrEmpty(result.containerName))
191202
{
192203
Trace.Info("Keyfile has ContainerName, reading from NamedContainer");
193-
return GetKeyFromNamedContainer();
204+
return GetKeyFromNamedContainer(useLegacyRsaImpl);
194205
}
195206

196-
var rsa = new RSACryptoServiceProvider();
207+
var rsa = useLegacyRsaImpl ? new RSACryptoServiceProvider() : RSA.Create();
197208
rsa.ImportParameters(result.rsaParameters);
198209
return rsa;
199210
}

Diff for: src/Agent.Listener/Configuration/IRSAKeyManager.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ public interface IRSAKeyManager : IAgentService
3535
void DeleteKey();
3636

3737
/// <summary>
38-
/// Gets the <c>RSACryptoServiceProvider</c> instance currently stored by the key manager.
38+
/// Gets the <c>RSA</c> instance currently stored by the key manager.
3939
/// </summary>
40-
/// <returns>An <c>RSACryptoServiceProvider</c> instance representing the key for the agent</returns>
40+
/// <param name="useLegacyRsaImpl">Use RSACryptoServiceProvider as the underlying implementation.</param>
41+
/// <returns>An <c>RSA</c> implementation representing the key for the agent</returns>
4142
/// <exception cref="CryptographicException">No key exists in the store</exception>
42-
RSA GetKey();
43+
RSA GetKey(bool useLegacyRsaImpl);
4344
}
4445

4546
public static class IRSAKeyManagerExtensions

Diff for: src/Agent.Listener/Configuration/OAuthCredential.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override VssCredentials GetVssCredentials(IHostContext context)
4242
// We expect the key to be in the machine store at this point. Configuration should have set all of
4343
// this up correctly so we can use the key to generate access tokens.
4444
var keyManager = context.GetService<IRSAKeyManager>();
45-
var signingCredentials = VssSigningCredentials.Create(() => keyManager.GetKey());
45+
var signingCredentials = VssSigningCredentials.Create(() => keyManager.GetKey(useLegacyRsaImpl: true)); // RSACryptoServiceProvider is fine for signatures
4646
var clientCredential = new VssOAuthJwtBearerClientCredential(clientId, authorizationUrl, signingCredentials);
4747
var agentCredential = new VssOAuthCredential(new Uri(oathEndpointUrl, UriKind.Absolute), VssOAuthGrant.ClientCredentials, clientCredential);
4848

Diff for: src/Agent.Listener/Configuration/RSAFileKeyManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void DeleteKey()
7070
}
7171
}
7272

73-
public RSA GetKey()
73+
public RSA GetKey(bool useLegacyRsaImpl)
7474
{
7575
if (!File.Exists(_keyFile))
7676
{
@@ -80,7 +80,7 @@ public RSA GetKey()
8080
Trace.Info("Loading RSA key parameters from file {0}", _keyFile);
8181

8282
var parameters = IOUtil.LoadObject<RSAParametersSerializable>(_keyFile).RSAParameters;
83-
var rsa = new RSACryptoServiceProvider();
83+
var rsa = useLegacyRsaImpl ? new RSACryptoServiceProvider() : RSA.Create();
8484
rsa.ImportParameters(parameters);
8585
return rsa;
8686
}

Diff for: src/Agent.Listener/MessageListener.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public async Task<Boolean> CreateSessionAsync(CancellationToken token)
9595
await _agentServer.ConnectAsync(new Uri(serverUrl), creds);
9696
Trace.Info("VssConnection created");
9797

98+
taskAgentSession.AgentCanHandleOaepSHA256 = true;
9899
_session = await _agentServer.CreateAgentSessionAsync(
99100
_settings.PoolId,
100101
taskAgentSession,
@@ -336,9 +337,10 @@ private ICryptoTransform GetMessageDecryptor(
336337
{
337338
// The agent session encryption key uses the AES symmetric algorithm
338339
var keyManager = HostContext.GetService<IRSAKeyManager>();
339-
using (var rsa = keyManager.GetKey())
340+
RSAEncryptionPadding rsaPadding = _session.EncryptionKey.EncryptionPadding == "OaepSHA256" ? RSAEncryptionPadding.OaepSHA256 : RSAEncryptionPadding.OaepSHA1;
341+
using (var rsa = keyManager.GetKey(useLegacyRsaImpl: rsaPadding == RSAEncryptionPadding.OaepSHA1))
340342
{
341-
return aes.CreateDecryptor(rsa.Decrypt(_session.EncryptionKey.Value, RSAEncryptionPadding.OaepSHA1), message.IV);
343+
return aes.CreateDecryptor(rsa.Decrypt(_session.EncryptionKey.Value, rsaPadding), message.IV);
342344
}
343345
}
344346
else

0 commit comments

Comments
 (0)