-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[KeyVault] Migrate BouncyCastle Library #28924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| using Microsoft.Azure.Commands.KeyVault.SecurityDomain.Common; | ||
| using Org.BouncyCastle.Crypto.Parameters; | ||
| using Org.BouncyCastle.OpenSsl; | ||
| using System; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
|
|
@@ -51,61 +49,25 @@ public void Load(KeyPath path) | |
|
|
||
| static RSAParameters RsaParamsFromPem(string path, string password) | ||
| { | ||
| using (var stream = File.OpenText(path)) | ||
| { | ||
| var reader = string.IsNullOrEmpty(password) ? new PemReader(stream) : new PemReader(stream, new PasswordFinder(password)); | ||
| var keyParameters = reader.ReadObject() as RsaPrivateCrtKeyParameters; | ||
| string pem = File.ReadAllText(path); | ||
|
|
||
| return ToRSAParameters(keyParameters); | ||
| } | ||
| } | ||
|
|
||
| static RSAParameters ToRSAParameters(RsaPrivateCrtKeyParameters privKey) | ||
| { | ||
| RSAParameters rp = new RSAParameters | ||
| using (RSA rsa = RSA.Create()) | ||
| { | ||
| Modulus = privKey.Modulus.ToByteArrayUnsigned(), | ||
| Exponent = privKey.PublicExponent.ToByteArrayUnsigned(), | ||
| P = privKey.P.ToByteArrayUnsigned(), | ||
| Q = privKey.Q.ToByteArrayUnsigned() | ||
| }; | ||
| rp.D = ConvertRSAParametersField(privKey.Exponent, rp.Modulus.Length); | ||
| rp.DP = ConvertRSAParametersField(privKey.DP, rp.P.Length); | ||
| rp.DQ = ConvertRSAParametersField(privKey.DQ, rp.Q.Length); | ||
| rp.InverseQ = ConvertRSAParametersField(privKey.QInv, rp.Q.Length); | ||
| return rp; | ||
| } | ||
|
|
||
|
|
||
| static byte[] ConvertRSAParametersField(Org.BouncyCastle.Math.BigInteger n, int size) | ||
| { | ||
| byte[] bs = n.ToByteArrayUnsigned(); | ||
| if (bs.Length == size) | ||
| return bs; | ||
| if (bs.Length > size) | ||
| throw new ArgumentException("Specified size too small", "size"); | ||
| byte[] padded = new byte[size]; | ||
| Array.Copy(bs, 0, padded, size - bs.Length, bs.Length); | ||
| return padded; | ||
| if (string.IsNullOrEmpty(password)) | ||
| { | ||
| rsa.ImportFromPem(pem); | ||
| } | ||
| else | ||
| { | ||
| rsa.ImportFromEncryptedPem(pem, password); | ||
|
Comment on lines
+58
to
+62
|
||
| } | ||
|
|
||
| return rsa.ExportParameters(true); | ||
| } | ||
| } | ||
|
|
||
| X509Certificate2 _cert; | ||
| RSA _key; | ||
| byte[] _thumbprint; | ||
|
|
||
| private class PasswordFinder : IPasswordFinder | ||
| { | ||
| private readonly string _password; | ||
|
|
||
| public PasswordFinder(string password) | ||
| { | ||
| _password = password; | ||
| } | ||
|
|
||
| public char[] GetPassword() | ||
| { | ||
| return _password.ToCharArray(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changelog entry uses technical acronyms "PEM", "PKCS#1", and "PKCS#8" without explanation. According to the ChangeLog.md guidelines for Azure PowerShell, less-obvious acronyms should be explained on first use in a release section, as the primary audience is Azure PowerShell users, not developers.
Consider updating to: