|
| 1 | +--- |
| 2 | +title: "Exchange Online Management Module - Could not use the certificate for signing" |
| 3 | +excerpt: "When trying to establish a connection to Exchange Online Could not use the certificate for signing error message is displayed." |
| 4 | +categories: |
| 5 | + - PowerShell |
| 6 | + - Exchange |
| 7 | + - Office 365 |
| 8 | + |
| 9 | +tags: |
| 10 | + - PowerShell |
| 11 | + - Office365 |
| 12 | + - Exchange |
| 13 | + |
| 14 | +toc: true |
| 15 | +header: |
| 16 | + teaser: "/assets/images/PowerShell_Logo.png" |
| 17 | +--- |
| 18 | + |
| 19 | +## Exchange Online Certificate Based authentication |
| 20 | + |
| 21 | +As I have written in my [previous post about TokenExpiry error message Microsoft is retiring ability to connect to Exchange Online via basic authentication](https://pscustomobject.github.io/powershell/exchange/office%20365/Cannot-bind-argument-to-parameter-Token-Expiry/). |
| 22 | + |
| 23 | +You can read my article on how to implement _Certificate Based authentication_ for Exchange Online [here](https://pscustomobject.github.io/powershell/office365/exchange/Exchange-Online-Certificate-Based-Authentication/). |
| 24 | + |
| 25 | +## Could not use the certificate for signing error message |
| 26 | + |
| 27 | +Today while I was updating code for one of our automations I created a request for a new certificate to use for authentication purposes. |
| 28 | + |
| 29 | +Once I deployed code to our test environment automation was failing the connection to Exchange Online with the following error |
| 30 | + |
| 31 | +```powershell |
| 32 | +[System.Management.Automation.RuntimeException] One or more errors occurred. |
| 33 | +[Microsoft.Identity.Client.MsalClientException] Could not use the certificate for signing. See inner exception for details. Possible cause: this may be a known issue with apps build against .NET Desktop 4.6 or lower. Either target a higher version of .NET desktop - 4.6.1 and above, or use a different certificate type (non-CNG) or sign your own assertion as described at aka.ms/msal-net-signed-assertion. |
| 34 | +[System.Security.Cryptography.CryptographicException] Invalid provider type specified. |
| 35 | +```` |
| 36 | +
|
| 37 | +Funnily enough the same certificate and cmdlets were working fine with PowerShell 7. |
| 38 | +
|
| 39 | +After quite some troubleshooting I've found out the problem was caused by the certificate's private key using [*Cryptography Next Generate (CNG)*](https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/network/cng-certificates-overview) template rather than RSA. |
| 40 | +
|
| 41 | +Not having direct access to the CA releasing the certificate I could not change this so I had to resort on either running the automation in PowerShell 7 or update the certificate itself. |
| 42 | +
|
| 43 | +Luckily this is easily done via OpenSSL. Let's see how. |
| 44 | +
|
| 45 | +## Convert Certificate private key from CNG to RSA |
| 46 | +
|
| 47 | +If you have installed Git, cygwin or Windows Subsystem for Linux you just need to fire a bash prompt and use the following commands: |
| 48 | +
|
| 49 | +```bash |
| 50 | +# Extract the public key from the cert |
| 51 | +OpenSSL pkcs12 -in "CNGCertificate.pfx" -nokeys -out "temp.cer" |
| 52 | +
|
| 53 | +# Extract the private key |
| 54 | +OpenSSL pkcs12 -in "CNGCertificate.pfx" -nocerts -out "temp.pem" |
| 55 | +
|
| 56 | +# Convert key to RSA |
| 57 | +OpenSSL rsa -inform PEM -in "temp.pem" -out "temp.rsa" |
| 58 | +
|
| 59 | +# Finally create a new pfx file |
| 60 | +OpenSSL pkcs12 -export -in "temp.cer" -inkey "temp.rsa" -out "RSACertificate.pfx" |
| 61 | +```` |
| 62 | +
|
| 63 | +**Note:** In the above commands I am not using a password for the certificate as everything is local to my machine but a password is definitely *required* when exporting a certificate together with the private key. |
| 64 | +{: .notice--warning} |
| 65 | +
|
| 66 | +Once the new pfx file has been created all *temporary* certificates can be safely removed form the system and connection to Exchange Online will go through just fine. |
| 67 | +
|
| 68 | +Again if you can use PowerShell 7 you will not face this issue but in case you're stuck with version 5.1 and facing this error message hopefully this post can save you some headaches. |
| 69 | +
|
| 70 | +Full credit for the solution goes to this [StackOverFlow thread](https://stackoverflow.com/questions/22581811/invalid-provider-type-specified-cryptographicexception-when-trying-to-load-pri/34103154#34103154) |
0 commit comments