|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.Diagnostics.Monitoring.TestCommon; |
| 5 | +using Microsoft.IdentityModel.Tokens; |
| 6 | +using System; |
| 7 | +using System.IdentityModel.Tokens.Jwt; |
| 8 | +using System.Security.Cryptography; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Text.Json.Serialization; |
| 11 | + |
| 12 | +namespace Microsoft.Diagnostics.Monitoring.Tool.FunctionalTests |
| 13 | +{ |
| 14 | + internal sealed class ApiKeySignInfo |
| 15 | + { |
| 16 | + public readonly JwtHeader Header; |
| 17 | + public readonly string PublicKeyEncoded; |
| 18 | + public readonly string PrivateKeyEncoded; |
| 19 | + |
| 20 | + private ApiKeySignInfo(JwtHeader header, string publicKeyEncoded, string privateKeyEncoded) |
| 21 | + { |
| 22 | + Header = header; |
| 23 | + PublicKeyEncoded = publicKeyEncoded; |
| 24 | + PrivateKeyEncoded = privateKeyEncoded; |
| 25 | + } |
| 26 | + |
| 27 | + public static ApiKeySignInfo Create(string algorithmName) |
| 28 | + { |
| 29 | + SigningCredentials signingCreds; |
| 30 | + JsonWebKey exportableJwk; |
| 31 | + JsonWebKey privateJwk; |
| 32 | + switch (algorithmName) |
| 33 | + { |
| 34 | + case SecurityAlgorithms.EcdsaSha256: |
| 35 | + case SecurityAlgorithms.EcdsaSha256Signature: |
| 36 | + case SecurityAlgorithms.EcdsaSha384: |
| 37 | + case SecurityAlgorithms.EcdsaSha384Signature: |
| 38 | + case SecurityAlgorithms.EcdsaSha512: |
| 39 | + case SecurityAlgorithms.EcdsaSha512Signature: |
| 40 | + ECDsa ecDsa = ECDsa.Create(GetEcCurveFromName(algorithmName)); |
| 41 | + ECDsaSecurityKey ecSecKey = new ECDsaSecurityKey(ecDsa); |
| 42 | + signingCreds = new SigningCredentials(ecSecKey, algorithmName); |
| 43 | + ECDsa pubEcDsa = ECDsa.Create(ecDsa.ExportParameters(false)); |
| 44 | + ECDsaSecurityKey pubEcSecKey = new ECDsaSecurityKey(pubEcDsa); |
| 45 | + exportableJwk = JsonWebKeyConverter.ConvertFromECDsaSecurityKey(pubEcSecKey); |
| 46 | + privateJwk = JsonWebKeyConverter.ConvertFromECDsaSecurityKey(ecSecKey); |
| 47 | + break; |
| 48 | + |
| 49 | + case SecurityAlgorithms.RsaSha256: |
| 50 | + case SecurityAlgorithms.RsaSha256Signature: |
| 51 | + case SecurityAlgorithms.RsaSha384: |
| 52 | + case SecurityAlgorithms.RsaSha384Signature: |
| 53 | + case SecurityAlgorithms.RsaSha512: |
| 54 | + case SecurityAlgorithms.RsaSha512Signature: |
| 55 | + RSA rsa = RSA.Create(GetRsaKeyLengthFromName(algorithmName)); |
| 56 | + RsaSecurityKey rsaSecKey = new RsaSecurityKey(rsa); |
| 57 | + signingCreds = new SigningCredentials(rsaSecKey, algorithmName); |
| 58 | + RSA pubRsa = RSA.Create(rsa.ExportParameters(false)); // lgtm[cs/weak-asymmetric-algorithm] Intentional testing rejection of weak algorithm |
| 59 | + RsaSecurityKey pubRsaSecKey = new RsaSecurityKey(pubRsa); |
| 60 | + exportableJwk = JsonWebKeyConverter.ConvertFromRSASecurityKey(pubRsaSecKey); |
| 61 | + privateJwk = JsonWebKeyConverter.ConvertFromRSASecurityKey(rsaSecKey); |
| 62 | + break; |
| 63 | + |
| 64 | + case SecurityAlgorithms.HmacSha256: |
| 65 | + case SecurityAlgorithms.HmacSha384: |
| 66 | + case SecurityAlgorithms.HmacSha512: |
| 67 | + HMAC hmac = GetHmacAlgorithmFromName(algorithmName); |
| 68 | + SymmetricSecurityKey hmacSecKey = new SymmetricSecurityKey(hmac.Key); |
| 69 | + signingCreds = new SigningCredentials(hmacSecKey, algorithmName); |
| 70 | + exportableJwk = JsonWebKeyConverter.ConvertFromSymmetricSecurityKey(hmacSecKey); |
| 71 | + privateJwk = JsonWebKeyConverter.ConvertFromSymmetricSecurityKey(hmacSecKey); |
| 72 | + break; |
| 73 | + |
| 74 | + default: |
| 75 | + throw new ArgumentException($"Algorithm name '{algorithmName}' not supported", nameof(algorithmName)); |
| 76 | + } |
| 77 | + |
| 78 | + JsonSerializerOptions serializerOptions = JsonSerializerOptionsFactory.Create(JsonIgnoreCondition.WhenWritingNull); |
| 79 | + |
| 80 | + string publicKeyJson = JsonSerializer.Serialize(exportableJwk, serializerOptions); |
| 81 | + string publicKeyEncoded = Base64UrlEncoder.Encode(publicKeyJson); |
| 82 | + |
| 83 | + string privateKeyJson = JsonSerializer.Serialize(privateJwk, serializerOptions); |
| 84 | + string privateKeyEncoded = Base64UrlEncoder.Encode(privateKeyJson); |
| 85 | + |
| 86 | + JwtHeader newHeader = new JwtHeader(signingCreds, null, JwtConstants.HeaderType); |
| 87 | + |
| 88 | + return new ApiKeySignInfo(newHeader, publicKeyEncoded, privateKeyEncoded); |
| 89 | + } |
| 90 | + |
| 91 | + private static HMAC GetHmacAlgorithmFromName(string algorithmName) |
| 92 | + { |
| 93 | + switch (algorithmName) |
| 94 | + { |
| 95 | + case SecurityAlgorithms.HmacSha256: |
| 96 | + return new HMACSHA256(); |
| 97 | + case SecurityAlgorithms.HmacSha384: |
| 98 | + return new HMACSHA384(); |
| 99 | + case SecurityAlgorithms.HmacSha512: |
| 100 | + return new HMACSHA512(); |
| 101 | + default: |
| 102 | + throw new ArgumentException($"Algorithm name '{algorithmName}' not supported", nameof(algorithmName)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static int GetRsaKeyLengthFromName(string algorithmName) |
| 107 | + { |
| 108 | + switch (algorithmName) |
| 109 | + { |
| 110 | + case SecurityAlgorithms.RsaSha256: |
| 111 | + case SecurityAlgorithms.RsaSha256Signature: |
| 112 | + return 2048; |
| 113 | + case SecurityAlgorithms.RsaSha384: |
| 114 | + case SecurityAlgorithms.RsaSha384Signature: |
| 115 | + return 3072; |
| 116 | + case SecurityAlgorithms.RsaSha512: |
| 117 | + case SecurityAlgorithms.RsaSha512Signature: |
| 118 | + return 4096; |
| 119 | + default: |
| 120 | + throw new ArgumentException($"Algorithm name '{algorithmName}' not supported", nameof(algorithmName)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static ECCurve GetEcCurveFromName(string algorithmName) |
| 125 | + { |
| 126 | + switch (algorithmName) |
| 127 | + { |
| 128 | + case SecurityAlgorithms.EcdsaSha256: |
| 129 | + case SecurityAlgorithms.EcdsaSha256Signature: |
| 130 | + return ECCurve.NamedCurves.nistP256; |
| 131 | + case SecurityAlgorithms.EcdsaSha384: |
| 132 | + case SecurityAlgorithms.EcdsaSha384Signature: |
| 133 | + return ECCurve.NamedCurves.nistP384; |
| 134 | + case SecurityAlgorithms.EcdsaSha512: |
| 135 | + case SecurityAlgorithms.EcdsaSha512Signature: |
| 136 | + return ECCurve.NamedCurves.nistP521; |
| 137 | + default: |
| 138 | + throw new ArgumentException($"Algorithm name '{algorithmName}' not supported", nameof(algorithmName)); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments