1- using CasDotnetSdk . Asymmetric . Linux ;
1+ using CasCoreLib ;
22using CasDotnetSdk . Asymmetric . Types ;
3- using CasDotnetSdk . Asymmetric . Windows ;
4-
53using CasDotnetSdk . Helpers ;
6- using CasDotnetSdk . Helpers . Types ;
7-
84using System ;
9- using System . Runtime . InteropServices ;
105
116namespace CasDotnetSdk . Asymmetric
127{
13- public class RSAWrapper : BaseWrapper
8+ public unsafe class RSAWrapper : BaseWrapper
149 {
1510 /// <summary>
1611 /// A wrapper class for RSA key creation, encryption, decryption, signing, and verification.
1712 /// </summary>
1813 public RSAWrapper ( )
1914 {
20-
2115 }
2216
2317 /// <summary>
2418 /// Signs data with an RSA private key.
2519 /// </summary>
26- /// <param name="privateKey"></param>
27- /// <param name="dataToSign"></param>
28- /// <returns></returns>
29- /// <exception cref="Exception"></exception>
30- ///
31-
3220 public byte [ ] Sign ( string privateKey , byte [ ] dataToSign )
3321 {
3422 if ( dataToSign == null || dataToSign . Length == 0 )
3523 {
3624 throw new Exception ( "You must provide allocated data to sign with RSA" ) ;
3725 }
3826
39- RsaSignBytesResults signResult = ( this . _platform == OSPlatform . Linux ) ?
40- RSALinuxWrapper . rsa_sign_with_key_bytes ( privateKey , dataToSign , dataToSign . Length ) :
41- RSAWindowsWrapper . rsa_sign_with_key_bytes ( privateKey , dataToSign , dataToSign . Length ) ; ;
42- CasErrorHandler . ThrowIfError ( signResult . error_code , "RSA sign" ) ;
43- byte [ ] result = new byte [ signResult . length ] ;
44- Marshal . Copy ( signResult . signature_raw_ptr , result , 0 , ( int ) signResult . length ) ;
45- FreeMemoryHelper . FreeBytesMemory ( signResult . signature_raw_ptr ) ;
46-
47-
48- return result ;
27+ fixed ( byte * privateKeyPtr = NativeString . ToCString ( privateKey ) )
28+ fixed ( byte * dataPtr = NativePin . Of ( dataToSign ) )
29+ {
30+ RsaSignBytesResults signResult = NativeMethods . rsa_sign_with_key_bytes ( privateKeyPtr , dataPtr , ( nuint ) dataToSign . Length ) ;
31+ CasErrorHandler . ThrowIfError ( signResult . error_code , "RSA sign" ) ;
32+ return NativeByteBuffer . CopyAndFree ( signResult . signature_raw_ptr , signResult . length ) ;
33+ }
4934 }
5035
51-
5236 /// <summary>
5337 /// Verifies data with an RSA public key.
5438 /// </summary>
55- /// <param name="publicKey"></param>
56- /// <param name="dataToVerify"></param>
57- /// <param name="signature"></param>
58- /// <returns></returns>
59- /// <exception cref="Exception"></exception>
60- ///
61-
6239 public bool Verify ( string publicKey , byte [ ] dataToVerify , byte [ ] signature )
6340 {
6441 if ( dataToVerify == null || dataToVerify . Length == 0 )
@@ -70,41 +47,33 @@ public bool Verify(string publicKey, byte[] dataToVerify, byte[] signature)
7047 throw new Exception ( "You must provide an allocated signature to verify with RSA" ) ;
7148 }
7249
73- CasVerifyResult result = ( this . _platform == OSPlatform . Linux ) ?
74- RSALinuxWrapper . rsa_verify_bytes ( publicKey , dataToVerify , dataToVerify . Length , signature , signature . Length ) :
75- RSAWindowsWrapper . rsa_verify_bytes ( publicKey , dataToVerify , dataToVerify . Length , signature , signature . Length ) ;
76- CasErrorHandler . ThrowIfError ( result . error_code , "RSA verify" ) ;
77-
78-
79- return result . is_valid ;
50+ fixed ( byte * publicKeyPtr = NativeString . ToCString ( publicKey ) )
51+ fixed ( byte * dataPtr = NativePin . Of ( dataToVerify ) )
52+ fixed ( byte * signaturePtr = NativePin . Of ( signature ) )
53+ {
54+ CasVerifyResult result = NativeMethods . rsa_verify_bytes ( publicKeyPtr , dataPtr , ( nuint ) dataToVerify . Length , signaturePtr , ( nuint ) signature . Length ) ;
55+ CasErrorHandler . ThrowIfError ( result . error_code , "RSA verify" ) ;
56+ return result . is_valid ;
57+ }
8058 }
8159
8260 /// <summary>
8361 /// Generates an RSA key based on the key size provided. (1024, 2048, 4096)
8462 /// </summary>
85- /// <param name="keySize"></param>
86- /// <returns></returns>
87- /// <exception cref="Exception"></exception>
88-
8963 public RsaKeyPairResult GetKeyPair ( int keySize )
9064 {
9165 if ( keySize != 1024 && keySize != 2048 && keySize != 4096 )
9266 {
9367 throw new Exception ( "Please pass in a valid key size." ) ;
9468 }
9569
96- RsaKeyPairStruct keyPairStruct = ( this . _platform == OSPlatform . Linux ) ?
97- RSALinuxWrapper . get_key_pair ( keySize ) :
98- RSAWindowsWrapper . get_key_pair ( keySize ) ;
99- CasErrorHandler . ThrowIfError ( keyPairStruct . error_code , "RSA key pair generation" ) ;
100- RsaKeyPairResult result = new RsaKeyPairResult ( )
70+ RsaKeyPair keyPair = NativeMethods . get_key_pair ( ( nuint ) keySize ) ;
71+ CasErrorHandler . ThrowIfError ( keyPair . error_code , "RSA key pair generation" ) ;
72+ return new RsaKeyPairResult ( )
10173 {
102- PrivateKey = Marshal . PtrToStringAnsi ( keyPairStruct . priv_key ) ,
103- PublicKey = Marshal . PtrToStringAnsi ( keyPairStruct . pub_key )
74+ PrivateKey = NativeString . ReadAndFree ( keyPair . priv_key ) ,
75+ PublicKey = NativeString . ReadAndFree ( keyPair . pub_key )
10476 } ;
105- FreeMemoryHelper . FreeCStringMemory ( keyPairStruct . pub_key ) ;
106- FreeMemoryHelper . FreeCStringMemory ( keyPairStruct . priv_key ) ;
107- return result ;
10877 }
10978 }
11079}
0 commit comments