EidAdapter.cs

Description du code

EidAdapter.cs est un fichier du projet BiblioBrol.
Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/src/.

Projet BiblioBrol :

Gestion de media en CSharp.

Pour plus d'infos, vous pouvez consulter la brève analyse.

Code source ou contenu du fichier

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using be.gaudry.bibliobrol.model.identity;
  5. using be.gaudry.model;
  6. using be.gaudry.bibliobrol.config;
  7.  
  8. namespace be.gaudry.bibliobrol.model.eid
  9. {
  10. class EidAdapter
  11. {
  12. //private const string RefactoredConfig.eidDllPath = "Eidlib.dll";
  13. #region Version Constants
  14. //
  15. // version info
  16. //
  17. private const int INTERFACE_VERSION = 1; // Changes each time the interface is modified
  18. private const int INTERFACE_COMPAT_VERSION = 1; // Stays until incompatible changes in existing functions
  19. #endregion
  20.  
  21. #region Field Constants
  22. internal const int MAX_CARD_NUMBER_LEN = 12;
  23. internal const int MAX_CHIP_NUMBER_LEN = 32;
  24. internal const int MAX_DATE_BEGIN_LEN = 10;
  25. internal const int MAX_DATE_END_LEN = 10;
  26. internal const int MAX_DELIVERY_MUNICIPALITY_LEN = 80;
  27. internal const int MAX_NATIONAL_NUMBER_LEN = 11;
  28. internal const int MAX_NAME_LEN = 110;
  29. internal const int MAX_FIRST_NAME1_LEN = 95;
  30. internal const int MAX_FIRST_NAME2_LEN = 50;
  31. internal const int MAX_FIRST_NAME3_LEN = 3;
  32. internal const int MAX_NATIONALITY_LEN = 3;
  33. internal const int MAX_BIRTHPLACE_LEN = 80;
  34. internal const int MAX_BIRTHDATE_LEN = 10;
  35. internal const int MAX_SEX_LEN = 1;
  36. internal const int MAX_NOBLE_CONDITION_LEN = 50;
  37. internal const int MAX_DOCUMENT_TYPE_LEN = 2;
  38. internal const int MAX_SPECIAL_STATUS_LEN = 2;
  39. internal const int MAX_HASH_PICTURE_LEN = 20;
  40. internal const int MAX_STREET_LEN = 80;
  41. internal const int MAX_STREET_NR = 10;
  42. internal const int MAX_STREET_BOX_NR = 6;
  43. internal const int MAX_ZIP_LEN = 4;
  44. internal const int MAX_MUNICIPALITY_LEN = 67;
  45. internal const int MAX_COUNTRY_LEN = 4;
  46. internal const int MAX_RAW_ADDRESS_LEN = 512;
  47. internal const int MAX_RAW_ID_LEN = 1024;
  48. internal const int MAX_PICTURE_LEN = 4096;
  49. internal const int MAX_CERT_LEN = 2048;
  50. internal const int MAX_CERT_NUMBER = 10;
  51. internal const int MAX_CERT_LABEL_LEN = 256;
  52. internal const int MAX_SIGNATURE_LEN = 256;
  53. internal const int MAX_CARD_DATA_LEN = 28;
  54. internal const int MAX_CARD_DATA_SIG_LEN = MAX_SIGNATURE_LEN + MAX_CARD_DATA_LEN;
  55. internal const int MAX_CHALLENGE_LEN = 20;
  56. internal const int MAX_RESPONSE_LEN = 128;
  57. #endregion
  58.  
  59. #region Constructors
  60. private EidAdapter()
  61. {
  62. }
  63. #endregion
  64.  
  65. #region Enumerations
  66. public enum ErrorCodeOptions : int
  67. {
  68. /// <summary>
  69. /// Function succeeded
  70. /// </summary>
  71. OK = 0,
  72. /// <summary>
  73. /// Unknown system error (see system error code)
  74. /// </summary>
  75. E_SYSTEM = 1,
  76. /// <summary>
  77. /// Unknown PC/SC error (see PC/SC error code)
  78. /// </summary>
  79. E_PCSC = 2,
  80. /// <summary>
  81. /// Unknown card error (see card status word)
  82. /// </summary>
  83. E_CARD = 3,
  84. /// <summary>
  85. /// Invalid parameter (NULL pointer, out of bound, etc.)
  86. /// </summary>
  87. E_BAD_PARAM = 4,
  88. /// <summary>
  89. /// An internal consistency check failed
  90. /// </summary>
  91. E_INTERNAL = 5,
  92. /// <summary>
  93. /// The supplied handle was invalid
  94. /// </summary>
  95. E_INVALID_HANDLE = 6,
  96. /// <summary>
  97. /// The data buffer to receive returned data is too small for the
  98. /// returned data
  99. /// </summary>
  100. E_INSUFFICIENT_BUFFER = 7,
  101. /// <summary>
  102. /// An internal communications error has been detected
  103. /// </summary>
  104. E_COMM_ERROR = 8,
  105. /// <summary>
  106. /// A specified timeout value has expired
  107. /// </summary>
  108. E_TIMEOUT = 9,
  109. /// <summary>
  110. /// Unknown card detected
  111. /// </summary>
  112. E_UNKNOWN_CARD = 10,
  113. /// <summary>
  114. /// Input on pinpad cancelled
  115. /// </summary>
  116. E_KEYPAD_CANCELLED = 11,
  117. /// <summary>
  118. /// Timout returned from pinpad
  119. /// </summary>
  120. E_KEYPAD_TIMEOUT = 12,
  121. /// <summary>
  122. /// The two PINs did not match
  123. /// </summary>
  124. E_KEYPAD_PIN_MISMATCH = 13,
  125. /// <summary>
  126. /// Message too long on pinpad
  127. /// </summary>
  128. E_KEYPAD_MSG_TOO_LONG = 14,
  129. /// <summary>
  130. /// Invalid PIN length
  131. /// </summary>
  132. E_INVALID_PIN_LENGTH = 15,
  133. /// <summary>
  134. /// Error in a signature verification or a certificate validation
  135. /// </summary>
  136. E_VERIFICATION = 16,
  137. /// <summary>
  138. /// Library not initialized
  139. /// </summary>
  140. E_NOT_INITIALIZED = 17,
  141. /// <summary>
  142. /// An internal error has been detected, but the source is unknown
  143. /// </summary>
  144. E_UNKNOWN = 18,
  145. /// <summary>
  146. /// Function is not supported
  147. /// </summary>
  148. E_UNSUPPORTED_FUNCTION = 19,
  149. /// <summary>
  150. /// Incorrect library version
  151. /// </summary>
  152. E_INCORRECT_VERSION = 20
  153. }
  154.  
  155. public enum OCSPPolicyOptions : int
  156. {
  157. /// <summary>
  158. /// OCSP Policy is Not Used
  159. /// </summary>
  160. NotUsed = 0,
  161. /// <summary>
  162. /// OCSP Policy is Optional
  163. /// </summary>
  164. Optional = 1,
  165. /// <summary>
  166. /// OCSP Policy is Mandatory
  167. /// </summary>
  168. Mandatory = 2
  169. }
  170.  
  171. public enum CRLPolicyOptions : int
  172. {
  173. /// <summary>
  174. /// CRL Policy is Not Used
  175. /// </summary>
  176. NotUsed = 0,
  177. /// <summary>
  178. /// CRL Policy is Optional
  179. /// </summary>
  180. Optional = 1,
  181. /// <summary>
  182. /// CRL Policy is Mandatory
  183. /// </summary>
  184. Mandatory = 2
  185. }
  186.  
  187. public enum PinTypeOptions : int
  188. {
  189. PKCS15 = 0,
  190. OS = 1
  191. }
  192.  
  193. public enum PinUsageOptions: int
  194. {
  195. Authentication = 1,
  196. Signing = 2
  197. }
  198.  
  199. #endregion
  200.  
  201. #region Structures
  202. [StructLayout(LayoutKind.Sequential)]
  203. public struct Status
  204. {
  205. /// <summary>
  206. /// General Error Code
  207. /// </summary>
  208. [MarshalAs(UnmanagedType.I4)]
  209. public ErrorCodeOptions General;
  210. /// <summary>
  211. /// System Error Code
  212. /// </summary>
  213. [MarshalAs(UnmanagedType.I4)]
  214. public int System;
  215. /// <summary>
  216. /// PC/SC Error Code
  217. /// </summary>
  218. [MarshalAs(UnmanagedType.I4)]
  219. public int PCSC;
  220. /// <summary>
  221. /// Card Status Word
  222. /// </summary>
  223. [MarshalAs(UnmanagedType.I2)]
  224. public short CSW;
  225. /// <summary>
  226. /// Reserved for future use
  227. /// </summary>
  228. [MarshalAs(UnmanagedType.I2)]
  229. public short RFU1;
  230. [MarshalAs(UnmanagedType.I2)]
  231. public short RFU2;
  232. [MarshalAs(UnmanagedType.I2)]
  233. public short RFU3;
  234. }
  235. [StructLayout(LayoutKind.Sequential)]
  236. public unsafe struct BEID_Bytes
  237. {
  238. public const int BEID_MAX_PICTURE_LEN = 4096;
  239.  
  240. public byte* data;
  241. #if ARCHITECTURE_x86_64
  242. public ulong length;
  243. #else
  244. public uint length;
  245. #endif
  246. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
  247. public byte[] rfu;
  248. };
  249. public class Bytes : DisposableObject
  250. {
  251. public BytePtr Data;
  252. public int Length;
  253. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.U1)]
  254. public byte[] RFU;
  255.  
  256. public Bytes(int length)
  257. {
  258. //
  259. // init
  260. //
  261. this.Data = new BytePtr(length);
  262. this.Length = length;
  263. this.RFU = new byte[6];
  264. }
  265.  
  266. public Bytes(byte[] data)
  267. {
  268. //
  269. // init
  270. //
  271. this.Data = new BytePtr(data);
  272. this.Length = data.Length;
  273. this.RFU = new byte[6];
  274. }
  275.  
  276.  
  277.  
  278. protected override void ReleaseUnmanagedResources()
  279. {
  280. BytePtr.Free(Data);
  281. }
  282. };
  283.  
  284. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 4)]
  285. public struct Certif
  286. {
  287. /// <summary>
  288. /// byte stream encoded certificate
  289. /// </summary>
  290. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_CERT_LEN, ArraySubType = UnmanagedType.U1)]
  291. public byte[] Data;
  292. /// <summary>
  293. /// Size in bytes of the encoded certificate
  294. /// </summary>
  295. public int Length;
  296. /// <summary>
  297. /// Label of the certificate (Authentication, Signature, CA, Root,…)
  298. /// </summary>
  299. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_CERT_LABEL_LEN + 1, ArraySubType = UnmanagedType.I1)]
  300. public char[] Label;
  301. /// <summary>
  302. /// Validation status
  303. /// </summary>
  304. public int Status;
  305. /// <summary>
  306. /// reserved for future use
  307. /// </summary>
  308. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.U1)]
  309. public byte[] RFU;
  310. };
  311.  
  312. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 4)]
  313. public struct CertifCheck
  314. {
  315. /// <summary>
  316. /// Policy used: 0=None/1=OCSP/2=CRL
  317. /// </summary>
  318. public int Policy;
  319. /// <summary>
  320. /// Array of BEID_Certif structures
  321. /// </summary>
  322. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_CERT_NUMBER * 2324, ArraySubType = UnmanagedType.U1)]
  323. public byte[] Certificates;
  324. /// <summary>
  325. /// Number of elements in Array
  326. /// </summary>
  327. public int Length;
  328. /// <summary>
  329. /// Status of signature (for ID and Address) or hash (for Picture) on retrieved field
  330. /// </summary>
  331. public int SignatureCheck;
  332. /// <summary>
  333. /// reserved for future use
  334. /// </summary>
  335. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.U1)]
  336. public byte[] RFU;
  337. };
  338.  
  339. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  340. public struct IDData
  341. {
  342. [MarshalAs(UnmanagedType.I2)]
  343. public short Version;
  344. [MarshalAs(UnmanagedType.ByValArray,
  345. SizeConst = MAX_CARD_NUMBER_LEN + 1)]
  346. public char[] CardNumber;
  347. [MarshalAs(UnmanagedType.ByValArray,
  348. SizeConst = MAX_CHIP_NUMBER_LEN + 1)]
  349. public char[] ChipNumber;
  350. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DATE_BEGIN_LEN + 1)]
  351. public char[] ValidityDateBegin;
  352. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DATE_END_LEN + 1)]
  353. public char[] ValidityDateEnd;
  354. [MarshalAs(UnmanagedType.ByValArray, SizeConst =
  355. MAX_DELIVERY_MUNICIPALITY_LEN + 1)]
  356. public byte[] Municipality;
  357. [MarshalAs(UnmanagedType.ByValArray,
  358. SizeConst = MAX_NATIONAL_NUMBER_LEN + 1)]
  359. public char[] NationalNumber;
  360. [MarshalAs(UnmanagedType.ByValArray,
  361. SizeConst = MAX_NAME_LEN + 1)]
  362. public byte[] Name;
  363. [MarshalAs(UnmanagedType.ByValArray,
  364. SizeConst = MAX_FIRST_NAME1_LEN + 1)]
  365. public byte[] FirstName1;
  366. [MarshalAs(UnmanagedType.ByValArray,
  367. SizeConst = MAX_FIRST_NAME2_LEN + 1)]
  368. public byte[] FirstName2;
  369. [MarshalAs(UnmanagedType.ByValArray,
  370. SizeConst = MAX_FIRST_NAME3_LEN + 1)]
  371. public byte[] FirstName3;
  372. [MarshalAs(UnmanagedType.ByValArray,
  373. SizeConst = MAX_NATIONALITY_LEN + 1)]
  374. public char[] Nationality;
  375. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_BIRTHPLACE_LEN + 1)]
  376. public byte[] BirthLocation;
  377. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_BIRTHDATE_LEN + 1)]
  378. public char[] BirthDate;
  379. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_SEX_LEN + 1)]
  380. public byte[] Sex;
  381. [MarshalAs(UnmanagedType.ByValArray,
  382. SizeConst = MAX_NOBLE_CONDITION_LEN + 1)]
  383. public byte[] NobleCondition;
  384. [MarshalAs(UnmanagedType.I4)]
  385. public int DocumentType;
  386. [MarshalAs(UnmanagedType.Bool)]
  387. public bool WhiteCane;
  388. [MarshalAs(UnmanagedType.Bool)]
  389. public bool YellowCane;
  390. [MarshalAs(UnmanagedType.Bool)]
  391. public bool ExtendedMinority;
  392. [MarshalAs(UnmanagedType.ByValArray,
  393. SizeConst = MAX_HASH_PICTURE_LEN, ArraySubType = UnmanagedType.U1)]
  394. public byte[] HashPhoto;
  395. [MarshalAs(UnmanagedType.ByValArray,
  396. SizeConst = 6, ArraySubType = UnmanagedType.U1)]
  397. public byte[] RFU;
  398. };
  399.  
  400.  
  401. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  402. public struct VersionInfo
  403. {
  404. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  405. public byte[] SerialNumber;
  406. [MarshalAs(UnmanagedType.U1)]
  407. public byte ComponentCode;
  408. [MarshalAs(UnmanagedType.U1)]
  409. public byte OSNumber;
  410. [MarshalAs(UnmanagedType.U1)]
  411. public byte OSVersion;
  412. [MarshalAs(UnmanagedType.U1)]
  413. public byte SoftmaskNumber;
  414. [MarshalAs(UnmanagedType.U1)]
  415. public byte SoftmaskVersion;
  416. [MarshalAs(UnmanagedType.U1)]
  417. public byte AppletVersion;
  418. [MarshalAs(UnmanagedType.U2)]
  419. public short GlobalOSVersion;
  420. [MarshalAs(UnmanagedType.U1)]
  421. public byte AppletInterfaceVersion;
  422. [MarshalAs(UnmanagedType.U1)]
  423. public byte PKCS1Support;
  424. [MarshalAs(UnmanagedType.U1)]
  425. public byte KeyExchangeVersion;
  426. [MarshalAs(UnmanagedType.U1)]
  427. public byte ApplicationLifeCycle;
  428. [MarshalAs(UnmanagedType.U1)]
  429. public byte GraphPerso;
  430. [MarshalAs(UnmanagedType.U1)]
  431. public byte ElecPerso;
  432. [MarshalAs(UnmanagedType.U1)]
  433. public byte ElecPersoInterface;
  434. [MarshalAs(UnmanagedType.U1)]
  435. public byte Reserved;
  436. [MarshalAs(UnmanagedType.ByValArray,
  437. SizeConst = 6, ArraySubType = UnmanagedType.U1)]
  438. public byte[] RFU;
  439. }
  440.  
  441. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  442. public struct Address
  443. {
  444. [MarshalAs(UnmanagedType.U2)]
  445. public short Version;
  446. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_STREET_LEN + 1)]
  447. public byte[] Street;
  448. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_STREET_NR + 1)]
  449. public char[] StreetNumber;
  450. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_STREET_BOX_NR + 1)]
  451. public char[] BoxNumber;
  452. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_ZIP_LEN + 1)]
  453. public char[] Zip;
  454. [MarshalAs(UnmanagedType.ByValArray,
  455. SizeConst = MAX_MUNICIPALITY_LEN + 1)]
  456. public byte[] Municipality;
  457. [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_COUNTRY_LEN + 1)]
  458. public char[] Country;
  459. [MarshalAs(UnmanagedType.ByValArray,
  460. SizeConst = 6, ArraySubType = UnmanagedType.U1)]
  461. public byte[] RFU;
  462. };
  463.  
  464. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  465. public struct Pin
  466. {
  467. [MarshalAs(UnmanagedType.I4)]
  468. PinTypeOptions PinType;
  469. [MarshalAs(UnmanagedType.U1)]
  470. byte ID;
  471. [MarshalAs(UnmanagedType.I4)]
  472. PinUsageOptions PinUsage;
  473. [MarshalAs(UnmanagedType.LPStr)]
  474. string ShortUsage;
  475. [MarshalAs(UnmanagedType.LPStr)]
  476. string LongUsage;
  477. [MarshalAs(UnmanagedType.ByValArray,
  478. SizeConst = 6, ArraySubType = UnmanagedType.U1)]
  479. public byte[] RFU;
  480.  
  481. public Pin(byte id, PinTypeOptions type,
  482. PinUsageOptions usage, string shortUsage,string longUsage)
  483. {
  484. this.ID = id;
  485. this.PinType = type;
  486. this.PinUsage = usage;
  487. this.ShortUsage = shortUsage;
  488. this.LongUsage = longUsage;
  489. this.RFU = new byte[6];
  490. }
  491. };
  492. #endregion
  493.  
  494. #region API
  495. [DllImport(Config.EidDllPath,
  496. EntryPoint="BEID_InitEx", SetLastError=true)]
  497. private static extern Status InitEx(string readerName,
  498. OCSPPolicyOptions ocsp, CRLPolicyOptions crl, out IntPtr cardHandle,
  499. int interfaceVersion, int interfaceCompVersion);
  500.  
  501. [DllImport(Config.EidDllPath, EntryPoint="BEID_Exit",
  502. SetLastError=true)]
  503. internal static extern Status Exit();
  504.  
  505. [DllImport(Config.EidDllPath, EntryPoint="BEID_GetID",
  506. SetLastError=true)]
  507. internal static extern Status GetID(out IDData data, out CertifCheck check);
  508.  
  509. [DllImport(Config.EidDllPath, EntryPoint="BEID_GetAddress",
  510. SetLastError=true)]
  511. internal static extern Status GetAddress(out Address address,
  512. out CertifCheck check);
  513.  
  514. [DllImport(Config.EidDllPath, EntryPoint="BEID_GetPicture",
  515. SetLastError=true)]
  516. private static extern Status GetPicture(
  517. ref BEID_Bytes bytes,
  518. out CertifCheck check
  519. );
  520. //
  521. // Mid Level API
  522. //
  523. [DllImport(Config.EidDllPath, EntryPoint="BEID_GetVersionInfo",
  524. SetLastError=true)]
  525. internal static extern Status GetVersionInfo(ref VersionInfo versionInfo,
  526. bool signature, Bytes SignedStatus);
  527.  
  528. [DllImport(Config.EidDllPath, EntryPoint="BEID_BeginTransaction",
  529. SetLastError=true)]
  530. internal static extern Status BeginTransaction();
  531.  
  532. [DllImport(Config.EidDllPath, EntryPoint="BEID_EndTransaction",
  533. SetLastError=true)]
  534. internal static extern Status EndTransaction();
  535.  
  536. [DllImport(Config.EidDllPath, EntryPoint="BEID_SelectApplication",
  537. SetLastError=true)]
  538. internal static extern Status SelectApplication(Bytes Application);
  539.  
  540. [DllImport(Config.EidDllPath, EntryPoint="BEID_VerifyPIN",
  541. SetLastError=true)]
  542. internal static extern Status VerifyPIN(ref Pin pinData, string pin,
  543. out int triesLeft);
  544.  
  545. [DllImport(Config.EidDllPath, EntryPoint="BEID_ChangePIN",
  546. SetLastError=true)]
  547. internal static extern Status ChangePin(ref Pin pinData, string oldPin,
  548. string newPin, out int triesLeft);
  549.  
  550. [DllImport(Config.EidDllPath, EntryPoint="BEID_GetPINStatus",
  551. SetLastError=true)]
  552. internal static extern Status GetPinStatus(ref Pin pinData,
  553. out int triesLeft, bool signature, out Bytes signedStatus);
  554.  
  555. [DllImport(Config.EidDllPath, EntryPoint="BEID_ReadFile",
  556. SetLastError=true)]
  557. internal static extern Status ReadFile(Bytes fileID, Bytes outData,
  558. ref Pin pinData);
  559.  
  560. [DllImport(Config.EidDllPath, EntryPoint="BEID_WriteFile",
  561. SetLastError=true)]
  562. internal static extern Status WriteFile(Bytes fileID, Bytes inData,
  563. ref Pin pinData);
  564. //
  565. // Low Level API
  566. //
  567. [DllImport(Config.EidDllPath, EntryPoint="BEID_FlushCache",
  568. SetLastError=true)]
  569. internal static extern Status FlushCache();
  570.  
  571. [DllImport(Config.EidDllPath, EntryPoint="BEID_SendAPDU",
  572. SetLastError=true)]
  573. internal static extern Status SendAPDU(Bytes cmdAPDU, ref Pin pinData,
  574. out Bytes respAPDU);
  575.  
  576. #endregion
  577.  
  578.  
  579. /// <summary>
  580. ///
  581. /// </summary>
  582. /// <param name="readerName">Card reader name as know by the OS. Empty string to use default reader.</param>
  583. /// <param name="ocsp">Value of enum OCSPPolicyOptions</param>
  584. /// <param name="crl">Value of enum CRLPolicyOptions</param>
  585. /// <param name="cardHandle"></param>
  586. /// <exception cref="System.DllNotFoundException">In this case, it will be impossible to do anything with the eid</exception>
  587. /// <returns></returns>
  588. internal static Status Init(string readerName, OCSPPolicyOptions ocsp,
  589. CRLPolicyOptions crl, out IntPtr cardHandle)
  590. {
  591. return InitEx(readerName, ocsp, crl,
  592. out cardHandle, INTERFACE_VERSION, INTERFACE_COMPAT_VERSION);
  593. }
  594. /// <summary>
  595. /// Init with default reader, no OCSPPolicyOptions and no CRLPolicyOptions
  596. /// </summary>
  597. /// <param name="readerName">Card reader name as know by the OS. Empty string to use default reader.</param>
  598. /// <exception cref="System.DllNotFoundException">In this case, it will be impossible to do anything with the eid</exception>
  599. /// <returns></returns>
  600. internal static Status Init()
  601. {
  602. IntPtr handle;
  603. return InitEx(
  604. "",
  605. EidAdapter.OCSPPolicyOptions.NotUsed,
  606. EidAdapter.CRLPolicyOptions.NotUsed,
  607. out handle,
  608. INTERFACE_VERSION,
  609. INTERFACE_COMPAT_VERSION
  610. );
  611. }
  612.  
  613. public static unsafe Status getPicture(out System.Drawing.Bitmap picture, out CertifCheck check)
  614. {
  615. BEID_Bytes _PictureData = new BEID_Bytes();
  616. Status status;
  617. byte [] _pictureBuffer;
  618. _pictureBuffer = new byte[MAX_PICTURE_LEN];
  619. _PictureData.length = MAX_PICTURE_LEN;
  620. fixed (byte* p = _pictureBuffer)
  621. {
  622. _PictureData.data = p;
  623. status = GetPicture(ref _PictureData, out check);
  624. }
  625. if (status.General == ErrorCodeOptions.OK)
  626. {
  627. System.IO.MemoryStream ms = new System.IO.MemoryStream(_pictureBuffer);
  628. //Console.WriteLine("GetPicture : " + ms.Length);
  629. picture = new System.Drawing.Bitmap(ms);
  630. return status;
  631. }
  632. else
  633. {
  634. picture = null;
  635. }
  636. return status;
  637. }
  638. public static bool testDllExists()
  639. {
  640. bool result = false;
  641. try
  642. {
  643. IntPtr handle;
  644. EidAdapter.Init(
  645. "",
  646. EidAdapter.OCSPPolicyOptions.NotUsed,
  647. EidAdapter.CRLPolicyOptions.NotUsed,
  648. out handle);
  649. EidAdapter.Exit();
  650. result = true;
  651. }
  652. catch (DllNotFoundException)
  653. {
  654.  
  655. }
  656. return result;
  657. }
  658. /// <summary>
  659. ///
  660. /// </summary>
  661. /// <param name="readCardBGW">BackgroundWorker to do an asynchroneous reading</param>
  662. /// <param name="e">DoWorkEventArgs to report progress and results of job</param>
  663. /// <exception cref="System.DllNotFoundException">In this case, it will be impossible to do anything with the eid</exception>
  664. /// <param name="user"></param>
  665. public static void readCard(System.ComponentModel.BackgroundWorker readCardBGW, System.ComponentModel.DoWorkEventArgs e, User user)
  666. {
  667. readCardBGW.ReportProgress(0, "Initialisation du lecteur");
  668. IntPtr handle;
  669. EidAdapter.Init(
  670. "",
  671. EidAdapter.OCSPPolicyOptions.NotUsed,
  672. EidAdapter.CRLPolicyOptions.NotUsed,
  673. out handle);
  674. EidAdapter.Status status;
  675. EidAdapter.CertifCheck check;
  676.  
  677. EidAdapter.IDData data;
  678.  
  679.  
  680. readCardBGW.ReportProgress(15, "Lecture de l'identité");
  681. //System.Diagnostics.Debug.Print("GetID : " + status.General);
  682. try
  683. {
  684. status = EidAdapter.GetID(out data, out check);
  685. //readCardBGW.ReportProgress(40, new object[] { status, data });
  686. if (EidAdapter.ErrorCodeOptions.OK == status.General)
  687. {
  688. EidValidity validity = new EidValidity();
  689.  
  690. validity.BeginDate = getDate(data.ValidityDateBegin);
  691. validity.EndDate = getDate(data.ValidityDateEnd);
  692. validity.Issuer = getString(data.Municipality);
  693. validity.ChipNumber = getString(data.ChipNumber);
  694. validity.CardNumber = getString(data.CardNumber);
  695.  
  696. readCardBGW.ReportProgress(30, validity);
  697.  
  698. user.LastName = getString(data.Name);
  699. user.FirstName = getString(data.FirstName1);
  700. user.FirstName2 = getString(data.FirstName2);
  701. user.FirstName3 = getString(data.FirstName3);
  702.  
  703. user.Birthdate = getDate(data.BirthDate);
  704.  
  705. try
  706. {
  707. user.Sex = (SEX)Enum.Parse(
  708. typeof(SEX),
  709. getString(data.Sex)
  710. );
  711. }
  712. catch(ArgumentException)
  713. {
  714. user.Sex = SEX._;
  715. }
  716. user.NationalNumber = getString(data.NationalNumber);
  717. user.BirthPlace = getString(data.BirthLocation);
  718. user.Nationality = getString(data.Nationality);
  719.  
  720. readCardBGW.ReportProgress(40, user);
  721. }
  722.  
  723. readCardBGW.ReportProgress(45, "Lecture de l'adresse");
  724. EidAdapter.Address address;
  725. status = EidAdapter.GetAddress(out address, out check);
  726. if (EidAdapter.ErrorCodeOptions.OK == status.General)
  727. {
  728. be.gaudry.bibliobrol.model.identity.Address ad = new be.gaudry.bibliobrol.model.identity.Address();
  729. ad.Street = getString(address.Street);
  730. ad.HouseNumber = getString(address.StreetNumber);
  731. ad.BoxNumber = getString(address.BoxNumber);
  732. ad.ZipCode = getString(address.Zip);
  733. ad.Municipality = getString(address.Municipality);
  734. ad.Country = getString(address.Country);
  735. user.Address = ad;
  736. readCardBGW.ReportProgress(60, user);
  737. }
  738.  
  739. System.Drawing.Bitmap photo;
  740. readCardBGW.ReportProgress(65, "Lecture de la photo");
  741. status = EidAdapter.getPicture(out photo, out check);
  742. if (EidAdapter.ErrorCodeOptions.OK == status.General)
  743. {
  744. readCardBGW.ReportProgress(90, photo);
  745. }
  746. EidAdapter.Exit();
  747. readCardBGW.ReportProgress(100, "Chargement terminé");
  748. }
  749. catch (AccessViolationException)
  750. {
  751. readCardBGW.ReportProgress(100, "Erreur de lecture");
  752. }
  753. }
  754.  
  755. /// <summary>
  756. ///
  757. /// </summary>
  758. /// <param name="characters"></param>
  759. /// <remarks>DEPRECATED</remarks>
  760. /// <returns></returns>
  761. public static string UTF8ByteArrayToString(byte[] characters)
  762. {
  763. int byteCount = 0;
  764. while (characters[byteCount] != 0)
  765. {
  766. byteCount++;
  767. }
  768.  
  769. UTF8Encoding encoding = new UTF8Encoding();
  770. string constructedString = encoding.GetString(characters, 0, byteCount);
  771. return (constructedString);
  772. }
  773. /// <summary>
  774. /// Get a string from a chars array (readed from eid card), and clean it
  775. /// </summary>
  776. /// <param name="str"></param>
  777. /// <returns></returns>
  778. private static string getString(char[] str)
  779. {
  780. return new string(str).Replace("\0", "").Trim();
  781. }
  782.  
  783. /// <summary>
  784. /// Get a string from a bytes array (readed from eid card), and clean it
  785. /// </summary>
  786. /// <param name="str"></param>
  787. /// <returns></returns>
  788. private static string getString(byte[] str)
  789. {
  790. return Encoding.UTF8.GetString(str).Replace("\0", "").Trim();
  791. }
  792.  
  793. /// <summary>
  794. /// Get a DateTime from a chars array (readed from eid card), and clean it
  795. /// </summary>
  796. /// <param name="str"></param>
  797. /// <returns></returns>
  798. private static DateTime getDate(char[] str)
  799. {
  800. return getDate(getString(str));
  801. }
  802.  
  803. public static DateTime getDate(string value)
  804. {
  805. if (value.Equals(string.Empty))
  806. return new DateTime(0L);
  807. try
  808. {
  809. int year, month, day;
  810. int.TryParse(value.Substring(0, 4), out year);
  811. int.TryParse(value.Substring(4, 2), out month);
  812. int.TryParse(value.Substring(6, 2), out day);
  813. return new DateTime(year, month, day);
  814. }
  815. catch (ArgumentOutOfRangeException)
  816. {
  817. return new DateTime(0L);
  818. }
  819. }
  820. }
  821. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/bibliobrol/src/model/eid/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1719974844 03/07/2024 04:47:24
Fichiers contenus dans /var/www/bin/sniplets/bibliobrol/src/model/eid/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .cs|.csEidAdapter.cs27.46 Ko31/10/2018 18:32:54-refusé-
Afficher le fichier .cs|.csMemory.cs4.31 Ko31/10/2018 18:32:54-refusé-
Afficher le fichier .cs|.csBytePtr.cs3.12 Ko31/10/2018 18:32:54-refusé-
Afficher le fichier .cs|.csStringPtr.cs1.81 Ko31/10/2018 18:32:55-refusé-
Afficher le fichier .cs|.csEidPerson.cs171 octets31/10/2018 18:32:54-refusé-

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

Document créé le 16/10/2009, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/cs-bibliobrol-source-rf-model/eid/EidAdapter.cs.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.