public void RSALocalKeyDecryptionPaddingHashInvalidThrows() { byte[] toEncrypt = new byte[] { 1, 2, 3, 4 }; ManagedRSAEncryption util = new ManagedRSAEncryption(); byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt, "Not a real padding hash function", PaddingFlags.OAEPPadding); }
public void RSALocalKeyDecryptionPaddingInvalidThrows() { byte[] toEncrypt = new byte[] { 1, 2, 3, 4 }; ManagedRSAEncryption util = new ManagedRSAEncryption(); byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt, null, PaddingFlags.PSSPadding + 10000); }
public void RSALocalKeyDecryptionProviderMissingThrows() { byte[] toEncrypt = new byte[] { 1, 2, 3, 4 }; ManagedRSAEncryption util = new ManagedRSAEncryption(); byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt); byte[] decrypted = util.DecryptWithLocalKey("FakeProviderName", TestKeyName, encrypted); }
public void EncryptionUtilitiesFullE2E() { byte[] originalData = new byte[] { 1, 2, 3, 4 }; ManagedRSAEncryption managed = new ManagedRSAEncryption(); X509Certificate2 testCert = new X509Certificate2(Convert.FromBase64String(TestCertBase64Encoded), TestCertPassword); byte[] encryptedWithLocal = managed.EncryptWithLocalKey(TestProvider, TestKeyName, originalData); byte[] recryptedWithDeviceCert = managed.RecryptPfxImportMessage(encryptedWithLocal, testCert, TestProvider, TestKeyName); byte[] decryptedWithDeviceCert = managed.DecryptWithCertificate(recryptedWithDeviceCert, testCert); CollectionAssert.AreEqual(originalData, decryptedWithDeviceCert); CollectionAssert.AreNotEqual(originalData, encryptedWithLocal); CollectionAssert.AreNotEqual(originalData, recryptedWithDeviceCert); CollectionAssert.AreNotEqual(encryptedWithLocal, recryptedWithDeviceCert); CollectionAssert.AreNotEqual(encryptedWithLocal, decryptedWithDeviceCert); CollectionAssert.AreNotEqual(recryptedWithDeviceCert, decryptedWithDeviceCert); }
public void RSALocalKeyEncryptionDecryptionOAEPE2E() { byte[] toEncrypt = new byte[] { 1, 2, 3, 4 }; ManagedRSAEncryption util = new ManagedRSAEncryption(); byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt, PaddingHashAlgorithmNames.SHA256, PaddingFlags.OAEPPadding); byte[] decrypted = util.DecryptWithLocalKey(TestProvider, TestKeyName, encrypted, PaddingHashAlgorithmNames.SHA256, PaddingFlags.OAEPPadding); CollectionAssert.AreNotEqual(encrypted, toEncrypt); CollectionAssert.AreNotEqual(encrypted, decrypted); CollectionAssert.AreEqual(toEncrypt, decrypted); Assert.AreNotEqual(0, encrypted.Length); Assert.AreNotEqual(0, decrypted.Length); Assert.IsNotNull(encrypted); Assert.IsNotNull(decrypted); }
public void RSALocalKeyEncryptionDecryptionE2E() { byte[] toEncrypt = new byte[] { 1, 2, 3, 4 }; ManagedRSAEncryption util = new ManagedRSAEncryption(); byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt); byte[] decrypted = util.DecryptWithLocalKey(TestProvider, TestKeyName, encrypted); CollectionAssert.AreNotEqual(encrypted, toEncrypt); CollectionAssert.AreNotEqual(encrypted, decrypted); CollectionAssert.AreEqual(toEncrypt, decrypted); Assert.AreNotEqual(0, encrypted.Length); Assert.AreNotEqual(0, decrypted.Length); Assert.IsNotNull(encrypted); Assert.IsNotNull(decrypted); }
protected override void ProcessRecord() { byte[] pfxData; if (this.ParameterSetName == PFXBase64String) { pfxData = Convert.FromBase64String(Base64EncodedPfx); } else { pfxData = File.ReadAllBytes(PathToPfxFile); } X509Certificate2 pfxCert = new X509Certificate2(); try { pfxCert.Import(pfxData, PfxPassword, X509KeyStorageFlags.DefaultKeySet); } catch (CryptographicException ex) { if (ex.HResult == ErrorCodeCantOpenFile) { ThrowTerminatingError( new ErrorRecord( new ArgumentException( string.Format("Could not Read Thumbprint on file at path: '{0}'. File must be a certificate.", PathToPfxFile), ex), Guid.NewGuid().ToString(), ErrorCategory.InvalidArgument, null)); } else if (ex.HResult == ErrorCodeNetworkPasswordIncorrect) { ThrowTerminatingError( new ErrorRecord( new ArgumentException("Could not Read Thumbprint. Verify Password is Correct.", ex), Guid.NewGuid().ToString(), ErrorCategory.InvalidArgument, null)); } else { ThrowTerminatingError( new ErrorRecord( new ArgumentException("Could not Read Thumbprint. Unknown Cause", ex), Guid.NewGuid().ToString(), ErrorCategory.InvalidArgument, null)); } } ManagedRSAEncryption encryptUtility = new ManagedRSAEncryption(); byte[] password = new byte[PfxPassword.Length]; GCHandle pinnedPasswordHandle = GCHandle.Alloc(password, GCHandleType.Pinned); byte[] encryptedPassword = null; try { ConvertSecureStringToByteArray(PfxPassword, ref password); string hashAlgorithm; int paddingFlags; switch (PaddingScheme) { case UserPfxPaddingScheme.Pkcs1: case UserPfxPaddingScheme.OaepSha1: ThrowTerminatingError( new ErrorRecord( new ArgumentException("Pkcs1 and OaepSha1 are no longer supported."), Guid.NewGuid().ToString(), ErrorCategory.InvalidArgument, null)); return; case UserPfxPaddingScheme.OaepSha256: hashAlgorithm = PaddingHashAlgorithmNames.SHA256; paddingFlags = PaddingFlags.OAEPPadding; break; case UserPfxPaddingScheme.OaepSha384: hashAlgorithm = PaddingHashAlgorithmNames.SHA384; paddingFlags = PaddingFlags.OAEPPadding; break; case UserPfxPaddingScheme.None: PaddingScheme = UserPfxPaddingScheme.OaepSha512; goto default; // Since C# doesn't allow switch-case fall-through! case UserPfxPaddingScheme.OaepSha512: default: hashAlgorithm = PaddingHashAlgorithmNames.SHA512; paddingFlags = PaddingFlags.OAEPPadding; break; } if (KeyFilePath != null) { encryptedPassword = encryptUtility.EncryptWithFileKey(KeyFilePath, password, hashAlgorithm, paddingFlags); } else { encryptedPassword = encryptUtility.EncryptWithLocalKey(ProviderName, KeyName, password, hashAlgorithm, paddingFlags); } } finally { if (password != null) { password.ZeroFill(); } if (pinnedPasswordHandle.IsAllocated) { pinnedPasswordHandle.Free(); } } string encryptedPasswordString = Convert.ToBase64String(encryptedPassword); UserPFXCertificate userPfxCertifiate = new UserPFXCertificate(); userPfxCertifiate.Thumbprint = pfxCert.Thumbprint.ToLowerInvariant(); userPfxCertifiate.IntendedPurpose = (UserPfxIntendedPurpose)IntendedPurpose; userPfxCertifiate.PaddingScheme = (UserPfxPaddingScheme)PaddingScheme; userPfxCertifiate.KeyName = KeyName; userPfxCertifiate.UserPrincipalName = UPN; userPfxCertifiate.ProviderName = ProviderName; userPfxCertifiate.StartDateTime = Convert.ToDateTime(pfxCert.GetEffectiveDateString(), CultureInfo.CurrentCulture); userPfxCertifiate.ExpirationDateTime = Convert.ToDateTime(pfxCert.GetExpirationDateString(), CultureInfo.CurrentCulture); userPfxCertifiate.CreatedDateTime = DateTime.Now; userPfxCertifiate.LastModifiedDateTime = DateTime.Now; userPfxCertifiate.EncryptedPfxPassword = encryptedPasswordString; userPfxCertifiate.EncryptedPfxBlob = pfxData; WriteObject(userPfxCertifiate); }