1 2 using System; 3 using System.Collections.Generic; 4 using Net.Sf.Pkcs11.Delegates; 5 using System.Runtime.InteropServices; 6 namespace Net.Sf.Pkcs11.Wrapper 7 { 8 9 /// <summary> 10 /// Wrapper around Pkcs11 (low-level). 11 /// </summary> 12 public class Pkcs11Module 13 { 14 /// <summary> 15 /// 16 /// </summary> 17 protected IntPtr hLib; 18 19 /// <summary> 20 /// Constructor. 21 /// </summary> 22 /// <param name="hLib"></param> Pkcs11Module(IntPtr hLib)23 protected Pkcs11Module(IntPtr hLib) 24 { 25 this.hLib= hLib; 26 } 27 28 /// <summary> 29 /// Creates an instance of Pkcs11Module 30 /// </summary> 31 /// <param name="moduleName"> 32 /// module to be loaded. it is the path of pkcs11 driver 33 /// <example> 34 /// <code> 35 /// Pkcs11Module pm=Pkcs11Module.GetInstance("gclib.dll"); 36 /// </code> 37 /// </example> 38 /// </param> 39 /// <returns></returns> GetInstance(string moduleName)40 internal static Pkcs11Module GetInstance(string moduleName){ 41 IntPtr hLib; 42 if ((hLib = KernelUtil.LoadLibrary(moduleName)) == IntPtr.Zero) 43 throw new Exception("Could not load module. Module name:" + moduleName); 44 return new Pkcs11Module(hLib); 45 } 46 47 /// <summary> 48 /// 49 /// </summary> Initialize()50 public void Initialize() 51 { 52 C_Initialize proc=(C_Initialize)DelegateUtil.GetDelegate(this.hLib,typeof(C_Initialize)); 53 checkCKR( proc(IntPtr.Zero)); 54 } 55 56 /// <summary> 57 /// 58 /// </summary> Finalize_()59 public void Finalize_(){ 60 C_Finalize proc=(C_Finalize)DelegateUtil.GetDelegate(this.hLib,typeof(C_Finalize)); 61 checkCKR( proc(IntPtr.Zero)); 62 } 63 64 /// <summary> 65 /// 66 /// </summary> 67 /// <returns></returns> GetInfo()68 public CK_INFO GetInfo() 69 { 70 C_GetInfo proc=(C_GetInfo)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetInfo)); 71 72 CK_INFO ckInfo=new CK_INFO(); 73 checkCKR( proc(ref ckInfo)); 74 75 return ckInfo; 76 } 77 78 /// <summary> 79 /// 80 /// </summary> 81 /// <param name="tokenPresent"></param> 82 /// <returns></returns> GetSlotList(bool tokenPresent)83 public uint[] GetSlotList(bool tokenPresent){ 84 85 C_GetSlotList proc=(C_GetSlotList)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetSlotList)); 86 87 uint pullVal=0; 88 checkCKR( proc(tokenPresent,null,ref pullVal)); 89 90 uint[] slots = new uint[pullVal]; 91 checkCKR( proc(tokenPresent,slots,ref pullVal)); 92 93 return slots; 94 } 95 96 /// <summary> 97 /// 98 /// </summary> 99 /// <param name="slotID"></param> 100 /// <returns></returns> GetSlotInfo(uint slotID)101 public CK_SLOT_INFO GetSlotInfo(uint slotID){ 102 103 C_GetSlotInfo proc=(C_GetSlotInfo)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetSlotInfo)); 104 105 CK_SLOT_INFO slotInfo=new CK_SLOT_INFO(); 106 checkCKR( proc(slotID, ref slotInfo)); 107 108 return slotInfo; 109 } 110 111 /// <summary> 112 /// 113 /// </summary> 114 /// <param name="slotID"></param> 115 /// <returns></returns> GetTokenInfo(uint slotID)116 public CK_TOKEN_INFO GetTokenInfo(uint slotID){ 117 118 C_GetTokenInfo proc=(C_GetTokenInfo)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetTokenInfo)); 119 120 CK_TOKEN_INFO tokenInfo=new CK_TOKEN_INFO(); 121 checkCKR( proc(slotID, ref tokenInfo)); 122 123 return tokenInfo; 124 } 125 126 /// <summary> 127 /// 128 /// </summary> 129 /// <param name="options"></param> 130 /// <returns></returns> WaitForSlotEvent(bool DO_NOT_BLOCK)131 public uint WaitForSlotEvent(bool DO_NOT_BLOCK){ 132 133 C_WaitForSlotEvent proc=(C_WaitForSlotEvent)DelegateUtil.GetDelegate(this.hLib,typeof(C_WaitForSlotEvent)); 134 135 uint slotId=0, flags=0; 136 137 if(DO_NOT_BLOCK) 138 flags=PKCS11Constants.CKF_DONT_BLOCK; 139 140 checkCKR(proc(flags, ref slotId, IntPtr.Zero)); 141 142 return slotId; 143 } 144 145 /// <summary> 146 /// 147 /// </summary> 148 /// <param name="slotId"></param> 149 /// <returns></returns> GetMechanismList(uint slotId)150 public CKM[] GetMechanismList(uint slotId){ 151 152 C_GetMechanismList proc=(C_GetMechanismList)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetMechanismList)); 153 154 uint pulCount=0; 155 checkCKR( proc(slotId,null,ref pulCount)); 156 157 CKM[] mechanismList = new CKM[pulCount]; 158 159 checkCKR( proc(slotId, mechanismList,ref pulCount)); 160 161 return mechanismList; 162 } 163 164 /// <summary> 165 /// 166 /// </summary> 167 /// <param name="slotId"></param> 168 /// <param name="mechanism"></param> 169 /// <returns></returns> GetMechanismInfo(uint slotId, CKM mechanism)170 public CK_MECHANISM_INFO GetMechanismInfo(uint slotId, CKM mechanism){ 171 172 C_GetMechanismInfo proc=(C_GetMechanismInfo)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetMechanismInfo)); 173 174 CK_MECHANISM_INFO mecInfo=new CK_MECHANISM_INFO(); 175 176 checkCKR(proc(slotId,mechanism,ref mecInfo) ); 177 178 return mecInfo; 179 } 180 181 /// <summary> 182 /// 183 /// </summary> 184 /// <param name="slotId"></param> 185 /// <param name="pin"></param> 186 /// <param name="label"></param> InitToken(uint slotId, string pin, string label)187 public void InitToken(uint slotId, string pin, string label){ 188 189 C_InitToken proc=(C_InitToken)DelegateUtil.GetDelegate(this.hLib,typeof(C_InitToken)); 190 191 byte[] pinBytes=System.Text.Encoding.UTF8.GetBytes(pin); 192 193 byte[] labelBytes=new byte[32]; 194 new List<byte>(System.Text.Encoding.UTF8.GetBytes(label+new String(' ',32 ))).CopyTo(0,labelBytes,0,32); 195 196 checkCKR(proc(slotId,pinBytes,(uint)pinBytes.Length,labelBytes)); 197 } 198 199 /// <summary> 200 /// 201 /// </summary> 202 /// <param name="hSession"></param> 203 /// <param name="pin"></param> InitPIN(uint hSession , string pin)204 public void InitPIN(uint hSession , string pin){ 205 206 C_InitPIN proc = (C_InitPIN)DelegateUtil.GetDelegate(this.hLib,typeof(C_InitPIN)); 207 208 byte[] pinBytes=System.Text.Encoding.UTF8.GetBytes(pin); 209 210 checkCKR(proc(hSession,pinBytes,(uint)pinBytes.Length)); 211 } 212 213 /// <summary> 214 /// 215 /// </summary> 216 /// <param name="hSession"></param> 217 /// <param name="oldPin"></param> 218 /// <param name="newPin"></param> SetPIN(uint hSession, string oldPin, string newPin)219 public void SetPIN (uint hSession, string oldPin, string newPin){ 220 221 C_SetPIN proc = (C_SetPIN)DelegateUtil.GetDelegate(this.hLib,typeof(C_SetPIN)); 222 223 byte[] oldPinBytes=System.Text.Encoding.UTF8.GetBytes(oldPin); 224 byte[] newPinBytes=System.Text.Encoding.UTF8.GetBytes(newPin); 225 226 checkCKR( 227 proc(hSession,oldPinBytes,(uint)oldPinBytes.Length,newPinBytes,(uint)newPinBytes.Length)); 228 } 229 230 /// <summary> 231 /// 232 /// </summary> 233 /// <param name="slotId"></param> 234 /// <param name="applicationId"></param> 235 /// <param name="readOnly"></param> 236 /// <returns></returns> OpenSession(uint slotId, uint applicationId, bool readOnly)237 public uint OpenSession(uint slotId, uint applicationId, bool readOnly){ 238 239 C_OpenSession proc= (C_OpenSession)DelegateUtil.GetDelegate(this.hLib,typeof(C_OpenSession)); 240 241 uint flags=PKCS11Constants.CKF_SERIAL_SESSION| (readOnly? 0: PKCS11Constants.CKF_RW_SESSION); 242 243 uint hSession=0; 244 245 checkCKR( proc(slotId,flags, ref applicationId, IntPtr.Zero, ref hSession) ); 246 247 return hSession; 248 } 249 250 /// <summary> 251 /// 252 /// </summary> 253 /// <param name="hSession"></param> CloseSession(uint hSession)254 public void CloseSession(uint hSession){ 255 256 C_CloseSession proc= (C_CloseSession)DelegateUtil.GetDelegate(this.hLib,typeof(C_CloseSession)); 257 258 checkCKR(proc(hSession)); 259 } 260 261 /// <summary> 262 /// 263 /// </summary> 264 /// <param name="slotId"></param> CloseAllSessions(uint slotId)265 public void CloseAllSessions(uint slotId){ 266 #if CDECL 267 [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)] 268 #endif 269 C_CloseAllSessions proc= (C_CloseAllSessions)DelegateUtil.GetDelegate(this.hLib,typeof(C_CloseAllSessions)); 270 271 checkCKR(proc(slotId)); 272 } 273 274 /// <summary> 275 /// 276 /// </summary> 277 /// <param name="hSession"></param> 278 /// <returns></returns> GetSessionInfo(uint hSession)279 public CK_SESSION_INFO GetSessionInfo(uint hSession){ 280 281 C_GetSessionInfo proc= (C_GetSessionInfo)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetSessionInfo)); 282 283 CK_SESSION_INFO sessionInfo=new CK_SESSION_INFO(); 284 285 checkCKR(proc(hSession, ref sessionInfo)); 286 287 return sessionInfo; 288 } 289 290 /// <summary> 291 /// 292 /// </summary> 293 /// <param name="hSession"></param> 294 /// <returns></returns> GetOperationState(uint hSession)295 public byte[] GetOperationState(uint hSession){ 296 297 C_GetOperationState proc= (C_GetOperationState)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetOperationState)); 298 299 uint pLen=0; 300 301 checkCKR(proc(hSession, null, ref pLen)); 302 303 byte[] opState=new byte[pLen]; 304 305 checkCKR(proc(hSession, opState, ref pLen)); 306 307 return opState; 308 } 309 310 /// <summary> 311 /// 312 /// </summary> 313 /// <param name="hSession"></param> 314 /// <param name="opState"></param> 315 /// <param name="hEncryptionKey"></param> 316 /// <param name="hAuthenticationKey"></param> SetOperationState(uint hSession, byte[] opState, uint hEncryptionKey, uint hAuthenticationKey)317 public void SetOperationState(uint hSession, byte[] opState, uint hEncryptionKey, uint hAuthenticationKey){ 318 319 C_SetOperationState proc= (C_SetOperationState)DelegateUtil.GetDelegate(this.hLib,typeof(C_SetOperationState)); 320 321 checkCKR ( proc(hSession, opState, (uint)opState.Length, hEncryptionKey, hAuthenticationKey ) ); 322 } 323 324 /// <summary> 325 /// 326 /// </summary> 327 /// <param name="hSession"></param> 328 /// <param name="userType"></param> 329 /// <param name="pin"></param> Login(uint hSession, CKU userType, string pin)330 public void Login(uint hSession, CKU userType, string pin){ 331 332 C_Login proc = (C_Login)DelegateUtil.GetDelegate(this.hLib,typeof(C_Login)); 333 334 byte[] pinBytes=System.Text.Encoding.UTF8.GetBytes(pin); 335 336 checkCKR(proc(hSession, userType, pinBytes, (uint)pinBytes.Length )); 337 } 338 339 /// <summary> 340 /// 341 /// </summary> 342 /// <param name="hSession"></param> Logout(uint hSession)343 public void Logout(uint hSession){ 344 345 C_Logout proc= (C_Logout)DelegateUtil.GetDelegate(this.hLib,typeof(C_Logout)); 346 347 checkCKR(proc(hSession)); 348 } 349 350 /// <summary> 351 /// 352 /// </summary> 353 /// <param name="hSession"></param> 354 /// <param name="template"></param> 355 /// <returns></returns> CreateObject(uint hSession, CK_ATTRIBUTE[] template)356 public uint CreateObject(uint hSession, CK_ATTRIBUTE[] template){ 357 358 C_CreateObject proc= (C_CreateObject)DelegateUtil.GetDelegate(this.hLib,typeof(C_CreateObject)); 359 360 uint hObj=0; 361 362 checkCKR(proc(hSession,template, (uint)template.Length,ref hObj)); 363 364 return hObj; 365 } 366 367 /// <summary> 368 /// 369 /// </summary> 370 /// <param name="hSession"></param> 371 /// <param name="hObj"></param> DestroyObject(uint hSession, uint hObj)372 public void DestroyObject(uint hSession, uint hObj){ 373 374 C_DestroyObject proc= (C_DestroyObject)DelegateUtil.GetDelegate(this.hLib,typeof(C_DestroyObject)); 375 376 checkCKR(proc.Invoke(hSession,hObj)); 377 } 378 379 //TODO: implement C_CopyObject 380 381 /// <summary> 382 /// 383 /// </summary> 384 /// <param name="hSession"></param> 385 /// <param name="hObj"></param> 386 /// <returns></returns> GetObjectSize(uint hSession, uint hObj)387 public uint GetObjectSize(uint hSession, uint hObj){ 388 389 C_GetObjectSize proc= (C_GetObjectSize)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetObjectSize)); 390 391 uint pulSize=0; 392 393 checkCKR(proc.Invoke(hSession,hObj, ref pulSize)); 394 395 return pulSize; 396 } 397 398 /// <summary> 399 /// 400 /// </summary> 401 /// <param name="hSession"></param> 402 /// <param name="hObj"></param> 403 /// <param name="template"></param> 404 /// <returns></returns> GetAttributeValue(uint hSession, uint hObj, CK_ATTRIBUTE[] template )405 public CK_ATTRIBUTE[] GetAttributeValue(uint hSession, uint hObj, CK_ATTRIBUTE[] template ){ 406 407 C_GetAttributeValue proc= (C_GetAttributeValue)DelegateUtil.GetDelegate(this.hLib,typeof(C_GetAttributeValue)); 408 for(int i=0;i<template.Length;i++){ 409 bool needsBuffer= template[i].pValue==IntPtr.Zero; 410 checkCKR(proc.Invoke(hSession,hObj, ref template[i], 1)); 411 if(needsBuffer&&template[i].ulValueLen>0 ){ 412 template[i].pValue=Marshal.AllocHGlobal((int) template[i].ulValueLen); 413 checkCKR(proc.Invoke(hSession,hObj, ref template[i], 1)); 414 } 415 } 416 417 return template; 418 } 419 420 /// <summary> 421 /// 422 /// </summary> 423 /// <param name="hSession"></param> 424 /// <param name="hObj"></param> 425 /// <param name="pTemplate"></param> SetAttributeValue(uint hSession, uint hObj, CK_ATTRIBUTE[] pTemplate)426 public void SetAttributeValue(uint hSession, uint hObj, CK_ATTRIBUTE[] pTemplate){ 427 428 C_SetAttributeValue proc= (C_SetAttributeValue)DelegateUtil.GetDelegate(this.hLib,typeof(C_SetAttributeValue)); 429 for(int i=0;i<pTemplate.Length;i++) 430 checkCKR(proc.Invoke(hSession,hObj, ref pTemplate[i], (uint)pTemplate.Length)); 431 } 432 433 /// <summary> 434 /// 435 /// </summary> 436 /// <param name="hSession"></param> 437 /// <param name="pTemplate"></param> FindObjectsInit(uint hSession, CK_ATTRIBUTE[] pTemplate)438 public void FindObjectsInit(uint hSession, CK_ATTRIBUTE[] pTemplate){ 439 440 C_FindObjectsInit proc= (C_FindObjectsInit)DelegateUtil.GetDelegate(this.hLib,typeof(C_FindObjectsInit)); 441 if(pTemplate==null || pTemplate.Length<1) 442 checkCKR(proc.Invoke(hSession, null, 0)); 443 else 444 checkCKR(proc.Invoke(hSession, pTemplate, (uint)pTemplate.Length)); 445 } 446 447 /// <summary> 448 /// 449 /// </summary> 450 /// <param name="hSession"></param> 451 /// <param name="maxCount"></param> 452 /// <returns></returns> FindObjects(uint hSession, uint maxCount)453 public uint[] FindObjects(uint hSession, uint maxCount){ 454 455 C_FindObjects proc= (C_FindObjects)DelegateUtil.GetDelegate(this.hLib,typeof(C_FindObjects)); 456 457 uint[] maxObjs=new uint[maxCount]; 458 459 uint pulCount=0; 460 461 /* get the objects */ 462 checkCKR(proc.Invoke(hSession, maxObjs,maxCount, ref pulCount)); 463 464 if(pulCount==maxCount){ 465 466 return maxObjs; 467 468 }else{/*if the count of the objects is less then maxcount then handle it */ 469 470 uint[] pulObjs=new uint[pulCount]; 471 472 Array.Copy(maxObjs,pulObjs,pulObjs.Length); 473 474 return pulObjs; 475 } 476 } 477 478 /// <summary> 479 /// 480 /// </summary> 481 /// <param name="hSession"></param> FindObjectsFinal(uint hSession)482 public void FindObjectsFinal(uint hSession){ 483 484 C_FindObjectsFinal proc= (C_FindObjectsFinal)DelegateUtil.GetDelegate(this.hLib,typeof(C_FindObjectsFinal)); 485 486 checkCKR(proc.Invoke(hSession)); 487 } 488 489 /// <summary> 490 /// 491 /// </summary> 492 /// <param name="hSession"></param> 493 /// <param name="pMechanism"></param> 494 /// <param name="hKey"></param> EncryptInit(uint hSession, CK_MECHANISM pMechanism, uint hKey)495 public void EncryptInit(uint hSession, CK_MECHANISM pMechanism, uint hKey){ 496 497 C_EncryptInit proc=(C_EncryptInit)DelegateUtil.GetDelegate(this.hLib,typeof(C_EncryptInit)); 498 499 checkCKR(proc.Invoke(hSession,ref pMechanism,hKey)); 500 } 501 502 /// <summary> 503 /// 504 /// </summary> 505 /// <param name="hSession"></param> 506 /// <param name="pData"></param> 507 /// <returns></returns> Encrypt(uint hSession, byte[] pData)508 public byte[] Encrypt(uint hSession, byte[] pData){ 509 510 C_Encrypt proc=(C_Encrypt)DelegateUtil.GetDelegate(this.hLib,typeof(C_Encrypt)); 511 512 uint size = 0; 513 514 checkCKR(proc.Invoke(hSession, pData,(uint)pData.Length, null, ref size)); 515 516 byte[] pEncryptedData=new byte[size]; 517 518 checkCKR(proc.Invoke(hSession, pData,(uint)pData.Length, pEncryptedData, ref size)); 519 520 return pEncryptedData; 521 } 522 523 /// <summary> 524 /// 525 /// </summary> 526 /// <param name="hSession"></param> 527 /// <param name="pPart"></param> 528 /// <returns></returns> EncryptUpdate(uint hSession, byte[] pPart)529 public byte[] EncryptUpdate(uint hSession, byte[] pPart){ 530 C_EncryptUpdate proc=(C_EncryptUpdate)DelegateUtil.GetDelegate(this.hLib,typeof(C_EncryptUpdate)); 531 532 uint size = 0; 533 534 checkCKR(proc.Invoke(hSession, pPart,(uint)pPart.Length, null, ref size)); 535 536 byte[] pEncryptedData=new byte[size]; 537 538 checkCKR(proc.Invoke(hSession, pPart,(uint)pPart.Length, pEncryptedData, ref size)); 539 540 return pEncryptedData; 541 } 542 543 /// <summary> 544 /// 545 /// </summary> 546 /// <param name="hSession"></param> 547 /// <returns></returns> EncryptFinal(uint hSession)548 public byte[] EncryptFinal(uint hSession){ 549 550 C_EncryptFinal proc=(C_EncryptFinal)DelegateUtil.GetDelegate(this.hLib,typeof(C_EncryptFinal)); 551 552 uint size = 0; 553 554 checkCKR(proc.Invoke(hSession, null, ref size)); 555 556 byte[] pEncryptedData=new byte[size]; 557 558 checkCKR(proc.Invoke(hSession, pEncryptedData, ref size)); 559 560 return pEncryptedData; 561 } 562 563 /// <summary> 564 /// 565 /// </summary> 566 /// <param name="hSession"></param> 567 /// <param name="pMechanism"></param> 568 /// <param name="hKey"></param> DecryptInit(uint hSession, CK_MECHANISM pMechanism, uint hKey)569 public void DecryptInit (uint hSession, CK_MECHANISM pMechanism, uint hKey){ 570 571 C_DecryptInit proc=(C_DecryptInit)DelegateUtil.GetDelegate(this.hLib,typeof(C_DecryptInit)); 572 573 checkCKR(proc.Invoke(hSession,ref pMechanism,hKey)); 574 } 575 576 /// <summary> 577 /// 578 /// </summary> 579 /// <param name="hSession"></param> 580 /// <param name="pEncryptedData"></param> 581 /// <returns></returns> Decrypt(uint hSession, byte[] pEncryptedData)582 public byte[] Decrypt(uint hSession, byte[] pEncryptedData){ 583 584 C_Decrypt proc=(C_Decrypt)DelegateUtil.GetDelegate(this.hLib,typeof(C_Decrypt)); 585 586 uint size = 0; 587 588 checkCKR(proc.Invoke(hSession, pEncryptedData,(uint)pEncryptedData.Length, null, ref size)); 589 590 byte[] pData=new byte[size]; 591 592 checkCKR(proc.Invoke(hSession, pEncryptedData,(uint)pEncryptedData.Length, pData, ref size)); 593 594 return pData; 595 } 596 597 /// <summary> 598 /// 599 /// </summary> 600 /// <param name="hSession"></param> 601 /// <param name="pEncryptedPart"></param> 602 /// <returns></returns> DecryptUpdate(uint hSession, byte[] pEncryptedPart)603 public byte[] DecryptUpdate(uint hSession, byte[] pEncryptedPart){ 604 605 C_DecryptUpdate proc=(C_DecryptUpdate)DelegateUtil.GetDelegate(this.hLib,typeof(C_DecryptUpdate)); 606 607 uint size = 0; 608 609 checkCKR(proc.Invoke(hSession, pEncryptedPart,(uint)pEncryptedPart.Length, null, ref size)); 610 611 byte[] pPart=new byte[size]; 612 613 checkCKR(proc.Invoke(hSession, pEncryptedPart,(uint)pEncryptedPart.Length, pPart, ref size)); 614 615 return pPart; 616 } 617 618 /// <summary> 619 /// 620 /// </summary> 621 /// <param name="hSession"></param> 622 /// <returns></returns> DecryptFinal(uint hSession)623 public byte[] DecryptFinal(uint hSession){ 624 625 C_DecryptFinal proc=(C_DecryptFinal)DelegateUtil.GetDelegate(this.hLib,typeof(C_DecryptFinal)); 626 627 uint size = 0; 628 629 checkCKR(proc.Invoke(hSession, null, ref size)); 630 631 byte[] pLastPart=new byte[size]; 632 633 checkCKR(proc.Invoke(hSession, pLastPart, ref size)); 634 635 return pLastPart; 636 } 637 638 639 /// <summary> 640 /// 641 /// </summary> 642 /// <param name="hSession"></param> 643 /// <param name="pMechanism"></param> 644 /// <param name="hKey"></param> DigestInit(uint hSession, CK_MECHANISM pMechanism)645 public void DigestInit (uint hSession, CK_MECHANISM pMechanism){ 646 647 C_DigestInit proc=(C_DigestInit)DelegateUtil.GetDelegate(this.hLib,typeof(C_DigestInit)); 648 649 checkCKR(proc.Invoke(hSession,ref pMechanism)); 650 } 651 652 653 /// <summary> 654 /// 655 /// </summary> 656 /// <param name="hSession"></param> 657 /// <param name="pData"></param> 658 /// <returns></returns> Digest(uint hSession, byte[] pData)659 public byte[] Digest(uint hSession, byte[] pData){ 660 661 C_Digest proc=(C_Digest)DelegateUtil.GetDelegate(this.hLib,typeof(C_Digest)); 662 663 uint size = 0; 664 665 checkCKR(proc.Invoke(hSession, pData,(uint)pData.Length, null, ref size)); 666 667 byte[] pDigest=new byte[size]; 668 669 checkCKR(proc.Invoke(hSession, pData,(uint)pData.Length, pDigest, ref size)); 670 671 return pDigest; 672 } 673 DigestUpdate(uint hSession, byte[] pPart)674 public void DigestUpdate(uint hSession, byte[] pPart){ 675 676 C_DigestUpdate proc=(C_DigestUpdate)DelegateUtil.GetDelegate(this.hLib,typeof(C_DigestUpdate)); 677 678 checkCKR(proc.Invoke(hSession, pPart,(uint)pPart.Length)); 679 680 return ; 681 } 682 683 /// <summary> 684 /// 685 /// </summary> 686 /// <param name="hSession"></param> 687 /// <param name="hKey"></param> DigestKey(uint hSession, uint hKey)688 public void DigestKey(uint hSession, uint hKey){ 689 690 C_DigestKey proc=(C_DigestKey)DelegateUtil.GetDelegate(this.hLib,typeof(C_DigestKey)); 691 692 checkCKR(proc.Invoke(hSession, hKey)); 693 } 694 695 /// <summary> 696 /// 697 /// </summary> 698 /// <param name="hSession"></param> 699 /// <returns></returns> DigestFinal(uint hSession)700 public byte[] DigestFinal(uint hSession){ 701 702 C_DigestFinal proc=(C_DigestFinal)DelegateUtil.GetDelegate(this.hLib,typeof(C_DigestFinal)); 703 704 uint size=0; 705 706 checkCKR(proc.Invoke(hSession, null,ref size)); 707 708 byte[] pDigest=new byte[size]; 709 710 checkCKR(proc.Invoke(hSession, pDigest,ref size)); 711 712 return pDigest; 713 } 714 715 /// <summary> 716 /// 717 /// </summary> 718 /// <param name="hSession"></param> 719 /// <param name="pMechanism"></param> 720 /// <param name="hKey"></param> SignInit(uint hSession, CK_MECHANISM pMechanism, uint hKey)721 public void SignInit (uint hSession, CK_MECHANISM pMechanism, uint hKey){ 722 C_SignInit proc=(C_SignInit)DelegateUtil.GetDelegate(this.hLib,typeof(C_SignInit)); 723 724 checkCKR(proc.Invoke(hSession,ref pMechanism,hKey)); 725 } 726 727 /// <summary> 728 /// 729 /// </summary> 730 /// <param name="hSession"></param> 731 /// <param name="pData"></param> 732 /// <returns></returns> Sign(uint hSession, byte[] pData)733 public byte[] Sign(uint hSession, byte[] pData){ 734 735 C_Sign proc=(C_Sign)DelegateUtil.GetDelegate(this.hLib,typeof(C_Sign)); 736 737 uint size = 0; 738 739 checkCKR(proc.Invoke(hSession, pData,(uint)pData.Length, null, ref size)); 740 741 byte[] pSignature=new byte[size]; 742 743 checkCKR(proc.Invoke(hSession, pData,(uint)pData.Length, pSignature, ref size)); 744 745 return pSignature; 746 } 747 748 /// <summary> 749 /// 750 /// </summary> 751 /// <param name="hSession"></param> 752 /// <param name="pPart"></param> SignUpdate(uint hSession, byte[] pPart)753 public void SignUpdate(uint hSession, byte[] pPart){ 754 755 C_SignUpdate proc=(C_SignUpdate)DelegateUtil.GetDelegate(this.hLib,typeof(C_SignUpdate)); 756 757 checkCKR(proc.Invoke(hSession, pPart,(uint)pPart.Length)); 758 759 return ; 760 } 761 762 /// <summary> 763 /// 764 /// </summary> 765 /// <param name="hSession"></param> 766 /// <returns></returns> SignFinal(uint hSession)767 public byte[] SignFinal(uint hSession){ 768 769 C_SignFinal proc=(C_SignFinal)DelegateUtil.GetDelegate(this.hLib,typeof(C_SignFinal)); 770 771 uint size=0; 772 773 checkCKR(proc.Invoke(hSession, null,ref size)); 774 775 byte[] pSignature=new byte[size]; 776 777 checkCKR(proc.Invoke(hSession, pSignature,ref size)); 778 779 return pSignature; 780 } 781 782 /// <summary> 783 /// 784 /// </summary> 785 /// <param name="hSession"></param> 786 /// <param name="pMechanism"></param> 787 /// <param name="hKey"></param> VerifyInit(uint hSession, CK_MECHANISM pMechanism, uint hKey)788 public void VerifyInit (uint hSession, CK_MECHANISM pMechanism, uint hKey){ 789 C_VerifyInit proc=(C_VerifyInit)DelegateUtil.GetDelegate(this.hLib,typeof(C_VerifyInit)); 790 791 checkCKR(proc.Invoke(hSession,ref pMechanism,hKey)); 792 } 793 794 /// <summary> 795 /// 796 /// </summary> 797 /// <param name="hSession"></param> 798 /// <param name="pData"></param> 799 /// <param name="signature"></param> Verify(uint hSession, byte[] pData, byte[] signature)800 public void Verify(uint hSession, byte[] pData, byte[] signature){ 801 802 C_Verify proc=(C_Verify)DelegateUtil.GetDelegate(this.hLib,typeof(C_Verify)); 803 804 checkCKR(proc.Invoke(hSession, pData,(uint)pData.Length, signature, (uint)signature.Length)); 805 } 806 807 VerifyUpdate(uint hSession, byte[] pPart)808 public void VerifyUpdate(uint hSession, byte[] pPart){ 809 810 C_VerifyUpdate proc=(C_VerifyUpdate)DelegateUtil.GetDelegate(this.hLib,typeof(C_VerifyUpdate)); 811 812 checkCKR(proc.Invoke(hSession, pPart,(uint)pPart.Length)); 813 } 814 815 /// <summary> 816 /// 817 /// </summary> 818 /// <param name="hSession"></param> 819 /// <returns></returns> VerifyFinal(uint hSession, byte[] signature)820 public void VerifyFinal(uint hSession, byte[] signature){ 821 822 C_VerifyFinal proc=(C_VerifyFinal)DelegateUtil.GetDelegate(this.hLib,typeof(C_VerifyFinal)); 823 824 checkCKR(proc.Invoke(hSession, signature, (uint)signature.Length )); 825 } 826 GenerateKey(uint hSession, CK_MECHANISM mech, CK_ATTRIBUTE[] template)827 public uint GenerateKey(uint hSession, CK_MECHANISM mech, CK_ATTRIBUTE[] template){ 828 C_GenerateKey proc=(C_GenerateKey)DelegateUtil.GetDelegate(this.hLib,typeof(C_GenerateKey)); 829 uint hKey=0; 830 checkCKR(proc.Invoke(hSession, ref mech, template, (uint)template.Length, ref hKey)); 831 return hKey; 832 } 833 GenerateKeyPair(uint hSession, CK_MECHANISM mech, CK_ATTRIBUTE[] pubKeyTemplate,CK_ATTRIBUTE[] privKeyTemplate)834 public KeyPairHandler GenerateKeyPair(uint hSession, CK_MECHANISM mech, CK_ATTRIBUTE[] pubKeyTemplate,CK_ATTRIBUTE[] privKeyTemplate){ 835 C_GenerateKeyPair proc=(C_GenerateKeyPair)DelegateUtil.GetDelegate(this.hLib,typeof(C_GenerateKeyPair)); 836 837 KeyPairHandler kp=new KeyPairHandler(); 838 checkCKR(proc.Invoke(hSession, ref mech, 839 pubKeyTemplate, (uint)pubKeyTemplate.Length, 840 privKeyTemplate, (uint)privKeyTemplate.Length, 841 ref kp.hPublicKey, 842 ref kp.hPrivateKey 843 ) 844 ); 845 return kp; 846 } 847 checkCKR(CKR retVal)848 protected void checkCKR(CKR retVal) 849 { 850 if (retVal != CKR.OK) 851 { 852 throw new TokenException(retVal); 853 } 854 } 855 } 856 }