VerifyCertificate.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136
  1. #include "StdAfx.h"
  2. #include "VerifyCertificate.h"
  3. time_t NowTime()
  4. {
  5. time_t t_Now = time(0);
  6. struct tm* tm_Now = localtime(&t_Now);
  7. tm_Now->tm_hour =0;
  8. tm_Now->tm_min = 0;
  9. tm_Now->tm_sec = 0;
  10. return mktime(tm_Now);
  11. }
  12. X509* LoadCert(const char * cert, const int certlen, const char *p12pass, const int format)
  13. {
  14. //CLogFile* log = new CLogFile("log.txt");
  15. //log->LOGERROR("test, %s", cert);
  16. if (NULL == cert)
  17. {
  18. //log->LOGERROR("Input cert path is null");
  19. printf("Input cert path is null");
  20. return NULL;
  21. }
  22. X509 * x509=NULL;
  23. BIO * in=NULL;
  24. if(certlen==0)
  25. {
  26. if((in=BIO_new_file(cert, "r"))==NULL)
  27. {
  28. //log->LOGERROR("BIO_new_file err, file path is %s", cert);
  29. return NULL;
  30. }
  31. }
  32. else
  33. {
  34. if((in=BIO_new_mem_buf((void*)cert,certlen))== NULL)
  35. {
  36. //log->LOGERROR("BIO_new_mem_buf err, file path is %s", cert);
  37. return NULL;
  38. }
  39. }
  40. if(format==FORMAT_DER)
  41. {
  42. x509=d2i_X509_bio(in,NULL);
  43. }
  44. else if(format==FORMAT_PEM)
  45. {
  46. x509=PEM_read_bio_X509(in,NULL,NULL,NULL);
  47. }
  48. else if(format==FORMAT_P12)
  49. {
  50. PKCS12 *p12 = d2i_PKCS12_bio(in, NULL);
  51. PKCS12_parse(p12, p12pass, NULL, &x509, NULL); PKCS12_free(p12);
  52. }
  53. else
  54. {
  55. BIO_free(in);
  56. return NULL;
  57. }
  58. BIO_free(in);
  59. if(x509)
  60. {
  61. return x509;
  62. }
  63. else
  64. {
  65. //log->LOGERROR("PEM_read_bio_X509 return null,file path is %s", cert);
  66. return NULL;
  67. }
  68. }
  69. X509_CRL* LoadCrl(const char *crlFile)
  70. {
  71. if (NULL == crlFile)
  72. {
  73. printf("Input crl path is null");
  74. return NULL;
  75. }
  76. X509_CRL *crl = NULL;
  77. BIO *in = NULL;
  78. if((in=BIO_new_file(crlFile, "r"))==NULL)
  79. {
  80. return NULL;
  81. }
  82. crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
  83. BIO_free(in);
  84. return crl;
  85. }
  86. //DWORD CheckCertLife(const char *pubCert, const int pubCertLen, const int certFormat)
  87. //{
  88. // //CLogFile* log = new CLogFile("log.txt");
  89. // //log->LOGERROR("test, %s", pubCert);
  90. // DWORD utcTime = 0;
  91. // struct tm *ptm = NULL;
  92. //
  93. // if (NULL == pubCert)
  94. // {
  95. // return INPUT_NULL_ERR;
  96. // }
  97. //
  98. // X509 *x509=LoadCert(pubCert,pubCertLen,NULL,certFormat);
  99. // if (NULL == x509)
  100. // {
  101. // return LOAD_CERT_ERR;
  102. // }
  103. //
  104. // //以加密锁内时钟为基准时间
  105. // int nRet = GetUTCTimeFromUkey(&utcTime);
  106. // if (nRet != DONGLE_SUCCESS)
  107. // {
  108. // return GET_UKEY_CLOCK_ERR;
  109. // }
  110. //
  111. // //小时+8转换为标准时间
  112. // if (sizeof(int) == 4)
  113. // {
  114. // ptm = _gmtime32((const __time32_t *)&utcTime);
  115. // }
  116. // else if(sizeof(int)==8)
  117. // {
  118. // ptm = _gmtime64((const __time64_t *)&utcTime);
  119. // }
  120. // else
  121. // {
  122. // ptm = gmtime((const time_t *)&utcTime);
  123. // }
  124. // ptm->tm_hour += 8;
  125. //
  126. // time_t ct = mktime(ptm);
  127. //
  128. // //CTime time = CTime::GetCurrentTime();
  129. // //time_t ct = time.GetTime();
  130. //
  131. // asn1_string_st *before=X509_get_notBefore(x509), *after=X509_get_notAfter(x509);
  132. // ASN1_UTCTIME *be=ASN1_STRING_dup(before), *af=ASN1_STRING_dup(after);
  133. //
  134. // int ret = ASN1_UTCTIME_cmp_time_t(be,ct);
  135. // if (ASN1_UTCTIME_cmp_time_t(be,ct) >= 0)
  136. // {
  137. // return CERT_NOT_START_VALIDITY_ERR;
  138. // }
  139. //
  140. // if (ASN1_UTCTIME_cmp_time_t(af,ct) <= 0)
  141. // {
  142. // return CERT_OVERTIME_ERR;
  143. // }
  144. //
  145. // M_ASN1_UTCTIME_free(be);
  146. // M_ASN1_UTCTIME_free(af);
  147. // X509_free(x509);
  148. //
  149. // return SUCCESS;
  150. //}
  151. DWORD CheckCertLife(const char *pubCert, const int pubCertLen, const int certFormat)
  152. {
  153. //CLogFile* log = new CLogFile("log.txt");
  154. //log->LOGERROR("test, %s", pubCert);
  155. DWORD utcTime = 0;
  156. struct tm *ptm = NULL;
  157. if (NULL == pubCert)
  158. {
  159. return INPUT_NULL_ERR;
  160. }
  161. X509 *x509=LoadCert(pubCert,pubCertLen,NULL,certFormat);
  162. if (NULL == x509)
  163. {
  164. return LOAD_CERT_ERR;
  165. }
  166. time_t ct = NowTime();
  167. asn1_string_st *before=X509_get_notBefore(x509), *after=X509_get_notAfter(x509);
  168. ASN1_UTCTIME *be=ASN1_STRING_dup(before), *af=ASN1_STRING_dup(after);
  169. int ret = ASN1_UTCTIME_cmp_time_t(be,ct);
  170. if (ASN1_UTCTIME_cmp_time_t(be,ct) >= 0)
  171. {
  172. return CERT_NOT_START_VALIDITY_ERR;
  173. }
  174. if (ASN1_UTCTIME_cmp_time_t(af,ct) <= 0)
  175. {
  176. return CERT_OVERTIME_ERR;
  177. }
  178. M_ASN1_UTCTIME_free(be);
  179. M_ASN1_UTCTIME_free(af);
  180. X509_free(x509);
  181. return SUCCESS;
  182. }
  183. DWORD VerifCrl(const char *strCrlFile, const char *pchCaCertFile)
  184. {
  185. if (NULL == strCrlFile || NULL == pchCaCertFile)
  186. {
  187. printf("Input parameter is null,crl path=%s, ca cert path=%s\n", strCrlFile, pchCaCertFile);
  188. return INPUT_NULL_ERR;
  189. }
  190. X509_CRL *crl = LoadCrl(strCrlFile);
  191. if (NULL == crl)
  192. {
  193. printf("load %s err\n",strCrlFile);
  194. return LOAD_CRL_ERR;
  195. }
  196. X509 *cert = LoadCert(pchCaCertFile, 0, NULL, FORMAT_PEM);
  197. if (NULL == cert)
  198. {
  199. printf("load %s err\n", pchCaCertFile);
  200. return LOAD_CERT_ERR;
  201. }
  202. EVP_PKEY *pubkey = X509_get_pubkey(cert);
  203. if (!pubkey)
  204. {
  205. printf("get CRL issuer public key from %s err\n", pchCaCertFile);
  206. return GET_PUBKEY_FROM_CERT_ERR;
  207. }
  208. //RSA* rasKey = RSA_new();
  209. //ReadPublicKeyFile(pchCaCertFile,rasKey);
  210. //ReadPrivateKeyFile(pchCaCertFile,NULL, &rasKey);
  211. //EVP_PKEY *pubkey = EVP_PKEY_new();
  212. //EVP_PKEY_assign_RSA(pubkey,rasKey);
  213. //if (0 == X509_CRL_verify(crl,cert->cert_info->key->pkey))
  214. int ret = X509_CRL_verify(crl,pubkey);
  215. if (ret < 0)
  216. {
  217. printf("Verify CRL failure,crl path=%s, ca cert path=%s\n", strCrlFile, pchCaCertFile);
  218. return CRL_VERIFY_ERR;
  219. }
  220. //下面会导致循环的直接崩溃
  221. //EVP_PKEY_free(pubkey);
  222. X509_CRL_free(crl);
  223. X509_free(cert);
  224. return SUCCESS;
  225. }
  226. DWORD CheckCertWithCrl(const char *pubCert, const int pubCertLen, const int certFormat, const char *crlData, const int crlLen)
  227. {
  228. if (NULL == pubCert || NULL == crlData)
  229. {
  230. printf("CheckCertWithCrl Input parameter is null,cert path=%s, crl path=%s\n", pubCert, crlData);
  231. return INPUT_NULL_ERR;
  232. }
  233. X509 *x509=LoadCert(pubCert,pubCertLen,NULL,certFormat);
  234. if (NULL == x509)
  235. {
  236. printf("load %s err\n", pubCert);
  237. return LOAD_CERT_ERR;
  238. }
  239. BIO * in=NULL;
  240. if(crlLen==0)
  241. {
  242. if((in=BIO_new_file(crlData, "r"))==NULL)
  243. return LOAD_CRL_ERR;
  244. }
  245. else
  246. {
  247. if((in=BIO_new_mem_buf((void*)crlData,crlLen))== NULL)
  248. return LOAD_CRL_ERR;
  249. }
  250. X509_CRL *crl=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);
  251. STACK_OF(X509_REVOKED) *revoked=crl->crl->revoked;
  252. X509_REVOKED *rc;
  253. ASN1_INTEGER *serial=X509_get_serialNumber(x509);
  254. int num=sk_X509_REVOKED_num(revoked);
  255. DWORD dwRet = SUCCESS;
  256. for(int i=0;i<num;i++)
  257. {
  258. rc=sk_X509_REVOKED_pop(revoked);
  259. if(ASN1_INTEGER_cmp(serial,rc->serialNumber)==0)
  260. dwRet = CERT_REVOKED_ERR;
  261. X509_REVOKED_free(rc);
  262. }
  263. ASN1_INTEGER_free(serial);
  264. X509_CRL_free(crl);
  265. X509_free(x509);
  266. EVP_cleanup();
  267. return dwRet;
  268. }
  269. DWORD VerifyCertSign(const char* pchRootCert, const char* pchSignCACert, const char *pchCert)
  270. {
  271. if (NULL == pchRootCert)
  272. {
  273. printf("rootcert path is null, %s\n", pchRootCert);
  274. return INPUT_NULL_ERR;
  275. }
  276. if (NULL == pchCert)
  277. {
  278. printf("cert path is null, %s\n", pchCert);
  279. return INPUT_NULL_ERR;
  280. }
  281. OpenSSL_add_all_algorithms();
  282. int rv;
  283. X509_STORE_CTX *ctx = NULL; //证书存储区句柄
  284. X509 *usrCert1 = NULL; //X509证书结构体,保存用户证书
  285. X509 *usrCert3 = NULL; //X509证书结构体,保存用户证书
  286. //X509 *caCert = NULL; //X509证书结构体,保存CA证书
  287. X509 *rootCert = NULL; //X509证书结构体,保存根证书
  288. STACK_OF(X509) *caCertStack = NULL;
  289. X509_STORE *rootCertStore = NULL; //证书存储区
  290. int j = 0;
  291. unsigned char *pTmp = NULL;
  292. BIO *pbio;
  293. pbio = BIO_new_file(pchRootCert,"r");
  294. rootCert = PEM_read_bio_X509(pbio, NULL,NULL,NULL);
  295. if (rootCert == NULL)
  296. {
  297. printf("Load %s err.\n", pchRootCert);
  298. return LOAD_CERT_ERR;
  299. }
  300. //BIO_free(pbio);
  301. //读取签名证书
  302. if (NULL != pchSignCACert)
  303. {
  304. pbio = BIO_new_file(pchSignCACert,"r");
  305. usrCert1 = PEM_read_bio_X509(pbio, NULL,NULL,NULL);
  306. if (usrCert1 == NULL)
  307. {
  308. printf("Load %s err.\n", pchSignCACert);
  309. return LOAD_CERT_ERR;
  310. }
  311. //BIO_free(pbio);
  312. }
  313. //读取待验证的用户证书,三级证书。
  314. if (NULL != pchCert)
  315. {
  316. pbio = BIO_new_file(pchCert,"r");
  317. usrCert3 = PEM_read_bio_X509(pbio, NULL,NULL,NULL);
  318. if (usrCert3 == NULL)
  319. {
  320. printf("Load %s err.\n", pchCert);
  321. return LOAD_CERT_ERR;
  322. }
  323. //BIO_free(pbio);
  324. }
  325. //新建X509证书存储区
  326. rootCertStore = X509_STORE_new();
  327. //添加根证书到证书存储区
  328. //**********************************************************************
  329. //★★★★★多级证书关键处在这里,将所有的根证书加到这里。
  330. X509_STORE_add_cert(rootCertStore,rootCert);
  331. if (NULL != usrCert1)
  332. {
  333. X509_STORE_add_cert(rootCertStore,usrCert1);
  334. }
  335. //**********************************************************************
  336. //设置检查CRL标志位,如果设置此标志位,则检查CRL,否则不检查CRL。
  337. //X509_STORE_set_flags(rootCertStore,X509_V_FLAG_CRL_CHECK);
  338. //新建证书存储区句柄
  339. ctx = X509_STORE_CTX_new();
  340. //初始化根证书存储区、用户证书
  341. //如果待验证跟证书为空,则表示验证根证书自验证
  342. if (NULL != usrCert3)
  343. {
  344. rv = X509_STORE_CTX_init(ctx,rootCertStore,usrCert3,caCertStack);
  345. }
  346. else
  347. {
  348. rv = X509_STORE_CTX_init(ctx,rootCertStore,rootCert,caCertStack);
  349. }
  350. if(rv != 1)
  351. {
  352. printf("X509_STORE_CTX_init err\n");
  353. BIO_free(pbio);
  354. X509_free(usrCert1);
  355. if (NULL != usrCert3)
  356. {
  357. X509_free(usrCert3);
  358. }
  359. X509_free(rootCert);
  360. X509_STORE_CTX_cleanup(ctx);
  361. X509_STORE_CTX_free(ctx);
  362. X509_STORE_free(rootCertStore);
  363. return CERT_SIGN_VERIFY_ERR;
  364. }
  365. //验证用户证书
  366. DWORD dwRet = SUCCESS;
  367. rv = X509_verify_cert(ctx);
  368. if(rv != 1)
  369. {
  370. printf("verify error= %d,info:%s\n",
  371. ctx->error,X509_verify_cert_error_string(ctx->error));
  372. switch (ctx->error)
  373. {
  374. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  375. dwRet = CERT_SIGNATURE_FAILURE_ERR;
  376. break;
  377. case X509_V_ERR_CERT_NOT_YET_VALID:
  378. dwRet = CERT_NOT_YET_VALID_ERR;
  379. break;
  380. case X509_V_ERR_CERT_HAS_EXPIRED:
  381. dwRet = CERT_HAS_EXPIRED_ERR;
  382. break;
  383. default:
  384. dwRet = CERT_SIGN_VERIFY_ERR;
  385. break;
  386. }
  387. }
  388. //else
  389. //{
  390. // printf("verify cer OK\n");
  391. //}
  392. BIO_free(pbio);
  393. X509_free(usrCert1);
  394. //下面会导致循环的第二次崩溃
  395. if (NULL != usrCert3)
  396. {
  397. X509_free(usrCert3);
  398. }
  399. X509_free(rootCert);
  400. X509_STORE_CTX_cleanup(ctx);
  401. X509_STORE_CTX_free(ctx);
  402. X509_STORE_free(rootCertStore);
  403. return dwRet;
  404. }
  405. DWORD VerifyCert(const char *strRootCert, const char *strCaCert, const char *strCert, const char *strCrl, const int iCertFormat)
  406. {
  407. if ((NULL == strRootCert)
  408. || (NULL == strCert))
  409. {
  410. printf("Input file path is null\n");
  411. return INPUT_NULL_ERR;
  412. }
  413. DWORD dwRet = SUCCESS;
  414. //证书HID验证
  415. char strCertHID[17] = {0};
  416. if (!GetHIDFromCert(strCert, FORMAT_PEM, strCertHID))
  417. {
  418. printf("Get UserID form %s err!", strCert);
  419. return dwRet;
  420. }
  421. USBKEY_INFO info;
  422. unsigned count = 0;
  423. dwRet = FindUsbKey(&info, &count);
  424. if (DONGLE_SUCCESS != dwRet)
  425. {
  426. printf("FindUsbKey fail, errcode=0x%08X!", dwRet);
  427. return dwRet;
  428. }
  429. char strUkeyHID[32] = {0};
  430. for (int i = 0; i < 8; i++)
  431. {
  432. sprintf(strUkeyHID+2*i, "%02X ", info.m_HID[i]);
  433. }
  434. if (0 != memcmp(strCertHID, strUkeyHID, 16))
  435. {
  436. printf("HID is not equal");
  437. return CERT_HID_VERIFY_ERR;
  438. }
  439. //校验证书有效期
  440. //因有的厂商Ukey不带时钟,且后期会对终端进行时间同步,openssl验证有效期接口会更加终端系统时间来校验证书时间,故不单独再对有效期进行校验
  441. if(0)
  442. {
  443. dwRet = CheckCertLife(strRootCert, 0, iCertFormat);
  444. if (SUCCESS != dwRet)
  445. {
  446. printf("%s is out of validity!", strRootCert);
  447. return dwRet;
  448. }
  449. if (NULL != strCaCert)
  450. {
  451. dwRet = CheckCertLife(strCaCert, 0, iCertFormat);
  452. if (SUCCESS != dwRet)
  453. {
  454. printf("%s is out of validity!", strCaCert);
  455. return dwRet;
  456. }
  457. }
  458. dwRet = CheckCertLife(strCert, 0, iCertFormat);
  459. if (SUCCESS != dwRet)
  460. {
  461. printf("%s is out of validity!", strCert);
  462. return dwRet;
  463. }
  464. }
  465. //通过CRL校验证书是否撤销,可选
  466. if (NULL != strCrl)
  467. {
  468. //校验CRL签名
  469. /*dwRet = VerifCrl(strCrl, strRootCert);
  470. if (SUCCESS != dwRet)
  471. {
  472. printf("verify %s with %s err!", strRootCert, strCrl);
  473. return dwRet;
  474. }*/
  475. //校验证书是否撤销
  476. dwRet = CheckCertWithCrl(strCert, 0, iCertFormat, strCrl, 0);
  477. if (SUCCESS != dwRet)
  478. {
  479. printf("%s has been revoked!", strCert);
  480. return dwRet;
  481. }
  482. }
  483. //校验证书签名
  484. dwRet = VerifyCertSign(strRootCert, strCaCert, strCert);
  485. if (SUCCESS != dwRet)
  486. {
  487. printf("%s is not signed by %s!", strCert, strCaCert);
  488. return dwRet;
  489. }
  490. return SUCCESS;
  491. }
  492. LIBEXPORT_API DWORD VerifyUserCert(const char *strRootCert, const char *strCaCert, const char *strUserCert, const char *strCrl, const int iCertFormat)
  493. {
  494. //校验证书
  495. DWORD dwRet = VerifyCert(strRootCert, strCaCert, strUserCert, strCrl, iCertFormat);
  496. if (SUCCESS != dwRet)
  497. {
  498. printf("Verify user cert err, %s!", strUserCert);
  499. return dwRet;
  500. }
  501. return SUCCESS;
  502. }
  503. LIBEXPORT_API DWORD VerifyOperatorCert(const char *strRootCert, const char *strCaCert, const char *strOperatorCert, const char *strCrl, const int iCertFormat)
  504. {
  505. //校验证书
  506. DWORD dwRet = VerifyCert(strRootCert, strCaCert, strOperatorCert, strCrl, iCertFormat);
  507. if (SUCCESS != dwRet)
  508. {
  509. printf("Verify operator cert err, %s!", strOperatorCert);
  510. return dwRet;
  511. }
  512. return SUCCESS;
  513. }
  514. LIBEXPORT_API bool GetVerifyTypeFromCert(const char *strUserCert, const int iCertFormat, char *strVerifyType)
  515. {
  516. if (NULL == strUserCert)
  517. {
  518. printf("Input user cert is null");
  519. return false;
  520. }
  521. //CLogFile* log = new CLogFile("log.txt");
  522. //log->LOGERROR("start log, %s", strUserCert);
  523. //获取证书扩展项
  524. string ext;
  525. int extLen;
  526. string extType;
  527. int extTypelen = strlen("VerifyType");
  528. int extValuelen = 0;
  529. //获取证书扩展项
  530. CERTEXT certExt[MAX_EXT_NUM];
  531. int extNumber = GetCertExtent(strUserCert, 0, iCertFormat, certExt);
  532. //扩展项内容格式定义:类型 + "," + 值,例:"OperatorType=normal"
  533. for (int i=0; i<extNumber; i++)
  534. {
  535. ext = certExt[i].VALUE;
  536. extLen = strlen(ext.c_str());
  537. if (extLen <= extTypelen)
  538. {
  539. continue;
  540. }
  541. //判断扩展类型是否为"VerifyType"
  542. extType = ext.substr(0, extTypelen);
  543. if (0 == strcmp((const char*)extType.c_str(),"VerifyType"))
  544. {
  545. extValuelen = extLen - extTypelen -1;
  546. string strVType = ext.substr(extTypelen+1, extValuelen);
  547. strcpy(strVerifyType, strVType.c_str());
  548. //log->LOGERROR("strVerifyType, %s", strVerifyType);
  549. return true;
  550. }
  551. }
  552. //log->LOGERROR("end log, %s", strUserCert);
  553. return false;
  554. }
  555. //LIBEXPORT_API bool CheckCertWithTerminalNumber(const char *strOperatorCert, const int iCertFormat, char *strTerminalNumber)
  556. //{
  557. // if (NULL == strOperatorCert)
  558. // {
  559. // printf("Input operator cert is null");
  560. // return false;
  561. // }
  562. //
  563. // string ext;
  564. // int extLen;
  565. // string extType;
  566. // int extTypelen = strlen("TerminalNoList");
  567. // int extValuelen = 0;
  568. //
  569. // //获取证书扩展项
  570. // /*CLogFile* log = new CLogFile("log.txt");
  571. // log->LOGERROR("start log, %s", strOperatorCert);*/
  572. // CERTEXT certExt[MAX_EXT_NUM];
  573. // int extNumber = GetCertExtent(strOperatorCert, 0, iCertFormat, certExt);
  574. //
  575. // //扩展项内容格式定义:类型 + "," + 值,例:"TerminalNoList=normal"
  576. // for (int i=0; i<extNumber; i++)
  577. // {
  578. // ext = certExt[i].VALUE;
  579. // extLen = strlen(ext.c_str());
  580. //
  581. // if (extLen <= extTypelen)
  582. // {
  583. // continue;
  584. // }
  585. //
  586. // //判断扩展类型是否为"TerminalNoList"
  587. // extType = ext.substr(0, extTypelen);
  588. // if (0 == strcmp((const char*)extType.c_str(),"TerminalNoList"))
  589. // {
  590. // extValuelen = extLen - extTypelen -1;
  591. // string strTerminalNoList = ext.substr(extTypelen+1, extValuelen);
  592. //
  593. // //判断终端列表中是否存在指定终端ID
  594. // if (-1 != strTerminalNoList.find(strTerminalNumber))
  595. // {
  596. // //log->LOGERROR("return ture log, %s", strOperatorCert);
  597. // return true;
  598. // }
  599. // }
  600. // }
  601. // //log->LOGERROR("return false log, %s", strOperatorCert);
  602. // return false;
  603. //}
  604. LIBEXPORT_API bool GetOperatorTypeFromCert(const char *strOperatorCert, const int iCertFormat, char *strOperatorType)
  605. {
  606. if (NULL == strOperatorCert)
  607. {
  608. printf("Input operator cert is null");
  609. return false;
  610. }
  611. string ext;
  612. int extLen;
  613. string extType;
  614. int extTypelen = strlen("OperatorType");
  615. int extValuelen = 0;
  616. //获取证书扩展项
  617. CERTEXT certExt[MAX_EXT_NUM];
  618. int extNumber = GetCertExtent(strOperatorCert, 0, iCertFormat, certExt);
  619. //扩展项内容格式定义:类型 + "," + 值,例:"OperatorType=normal"
  620. for (int i=0; i<extNumber; i++)
  621. {
  622. ext = certExt[i].VALUE;
  623. extLen = strlen(ext.c_str());
  624. if (extLen <= extTypelen)
  625. {
  626. continue;
  627. }
  628. //判断扩展类型是否为"OperatorType"
  629. extType = ext.substr(0, extTypelen);
  630. if (0 == strcmp((const char*)extType.c_str(),"OperatorType"))
  631. {
  632. extValuelen = extLen - extTypelen -1;
  633. string strOptType = ext.substr(extTypelen+1, extValuelen);
  634. strcpy(strOperatorType, strOptType.c_str());
  635. return true;
  636. }
  637. }
  638. return false;
  639. }
  640. //LIBEXPORT_API bool GetTelephoneNoFromCert(const char *strOperatorCert, const int iCertFormat, char *strTelephoneNo)
  641. //{
  642. // if (NULL == strOperatorCert)
  643. // {
  644. // printf("Input operator cert is null");
  645. // return false;
  646. // }
  647. //
  648. // string ext;
  649. // int extLen;
  650. // string extType;
  651. // int extTypelen = strlen("TelephoneNumber");
  652. // int extValuelen = 0;
  653. //
  654. // //获取证书扩展项
  655. // CERTEXT certExt[MAX_EXT_NUM];
  656. // int extNumber = GetCertExtent(strOperatorCert, 0, iCertFormat, certExt);
  657. //
  658. // //扩展项内容格式定义:类型 + "," + 值,例:"TelephoneNumber=18600590168"
  659. // for (int i=0; i<extNumber; i++)
  660. // {
  661. // ext = certExt[i].VALUE;
  662. // extLen = strlen(ext.c_str());
  663. //
  664. // if (extLen <= extTypelen)
  665. // {
  666. // continue;
  667. // }
  668. //
  669. // //判断扩展类型是否为"TelephoneNumber"
  670. // extType = ext.substr(0, extTypelen);
  671. // if (0 == strcmp((const char*)extType.c_str(),"TelephoneNumber"))
  672. // {
  673. // extValuelen = extLen - extTypelen -1;
  674. // string strTelNo = ext.substr(extTypelen+1, extValuelen);
  675. //
  676. // strcpy(strTelephoneNo, strTelNo.c_str());
  677. // return true;
  678. // }
  679. // }
  680. //
  681. // return false;
  682. //}
  683. LIBEXPORT_API bool GetVersionNoLenFromCert(const char *strUserCert, const int iCertFormat, int& iVersionNoLen)
  684. {
  685. if (NULL == strUserCert)
  686. {
  687. printf("Input operator cert is null");
  688. return false;
  689. }
  690. string ext;
  691. int extLen;
  692. string extType;
  693. int extTypelen = strlen("Version");
  694. int extValuelen = 0;
  695. //获取证书扩展项
  696. CERTEXT certExt[MAX_EXT_NUM];
  697. int extNumber = GetCertExtent(strUserCert, 0, iCertFormat, certExt);
  698. //扩展项内容格式定义:类型 + "," + 值,例:"Version=100001"
  699. for (int i=0; i<extNumber; i++)
  700. {
  701. ext = certExt[i].VALUE;
  702. extLen = strlen(ext.c_str());
  703. if (extLen <= extTypelen)
  704. {
  705. continue;
  706. }
  707. //判断扩展类型是否为"Version"
  708. extType = ext.substr(0, extTypelen);
  709. if (0 == strcmp((const char*)extType.c_str(),"Version"))
  710. {
  711. extValuelen = extLen - extTypelen -1;
  712. string strVersion = ext.substr(extTypelen+1, extValuelen);
  713. iVersionNoLen = strlen(strVersion.c_str());
  714. return true;
  715. }
  716. }
  717. return false;
  718. }
  719. LIBEXPORT_API bool GetVersionNoFromCert(const char *strUserCert, const int iCertFormat, char *strVersionNo, const int iVersionNoLen)
  720. {
  721. if (NULL == strUserCert)
  722. {
  723. printf("Input operator cert is null");
  724. return false;
  725. }
  726. if (8 > iVersionNoLen)
  727. {
  728. printf("the versionNo buf len is smaller than 8");
  729. return false;
  730. }
  731. string ext;
  732. int extLen;
  733. string extType;
  734. int extTypelen = strlen("Version");
  735. int extValuelen = 0;
  736. //获取证书扩展项
  737. CERTEXT certExt[MAX_EXT_NUM];
  738. int extNumber = GetCertExtent(strUserCert, 0, iCertFormat, certExt);
  739. //扩展项内容格式定义:类型 + "," + 值,例:"Version=100001"
  740. for (int i=0; i<extNumber; i++)
  741. {
  742. ext = certExt[i].VALUE;
  743. extLen = strlen(ext.c_str());
  744. if (extLen <= extTypelen)
  745. {
  746. continue;
  747. }
  748. //判断扩展类型是否为"Version"
  749. extType = ext.substr(0, extTypelen);
  750. if (0 == strcmp((const char*)extType.c_str(),"Version"))
  751. {
  752. extValuelen = extLen - extTypelen -1;
  753. string strTelNo = ext.substr(extTypelen+1, extValuelen);
  754. strcpy(strVersionNo, strTelNo.c_str());
  755. return true;
  756. }
  757. }
  758. return false;
  759. }
  760. LIBEXPORT_API bool GetTaskInfoFromCert(const char *strOperatorCert, const int iCertFormat, char *strTaskInfo)
  761. {
  762. if (NULL == strOperatorCert)
  763. {
  764. printf("Input operator cert is null");
  765. return false;
  766. }
  767. string ext;
  768. int extLen;
  769. string extType;
  770. int extTypelen = strlen("TaskInfo");
  771. int extValuelen = 0;
  772. //获取证书扩展项
  773. CERTEXT certExt[MAX_EXT_NUM];
  774. int extNumber = GetCertExtent(strOperatorCert, 0, iCertFormat, certExt);
  775. //扩展项内容格式定义:类型 + "," + 值,例:"TaskInfo="
  776. for (int i=0; i<extNumber; i++)
  777. {
  778. ext = certExt[i].VALUE;
  779. extLen = strlen(ext.c_str());
  780. if (extLen <= extTypelen)
  781. {
  782. continue;
  783. }
  784. //判断扩展类型是否为"TaskInfo"
  785. extType = ext.substr(0, extTypelen);
  786. //if (0 == strcmp((const char*)extType.c_str(),"TaskInfo"))
  787. //"."之后的那个字符是什么意义?是和点一块标识长度?中文原因导致的?
  788. //extType = ext.substr(1, extTypelen);
  789. if (0 == strcmp((const char*)extType.c_str(),"TaskInfo"))
  790. {
  791. extValuelen = extLen - extTypelen -1;
  792. string strTelNo = ext.substr(extTypelen+1, extValuelen);
  793. strcpy(strTaskInfo, strTelNo.c_str());
  794. return true;
  795. }
  796. }
  797. return false;
  798. }
  799. //LIBEXPORT_API bool GetHIDFromCert(const char *strCert, const int iCertFormat, char *strHID)
  800. //{
  801. // if (NULL == strCert || NULL == strHID)
  802. // {
  803. // return false;
  804. // }
  805. //
  806. // X509 * x509=NULL;
  807. // BIO * in=NULL;
  808. // char commonName [512] = {0};
  809. // X509_NAME * subjectName = NULL;
  810. //
  811. // x509 = LoadCert(strCert, 0, NULL, FORMAT_PEM);
  812. // if (NULL == x509)
  813. // {
  814. // return false;
  815. // }
  816. //
  817. // subjectName = X509_get_subject_name(x509);
  818. // if (NULL == subjectName)
  819. // {
  820. // return false;
  821. // }
  822. //
  823. // X509_NAME_get_text_by_NID(subjectName, NID_commonName, commonName, 512);
  824. //
  825. // //memcpy(strHID, commonName, strlen(commonName));
  826. // memcpy(strHID, commonName, 16);
  827. // return true;
  828. //}
  829. LIBEXPORT_API bool GetHIDFromCert(const char *strCert, const int iCertFormat, char *strHID)
  830. {
  831. if (NULL == strCert || NULL == strHID)
  832. {
  833. return false;
  834. }
  835. string ext;
  836. int extLen;
  837. string extType;
  838. int extTypelen = strlen("UkeyID");
  839. int extValuelen = 0;
  840. //获取证书扩展项
  841. CERTEXT certExt[MAX_EXT_NUM];
  842. int extNumber = GetCertExtent(strCert, 0, iCertFormat, certExt);
  843. //扩展项内容格式定义:类型 + "," + 值,例:"UkeyID=080A131208160A13"
  844. for (int i=0; i<extNumber; i++)
  845. {
  846. ext = certExt[i].VALUE;
  847. extLen = strlen(ext.c_str());
  848. if (extLen <= extTypelen)
  849. {
  850. continue;
  851. }
  852. //判断扩展类型是否为"UkeyID"
  853. extType = ext.substr(0, extTypelen);
  854. if (0 == strcmp((const char*)extType.c_str(),"UkeyID"))
  855. {
  856. extValuelen = extLen - extTypelen -1;
  857. string strValue = ext.substr(extTypelen+1, extValuelen);
  858. strcpy(strHID, strValue.c_str());
  859. return true;
  860. }
  861. }
  862. return true;
  863. }
  864. //LIBEXPORT_API bool GetUserIDFromCert(const char *strCert, const int iCertFormat, char *strUserID)
  865. //{
  866. // if (NULL == strCert || NULL == strUserID)
  867. // {
  868. // return false;
  869. // }
  870. //
  871. // X509 * x509=NULL;
  872. // BIO * in=NULL;
  873. // char commonName [512] = {0};
  874. // X509_NAME * subjectName = NULL;
  875. //
  876. // x509 = LoadCert(strCert, 0, NULL, FORMAT_PEM);
  877. // if (NULL == x509)
  878. // {
  879. // return false;
  880. // }
  881. //
  882. // subjectName = X509_get_subject_name(x509);
  883. // if (NULL == subjectName)
  884. // {
  885. // return false;
  886. // }
  887. //
  888. // X509_NAME_get_text_by_NID(subjectName, NID_commonName, commonName, 512);
  889. //
  890. // //memcpy(strHID, commonName, strlen(commonName));
  891. // memcpy(strUserID, commonName, 16);
  892. // return true;
  893. //}
  894. LIBEXPORT_API bool GetUserInfoFromCert(const char *strCert, const int iCertFormat, char *strUserInfo)
  895. {
  896. if (NULL == strCert || NULL == strUserInfo)
  897. {
  898. return false;
  899. }
  900. string ext;
  901. int extLen;
  902. string extType;
  903. int extTypelen = strlen("UserInfo");
  904. int extValuelen = 0;
  905. //获取证书扩展项
  906. CERTEXT certExt[MAX_EXT_NUM];
  907. int extNumber = GetCertExtent(strCert, 0, iCertFormat, certExt);
  908. //扩展项内容格式定义:类型 + "=" + 值,例:"UserInfo=UserID=SP00000004;UserName=RVC0001;AuthorierID=SP00000001;AuthorizeTime=2014/8/13 18:46:29;"
  909. for (int i=0; i<extNumber; i++)
  910. {
  911. ext = certExt[i].VALUE;
  912. extLen = strlen(ext.c_str());
  913. if (extLen <= extTypelen)
  914. {
  915. continue;
  916. }
  917. //判断扩展类型是否为"UserInfo"
  918. extType = ext.substr(0, extTypelen);
  919. if (0 == strcmp((const char*)extType.c_str(),"UserInfo"))
  920. {
  921. extValuelen = extLen - extTypelen -1;
  922. string strValue = ext.substr(extTypelen+1, extValuelen);
  923. strcpy(strUserInfo, strValue.c_str());
  924. return true;
  925. }
  926. }
  927. return true;
  928. }
  929. LIBEXPORT_API bool GetOfficeIDFromCert(const char *strCert, const int iCertFormat, char *strOfficeID)
  930. {
  931. if (NULL == strCert || NULL == strOfficeID)
  932. {
  933. return false;
  934. }
  935. char cUserInfo[MAX_EXT_LEN] = {0};
  936. bool bRet = GetUserInfoFromCert(strCert, iCertFormat, cUserInfo);
  937. if (!bRet)
  938. {
  939. return false;
  940. }
  941. string strUserInfo = cUserInfo;
  942. int len = strUserInfo.length();
  943. int nPos = strUserInfo.find("OfficeID=");
  944. if (nPos < 0)
  945. {
  946. return false;
  947. }
  948. string strRecord = strUserInfo.substr(nPos, len-1);
  949. nPos = strRecord.find_first_of(";");
  950. if (nPos < 0)
  951. {
  952. return false;
  953. }
  954. string strOfficeIDRecord = strRecord.substr(0, nPos);
  955. if (strOfficeIDRecord.length() <= 9)
  956. {
  957. return false;
  958. }
  959. string offiiceID = strOfficeIDRecord.substr(9,nPos -9);
  960. memcpy(strOfficeID, offiiceID.c_str(), MAX_OFFICEID_LEN);
  961. return true;
  962. }
  963. LIBEXPORT_API bool GetSapFromCert(const char *strCert, const int iCertFormat, char *strSAP)
  964. {
  965. if (NULL == strCert || NULL == strSAP)
  966. {
  967. return false;
  968. }
  969. char cUserInfo[MAX_EXT_LEN] = {0};
  970. bool bRet = GetUserInfoFromCert(strCert, iCertFormat, cUserInfo);
  971. if (!bRet)
  972. {
  973. return false;
  974. }
  975. string strUserInfo = cUserInfo;
  976. int len = strUserInfo.length();
  977. int nPos = strUserInfo.find("SAP=");
  978. if (nPos < 0)
  979. {
  980. return false;
  981. }
  982. string strRecord = strUserInfo.substr(nPos, len-1);
  983. nPos = strRecord.find_first_of(";");
  984. if (nPos < 0)
  985. {
  986. return false;
  987. }
  988. string strSapRecord = strRecord.substr(0, nPos);
  989. if (strSapRecord.length() <= 4)
  990. {
  991. return false;
  992. }
  993. string SAP = strSapRecord.substr(4,nPos -4);
  994. memcpy(strSAP, SAP.c_str(), MAX_SAP_LEN);
  995. return true;
  996. }
  997. LIBEXPORT_API bool GetUserIDFromCert(const char *strCert, const int iCertFormat, char *strUserID)
  998. {
  999. if (NULL == strCert || NULL == strUserID)
  1000. {
  1001. return false;
  1002. }
  1003. char cUserInfo[MAX_EXT_LEN] = {0};
  1004. bool bRet = GetUserInfoFromCert(strCert, iCertFormat, cUserInfo);
  1005. if (!bRet)
  1006. {
  1007. return false;
  1008. }
  1009. string strUserInfo = cUserInfo;
  1010. int len = strUserInfo.length();
  1011. int nPos = strUserInfo.find("UserID=");
  1012. if (nPos < 0)
  1013. {
  1014. return false;
  1015. }
  1016. string strRecord = strUserInfo.substr(nPos, len-1);
  1017. nPos = strRecord.find_first_of(";");
  1018. if (nPos < 0)
  1019. {
  1020. return false;
  1021. }
  1022. string strUserIDRecord = strRecord.substr(0, nPos);
  1023. if (strUserIDRecord.length() <= 7)
  1024. {
  1025. return false;
  1026. }
  1027. string userID = strUserIDRecord.substr(7,nPos -7);
  1028. memcpy(strUserID, userID.c_str(), MAX_USERID_LEN);
  1029. return true;
  1030. }
  1031. LIBEXPORT_API bool GetRecommenderOfficeIDFromCert(const char *strCert, const int iCertFormat, char *strRecommenderOfficeID)
  1032. {
  1033. if (NULL == strCert || NULL == strRecommenderOfficeID)
  1034. {
  1035. return false;
  1036. }
  1037. char cUserInfo[MAX_EXT_LEN] = {0};
  1038. bool bRet = GetUserInfoFromCert(strCert, iCertFormat, cUserInfo);
  1039. if (!bRet)
  1040. {
  1041. return false;
  1042. }
  1043. string strUserInfo = cUserInfo;
  1044. int len = strUserInfo.length();
  1045. int nPos = strUserInfo.find("RecommenderOfficeID=");
  1046. if (nPos < 0)
  1047. {
  1048. return false;
  1049. }
  1050. string strRecord = strUserInfo.substr(nPos, len-1);
  1051. nPos = strRecord.find_first_of(";");
  1052. if (nPos < 0)
  1053. {
  1054. return false;
  1055. }
  1056. string strRecommenderOfficeIDRecord = strRecord.substr(0, nPos);
  1057. if (strRecommenderOfficeIDRecord.length() <= 20)
  1058. {
  1059. return false;
  1060. }
  1061. string recommenderOfficeID = strRecommenderOfficeIDRecord.substr(20,nPos -20);
  1062. memcpy(strRecommenderOfficeID, recommenderOfficeID.c_str(), MAX_OFFICEID_LEN);
  1063. return true;
  1064. }
  1065. LIBEXPORT_API bool GetAuthorizerIDFromCert(const char *strCert, const int iCertFormat, char *strAuthorizerID)
  1066. {
  1067. if (NULL == strCert || NULL == strAuthorizerID)
  1068. {
  1069. return false;
  1070. }
  1071. string ext;
  1072. int extLen;
  1073. string extType;
  1074. int extTypelen = strlen("AuthorierID");
  1075. int extValuelen = 0;
  1076. //获取证书扩展项
  1077. CERTEXT certExt[MAX_EXT_NUM];
  1078. int extNumber = GetCertExtent(strCert, 0, iCertFormat, certExt);
  1079. //扩展项内容格式定义:类型 + "," + 值,例:"AuthorierID=SP00000004"
  1080. for (int i=0; i<extNumber; i++)
  1081. {
  1082. ext = certExt[i].VALUE;
  1083. extLen = strlen(ext.c_str());
  1084. if (extLen <= extTypelen)
  1085. {
  1086. continue;
  1087. }
  1088. //判断扩展类型是否为"UserName"
  1089. extType = ext.substr(0, extTypelen);
  1090. if (0 == strcmp((const char*)extType.c_str(),"AuthorierID"))
  1091. {
  1092. extValuelen = extLen - extTypelen -1;
  1093. string strValue = ext.substr(extTypelen+1, extValuelen);
  1094. strcpy(strAuthorizerID, strValue.c_str());
  1095. return true;
  1096. }
  1097. }
  1098. return true;
  1099. }
  1100. LIBEXPORT_API bool GetAuthorizeTimeFromCert(const char *strCert, const int iCertFormat, char *strAuthorizeTime)
  1101. {
  1102. if (NULL == strCert || NULL == strAuthorizeTime)
  1103. {
  1104. return false;
  1105. }
  1106. string ext;
  1107. int extLen;
  1108. string extType;
  1109. int extTypelen = strlen("AuthorizeTime");
  1110. int extValuelen = 0;
  1111. //获取证书扩展项
  1112. CERTEXT certExt[MAX_EXT_NUM];
  1113. int extNumber = GetCertExtent(strCert, 0, iCertFormat, certExt);
  1114. //扩展项内容格式定义:类型 + "," + 值,例:"AuthorizeTime=2014/5/8 18:46:29"
  1115. for (int i=0; i<extNumber; i++)
  1116. {
  1117. ext = certExt[i].VALUE;
  1118. extLen = strlen(ext.c_str());
  1119. if (extLen <= extTypelen)
  1120. {
  1121. continue;
  1122. }
  1123. //判断扩展类型是否为"UserName"
  1124. extType = ext.substr(0, extTypelen);
  1125. if (0 == strcmp((const char*)extType.c_str(),"AuthorizeTime"))
  1126. {
  1127. extValuelen = extLen - extTypelen -1;
  1128. string strValue = ext.substr(extTypelen+1, extValuelen);
  1129. strcpy(strAuthorizeTime, strValue.c_str());
  1130. return true;
  1131. }
  1132. }
  1133. return true;
  1134. }
  1135. LIBEXPORT_API bool GetSerialNoFromCert(const char *strCert, const int iCertFormat, char *strSerialNo)
  1136. {
  1137. if (NULL == strCert || NULL == strSerialNo)
  1138. {
  1139. return false;
  1140. }
  1141. X509 * x509=NULL;
  1142. BIO * in=NULL;
  1143. char serialNumber[512] = {0};
  1144. X509_NAME * subjectName = NULL;
  1145. x509 = LoadCert(strCert, 0, NULL, FORMAT_PEM);
  1146. if (NULL == x509)
  1147. {
  1148. return false;
  1149. }
  1150. ASN1_INTEGER *serial = X509_get_serialNumber(x509);
  1151. //ANS1_INTEGER 转字符串
  1152. long lSerial = ASN1_INTEGER_get(serial);
  1153. ltoa(lSerial, strSerialNo, 10);
  1154. return true;
  1155. }
  1156. LIBEXPORT_API bool GetValidityFromCert(const char *strCert, const int iCertFormat, char *strStartTime, char *strEndTime)
  1157. {
  1158. DWORD utcTime = 0;
  1159. if (NULL == strCert)
  1160. {
  1161. return false;
  1162. }
  1163. X509 *x509=LoadCert(strCert,0,NULL,iCertFormat);
  1164. if (NULL == x509)
  1165. {
  1166. return false;
  1167. }
  1168. asn1_string_st *before=X509_get_notBefore(x509), *after=X509_get_notAfter(x509);
  1169. //ASN1_UTCTIME *be=ASN1_STRING_dup(before), *af=ASN1_STRING_dup(after);
  1170. ASN1_GENERALIZEDTIME *be = ASN1_STRING_dup(before);
  1171. ASN1_GENERALIZEDTIME *af = ASN1_STRING_dup(after);
  1172. strcpy(strStartTime, (const char*)be->data);
  1173. strcpy(strEndTime, (const char*)af->data);
  1174. M_ASN1_UTCTIME_free(be);
  1175. M_ASN1_UTCTIME_free(af);
  1176. X509_free(x509);
  1177. return true;
  1178. }
  1179. LIBEXPORT_API bool GetUserTypeFromCert(const char *strCert, const int iCertFormat, char *strUserType)
  1180. {
  1181. if (NULL == strCert || NULL == strUserType)
  1182. {
  1183. return false;
  1184. }
  1185. string ext;
  1186. int extLen;
  1187. string extType;
  1188. int extTypelen = strlen("UserType");
  1189. int extValuelen = 0;
  1190. //获取证书扩展项
  1191. CERTEXT certExt[MAX_EXT_NUM];
  1192. int extNumber = GetCertExtent(strCert, 0, iCertFormat, certExt);
  1193. //扩展项内容格式定义:类型 + "," + 值,例:"UserType=0"
  1194. for (int i=0; i<extNumber; i++)
  1195. {
  1196. ext = certExt[i].VALUE;
  1197. extLen = strlen(ext.c_str());
  1198. if (extLen <= extTypelen)
  1199. {
  1200. continue;
  1201. }
  1202. //判断扩展类型是否为"UserType"
  1203. extType = ext.substr(0, extTypelen);
  1204. if (0 == strcmp((const char*)extType.c_str(),"UserType"))
  1205. {
  1206. extValuelen = extLen - extTypelen -1;
  1207. string strValue = ext.substr(extTypelen+1, extValuelen);
  1208. strcpy(strUserType, strValue.c_str());
  1209. return true;
  1210. }
  1211. }
  1212. return true;
  1213. }
  1214. LIBEXPORT_API DWORD RsaPublicEncrypt(const unsigned char* srcData, unsigned int srcDataLen, unsigned char* destData,const char *strUserCert, const int iCertFormat)
  1215. {
  1216. //加载证书
  1217. X509 *cert = LoadCert(strUserCert, 0, NULL, iCertFormat);
  1218. if (NULL == cert)
  1219. {
  1220. return LOAD_CERT_ERR;
  1221. }
  1222. //提取公钥
  1223. EVP_PKEY *pubkey = X509_get_pubkey(cert);
  1224. if (NULL == pubkey)
  1225. {
  1226. return GET_PUBKEY_FROM_CERT_ERR;
  1227. }
  1228. //读取PEM文件后,需要将N值(pRsa->n->d)倒序
  1229. //因为openssl的字节序和pc的字节序不同,因此需要将openssl中的n(pRsa->n->d )值进行倒序
  1230. /*BYTE byN[128] = {0};
  1231. BYTE * pN = (BYTE*)pubkey->pkey.rsa->n->d;
  1232. for (int k = 0; k < 128; k++)
  1233. {
  1234. byN[k] = pN[127-k];
  1235. }
  1236. memcpy(pN, byN, 128);*/
  1237. RSA* rsa = EVP_PKEY_get1_RSA(pubkey);
  1238. if (NULL == rsa)
  1239. {
  1240. return RSA_PUBKEY_ENC_ERR;
  1241. }
  1242. if (128 != RSA_size(rsa))
  1243. {
  1244. return RSK_KEY_LEN_ERR;
  1245. }
  1246. //如果直接用pubkey->pkey.rsa,在自己机器上可以,但到其它环境上该值为空。怀疑是本机安装了openssl的原因
  1247. //int iRet = RSA_public_encrypt(srcDataLen, (unsigned char *)srcData, destData, pubkey->pkey.rsa, RSA_PKCS1_PADDING);
  1248. int iRet = RSA_public_encrypt(srcDataLen, (unsigned char *)srcData, destData, rsa, RSA_PKCS1_PADDING);
  1249. if (iRet < 0)
  1250. {
  1251. return RSA_PUBKEY_ENC_ERR;
  1252. }
  1253. return SUCCESS;
  1254. }
  1255. LIBEXPORT_API DWORD RsaPublicDecrypt(const unsigned char* srcData, unsigned int srcDataLen, unsigned char* destData,const char *strUserCert, const int iCertFormat)
  1256. {
  1257. //加载证书
  1258. X509 *cert = LoadCert(strUserCert, 0, NULL, iCertFormat);
  1259. if (NULL == cert)
  1260. {
  1261. return LOAD_CERT_ERR;
  1262. }
  1263. //提取公钥
  1264. EVP_PKEY *pubkey = X509_get_pubkey(cert);
  1265. if (NULL == pubkey)
  1266. {
  1267. return GET_PUBKEY_FROM_CERT_ERR;
  1268. }
  1269. //读取PEM文件后,需要将N值(pRsa->n->d)倒序
  1270. //因为openssl的字节序和pc的字节序不同,因此需要将openssl中的n(pRsa->n->d )值进行倒序
  1271. /*BYTE byN[128] = {0};
  1272. BYTE * pN = (BYTE*)pubkey->pkey.rsa->n->d;
  1273. for (int k = 0; k < 128; k++)
  1274. {
  1275. byN[k] = pN[127-k];
  1276. }
  1277. memcpy(pN, byN, 128);*/
  1278. RSA* rsa = EVP_PKEY_get1_RSA(pubkey);
  1279. if (NULL == rsa)
  1280. {
  1281. return RSA_PUBKEY_ENC_ERR;
  1282. }
  1283. if (128 != RSA_size(rsa))
  1284. {
  1285. return RSK_KEY_LEN_ERR;
  1286. }
  1287. //如果直接用pubkey->pkey.rsa,在自己机器上可以,但到其它环境上该值为空。怀疑是本机安装了openssl的原因
  1288. //int iRet = RSA_public_encrypt(srcDataLen, (unsigned char *)srcData, destData, pubkey->pkey.rsa, RSA_PKCS1_PADDING);
  1289. int iRet = RSA_public_encrypt(srcDataLen, (unsigned char *)srcData, destData, rsa, RSA_PKCS1_PADDING);
  1290. if (iRet < 0)
  1291. {
  1292. return RSA_PUBKEY_ENC_ERR;
  1293. }
  1294. return SUCCESS;
  1295. }
  1296. LIBEXPORT_API DWORD RsaPublicDecryptByKey(const unsigned char* srcData, unsigned int srcDataLen, unsigned char* destData,const char *strPubPath, const int iCertFormat)
  1297. {
  1298. BIO *bp = NULL;
  1299. RSA* rsa = NULL;
  1300. OpenSSL_add_all_algorithms();
  1301. bp = BIO_new_file(strPubPath,"rb");
  1302. if(NULL == bp)
  1303. {
  1304. return FALSE;
  1305. }
  1306. rsa = PEM_read_bio_RSAPublicKey(bp,NULL,NULL,NULL);
  1307. if (rsa != NULL)
  1308. {
  1309. printf("PEM_read_bio_RSAPublicKey ok!\n");
  1310. }
  1311. else
  1312. {
  1313. printf("PEM_read_bio_RSAPublicKey err!\n");
  1314. return FALSE;
  1315. }
  1316. BIO_free(bp);
  1317. if (128 != RSA_size(rsa))
  1318. {
  1319. return RSA_PUBKEY_ENC_ERR;
  1320. }
  1321. //读取PEM文件后,需要将N值(pRsa->n->d)倒序
  1322. //因为openssl的字节序和pc的字节序不同,因此需要将openssl中的n(pRsa->n->d )值进行倒序
  1323. BYTE byN[128] = {0};
  1324. BYTE * pN = (BYTE*)rsa->n->d;
  1325. for (int k = 0; k < 128; k++)
  1326. {
  1327. byN[k] = pN[127-k];
  1328. }
  1329. memcpy(pN, byN, 128);
  1330. //如果直接用pubkey->pkey.rsa,在自己机器上可以,但到其它环境上该值为空。怀疑是本机安装了openssl的原因
  1331. int iRet = RSA_public_decrypt(srcDataLen, (unsigned char *)srcData, destData, rsa, RSA_PKCS1_PADDING);
  1332. if (iRet < 0)
  1333. {
  1334. return RSA_PUBKEY_ENC_ERR;
  1335. }
  1336. return SUCCESS;
  1337. }
  1338. LIBEXPORT_API DWORD RsaPrivateDecrypt(const unsigned char* srcData, unsigned char* destData,const char *strPriKey)
  1339. {
  1340. if (NULL == strPriKey)
  1341. {
  1342. printf("Private key file name is null\n");
  1343. return INPUT_NULL_ERR;
  1344. }
  1345. BIO* in = NULL;
  1346. RSA* rsaKey = RSA_new();
  1347. //读取私钥
  1348. OpenSSL_add_all_algorithms();
  1349. in=BIO_new_file(strPriKey,"rb");
  1350. if (NULL == in)
  1351. {
  1352. return LOAD_PRIKEY_ERR;
  1353. }
  1354. PEM_read_bio_RSAPrivateKey(in,&rsaKey,NULL,NULL);
  1355. if ((rsaKey)->d!=NULL)
  1356. {
  1357. printf("PEM_read_bio_RSAPrivateKey ok!\n");
  1358. }
  1359. else
  1360. {
  1361. printf("PEM_read_bio_RSAPrivateKey err!\n");
  1362. BIO_free(in);
  1363. return LOAD_PRIKEY_ERR;
  1364. }
  1365. BIO_free(in);
  1366. //unsigned char test[1024] = {0};
  1367. //int iRet = RSA_private_decrypt(RSA_size(rsaKey) , srcData , test , rsaKey , RSA_PKCS1_PADDING);
  1368. int iRet = RSA_private_decrypt(RSA_size(rsaKey) , srcData , destData , rsaKey , RSA_PKCS1_PADDING);
  1369. if (iRet < 0)
  1370. {
  1371. return RSA_PRIKEY_DEC_ERR;
  1372. }
  1373. return SUCCESS;
  1374. }
  1375. LIBEXPORT_API DWORD TDesEncrypt(const unsigned char* srcData, unsigned int srcDataLen, unsigned char* destData,const char *strKey)
  1376. {
  1377. int count;
  1378. int i;
  1379. unsigned char *data = (unsigned char *)srcData; /* 明文 */
  1380. int data_rest;
  1381. unsigned char ch;
  1382. unsigned char *src = NULL; /* 补齐后的明文 */
  1383. unsigned char *dst = NULL; /* 加密后的密文 */
  1384. int len;
  1385. unsigned char in[8];
  1386. unsigned char out[8];
  1387. char *k = (char*)strKey; /* 原始密钥 */
  1388. int key_len;
  1389. //#define LEN_OF_KEY 24
  1390. #define LEN_OF_KEY 16
  1391. unsigned char key[LEN_OF_KEY]; /* 补齐后的密钥 */
  1392. unsigned char block_key[9];
  1393. DES_key_schedule ks,ks2;
  1394. /* 构造补齐后的密钥 */
  1395. key_len = strlen(k);
  1396. memcpy(key, k, key_len);
  1397. memset(key + key_len, 0x00, LEN_OF_KEY - key_len);
  1398. /* 分析补齐明文所需空间及补齐填充数据 */
  1399. data_rest = srcDataLen % 8;
  1400. len = srcDataLen + (8 - data_rest);
  1401. ch = 8 - data_rest;
  1402. src = (unsigned char*)malloc(len);
  1403. if (NULL == src )
  1404. {
  1405. if (NULL != src)
  1406. {
  1407. free(src);
  1408. src = NULL;
  1409. }
  1410. return NEW_MEM_ERR;
  1411. }
  1412. else
  1413. {
  1414. /* 构造补齐后的加密内容 */
  1415. memset(src, 0, len);
  1416. memcpy(src, data, srcDataLen);
  1417. memset(src + srcDataLen, ch, 8 - data_rest);
  1418. /* 密钥置换 */
  1419. memset(block_key, 0, sizeof(block_key));
  1420. memcpy(block_key, key + 0, 8);
  1421. DES_set_key_unchecked((const_DES_cblock*)block_key, &ks);
  1422. memcpy(block_key, key + 8, 8);
  1423. DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2);
  1424. /*memcpy(block_key, key + 16, 8);
  1425. DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3);*/
  1426. /* 循环加密,每8字节一次 */
  1427. count = len / 8;
  1428. for (i = 0; i < count; i++)
  1429. {
  1430. memset(in, 0, 8);
  1431. memset(out, 0, 8);
  1432. memcpy(in, src + 8 * i, 8);
  1433. /* 加密 */
  1434. DES_ecb3_encrypt((const_DES_cblock*)in, (DES_cblock*)out, &ks, &ks2, &ks, DES_ENCRYPT);
  1435. /* 拷贝密文 */
  1436. memcpy(destData + 8 * i, out, 8);
  1437. }
  1438. printf("after encrypt :");
  1439. for (i = 0; i < len; i++)
  1440. {
  1441. printf("0x%.2X ", *(destData + i));
  1442. }
  1443. printf("\n");
  1444. }
  1445. if (NULL != src)
  1446. {
  1447. free(src);
  1448. src = NULL;
  1449. }
  1450. return SUCCESS;
  1451. }
  1452. LIBEXPORT_API DWORD TDesDecrypt(const unsigned char* srcData, unsigned int srcDataLen, unsigned char* destData,const char *strKey)
  1453. {
  1454. unsigned int count;
  1455. unsigned int i;
  1456. unsigned char *dst = NULL; /* 解密后的明文 */
  1457. unsigned char in[8];
  1458. unsigned char out[8];
  1459. char *k = (char*)strKey; /* 原始密钥 */
  1460. int key_len;
  1461. //#define LEN_OF_KEY 24
  1462. #define LEN_OF_KEY 16
  1463. unsigned char key[LEN_OF_KEY]; /* 补齐后的密钥 */
  1464. unsigned char block_key[9];
  1465. DES_key_schedule ks,ks2;
  1466. /* 构造补齐后的密钥 */
  1467. key_len = strlen(k);
  1468. memcpy(key, k, key_len);
  1469. memset(key + key_len, 0x00, LEN_OF_KEY - key_len);
  1470. /* 密钥置换 */
  1471. memset(block_key, 0, sizeof(block_key));
  1472. memcpy(block_key, key + 0, 8);
  1473. DES_set_key_unchecked((const_DES_cblock*)block_key, &ks);
  1474. memcpy(block_key, key + 8, 8);
  1475. DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2);
  1476. /*memcpy(block_key, key + 16, 8);
  1477. DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3);*/
  1478. /* 循环解密,每8字节一次 */
  1479. count = srcDataLen / 8;
  1480. for (i = 0; i < count; i++)
  1481. {
  1482. memset(in, 0, 8);
  1483. memset(out, 0, 8);
  1484. memcpy(in, srcData + 8 * i, 8);
  1485. /* 解密 */
  1486. DES_ecb3_encrypt((const_DES_cblock*)in, (DES_cblock*)out, &ks, &ks2, &ks, DES_DECRYPT);
  1487. /* 将解密后的明文 */
  1488. memcpy(destData + 8 * i, out, 8);
  1489. }
  1490. /*printf("after decrypt :");
  1491. for (i = 0; i < srcDataLen; i++)
  1492. {
  1493. printf("0x%.2X ", *(destData + i));
  1494. }
  1495. printf("\n");*/
  1496. return SUCCESS;
  1497. }
  1498. RSA* S_GetPubKey(char* pubPath)
  1499. {
  1500. RSA *rsaK = NULL;
  1501. BIO *BP = NULL;
  1502. //
  1503. if(NULL == pubPath) return 0;
  1504. //
  1505. BP = BIO_new(BIO_s_file());
  1506. if(NULL == BP) return NULL;
  1507. //
  1508. BIO_read_filename(BP, pubPath);
  1509. rsaK = PEM_read_bio_RSAPublicKey(BP, NULL, NULL, NULL);
  1510. //rsaK = PEM_read_bio_RSA_PUBKEY(BP, NULL,NULL,NULL);
  1511. //
  1512. if(BP) BIO_free_all(BP);
  1513. //
  1514. return rsaK;
  1515. }
  1516. bool ReadPublicKeyFile(const char *pchPublicKeyFile, RSA** rsaKey)
  1517. {
  1518. if (NULL == pchPublicKeyFile)
  1519. {
  1520. printf("Public key file name is null\n");
  1521. return FALSE;
  1522. }
  1523. //读取公钥
  1524. BIO* in = NULL;
  1525. OpenSSL_add_all_algorithms();
  1526. in = BIO_new_file(pchPublicKeyFile,"rb");
  1527. //RSA* read = RSA_new();
  1528. /*rsaKey=*/PEM_read_bio_RSAPublicKey(in,rsaKey,NULL,NULL);
  1529. if (rsaKey != NULL)
  1530. {
  1531. printf("PEM_read_bio_RSAPublicKey ok!\n");
  1532. }
  1533. else
  1534. {
  1535. printf("PEM_read_bio_RSAPublicKey err!\n");
  1536. BIO_free(in);
  1537. return FALSE;
  1538. }
  1539. BIO_free(in);
  1540. return TRUE;
  1541. }
  1542. //LIBEXPORT_API DWORD PEM_2_RSAPUBKEY(char* pPubFile, RSA_PUBLIC_KEY* pPubKey)
  1543. //{
  1544. // BOOL result = FALSE;
  1545. // int len;
  1546. // RSA* pRSA = RSA_new();
  1547. //
  1548. // if(pPubFile != NULL)
  1549. // {
  1550. // if(!ReadPublicKeyFile(pPubFile, &pRSA))
  1551. // {
  1552. // return LOAD_PUBKEY_ERR;
  1553. // }
  1554. //
  1555. // /*pRSA = S_GetPubKey(pPubFile);
  1556. // if(pRSA == NULL)
  1557. // {
  1558. // return LOAD_PUBKEY_ERR;
  1559. // }*/
  1560. //
  1561. // len = pRSA->n->top*4;
  1562. // memcpy(&pPubKey->modulus, pRSA->e->d, 4);
  1563. // memcpy(&pPubKey->exponent, pRSA->n->d, len);
  1564. // pPubKey->bits = len * 8;
  1565. // }
  1566. //
  1567. // return SUCCESS;
  1568. //}
  1569. int GetCertExtent(const char *pubCert,const int pubCertLen,const int certFormat, LPCERTEXT ext)
  1570. {
  1571. if (NULL == pubCert)
  1572. {
  1573. printf("Input cert path is null");
  1574. return INPUT_NULL_ERR;
  1575. }
  1576. if (NULL == ext)
  1577. {
  1578. printf("Input cert extentions contain is null");
  1579. return INPUT_NULL_ERR;
  1580. }
  1581. X509_EXTENSION *ex;
  1582. ASN1_OBJECT *obj;
  1583. int fn_nid;
  1584. string sn;
  1585. string ln;
  1586. BIO *bio;
  1587. BUF_MEM *bptr;
  1588. X509 *x509=LoadCert(pubCert,pubCertLen,NULL,certFormat);
  1589. if (NULL == x509)
  1590. {
  1591. return LOAD_CERT_ERR;
  1592. }
  1593. STACK_OF(X509_EXTENSION) *exts=x509->cert_info->extensions;
  1594. int count=sk_X509_EXTENSION_num(exts);
  1595. for (int i=0; i<count; i++)
  1596. {
  1597. ex=sk_X509_EXTENSION_value(exts, i);
  1598. obj=X509_EXTENSION_get_object(ex);
  1599. fn_nid = OBJ_obj2nid(obj);
  1600. ext[i].IOID=fn_nid;
  1601. sn = OBJ_nid2sn(fn_nid);
  1602. ln = OBJ_nid2ln(fn_nid);
  1603. bio=BIO_new(BIO_s_mem());
  1604. BIO_set_close(bio, BIO_CLOSE);
  1605. if(!X509V3_EXT_print(bio, ex, X509_FLAG_COMPAT, 1))
  1606. M_ASN1_OCTET_STRING_print(bio,ex->value);
  1607. BIO_get_mem_ptr(bio, &bptr);
  1608. memcpy(ext[i].VALUE,bptr->data,bptr->length);
  1609. //根据长度值的长度,截取实际扩展项值
  1610. //规律:长度<128,2个字节长度位;128<长度<256,3个字节长度位;长度>256,4个字节长度位
  1611. string str = ext[i].VALUE;
  1612. string newstr = "";
  1613. int len = strlen(ext[i].VALUE);
  1614. if (len >= 260)
  1615. {
  1616. newstr = str.substr(4,-1);
  1617. strcpy_s(ext[i].VALUE, newstr.c_str());
  1618. }
  1619. else if (len >= 131)
  1620. {
  1621. newstr = str.substr(3,-1);
  1622. strcpy_s(ext[i].VALUE, newstr.c_str());
  1623. }
  1624. else
  1625. {
  1626. newstr = str.substr(2,-1);
  1627. strcpy_s(ext[i].VALUE, newstr.c_str());
  1628. }
  1629. BIO_free(bio);
  1630. }
  1631. X509_free(x509);
  1632. return count;
  1633. }
  1634. //DWORD GetUTCTimeFromUkey(DWORD* pdwUTCTime)
  1635. //{
  1636. // DONGLE_INFO KeyInfo;
  1637. // DONGLE_HANDLE handle;
  1638. // int count = 0;
  1639. //
  1640. // DWORD dwRet = Dongle_Enum(&KeyInfo, &count);
  1641. // if (dwRet != DONGLE_SUCCESS)
  1642. // {
  1643. // return dwRet;
  1644. // }
  1645. //
  1646. // dwRet = Dongle_Open(&handle, 0);
  1647. // if (dwRet != DONGLE_SUCCESS)
  1648. // {
  1649. // return dwRet;
  1650. // }
  1651. //
  1652. // dwRet = Dongle_GetUTCTime(handle, pdwUTCTime);
  1653. // if (dwRet != DONGLE_SUCCESS)
  1654. // {
  1655. // return dwRet;
  1656. // }
  1657. //
  1658. // Dongle_Close(handle);
  1659. //
  1660. // return DONGLE_SUCCESS;
  1661. //}
  1662. //
  1663. //DWORD GetArmHID(char *strHID)
  1664. //{
  1665. // if (NULL == strHID)
  1666. // {
  1667. // return INPUT_NULL_ERR;
  1668. // }
  1669. //
  1670. // DONGLE_INFO KeyInfo;
  1671. // int count = 0;
  1672. // DWORD dwRet = 0;
  1673. //
  1674. // dwRet = Dongle_Enum(&KeyInfo, &count);
  1675. // if (dwRet != DONGLE_SUCCESS)
  1676. // {
  1677. // printf("Dongle_Enum failed!<error code = %08x>\n", dwRet);
  1678. // return dwRet;
  1679. // }
  1680. //
  1681. // for (int i = 0; i < 8; i++)
  1682. // {
  1683. // sprintf(strHID+2*i, "%02X ", KeyInfo.m_HID[i]);
  1684. // }
  1685. //
  1686. // return DONGLE_SUCCESS;
  1687. //}
  1688. //
  1689. //DWORD GetS4HID(char *strHID)
  1690. //{
  1691. // if (NULL == strHID)
  1692. // {
  1693. // return INPUT_NULL_ERR;
  1694. // }
  1695. //
  1696. // SENSE4_CONTEXT * s4CtxList =NULL ;
  1697. // DWORD dwSize=0;
  1698. // DWORD dwRet = 0;
  1699. //
  1700. // //枚举系统连接了多少个设备,这一步不用判断返回值,第一个参数输入NULL
  1701. // dwRet = S4Enum(NULL,&dwSize);
  1702. // if (S4_SUCCESS != dwRet && S4_INSUFFICIENT_BUFFER != dwRet)
  1703. // {
  1704. // printf("S4Enum failed!<error code = %08x>\n", dwRet);
  1705. // return dwRet;
  1706. // }
  1707. //
  1708. // //如果得到的设备上下文内存数量为0或者不为sizeof(SENSE4_CONTEXT)的倍数,
  1709. // //说明没有找到设备或者列举设备出错。
  1710. // if ((0 == dwSize) || (dwSize % sizeof(SENSE4_CONTEXT)))
  1711. // {
  1712. // printf("S4Enum return size err!\n");
  1713. // return S4_NO_LIST;
  1714. // }
  1715. //
  1716. // int nCount = dwSize / sizeof(SENSE4_CONTEXT);
  1717. //
  1718. // //根据返回的设备个数,分配空间给程序使用
  1719. // s4CtxList = (SENSE4_CONTEXT *)malloc(dwSize);
  1720. //
  1721. // //根据分配的空间,再次枚举设备,这次要判断函数返回值
  1722. // dwRet = S4Enum(s4CtxList,&dwSize);
  1723. // if(S4_SUCCESS != dwRet)
  1724. // {
  1725. // free(s4CtxList);
  1726. // s4CtxList = NULL;
  1727. // printf("Enum device failed.<error code = %08x>\n", dwRet);
  1728. // return dwRet;
  1729. // }
  1730. //
  1731. // S4OPENINFO S4_OpenInfo;
  1732. // S4_OpenInfo.dwS4OpenInfoSize = sizeof(S4OPENINFO);
  1733. // S4_OpenInfo.dwShareMode = S4_EXCLUSIZE_MODE;
  1734. //
  1735. // dwRet = S4OpenEx(s4CtxList, &S4_OpenInfo);
  1736. // if (S4_SUCCESS != dwRet)
  1737. // {
  1738. // if (S4_DEVICE_BUSY == dwRet)
  1739. // {
  1740. // printf("device is busy, start to close, and then open.\n");
  1741. // dwRet = S4Close(s4CtxList);
  1742. // if(S4_SUCCESS != dwRet)
  1743. // {
  1744. // free(s4CtxList);
  1745. // s4CtxList = NULL;
  1746. // printf("S4Close failure.<error code = %08x>\n", dwRet);
  1747. // return dwRet;
  1748. // }
  1749. //
  1750. // dwRet = S4OpenEx(s4CtxList, &S4_OpenInfo);
  1751. // if(S4_SUCCESS != dwRet)
  1752. // {
  1753. // free(s4CtxList);
  1754. // s4CtxList = NULL;
  1755. // printf("S4OpenEx failure.<error code = %08x>\n", dwRet);
  1756. // return dwRet;
  1757. // }
  1758. // }
  1759. // else
  1760. // {
  1761. // free(s4CtxList);
  1762. // s4CtxList = NULL;
  1763. // printf("S4OpenEx failure.<error code = %08x>\n", dwRet);
  1764. // return dwRet;
  1765. // }
  1766. // }
  1767. //
  1768. // //获取设备序列号
  1769. // DWORD len = 0;
  1770. // unsigned char cSerialNumber[8] = {0};
  1771. // dwRet = S4Control(s4CtxList, S4_GET_SERIAL_NUMBER, NULL, 0, (void*)cSerialNumber, 8, &len);
  1772. // if (S4_SUCCESS != dwRet)
  1773. // {
  1774. // free(s4CtxList);
  1775. // s4CtxList = NULL;
  1776. // printf("Get Serial number failed!<error code = %08x>\n", dwRet);
  1777. // return dwRet;
  1778. // }
  1779. //
  1780. // for (int i = 0; i < 8; i++)
  1781. // {
  1782. // sprintf(strHID+2*i, "%02X ", cSerialNumber[i]);
  1783. // }
  1784. //
  1785. // free(s4CtxList);
  1786. // s4CtxList = NULL;
  1787. //
  1788. // return S4_SUCCESS;
  1789. //}
  1790. //
  1791. //DWORD GetGM3000HID(char *strHID)
  1792. //{
  1793. // DWORD dwRet = 0;
  1794. // char szDevName[256] = {0};
  1795. // ULONG ulNameLen = 256;
  1796. // DEVHANDLE hHandle = NULL;
  1797. //
  1798. // dwRet = SKF_EnumDev(1, szDevName, &ulNameLen);
  1799. // if (SAR_OK != dwRet)
  1800. // {
  1801. // printf("SKF_EnumDev failed! <error code = 0x%08x>\n", dwRet);
  1802. // return dwRet;
  1803. // }
  1804. //
  1805. // dwRet = SKF_ConnectDev(szDevName, &hHandle);
  1806. // if (SAR_OK != dwRet)
  1807. // {
  1808. // printf("SKF_ConnectDev failed! <error code = 0x%08x>\n", dwRet);
  1809. // return dwRet;
  1810. // }
  1811. //
  1812. // DEVINFO stDevInfo = {0};
  1813. // dwRet = SKF_GetDevInfo(hHandle, &stDevInfo);
  1814. // if (SAR_OK != dwRet)
  1815. // {
  1816. // printf("SKF_GetDevInfo failed! <error code = 0x%08x>\n", dwRet);
  1817. // return dwRet;
  1818. // }
  1819. //
  1820. // memcpy(strHID, stDevInfo.SerialNumber, 16);
  1821. //
  1822. // return SAR_OK;
  1823. //}
  1824. //
  1825. //LIBEXPORT_API DWORD GetHIDFromUKey(char *strHID)
  1826. //{
  1827. // if (NULL == strHID)
  1828. // {
  1829. // return INPUT_NULL_ERR;
  1830. // }
  1831. //
  1832. // DWORD dwRet = 0;
  1833. //
  1834. // dwRet = GetArmHID(strHID);
  1835. // if (DONGLE_SUCCESS == dwRet)
  1836. // {
  1837. // return DONGLE_SUCCESS;
  1838. // }
  1839. // else if(DONGLE_NOT_FOUND == dwRet)
  1840. // {
  1841. // dwRet = GetS4HID(strHID);
  1842. // if (S4_SUCCESS == dwRet)
  1843. // {
  1844. // return S4_SUCCESS;
  1845. // }
  1846. // else if (S4_NO_LIST == dwRet)
  1847. // {
  1848. // dwRet = GetGM3000HID(strHID);
  1849. // if (SAR_OK != dwRet)
  1850. // {
  1851. // return dwRet;
  1852. // }
  1853. //
  1854. // return SAR_OK;
  1855. // }
  1856. // else
  1857. // {
  1858. // return dwRet;
  1859. // }
  1860. // }
  1861. // else
  1862. // {
  1863. // return dwRet;
  1864. // }
  1865. //
  1866. // return DONGLE_SUCCESS;
  1867. //}