public async Task <string> CreateDeviceAuthChallengeResponse(IDictionary <string, string> challengeData) { string authHeaderTemplate = "PKeyAuth {0}, Context=\"{1}\", Version=\"{2}\""; X509Certificate2 certificate = FindCertificate(challengeData); DeviceAuthJWTResponse response = new DeviceAuthJWTResponse(challengeData["SubmitUrl"], challengeData["nonce"], Convert.ToBase64String(certificate.GetRawCertData())); CngKey key = GetCngPrivateKey(certificate); byte[] sig = null; using (RSACng rsa = new RSACng(key)) { rsa.SignatureHashAlgorithm = CngAlgorithm.Sha256; sig = rsa.SignData(response.GetResponseToSign().ToByteArray()); } string signedJwt = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", response.GetResponseToSign(), Base64UrlEncoder.Encode(sig)); string authToken = string.Format(CultureInfo.CurrentCulture, " AuthToken=\"{0}\"", signedJwt); Task <string> resultTask = Task.Factory.StartNew( () => { return(string.Format(authHeaderTemplate, authToken, challengeData["Context"], challengeData["Version"])); }); return(await resultTask); }
public async Task <string> CreateDeviceAuthChallengeResponse(IDictionary <string, string> challengeData) { string authHeaderTemplate = "PKeyAuth {0}, Context=\"{1}\", Version=\"{2}\""; Certificate certificate = await FindCertificate(challengeData); DeviceAuthJWTResponse response = new DeviceAuthJWTResponse(challengeData["SubmitUrl"], challengeData["nonce"], Convert.ToBase64String(certificate.GetCertificateBlob().ToArray())); IBuffer input = CryptographicBuffer.ConvertStringToBinary(response.GetResponseToSign(), BinaryStringEncoding.Utf8); CryptographicKey keyPair = await PersistedKeyProvider.OpenKeyPairFromCertificateAsync(certificate, HashAlgorithmNames.Sha256, CryptographicPadding.RsaPkcs1V15); IBuffer signed = await CryptographicEngine.SignAsync(keyPair, input); string signedJwt = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", response.GetResponseToSign(), Base64UrlEncoder.Encode(signed.ToArray())); string authToken = string.Format(CultureInfo.CurrentCulture, " AuthToken=\"{0}\"", signedJwt); return(string.Format(authHeaderTemplate, authToken, challengeData["Context"], challengeData["Version"])); }