cryptapi.c
上传用户:nbcables
上传日期:2007-01-11
资源大小:1243k
文件大小:71k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <string.h>
  4. #define FUNC_OK             0
  5. #define FUNC_ERR            1
  6. #define CPTR            *               
  7. #define CCPTR           **
  8. #define PUB_FUNC_RTN    uint32                  
  9. #include "ca_common.h"
  10. #include "csp_api.h"
  11. #include "md2.h"/*../soft_crypt/*/
  12. #include "md5.h"/*../soft_crypt/*/
  13. #include "sha.h"/*../soft_crypt/*/
  14. #include "common.h"
  15. #include "g_val.h"
  16. #include "asn1.h"
  17. #include "cryptapi.h"
  18. #include "rsaref.h"
  19. static uint8  MD2_DIGEST_INFO[] = {
  20.            0x30, 0x20, 
  21.      0x30, 0x0c, 
  22.        0x06, 0x08, 
  23.          0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x02,
  24.                        0x05, 0x00,
  25.                  0x04, 0x10 
  26. };
  27.  
  28. static uint8  MD5_DIGEST_INFO[] = {
  29.            0x30, 0x20, 
  30.      0x30, 0x0c, 
  31.        0x06, 0x08, 
  32.          0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05,
  33.                        0x05, 0x00,
  34.                  0x04, 0x10 
  35. };
  36.  
  37. static uint8  SHA1_DIGEST_INFO[] = {
  38.            0x30, 0x21, 
  39.      0x30, 0x09, 
  40.        0x06, 0x05, 
  41.          0x2b, 0x0e, 0x03, 0x02, 0x1a,
  42.                        0x05, 0x00,
  43.                  0x04, 0x14 
  44. };
  45. char *del_rn(char *pbuf);
  46. CRET Crypt_Get_Version(char* version)
  47. {
  48. // int rtn_code;
  49. strcpy(version, "2.0");
  50. return 0;
  51. }
  52. CRET Crypt_InitEnvironment(void)
  53. {
  54. int rtn_code;
  55.     rtn_code = CSP_InitEnvironment();
  56.     if(rtn_code != RTN_OK){
  57. return FUNC_ERR;
  58. }
  59. return FUNC_OK;
  60. }
  61. CRET Crypt_ClearEnvironment(void)
  62. {
  63. int rtn_code;
  64.     rtn_code = CSP_ClearEnvironment();
  65.     if(rtn_code != RTN_OK){
  66. return FUNC_ERR;
  67. }
  68. return FUNC_OK;
  69. }
  70. CRET CA_Crypt_Gen_Hash(
  71.         int    uid_algorithm,
  72.         unsigned char    *msg,
  73.         int              msgSize,
  74.         unsigned char    *hash,
  75. int              *hashSize)
  76. {
  77. return Crypt_Gen_Hash(
  78.         uid_algorithm,
  79.         msg,
  80.         msgSize,
  81.         hash,
  82. hashSize);
  83. }
  84. int Crypt_Gen_Hash(
  85.         int    uid_algorithm,
  86.         unsigned char    *msg,
  87.         int              msgSize,
  88.         unsigned char    *hash,
  89. int              *hashSize)
  90. {
  91. switch(uid_algorithm){
  92.         case UID_md2WithRSAEncryption:
  93. MD2(msg,msgSize,hash);
  94. *hashSize = 16;
  95. break;
  96.         case UID_md5WithRSAEncryption:
  97. MD5(msg,msgSize,hash);
  98. *hashSize = 16;
  99. break;
  100.         case UID_md5:
  101. MD5(msg,msgSize,hash);
  102. *hashSize = 16;
  103. break;
  104.         case UID_sha1WithRSASignature:
  105. SHA1(msg,msgSize,hash);
  106. *hashSize = 20;
  107. break;
  108.         case UID_sha1:
  109. SHA1(msg,msgSize,hash);
  110. *hashSize = 20;
  111. break;
  112. default:
  113. return FUNC_ERR;
  114.   }
  115. return FUNC_OK;
  116. }
  117. CRET Crypt_Gen_Encoded_Hash(
  118.         int    uid_algorithm,
  119.         unsigned char    *msg,
  120.         int              msgSize,
  121.         unsigned char    *hash,
  122. int              *hashSize)
  123. {
  124. unsigned char digestValue[32];
  125. int i;
  126. switch(uid_algorithm){
  127.         case UID_md2WithRSAEncryption:
  128. MD2(msg,msgSize,digestValue);
  129. i = sizeof(MD2_DIGEST_INFO);
  130.             memcpy(hash,MD2_DIGEST_INFO,i);
  131.             memcpy(&hash[i],digestValue,16);
  132. *hashSize = i + 16;
  133. break;
  134.         case UID_md5WithRSAEncryption:
  135. MD5(msg,msgSize,digestValue);
  136. i = sizeof(MD5_DIGEST_INFO);
  137.             memcpy(hash,MD5_DIGEST_INFO,i);
  138.             memcpy(&hash[i],digestValue,16);
  139. *hashSize = i + 16;
  140. break;
  141.         case UID_md5:
  142. MD5(msg,msgSize,digestValue);
  143. i = sizeof(MD5_DIGEST_INFO);
  144.             memcpy(hash,MD5_DIGEST_INFO,i);
  145.             memcpy(&hash[i],digestValue,16);
  146. *hashSize = i + 16;
  147.   break;
  148.         case UID_sha1WithRSASignature:
  149. SHA1(msg,msgSize,digestValue);
  150. i = sizeof(SHA1_DIGEST_INFO);
  151.             memcpy(hash,SHA1_DIGEST_INFO,i);
  152.             memcpy(&hash[i],digestValue,20);
  153. *hashSize = i + 20;
  154. break;
  155.         case UID_sha1:
  156. SHA1(msg,msgSize,digestValue);
  157. i = sizeof(SHA1_DIGEST_INFO);
  158.             memcpy(hash,SHA1_DIGEST_INFO,i);
  159.             memcpy(&hash[i],digestValue,20);
  160. *hashSize = i + 20;
  161.   break;
  162. default:
  163. return FUNC_ERR;
  164.   }
  165. return FUNC_OK;
  166. }
  167. int c2d_RSA_PUBLIC_KEY(
  168.         RSA_PUBLIC_KEY  *rsaPublicKey,
  169.         uint8   *rsaPublicKeyDERString,
  170.         uint32  *rsaPublicKeyDERStringLen)
  171. {
  172. /*
  173. RSAPublicKey ::= SEQUENCE {
  174.     modulus            INTEGER, -- n
  175.     publicExponent     INTEGER  -- e -- }
  176.  
  177. typedef struct {
  178.     uint16    bits;    
  179.     UCHAR     modulus[MAX_RSA_MODULUS_LEN]; 
  180.     UCHAR     publicExponent [MAX_RSA_MODULUS_LEN];  
  181. }RSA_PUBLIC_KEY;
  182. */
  183.   DATA_BUFFER t_buf;
  184.   ASN1_SEQUENCE *p_head,*p_cur,*p_tail;
  185. uint8 buf[8]; 
  186. uint32 rtn_code;
  187. uint32 i,j,flag;
  188. p_head = 0;
  189. p_cur = 0;
  190. flag = 0;
  191. /* encode the n */
  192. if(flag == 0){
  193. flag = 1;
  194.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  195.         if(rtn_code != FUNC_OK){
  196.         return FUNC_ERR;
  197. }
  198.         p_head = p_tail;
  199.         p_cur = p_tail;
  200. }
  201. else{
  202.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  203.         if(rtn_code != FUNC_OK){
  204. if(p_head != 0)
  205. free_ASN1_SEQUENCE(p_head);
  206.         return FUNC_ERR;
  207. }
  208.         p_cur->next = p_tail;
  209.         p_cur = p_tail;
  210. }
  211. j = 0;
  212. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  213. if(rsaPublicKey->modulus[i] == 0){
  214. j++;
  215. }
  216. else{
  217. break;
  218. }
  219. }
  220. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  221. t_buf.data = &rsaPublicKey->modulus[j];
  222. if(t_buf.length == 0){
  223.         buf[0] = 0;
  224.     t_buf.length = 1;
  225.     t_buf.data = buf;
  226.     }
  227.     rtn_code = ASN1_Encode_Integer(
  228.                    t_buf,
  229.        &p_cur->data);
  230.     if(rtn_code != FUNC_OK){
  231. if(p_head!=0)
  232. free_ASN1_SEQUENCE(p_head);
  233. return FUNC_ERR;
  234. }
  235. /* encode the e */
  236. if(flag == 0){
  237. flag = 1;
  238.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  239.         if(rtn_code != FUNC_OK){
  240.         return FUNC_ERR;
  241. }
  242.         p_head = p_tail;
  243.         p_cur = p_tail;
  244. }
  245. else{
  246.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  247.         if(rtn_code != FUNC_OK){
  248. if(p_head!=0)
  249. free_ASN1_SEQUENCE(p_head);
  250.         return FUNC_ERR;
  251. }
  252.         p_cur->next = p_tail;
  253.         p_cur = p_tail;
  254. }
  255. j = 0;
  256. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  257. if(rsaPublicKey->publicExponent[i] == 0){
  258. j++;
  259. }
  260. else{
  261. break;
  262. }
  263. }
  264. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  265. t_buf.data = &rsaPublicKey->publicExponent[j];
  266. if(t_buf.length == 0){
  267.         buf[0] = 0;
  268.     t_buf.length = 1;
  269.     t_buf.data = buf;
  270.     }
  271.     rtn_code = ASN1_Encode_Integer(
  272.                    t_buf,
  273.        &p_cur->data);
  274.     if(rtn_code != FUNC_OK){
  275. if(p_head!=0)
  276.         free_ASN1_SEQUENCE(p_head);
  277. return FUNC_ERR;
  278. }
  279.  
  280.     /* encode the whole sequence */
  281.     rtn_code = ASN1_Encode_Sequence(
  282.                    p_head,
  283.    &t_buf);
  284.     if(rtn_code != FUNC_OK){
  285. if(p_head!=0)
  286.         free_ASN1_SEQUENCE(p_head);
  287. return FUNC_ERR;
  288. }
  289.     *rsaPublicKeyDERStringLen = t_buf.length;
  290. memcpy(rsaPublicKeyDERString,t_buf.data,*rsaPublicKeyDERStringLen);
  291. rtn_code = free_ASN1_SEQUENCE(p_head);
  292.     if(rtn_code != FUNC_OK){
  293. if(p_head!=0)
  294.         free_DATA_BUFFER(&t_buf);
  295. return FUNC_ERR;
  296. }
  297.     rtn_code = free_DATA_BUFFER(&t_buf);
  298.     if(rtn_code != FUNC_OK){
  299. return FUNC_ERR;
  300. }
  301. return FUNC_OK;
  302. }
  303. int d2c_RSA_PUBLIC_KEY(
  304.         uint8  *rsaPublicKeyDERString,
  305.         uint32 rsaPublicKeyDERStringLen,
  306.         RSA_PUBLIC_KEY  *rsaPublicKey)
  307. {
  308.   DATA_BUFFER t_buf;
  309.   ASN1_SEQUENCE *p_head,*p_cur;
  310. uint32 rtn_code;
  311. uint32 i;
  312. memset(rsaPublicKey,0,sizeof(RSA_PUBLIC_KEY));
  313. /* decode the sequence */
  314.     t_buf.length = rsaPublicKeyDERStringLen;
  315. t_buf.data = rsaPublicKeyDERString;
  316.     rtn_code = ASN1_Decode_Sequence(
  317.          t_buf,
  318.        &p_head);
  319. p_cur = p_head;
  320.     /* decode the n */
  321. if(p_cur == NULL){
  322. return FUNC_ERR;
  323. }
  324.     rtn_code = ASN1_Decode_Integer(
  325.            p_cur->data,
  326.                    &t_buf);
  327.     if(rtn_code != FUNC_OK){
  328. free_ASN1_SEQUENCE(p_head);
  329. return FUNC_ERR;
  330. }
  331.     if(t_buf.length <= 64){
  332. rsaPublicKey->bits = 512;
  333. }
  334. else{
  335.         if(t_buf.length <= 128){
  336.     rsaPublicKey->bits = 1024;
  337. }
  338. else{
  339. if(t_buf.length <= 256){
  340.         rsaPublicKey->bits = 2048;
  341. }
  342. else{
  343.         return FUNC_ERR;
  344. }
  345. }
  346. }
  347.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  348.     memcpy(&rsaPublicKey->modulus[i],t_buf.data,t_buf.length);
  349. rtn_code = free_DATA_BUFFER(&t_buf);
  350.     if(rtn_code != FUNC_OK){
  351. free_ASN1_SEQUENCE(p_head);
  352. return FUNC_ERR;
  353. }
  354.     /* decode the e */
  355. p_cur = p_cur->next;
  356. if(p_cur == NULL){
  357. free_ASN1_SEQUENCE(p_head);
  358. return FUNC_ERR;
  359. }
  360.     rtn_code = ASN1_Decode_Integer(
  361.            p_cur->data,
  362.                    &t_buf);
  363.     if(rtn_code != FUNC_OK){
  364. free_ASN1_SEQUENCE(p_head);
  365. return FUNC_ERR;
  366. }
  367.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  368.     memcpy(&rsaPublicKey->publicExponent[i],t_buf.data,t_buf.length);
  369. rtn_code = free_DATA_BUFFER(&t_buf);
  370.     if(rtn_code != FUNC_OK){
  371. free_ASN1_SEQUENCE(p_head);
  372. return FUNC_ERR;
  373. }
  374. rtn_code = free_ASN1_SEQUENCE(p_head);
  375.     if(rtn_code != FUNC_OK){
  376. return FUNC_ERR;
  377. }
  378. return FUNC_OK;
  379. }
  380. int c2d_RSA_PRIVATE_KEY(
  381.         RSA_PRIVATE_KEY  *rsaPrivateKey,
  382.         uint8  *rsaPrivateKeyDERString,
  383.         uint32 *rsaPrivateKeyDERStringLen)
  384. {    
  385. /*
  386. RSAPrivateKey ::= SEQUENCE {
  387.   version Version,
  388.   modulus INTEGER, -- n
  389.   publicExponent INTEGER, -- e
  390.   privateExponent INTEGER, -- d
  391.   prime1 INTEGER, -- p
  392.   prime2 INTEGER, -- q
  393.   exponent1 INTEGER, -- d mod (p-1)
  394.   exponent2 INTEGER, -- d mod (q-1)
  395.   coefficient INTEGER -- (inverse of q) mod p }
  396. typedef struct rsa_pri_key{
  397. uint16    bits;     
  398. UCHAR     modulus[MAX_RSA_MODULUS_LEN]; 
  399. UCHAR     publicExponent[MAX_RSA_MODULUS_LEN]; 
  400. UCHAR     privateExponent[MAX_RSA_MODULUS_LEN];          
  401. UCHAR     prime[2][MAX_RSA_PRIME_LEN];               
  402. UCHAR     exponent[2][MAX_RSA_PRIME_LEN];               
  403. UCHAR     coefficient [MAX_RSA_PRIME_LEN];               
  404. UCHAR   N[MAX_RSA_MODULUS_LEN];
  405. UCHAR   P[MAX_RSA_MODULUS_LEN];
  406. UCHAR   Q[MAX_RSA_MODULUS_LEN];
  407. UCHAR   d1[MAX_RSA_MODULUS_LEN];
  408. UCHAR   d2[MAX_RSA_MODULUS_LEN];
  409. UCHAR   P_[MAX_RSA_MODULUS_LEN];
  410. UCHAR   Q_[MAX_RSA_MODULUS_LEN];
  411. UCHAR   S[MAX_RSA_MODULUS_LEN];
  412. UCHAR   T[MAX_RSA_MODULUS_LEN];
  413. } RSA_PRIVATE_KEY;
  414. */
  415. DATA_BUFFER t_buf;
  416.   ASN1_SEQUENCE *p_head,*p_cur,*p_tail;
  417. uint8 buf[8]; 
  418. uint32 rtn_code;
  419. uint32 i,j,flag;
  420.     flag = 0;
  421. p_head = 0;
  422. p_cur = 0;
  423. /* encode the version */
  424. if(flag == 0){
  425. flag = 1;
  426.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  427.         if(rtn_code != FUNC_OK){
  428.         return FUNC_ERR;
  429. }
  430.         p_head = p_tail;
  431.         p_cur = p_tail;
  432. }
  433. else{
  434.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  435.         if(rtn_code != FUNC_OK){
  436.             free_ASN1_SEQUENCE(p_head);
  437.         return FUNC_ERR;
  438. }
  439.         p_cur->next = p_tail;
  440.         p_cur = p_tail;
  441. }
  442.     buf[0] = 0;
  443.  
  444. t_buf.length = 1;
  445. t_buf.data = buf;
  446.     rtn_code = ASN1_Encode_Integer(
  447.                    t_buf,
  448.        &p_cur->data);
  449.     if(rtn_code != FUNC_OK){
  450.         free_ASN1_SEQUENCE(p_head);
  451. return FUNC_ERR;
  452. }
  453. /* encode the modulus */
  454. if(flag == 0){
  455. flag = 1;
  456.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  457.         if(rtn_code != FUNC_OK){
  458.         return FUNC_ERR;
  459. }
  460.         p_head = p_tail;
  461.         p_cur = p_tail;
  462. }
  463. else{
  464.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  465.         if(rtn_code != FUNC_OK){
  466.             free_ASN1_SEQUENCE(p_head);
  467.         return FUNC_ERR;
  468. }
  469.         p_cur->next = p_tail;
  470.         p_cur = p_tail;
  471. }
  472. j = 0;
  473. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  474. if(rsaPrivateKey->modulus[i] == 0){
  475. j++;
  476. }
  477. else{
  478. break;
  479. }
  480. }
  481. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  482. t_buf.data = &rsaPrivateKey->modulus[j];
  483. if(t_buf.length == 0){
  484.         buf[0] = 0;
  485.     t_buf.length = 1;
  486.     t_buf.data = buf;
  487.     }
  488.     rtn_code = ASN1_Encode_Integer(
  489.                    t_buf,
  490.        &p_cur->data);
  491.     if(rtn_code != FUNC_OK){
  492.         free_ASN1_SEQUENCE(p_head);
  493. return FUNC_ERR;
  494. }
  495. /* encode the publicExponent */
  496. if(flag == 0){
  497. flag = 1;
  498.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  499.         if(rtn_code != FUNC_OK){
  500.         return FUNC_ERR;
  501. }
  502.         p_head = p_tail;
  503.         p_cur = p_tail;
  504. }
  505. else{
  506.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  507.         if(rtn_code != FUNC_OK){
  508.             free_ASN1_SEQUENCE(p_head);
  509.         return FUNC_ERR;
  510. }
  511.         p_cur->next = p_tail;
  512.         p_cur = p_tail;
  513. }
  514. j = 0;
  515. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  516. if(rsaPrivateKey->publicExponent[i] == 0){
  517. j++;
  518. }
  519. else{
  520. break;
  521. }
  522. }
  523. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  524. t_buf.data = &rsaPrivateKey->publicExponent[j];
  525. if(t_buf.length == 0){
  526.         buf[0] = 0;
  527.     t_buf.length = 1;
  528.     t_buf.data = buf;
  529.     }
  530.     rtn_code = ASN1_Encode_Integer(
  531.                    t_buf,
  532.        &p_cur->data);
  533.     if(rtn_code != FUNC_OK){
  534.         free_ASN1_SEQUENCE(p_head);
  535. return FUNC_ERR;
  536. }
  537. /* encode the privateExponent */
  538. if(flag == 0){
  539. flag = 1;
  540.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  541.         if(rtn_code != FUNC_OK){
  542.         return FUNC_ERR;
  543. }
  544.         p_head = p_tail;
  545.         p_cur = p_tail;
  546. }
  547. else{
  548.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  549.         if(rtn_code != FUNC_OK){
  550.             free_ASN1_SEQUENCE(p_head);
  551.         return FUNC_ERR;
  552. }
  553.         p_cur->next = p_tail;
  554.         p_cur = p_tail;
  555. }
  556. j = 0;
  557. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  558. if(rsaPrivateKey->privateExponent[i] == 0){
  559. j++;
  560. }
  561. else{
  562. break;
  563. }
  564. }
  565. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  566. t_buf.data = &rsaPrivateKey->privateExponent[j];
  567. if(t_buf.length == 0){
  568.         buf[0] = 0;
  569.     t_buf.length = 1;
  570.     t_buf.data = buf;
  571.     }
  572.     rtn_code = ASN1_Encode_Integer(
  573.                    t_buf,
  574.        &p_cur->data);
  575.     if(rtn_code != FUNC_OK){
  576.         free_ASN1_SEQUENCE(p_head);
  577. return FUNC_ERR;
  578. }
  579. /* encode the prime[0] */
  580. if(flag == 0){
  581. flag = 1;
  582.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  583.         if(rtn_code != FUNC_OK){
  584.         return FUNC_ERR;
  585. }
  586.         p_head = p_tail;
  587.         p_cur = p_tail;
  588. }
  589. else{
  590.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  591.         if(rtn_code != FUNC_OK){
  592.             free_ASN1_SEQUENCE(p_head);
  593.         return FUNC_ERR;
  594. }
  595.         p_cur->next = p_tail;
  596.         p_cur = p_tail;
  597. }
  598. j = 0;
  599. for(i=0;i<MAX_RSA_PRIME_LEN;i++){
  600. if(rsaPrivateKey->prime[0][i] == 0){
  601. j++;
  602. }
  603. else{
  604. break;
  605. }
  606. }
  607. t_buf.length = MAX_RSA_PRIME_LEN - j;
  608. t_buf.data = &rsaPrivateKey->prime[0][j];
  609. if(t_buf.length == 0){
  610.         buf[0] = 0;
  611.     t_buf.length = 1;
  612.     t_buf.data = buf;
  613.     }
  614.     rtn_code = ASN1_Encode_Integer(
  615.                    t_buf,
  616.        &p_cur->data);
  617.     if(rtn_code != FUNC_OK){
  618.         free_ASN1_SEQUENCE(p_head);
  619. return FUNC_ERR;
  620. }
  621. /* encode the prime[1] */
  622. if(flag == 0){
  623. flag = 1;
  624.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  625.         if(rtn_code != FUNC_OK){
  626.         return FUNC_ERR;
  627. }
  628.         p_head = p_tail;
  629.         p_cur = p_tail;
  630. }
  631. else{
  632.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  633.         if(rtn_code != FUNC_OK){
  634.             free_ASN1_SEQUENCE(p_head);
  635.         return FUNC_ERR;
  636. }
  637.         p_cur->next = p_tail;
  638.         p_cur = p_tail;
  639. }
  640. j = 0;
  641. for(i=0;i<MAX_RSA_PRIME_LEN;i++){
  642. if(rsaPrivateKey->prime[1][i] == 0){
  643. j++;
  644. }
  645. else{
  646. break;
  647. }
  648. }
  649. t_buf.length = MAX_RSA_PRIME_LEN - j;
  650. t_buf.data = &rsaPrivateKey->prime[1][j];
  651. if(t_buf.length == 0){
  652.         buf[0] = 0;
  653.     t_buf.length = 1;
  654.     t_buf.data = buf;
  655.     }
  656.     rtn_code = ASN1_Encode_Integer(
  657.                    t_buf,
  658.        &p_cur->data);
  659.     if(rtn_code != FUNC_OK){
  660.         free_ASN1_SEQUENCE(p_head);
  661. return FUNC_ERR;
  662. }
  663. /* encode the exponent[0] */
  664. if(flag == 0){
  665. flag = 1;
  666.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  667.         if(rtn_code != FUNC_OK){
  668.         return FUNC_ERR;
  669. }
  670.         p_head = p_tail;
  671.         p_cur = p_tail;
  672. }
  673. else{
  674.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  675.         if(rtn_code != FUNC_OK){
  676.             free_ASN1_SEQUENCE(p_head);
  677.         return FUNC_ERR;
  678. }
  679.         p_cur->next = p_tail;
  680.         p_cur = p_tail;
  681. }
  682. j = 0;
  683. for(i=0;i<MAX_RSA_PRIME_LEN;i++){
  684. if(rsaPrivateKey->exponent[0][i] == 0){
  685. j++;
  686. }
  687. else{
  688. break;
  689. }
  690. }
  691. t_buf.length = MAX_RSA_PRIME_LEN - j;
  692. t_buf.data = &rsaPrivateKey->exponent[0][j];
  693. if(t_buf.length == 0){
  694.         buf[0] = 0;
  695.     t_buf.length = 1;
  696.     t_buf.data = buf;
  697.     }
  698.     rtn_code = ASN1_Encode_Integer(
  699.                    t_buf,
  700.        &p_cur->data);
  701.     if(rtn_code != FUNC_OK){
  702.         free_ASN1_SEQUENCE(p_head);
  703. return FUNC_ERR;
  704. }
  705. /* encode the exponent[1] */
  706. if(flag == 0){
  707. flag = 1;
  708.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  709.         if(rtn_code != FUNC_OK){
  710.         return FUNC_ERR;
  711. }
  712.         p_head = p_tail;
  713.         p_cur = p_tail;
  714. }
  715. else{
  716.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  717.         if(rtn_code != FUNC_OK){
  718.             free_ASN1_SEQUENCE(p_head);
  719.         return FUNC_ERR;
  720. }
  721.         p_cur->next = p_tail;
  722.         p_cur = p_tail;
  723. }
  724. j = 0;
  725. for(i=0;i<MAX_RSA_PRIME_LEN;i++){
  726. if(rsaPrivateKey->exponent[1][i] == 0){
  727. j++;
  728. }
  729. else{
  730. break;
  731. }
  732. }
  733. t_buf.length = MAX_RSA_PRIME_LEN - j;
  734. t_buf.data = &rsaPrivateKey->exponent[1][j];
  735. if(t_buf.length == 0){
  736.         buf[0] = 0;
  737.     t_buf.length = 1;
  738.     t_buf.data = buf;
  739.     }
  740.     rtn_code = ASN1_Encode_Integer(
  741.                    t_buf,
  742.        &p_cur->data);
  743.     if(rtn_code != FUNC_OK){
  744.         free_ASN1_SEQUENCE(p_head);
  745. return FUNC_ERR;
  746. }
  747. /* encode the coefficient */
  748. if(flag == 0){
  749. flag = 1;
  750.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  751.         if(rtn_code != FUNC_OK){
  752.         return FUNC_ERR;
  753. }
  754.         p_head = p_tail;
  755.         p_cur = p_tail;
  756. }
  757. else{
  758.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  759.         if(rtn_code != FUNC_OK){
  760.             free_ASN1_SEQUENCE(p_head);
  761.         return FUNC_ERR;
  762. }
  763.         p_cur->next = p_tail;
  764.         p_cur = p_tail;
  765. }
  766. j = 0;
  767. for(i=0;i<MAX_RSA_PRIME_LEN;i++){
  768. if(rsaPrivateKey->coefficient[i] == 0){
  769. j++;
  770. }
  771. else{
  772. break;
  773. }
  774. }
  775. t_buf.length = MAX_RSA_PRIME_LEN - j;
  776. t_buf.data = &rsaPrivateKey->coefficient[j];
  777. if(t_buf.length == 0){
  778.         buf[0] = 0;
  779.     t_buf.length = 1;
  780.     t_buf.data = buf;
  781.     }
  782.     rtn_code = ASN1_Encode_Integer(
  783.                    t_buf,
  784.        &p_cur->data);
  785.     if(rtn_code != FUNC_OK){
  786.         free_ASN1_SEQUENCE(p_head);
  787. return FUNC_ERR;
  788. }
  789. /***************************************/
  790. /*  
  791. if(flag == 0){
  792. flag = 1;
  793.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  794.         if(rtn_code != FUNC_OK){
  795.         return FUNC_ERR;
  796. }
  797.         p_head = p_tail;
  798.         p_cur = p_tail;
  799. }
  800. else{
  801.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  802.         if(rtn_code != FUNC_OK){
  803.             free_ASN1_SEQUENCE(p_head);
  804.         return FUNC_ERR;
  805. }
  806.         p_cur->next = p_tail;
  807.         p_cur = p_tail;
  808. }
  809. j = 0;
  810. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  811. if(rsaPrivateKey->N[i] == 0){
  812. j++;
  813. }
  814. else{
  815. break;
  816. }
  817. }
  818. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  819. t_buf.data = &rsaPrivateKey->N[j];
  820. if(t_buf.length == 0){
  821.         buf[0] = 0;
  822.     t_buf.length = 1;
  823.     t_buf.data = buf;
  824.     }
  825.     rtn_code = ASN1_Encode_Integer(
  826.                    t_buf,
  827.        &p_cur->data);
  828.     if(rtn_code != FUNC_OK){
  829.         free_ASN1_SEQUENCE(p_head);
  830. return FUNC_ERR;
  831. }
  832.  
  833.  
  834. if(flag == 0){
  835. flag = 1;
  836.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  837.         if(rtn_code != FUNC_OK){
  838.         return FUNC_ERR;
  839. }
  840.         p_head = p_tail;
  841.         p_cur = p_tail;
  842. }
  843. else{
  844.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  845.         if(rtn_code != FUNC_OK){
  846.             free_ASN1_SEQUENCE(p_head);
  847.         return FUNC_ERR;
  848. }
  849.         p_cur->next = p_tail;
  850.         p_cur = p_tail;
  851. }
  852. j = 0;
  853. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  854. if(rsaPrivateKey->P[i] == 0){
  855. j++;
  856. }
  857. else{
  858. break;
  859. }
  860. }
  861. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  862. t_buf.data = &rsaPrivateKey->P[j];
  863. if(t_buf.length == 0){
  864.         buf[0] = 0;
  865.     t_buf.length = 1;
  866.     t_buf.data = buf;
  867.     }
  868.     rtn_code = ASN1_Encode_Integer(
  869.                    t_buf,
  870.        &p_cur->data);
  871.     if(rtn_code != FUNC_OK){
  872.         free_ASN1_SEQUENCE(p_head);
  873. return FUNC_ERR;
  874. }
  875.  
  876. if(flag == 0){
  877. flag = 1;
  878.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  879.         if(rtn_code != FUNC_OK){
  880.         return FUNC_ERR;
  881. }
  882.         p_head = p_tail;
  883.         p_cur = p_tail;
  884. }
  885. else{
  886.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  887.         if(rtn_code != FUNC_OK){
  888.             free_ASN1_SEQUENCE(p_head);
  889.         return FUNC_ERR;
  890. }
  891.         p_cur->next = p_tail;
  892.         p_cur = p_tail;
  893. }
  894. j = 0;
  895. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  896. if(rsaPrivateKey->Q[i] == 0){
  897. j++;
  898. }
  899. else{
  900. break;
  901. }
  902. }
  903. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  904. t_buf.data = &rsaPrivateKey->Q[j];
  905. if(t_buf.length == 0){
  906.         buf[0] = 0;
  907.     t_buf.length = 1;
  908.     t_buf.data = buf;
  909.     }
  910.     rtn_code = ASN1_Encode_Integer(
  911.                    t_buf,
  912.        &p_cur->data);
  913.     if(rtn_code != FUNC_OK){
  914.         free_ASN1_SEQUENCE(p_head);
  915. return FUNC_ERR;
  916. }
  917.  
  918. if(flag == 0){
  919. flag = 1;
  920.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  921.         if(rtn_code != FUNC_OK){
  922.         return FUNC_ERR;
  923. }
  924.         p_head = p_tail;
  925.         p_cur = p_tail;
  926. }
  927. else{
  928.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  929.         if(rtn_code != FUNC_OK){
  930.             free_ASN1_SEQUENCE(p_head);
  931.         return FUNC_ERR;
  932. }
  933.         p_cur->next = p_tail;
  934.         p_cur = p_tail;
  935. }
  936. j = 0;
  937. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  938. if(rsaPrivateKey->d1[i] == 0){
  939. j++;
  940. }
  941. else{
  942. break;
  943. }
  944. }
  945. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  946. t_buf.data = &rsaPrivateKey->d1[j];
  947. if(t_buf.length == 0){
  948.         buf[0] = 0;
  949.     t_buf.length = 1;
  950.     t_buf.data = buf;
  951.     }
  952.     rtn_code = ASN1_Encode_Integer(
  953.                    t_buf,
  954.        &p_cur->data);
  955.     if(rtn_code != FUNC_OK){
  956.         free_ASN1_SEQUENCE(p_head);
  957. return FUNC_ERR;
  958. }
  959.  
  960. if(flag == 0){
  961. flag = 1;
  962.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  963.         if(rtn_code != FUNC_OK){
  964.         return FUNC_ERR;
  965. }
  966.         p_head = p_tail;
  967.         p_cur = p_tail;
  968. }
  969. else{
  970.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  971.         if(rtn_code != FUNC_OK){
  972.             free_ASN1_SEQUENCE(p_head);
  973.         return FUNC_ERR;
  974. }
  975.         p_cur->next = p_tail;
  976.         p_cur = p_tail;
  977. }
  978. j = 0;
  979. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  980. if(rsaPrivateKey->d2[i] == 0){
  981. j++;
  982. }
  983. else{
  984. break;
  985. }
  986. }
  987. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  988. t_buf.data = &rsaPrivateKey->d2[j];
  989. if(t_buf.length == 0){
  990.         buf[0] = 0;
  991.     t_buf.length = 1;
  992.     t_buf.data = buf;
  993.     }
  994.     rtn_code = ASN1_Encode_Integer(
  995.                    t_buf,
  996.        &p_cur->data);
  997.     if(rtn_code != FUNC_OK){
  998.         free_ASN1_SEQUENCE(p_head);
  999. return FUNC_ERR;
  1000. }
  1001.  
  1002. if(flag == 0){
  1003. flag = 1;
  1004.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1005.         if(rtn_code != FUNC_OK){
  1006.         return FUNC_ERR;
  1007. }
  1008.         p_head = p_tail;
  1009.         p_cur = p_tail;
  1010. }
  1011. else{
  1012.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1013.         if(rtn_code != FUNC_OK){
  1014.             free_ASN1_SEQUENCE(p_head);
  1015.         return FUNC_ERR;
  1016. }
  1017.         p_cur->next = p_tail;
  1018.         p_cur = p_tail;
  1019. }
  1020. j = 0;
  1021. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  1022. if(rsaPrivateKey->P_[i] == 0){
  1023. j++;
  1024. }
  1025. else{
  1026. break;
  1027. }
  1028. }
  1029. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  1030. t_buf.data = &rsaPrivateKey->P_[j];
  1031. if(t_buf.length == 0){
  1032.         buf[0] = 0;
  1033.     t_buf.length = 1;
  1034.     t_buf.data = buf;
  1035.     }
  1036.     rtn_code = ASN1_Encode_Integer(
  1037.                    t_buf,
  1038.        &p_cur->data);
  1039.     if(rtn_code != FUNC_OK){
  1040.         free_ASN1_SEQUENCE(p_head);
  1041. return FUNC_ERR;
  1042. }
  1043.  
  1044. if(flag == 0){
  1045. flag = 1;
  1046.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1047.         if(rtn_code != FUNC_OK){
  1048.         return FUNC_ERR;
  1049. }
  1050.         p_head = p_tail;
  1051.         p_cur = p_tail;
  1052. }
  1053. else{
  1054.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1055.         if(rtn_code != FUNC_OK){
  1056.             free_ASN1_SEQUENCE(p_head);
  1057.         return FUNC_ERR;
  1058. }
  1059.         p_cur->next = p_tail;
  1060.         p_cur = p_tail;
  1061. }
  1062. j = 0;
  1063. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  1064. if(rsaPrivateKey->Q_[i] == 0){
  1065. j++;
  1066. }
  1067. else{
  1068. break;
  1069. }
  1070. }
  1071. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  1072. t_buf.data = &rsaPrivateKey->Q_[j];
  1073. if(t_buf.length == 0){
  1074.         buf[0] = 0;
  1075.     t_buf.length = 1;
  1076.     t_buf.data = buf;
  1077.     }
  1078.     rtn_code = ASN1_Encode_Integer(
  1079.                    t_buf,
  1080.        &p_cur->data);
  1081.     if(rtn_code != FUNC_OK){
  1082.         free_ASN1_SEQUENCE(p_head);
  1083. return FUNC_ERR;
  1084. }
  1085.  
  1086. if(flag == 0){
  1087. flag = 1;
  1088.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1089.         if(rtn_code != FUNC_OK){
  1090.         return FUNC_ERR;
  1091. }
  1092.         p_head = p_tail;
  1093.         p_cur = p_tail;
  1094. }
  1095. else{
  1096.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1097.         if(rtn_code != FUNC_OK){
  1098.             free_ASN1_SEQUENCE(p_head);
  1099.         return FUNC_ERR;
  1100. }
  1101.         p_cur->next = p_tail;
  1102.         p_cur = p_tail;
  1103. }
  1104. j = 0;
  1105. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  1106. if(rsaPrivateKey->S[i] == 0){
  1107. j++;
  1108. }
  1109. else{
  1110. break;
  1111. }
  1112. }
  1113. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  1114. t_buf.data = &rsaPrivateKey->S[j];
  1115. if(t_buf.length == 0){
  1116.         buf[0] = 0;
  1117.     t_buf.length = 1;
  1118.     t_buf.data = buf;
  1119.     }
  1120.     rtn_code = ASN1_Encode_Integer(
  1121.                    t_buf,
  1122.        &p_cur->data);
  1123.     if(rtn_code != FUNC_OK){
  1124.         free_ASN1_SEQUENCE(p_head);
  1125. return FUNC_ERR;
  1126. }
  1127.  
  1128. if(flag == 0){
  1129. flag = 1;
  1130.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1131.         if(rtn_code != FUNC_OK){
  1132.         return FUNC_ERR;
  1133. }
  1134.         p_head = p_tail;
  1135.         p_cur = p_tail;
  1136. }
  1137. else{
  1138.         rtn_code = new_ASN1_SEQUENCE(&p_tail);
  1139.         if(rtn_code != FUNC_OK){
  1140.             free_ASN1_SEQUENCE(p_head);
  1141.         return FUNC_ERR;
  1142. }
  1143.         p_cur->next = p_tail;
  1144.         p_cur = p_tail;
  1145. }
  1146. j = 0;
  1147. for(i=0;i<MAX_RSA_MODULUS_LEN;i++){
  1148. if(rsaPrivateKey->T[i] == 0){
  1149. j++;
  1150. }
  1151. else{
  1152. break;
  1153. }
  1154. }
  1155. t_buf.length = MAX_RSA_MODULUS_LEN - j;
  1156. t_buf.data = &rsaPrivateKey->T[j];
  1157. if(t_buf.length == 0){
  1158.         buf[0] = 0;
  1159.     t_buf.length = 1;
  1160.     t_buf.data = buf;
  1161.     }
  1162.     rtn_code = ASN1_Encode_Integer(
  1163.                    t_buf,
  1164.        &p_cur->data);
  1165.     if(rtn_code != FUNC_OK){
  1166.         free_ASN1_SEQUENCE(p_head);
  1167. return FUNC_ERR;
  1168. }
  1169. */
  1170. /****************************************/
  1171.     /* encode the whole sequence */
  1172.     rtn_code = ASN1_Encode_Sequence(
  1173.                    p_head,
  1174.    &t_buf);
  1175.     if(rtn_code != FUNC_OK){
  1176.         free_ASN1_SEQUENCE(p_head);
  1177. return FUNC_ERR;
  1178. }
  1179.     *rsaPrivateKeyDERStringLen = t_buf.length;
  1180. memcpy(rsaPrivateKeyDERString,t_buf.data,*rsaPrivateKeyDERStringLen);
  1181. rtn_code = free_ASN1_SEQUENCE(p_head);
  1182.     if(rtn_code != FUNC_OK){
  1183.         free_DATA_BUFFER(&t_buf);
  1184. return FUNC_ERR;
  1185. }
  1186.     rtn_code = free_DATA_BUFFER(&t_buf);
  1187.     if(rtn_code != FUNC_OK){
  1188. return FUNC_ERR;
  1189. }
  1190. return FUNC_OK;
  1191. }
  1192. int d2c_RSA_PRIVATE_KEY(
  1193.         uint8  *rsaPrivateKeyDERString,
  1194.         uint32 rsaPrivateKeyDERStringLen,
  1195.         RSA_PRIVATE_KEY  *rsaPrivateKey)
  1196. {
  1197. /*
  1198. typedef struct rsa_pri_key{
  1199. uint16    bits;     
  1200. UCHAR     modulus[MAX_RSA_MODULUS_LEN]; 
  1201. UCHAR     publicExponent[MAX_RSA_MODULUS_LEN]; 
  1202. UCHAR     privateExponent[MAX_RSA_MODULUS_LEN];          
  1203. UCHAR     prime[2][MAX_RSA_PRIME_LEN];               
  1204. UCHAR     exponent[2][MAX_RSA_PRIME_LEN];               
  1205. UCHAR     coefficient [MAX_RSA_PRIME_LEN];               
  1206. UCHAR   N[MAX_RSA_MODULUS_LEN];
  1207. UCHAR   P[MAX_RSA_MODULUS_LEN];
  1208. UCHAR   Q[MAX_RSA_MODULUS_LEN];
  1209. UCHAR   d1[MAX_RSA_MODULUS_LEN];
  1210. UCHAR   d2[MAX_RSA_MODULUS_LEN];
  1211. UCHAR   P_[MAX_RSA_MODULUS_LEN];
  1212. UCHAR   Q_[MAX_RSA_MODULUS_LEN];
  1213. UCHAR   S[MAX_RSA_MODULUS_LEN];
  1214. UCHAR   T[MAX_RSA_MODULUS_LEN];
  1215. } RSA_PRIVATE_KEY;
  1216. */
  1217.   DATA_BUFFER t_buf;
  1218.   ASN1_SEQUENCE *p_head,*p_cur;
  1219. uint32 rtn_code;
  1220. uint32 i;
  1221. memset(rsaPrivateKey,0,sizeof(RSA_PRIVATE_KEY));
  1222. /* decode the sequence */
  1223.     t_buf.length = rsaPrivateKeyDERStringLen;
  1224. t_buf.data = rsaPrivateKeyDERString;
  1225.     rtn_code = ASN1_Decode_Sequence(
  1226.          t_buf,
  1227.        &p_head);
  1228. p_cur = p_head;
  1229.     /* decode the modulus */
  1230. p_cur = p_cur->next; /* igore the version */
  1231. if(p_cur == NULL){
  1232. return FUNC_ERR;
  1233. }
  1234.     rtn_code = ASN1_Decode_Integer(
  1235.            p_cur->data,
  1236.                    &t_buf);
  1237.     if(rtn_code != FUNC_OK){
  1238. free_ASN1_SEQUENCE(p_head);
  1239. return FUNC_ERR;
  1240. }
  1241.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1242.     memcpy(&rsaPrivateKey->modulus[i],t_buf.data,t_buf.length);
  1243. ////////////////////////////  decode the bits  //////////////////
  1244. if(t_buf.length <= 64){
  1245. rsaPrivateKey->bits = 512;
  1246. }
  1247. else{
  1248.         if(t_buf.length <= 128){
  1249.     rsaPrivateKey->bits = 1024;
  1250. }
  1251. else{
  1252. if(t_buf.length <= 256){
  1253.         rsaPrivateKey->bits = 2048;
  1254. }
  1255. else{
  1256.         return FUNC_ERR;
  1257. }
  1258. }
  1259. }
  1260. ///////////////////////////////////////////////////////////////
  1261. rtn_code = free_DATA_BUFFER(&t_buf);
  1262.     if(rtn_code != FUNC_OK){
  1263. free_ASN1_SEQUENCE(p_head);
  1264. return FUNC_ERR;
  1265. }
  1266.     /* decode the publicExponent */
  1267. p_cur = p_cur->next;
  1268. if(p_cur == NULL){
  1269. free_ASN1_SEQUENCE(p_head);
  1270. return FUNC_ERR;
  1271. }
  1272.     rtn_code = ASN1_Decode_Integer(
  1273.            p_cur->data,
  1274.                    &t_buf);
  1275.     if(rtn_code != FUNC_OK){
  1276. free_ASN1_SEQUENCE(p_head);
  1277. return FUNC_ERR;
  1278. }
  1279.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1280.     memcpy(&rsaPrivateKey->publicExponent[i],t_buf.data,t_buf.length);
  1281. rtn_code = free_DATA_BUFFER(&t_buf);
  1282.     if(rtn_code != FUNC_OK){
  1283. free_ASN1_SEQUENCE(p_head);
  1284. return FUNC_ERR;
  1285. }
  1286.     /* decode the privateExponent */
  1287. p_cur = p_cur->next;
  1288. if(p_cur == NULL){
  1289. free_ASN1_SEQUENCE(p_head);
  1290. return FUNC_ERR;
  1291. }
  1292.     rtn_code = ASN1_Decode_Integer(
  1293.            p_cur->data,
  1294.                    &t_buf);
  1295.     if(rtn_code != FUNC_OK){
  1296. free_ASN1_SEQUENCE(p_head);
  1297. return FUNC_ERR;
  1298. }
  1299.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1300.     memcpy(&rsaPrivateKey->privateExponent[i],t_buf.data,t_buf.length);
  1301. rtn_code = free_DATA_BUFFER(&t_buf);
  1302.     if(rtn_code != FUNC_OK){
  1303. free_ASN1_SEQUENCE(p_head);
  1304. return FUNC_ERR;
  1305. }
  1306.     /* decode the prime[0] */
  1307. p_cur = p_cur->next;
  1308. if(p_cur == NULL){
  1309. free_ASN1_SEQUENCE(p_head);
  1310. return FUNC_ERR;
  1311. }
  1312.     rtn_code = ASN1_Decode_Integer(
  1313.            p_cur->data,
  1314.                    &t_buf);
  1315.     if(rtn_code != FUNC_OK){
  1316. free_ASN1_SEQUENCE(p_head);
  1317. return FUNC_ERR;
  1318. }
  1319.     i = MAX_RSA_PRIME_LEN - t_buf.length;
  1320.     memcpy(&rsaPrivateKey->prime[0][i],t_buf.data,t_buf.length);
  1321. rtn_code = free_DATA_BUFFER(&t_buf);
  1322.     if(rtn_code != FUNC_OK){
  1323. free_ASN1_SEQUENCE(p_head);
  1324. return FUNC_ERR;
  1325. }
  1326.     /* decode the prime[1] */
  1327. p_cur = p_cur->next;
  1328. if(p_cur == NULL){
  1329. free_ASN1_SEQUENCE(p_head);
  1330. return FUNC_ERR;
  1331. }
  1332.     rtn_code = ASN1_Decode_Integer(
  1333.            p_cur->data,
  1334.                    &t_buf);
  1335.     if(rtn_code != FUNC_OK){
  1336. free_ASN1_SEQUENCE(p_head);
  1337. return FUNC_ERR;
  1338. }
  1339.     i = MAX_RSA_PRIME_LEN - t_buf.length;
  1340.     memcpy(&rsaPrivateKey->prime[1][i],t_buf.data,t_buf.length);
  1341. rtn_code = free_DATA_BUFFER(&t_buf);
  1342.     if(rtn_code != FUNC_OK){
  1343. free_ASN1_SEQUENCE(p_head);
  1344. return FUNC_ERR;
  1345. }
  1346.     /* decode the exponent[0] */
  1347. p_cur = p_cur->next;
  1348. if(p_cur == NULL){
  1349. free_ASN1_SEQUENCE(p_head);
  1350. return FUNC_ERR;
  1351. }
  1352.     rtn_code = ASN1_Decode_Integer(
  1353.            p_cur->data,
  1354.                    &t_buf);
  1355.     if(rtn_code != FUNC_OK){
  1356. free_ASN1_SEQUENCE(p_head);
  1357. return FUNC_ERR;
  1358. }
  1359.     i = MAX_RSA_PRIME_LEN - t_buf.length;
  1360.     memcpy(&rsaPrivateKey->exponent[0][i],t_buf.data,t_buf.length);
  1361. rtn_code = free_DATA_BUFFER(&t_buf);
  1362.     if(rtn_code != FUNC_OK){
  1363. free_ASN1_SEQUENCE(p_head);
  1364. return FUNC_ERR;
  1365. }
  1366.     /* decode the exponent[1] */
  1367. p_cur = p_cur->next;
  1368. if(p_cur == NULL){
  1369. free_ASN1_SEQUENCE(p_head);
  1370. return FUNC_ERR;
  1371. }
  1372.     rtn_code = ASN1_Decode_Integer(
  1373.            p_cur->data,
  1374.                    &t_buf);
  1375.     if(rtn_code != FUNC_OK){
  1376. free_ASN1_SEQUENCE(p_head);
  1377. return FUNC_ERR;
  1378. }
  1379.     i = MAX_RSA_PRIME_LEN - t_buf.length;
  1380.     memcpy(&rsaPrivateKey->exponent[1][i],t_buf.data,t_buf.length);
  1381. rtn_code = free_DATA_BUFFER(&t_buf);
  1382.     if(rtn_code != FUNC_OK){
  1383. free_ASN1_SEQUENCE(p_head);
  1384. return FUNC_ERR;
  1385. }
  1386.     /* decode the coefficient */
  1387. p_cur = p_cur->next;
  1388. if(p_cur == NULL){
  1389. free_ASN1_SEQUENCE(p_head);
  1390. return FUNC_ERR;
  1391. }
  1392.     rtn_code = ASN1_Decode_Integer(
  1393.            p_cur->data,
  1394.                    &t_buf);
  1395.     if(rtn_code != FUNC_OK){
  1396. free_ASN1_SEQUENCE(p_head);
  1397. return FUNC_ERR;
  1398. }
  1399.     i = MAX_RSA_PRIME_LEN - t_buf.length;
  1400.     memcpy(&rsaPrivateKey->coefficient[i],t_buf.data,t_buf.length);
  1401. rtn_code = free_DATA_BUFFER(&t_buf);
  1402.     if(rtn_code != FUNC_OK){
  1403. free_ASN1_SEQUENCE(p_head);
  1404. return FUNC_ERR;
  1405. }
  1406. /***************************************/
  1407. /*     
  1408. p_cur = p_cur->next;
  1409. if(p_cur == NULL){
  1410. free_ASN1_SEQUENCE(p_head);
  1411. return FUNC_ERR;
  1412. }
  1413.     rtn_code = ASN1_Decode_Integer(
  1414.            p_cur->data,
  1415.                    &t_buf);
  1416.     if(rtn_code != FUNC_OK){
  1417. free_ASN1_SEQUENCE(p_head);
  1418. return FUNC_ERR;
  1419. }
  1420.     if(t_buf.length <= 64){
  1421. rsaPrivateKey->bits = 512;
  1422. }
  1423. else{
  1424.         if(t_buf.length <= 128){
  1425.     rsaPrivateKey->bits = 1024;
  1426. }
  1427. else{
  1428. if(t_buf.length <= 256){
  1429.         rsaPrivateKey->bits = 2048;
  1430. }
  1431. else{
  1432.         return FUNC_ERR;
  1433. }
  1434. }
  1435. }
  1436.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1437.     memcpy(&rsaPrivateKey->N[i],t_buf.data,t_buf.length);
  1438. rtn_code = free_DATA_BUFFER(&t_buf);
  1439.     if(rtn_code != FUNC_OK){
  1440. free_ASN1_SEQUENCE(p_head);
  1441. return FUNC_ERR;
  1442. }
  1443.  
  1444.     
  1445. p_cur = p_cur->next;
  1446. if(p_cur == NULL){
  1447. free_ASN1_SEQUENCE(p_head);
  1448. return FUNC_ERR;
  1449. }
  1450.     rtn_code = ASN1_Decode_Integer(
  1451.            p_cur->data,
  1452.                    &t_buf);
  1453.     if(rtn_code != FUNC_OK){
  1454. free_ASN1_SEQUENCE(p_head);
  1455. return FUNC_ERR;
  1456. }
  1457.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1458.     memcpy(&rsaPrivateKey->P[i],t_buf.data,t_buf.length);
  1459. rtn_code = free_DATA_BUFFER(&t_buf);
  1460.     if(rtn_code != FUNC_OK){
  1461. free_ASN1_SEQUENCE(p_head);
  1462. return FUNC_ERR;
  1463. }
  1464.    
  1465. p_cur = p_cur->next;
  1466. if(p_cur == NULL){
  1467. free_ASN1_SEQUENCE(p_head);
  1468. return FUNC_ERR;
  1469. }
  1470.     rtn_code = ASN1_Decode_Integer(
  1471.            p_cur->data,
  1472.                    &t_buf);
  1473.     if(rtn_code != FUNC_OK){
  1474. free_ASN1_SEQUENCE(p_head);
  1475. return FUNC_ERR;
  1476. }
  1477.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1478.     memcpy(&rsaPrivateKey->Q[i],t_buf.data,t_buf.length);
  1479. rtn_code = free_DATA_BUFFER(&t_buf);
  1480.     if(rtn_code != FUNC_OK){
  1481. free_ASN1_SEQUENCE(p_head);
  1482. return FUNC_ERR;
  1483. }
  1484.     
  1485. p_cur = p_cur->next;
  1486. if(p_cur == NULL){
  1487. free_ASN1_SEQUENCE(p_head);
  1488. return FUNC_ERR;
  1489. }
  1490.     rtn_code = ASN1_Decode_Integer(
  1491.            p_cur->data,
  1492.                    &t_buf);
  1493.     if(rtn_code != FUNC_OK){
  1494. free_ASN1_SEQUENCE(p_head);
  1495. return FUNC_ERR;
  1496. }
  1497.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1498.     memcpy(&rsaPrivateKey->d1[i],t_buf.data,t_buf.length);
  1499. rtn_code = free_DATA_BUFFER(&t_buf);
  1500.     if(rtn_code != FUNC_OK){
  1501. free_ASN1_SEQUENCE(p_head);
  1502. return FUNC_ERR;
  1503. }
  1504.   
  1505. p_cur = p_cur->next;
  1506. if(p_cur == NULL){
  1507. free_ASN1_SEQUENCE(p_head);
  1508. return FUNC_ERR;
  1509. }
  1510.     rtn_code = ASN1_Decode_Integer(
  1511.            p_cur->data,
  1512.                    &t_buf);
  1513.     if(rtn_code != FUNC_OK){
  1514. free_ASN1_SEQUENCE(p_head);
  1515. return FUNC_ERR;
  1516. }
  1517.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1518.     memcpy(&rsaPrivateKey->d2[i],t_buf.data,t_buf.length);
  1519. rtn_code = free_DATA_BUFFER(&t_buf);
  1520.     if(rtn_code != FUNC_OK){
  1521. free_ASN1_SEQUENCE(p_head);
  1522. return FUNC_ERR;
  1523. }
  1524.   
  1525. p_cur = p_cur->next;
  1526. if(p_cur == NULL){
  1527. free_ASN1_SEQUENCE(p_head);
  1528. return FUNC_ERR;
  1529. }
  1530.     rtn_code = ASN1_Decode_Integer(
  1531.            p_cur->data,
  1532.                    &t_buf);
  1533.     if(rtn_code != FUNC_OK){
  1534. free_ASN1_SEQUENCE(p_head);
  1535. return FUNC_ERR;
  1536. }
  1537.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1538.     memcpy(&rsaPrivateKey->P_[i],t_buf.data,t_buf.length);
  1539. rtn_code = free_DATA_BUFFER(&t_buf);
  1540.     if(rtn_code != FUNC_OK){
  1541. free_ASN1_SEQUENCE(p_head);
  1542. return FUNC_ERR;
  1543. }
  1544.    
  1545. p_cur = p_cur->next;
  1546. if(p_cur == NULL){
  1547. free_ASN1_SEQUENCE(p_head);
  1548. return FUNC_ERR;
  1549. }
  1550.     rtn_code = ASN1_Decode_Integer(
  1551.            p_cur->data,
  1552.                    &t_buf);
  1553.     if(rtn_code != FUNC_OK){
  1554. free_ASN1_SEQUENCE(p_head);
  1555. return FUNC_ERR;
  1556. }
  1557.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1558.     memcpy(&rsaPrivateKey->Q_[i],t_buf.data,t_buf.length);
  1559. rtn_code = free_DATA_BUFFER(&t_buf);
  1560.     if(rtn_code != FUNC_OK){
  1561. free_ASN1_SEQUENCE(p_head);
  1562. return FUNC_ERR;
  1563. }
  1564.    
  1565. p_cur = p_cur->next;
  1566. if(p_cur == NULL){
  1567. free_ASN1_SEQUENCE(p_head);
  1568. return FUNC_ERR;
  1569. }
  1570.     rtn_code = ASN1_Decode_Integer(
  1571.            p_cur->data,
  1572.                    &t_buf);
  1573.     if(rtn_code != FUNC_OK){
  1574. free_ASN1_SEQUENCE(p_head);
  1575. return FUNC_ERR;
  1576. }
  1577.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1578.     memcpy(&rsaPrivateKey->S[i],t_buf.data,t_buf.length);
  1579. rtn_code = free_DATA_BUFFER(&t_buf);
  1580.     if(rtn_code != FUNC_OK){
  1581. free_ASN1_SEQUENCE(p_head);
  1582. return FUNC_ERR;
  1583. }
  1584.    
  1585. p_cur = p_cur->next;
  1586. if(p_cur == NULL){
  1587. free_ASN1_SEQUENCE(p_head);
  1588. return FUNC_ERR;
  1589. }
  1590.     rtn_code = ASN1_Decode_Integer(
  1591.            p_cur->data,
  1592.                    &t_buf);
  1593.     if(rtn_code != FUNC_OK){
  1594. free_ASN1_SEQUENCE(p_head);
  1595. return FUNC_ERR;
  1596. }
  1597.     i = MAX_RSA_MODULUS_LEN - t_buf.length;
  1598.     memcpy(&rsaPrivateKey->T[i],t_buf.data,t_buf.length);
  1599. rtn_code = free_DATA_BUFFER(&t_buf);
  1600.     if(rtn_code != FUNC_OK){
  1601. free_ASN1_SEQUENCE(p_head);
  1602. return FUNC_ERR;
  1603. }
  1604. */
  1605. /******************************************/
  1606. rtn_code = free_ASN1_SEQUENCE(p_head);
  1607.     if(rtn_code != FUNC_OK){
  1608. return FUNC_ERR;
  1609. }
  1610. return FUNC_OK;
  1611. }
  1612.   
  1613. CRET Crypt_GenRSAKeyPair(
  1614.         char          *keyName,
  1615.         char          *password,
  1616. unsigned char *DerPubkey,
  1617. int   *DerPubkeyLen,
  1618. unsigned char *DerPrikey,
  1619. int   *DerPrikeyLen)
  1620. {
  1621.     int rtn_code;
  1622.     int keyNumber;
  1623.     RSA_PUBLIC_KEY  cardRsaPublicKey;
  1624.     RSA_PRIVATE_KEY cardRsaPrivateKey;
  1625.  
  1626.     keyNumber = 0;
  1627. if(strcmp(keyName,"keypair.db1") == 0){
  1628.         keyNumber = 1;
  1629. }
  1630. if(strcmp(keyName,"keypair.db2") == 0){
  1631.         keyNumber = 2;
  1632. }
  1633. if(strcmp(keyName,"keypair.db3") == 0){
  1634.         keyNumber = 3;
  1635. }
  1636. if(strcmp(keyName,"keypair.db4") == 0){
  1637.         keyNumber = 4;
  1638. }
  1639. if(strcmp(keyName,"keypair.db5") == 0){
  1640.         keyNumber = 5;
  1641. }
  1642.      
  1643. //printf("key filename %snn", keyname);
  1644.     rtn_code = CSP_GenRSAKeyPair(
  1645.                    (USHORT)keyNumber,
  1646.                    &cardRsaPublicKey,
  1647.                    &cardRsaPrivateKey);
  1648.     if(rtn_code != 0){
  1649.         return FUNC_ERR;
  1650.     }
  1651.     rtn_code = c2d_RSA_PUBLIC_KEY(&cardRsaPublicKey,
  1652.                        DerPubkey,
  1653.            DerPubkeyLen);
  1654.     if(rtn_code != FUNC_OK){
  1655.         return FUNC_ERR;
  1656.     }
  1657. rtn_code = c2d_RSA_PRIVATE_KEY(
  1658.         &cardRsaPrivateKey,
  1659.         DerPrikey,
  1660.         DerPrikeyLen);
  1661.     if(rtn_code != FUNC_OK){
  1662.         return FUNC_ERR;
  1663.     }
  1664.     if(keyNumber == 0){
  1665. rtn_code = Crypt_SavePemKey2File(
  1666.         keyName,
  1667. password,
  1668. /*&cardRsaPublicKey,*/ DerPubkey,
  1669. /*sizeof(RSA_PUBLIC_KEY),*/ *DerPubkeyLen,
  1670. /*&cardRsaPrivateKey,*/ DerPrikey,
  1671. /*sizeof(RSA_PRIVATE_KEY));*/ *DerPrikeyLen);
  1672. if(rtn_code != 0)
  1673. return -1;
  1674. }
  1675. return FUNC_OK;
  1676. }
  1677. int Crypt_SavePemKey2File(
  1678.         char          *keyName,
  1679.         char          *password,
  1680. unsigned char *DerPubkey,
  1681. int   DerPubkeyLen,
  1682. unsigned char *DerPrikey,
  1683. int   DerPrikeyLen)
  1684. {
  1685. FILE    *f;
  1686. unsigned char pem_key[2048], hashPasswd[20], Enc_DerPubkey[2048];
  1687. int rtn_code, pem_keySize, hashPasswdSize, Enc_DerPubkeySize;
  1688.     f = fopen(keyName,"wb");
  1689.     if(f == NULL){
  1690.     return FUNC_ERR;
  1691.         }
  1692. rtn_code = Crypt_Gen_Hash(
  1693. UID_md5,
  1694. password,
  1695. strlen(password),
  1696. hashPasswd,
  1697. &hashPasswdSize);
  1698. if(rtn_code != 0)
  1699. return -1;
  1700. /*
  1701. rtn_code = iCrypt_Enc_Data_S(
  1702. DerPubkey,
  1703. DerPubkeyLen,
  1704. Enc_DerPubkey,
  1705. &Enc_DerPubkeySize,
  1706. hashPasswd);
  1707. if(rtn_code != 0)
  1708. return -1;
  1709. */     
  1710. rtn_code = DER2PEM_data_format(
  1711.                    DerPubkey,//Enc_DerPubkey, 
  1712.                    DerPubkeyLen,//Enc_DerPubkeySize,
  1713.                    pem_key,
  1714.                    &pem_keySize,
  1715.                    PublicKey_Begin_String, 
  1716.                    PublicKey_End_String);
  1717.     if(rtn_code != FUNC_OK){
  1718.         printf("DER2PEM_data_format error...n");
  1719.         return -1;
  1720.     }
  1721. fwrite(pem_key,1,pem_keySize,f);
  1722. rtn_code = iCrypt_Enc_Data_S(
  1723. DerPrikey,
  1724. DerPrikeyLen,
  1725. Enc_DerPubkey,
  1726. &Enc_DerPubkeySize,
  1727. hashPasswd);
  1728. if(rtn_code != 0)
  1729. return -1;
  1730. rtn_code = DER2PEM_data_format(
  1731.                    Enc_DerPubkey, 
  1732.                    Enc_DerPubkeySize,
  1733.                    pem_key,
  1734.                    &pem_keySize,
  1735.                    PrivateKey_Begin_String, 
  1736.                    PrivateKey_End_String);
  1737.     if(rtn_code != FUNC_OK){
  1738.         printf("DER2PEM_data_format error...n");
  1739.         return -1;
  1740.     }
  1741. fwrite(pem_key,1,pem_keySize,f);
  1742. fclose(f);
  1743. return 0;
  1744. }
  1745. CRET Crypt_ChgKeyPasswd(
  1746.         char          *keyName,
  1747. char          *OldPasswd,
  1748. char          *NewPasswd)
  1749. {
  1750. unsigned char DerPubKey[1024], DerPrikey[1024];
  1751. int rtn_code, DerPubKeyLen, DerPrikeyLen, keyNumber;
  1752. //    RSA_PUBLIC_KEY  cardRsaPublicKey;
  1753. //    RSA_PRIVATE_KEY cardRsaPrivateKey;
  1754.     keyNumber = 0;
  1755. if(strcmp(keyName,"keypair.db1") == 0){
  1756.         keyNumber = 1;
  1757. }
  1758. if(strcmp(keyName,"keypair.db2") == 0){
  1759.         keyNumber = 2;
  1760. }
  1761. if(strcmp(keyName,"keypair.db3") == 0){
  1762.         keyNumber = 3;
  1763. }
  1764. if(strcmp(keyName,"keypair.db4") == 0){
  1765.         keyNumber = 4;
  1766. }
  1767. if(strcmp(keyName,"keypair.db5") == 0){
  1768.         keyNumber = 5;
  1769. }
  1770.      
  1771.     if(keyNumber == 0){
  1772. rtn_code =  RestoreKeyFromFile(
  1773. keyName,
  1774. OldPasswd,
  1775. DerPubKey,//&cardRsaPublicKey,
  1776. &DerPubKeyLen,
  1777. DerPrikey,//&cardRsaPrivateKey,
  1778. &DerPrikeyLen);
  1779. if(rtn_code != 0)
  1780. return -1;
  1781. rtn_code = Crypt_SavePemKey2File(
  1782.         keyName,
  1783. NewPasswd,
  1784. DerPubKey,//&cardRsaPublicKey,
  1785. DerPubKeyLen,//sizeof(RSA_PUBLIC_KEY),
  1786. DerPrikey,//&cardRsaPrivateKey,
  1787. DerPrikeyLen);//sizeof(RSA_PRIVATE_KEY));
  1788. if(rtn_code != 0)
  1789. return -1;
  1790. }
  1791. return 0;
  1792. }
  1793. CRET Crypt_Decode_Pubkey_Info(
  1794. unsigned char *DerPublicKeyInfo,
  1795. int DerPublicKeyInfoSize,
  1796. int *bits,
  1797. unsigned char *modulus,
  1798. unsigned char   *publicExponent)
  1799. {
  1800. //    unsigned char subjectPublicKeyInfo[512];
  1801. //    int  subjectPublicKeyInfoSize;
  1802.     RSA_PUBLIC_KEY  rsaPublicKey;
  1803. int rtn_code;
  1804. /*
  1805. typedef struct rsa_pub_key{
  1806.     uint16       bits;    // length in bits of modulus 
  1807. UCHAR     modulus[MAX_RSA_MODULUS_LEN]; 
  1808.                    // modulus  ----- n 
  1809. UCHAR     publicExponent[MAX_RSA_MODULUS_LEN];  
  1810.                    // public exponent  -----  e  
  1811. } RSA_PUBLIC_KEY;
  1812. */
  1813.     
  1814. rtn_code = d2c_RSA_PUBLIC_KEY(
  1815.                    DerPublicKeyInfo,
  1816.                    DerPublicKeyInfoSize,
  1817.                    &rsaPublicKey);
  1818.     if(rtn_code != FUNC_OK){
  1819.         return FUNC_ERR;
  1820.     }
  1821. *bits = rsaPublicKey.bits;
  1822. memcpy(modulus, rsaPublicKey.modulus, sizeof(rsaPublicKey.modulus));
  1823. memcpy(publicExponent, rsaPublicKey.publicExponent, sizeof(rsaPublicKey.publicExponent));
  1824. return 0;
  1825. }
  1826. CRET Crypt_Decode_Prikey_Info(
  1827. unsigned char *DerPrivateKeyInfo,
  1828. int DerPrivateKeyInfoSize,
  1829. int *bits,
  1830. unsigned char *modulus,
  1831. unsigned char   *publicExponent,
  1832. unsigned char   *privateExponent,
  1833. unsigned char   *prime,
  1834. unsigned char   *exponent,
  1835. unsigned char   *coefficient)
  1836. {
  1837. // unsigned char DerPrikey[1024];
  1838. // int DerPrikeyLen;
  1839.     RSA_PRIVATE_KEY  cardRsaPrivateKey;
  1840. int rtn_code;
  1841. rtn_code = d2c_RSA_PRIVATE_KEY(
  1842. DerPrivateKeyInfo,
  1843. DerPrivateKeyInfoSize,
  1844. &cardRsaPrivateKey);
  1845. if(rtn_code != 0)
  1846. return -1;
  1847. *bits = cardRsaPrivateKey.bits;
  1848. memcpy(modulus, cardRsaPrivateKey.modulus, sizeof(cardRsaPrivateKey.modulus));
  1849. memcpy(publicExponent, cardRsaPrivateKey.publicExponent, sizeof(cardRsaPrivateKey.publicExponent));
  1850. memcpy(privateExponent, cardRsaPrivateKey.privateExponent, sizeof(cardRsaPrivateKey.privateExponent));
  1851. memcpy(prime, cardRsaPrivateKey.prime[2], sizeof(cardRsaPrivateKey.prime[2]));
  1852. memcpy(exponent, cardRsaPrivateKey.exponent[2], sizeof(cardRsaPrivateKey.exponent[2]));
  1853. memcpy(coefficient, cardRsaPrivateKey.coefficient, sizeof(cardRsaPrivateKey.coefficient));
  1854. return 0;
  1855. }
  1856. CRET Crypt_RestorePublicKey(
  1857.         char          *keyName,
  1858. char          *password,
  1859. unsigned char *DerPubkey,
  1860. int   *DerPubkeyLen)
  1861. {
  1862. //int rtn_code;
  1863. unsigned char DerPriKey[1024];
  1864. int DerPriKeyLen;
  1865. return RestoreKeyFromFile(
  1866. keyName,
  1867. password,
  1868. DerPubkey,
  1869. DerPubkeyLen,
  1870. DerPriKey,
  1871. &DerPriKeyLen);
  1872. }
  1873. CRET Crypt_RestorePriKey(
  1874.         char          *keyName,
  1875. char          *password,
  1876. unsigned char *DerPrikey,
  1877. int   *DerPrikeyLen)
  1878. {
  1879. //int rtn_code;
  1880. unsigned char DerPubKey[1024];
  1881. int DerPubKeyLen;
  1882. return RestoreKeyFromFile(
  1883. keyName,
  1884. password,
  1885. DerPubKey,
  1886. &DerPubKeyLen,
  1887. DerPrikey,
  1888. DerPrikeyLen);
  1889. }
  1890. int RestoreKeyFromFile(
  1891.         char          *keyName,
  1892.         char          *password,
  1893. unsigned char *DerPubkey,
  1894. int   *DerPubkeyLen,
  1895. unsigned char *DerPrikey,
  1896. int   *DerPrikeyLen)
  1897. {
  1898. FILE    *f;
  1899. unsigned char pem_key[2048], hashPasswd[20], Dec_Derkey[1024], buffer[128];
  1900. int rtn_code,  hashPasswdSize, Dec_DerkeySize, ptr_offset;
  1901.     ptr_offset=0;
  1902. f = fopen(keyName,"r");
  1903.     if(f == NULL){
  1904. printf("open failed! keyname=%sn", keyName);
  1905.     return FUNC_ERR;
  1906.         }
  1907. do{
  1908. fgets(buffer, sizeof(buffer), f);
  1909. //printf("len=%d, buffer:%sn", strlen(buffer), buffer);
  1910.                 del_rn(buffer);
  1911. //printf("after del_rn, len=%d, buffer:%sn", strlen(buffer), buffer);
  1912. memcpy(pem_key+ptr_offset, buffer,strlen(buffer));
  1913. ptr_offset += strlen(buffer);
  1914. }
  1915. while(strcmp(buffer,PublicKey_End_String2));
  1916.     rtn_code = PEM2DER_data_format(
  1917.                        pem_key,
  1918.                        strlen(pem_key),
  1919.                        DerPubkey,//Dec_Derkey, 
  1920.                        DerPubkeyLen);//&Dec_DerkeySize);
  1921.     if(rtn_code != FUNC_OK){
  1922.         printf("PEM2DER_data_format error...n");
  1923. fclose(f);
  1924.         return -1;
  1925.     }
  1926. rtn_code = Crypt_Gen_Hash(
  1927. UID_md5,
  1928. password,
  1929. strlen(password),
  1930. hashPasswd,
  1931. &hashPasswdSize);
  1932. if(rtn_code != 0){
  1933. fclose(f);
  1934. return -1;
  1935. }
  1936. /*
  1937. rtn_code = iCrypt_Dec_Data_S(
  1938. Dec_Derkey,
  1939. Dec_DerkeySize,
  1940. DerPubkey,
  1941. DerPubkeyLen,
  1942. hashPasswd);
  1943. if(rtn_code != 0){
  1944. fclose(f);
  1945. return -1;
  1946. }
  1947. */
  1948. ptr_offset = 0;
  1949. do{
  1950. fgets(buffer, sizeof(buffer), f);
  1951. //printf("len=%d, buffer=%sn", strlen(buffer), buffer);
  1952.                 del_rn(buffer);
  1953. //printf("after del_rn:len=%d, buffer=%sn", strlen(buffer), buffer);
  1954. memcpy(pem_key+ptr_offset, buffer,strlen(buffer));
  1955. ptr_offset += strlen(buffer);
  1956. }
  1957. while(strcmp(buffer,PrivateKey_End_String2));
  1958. fclose(f);
  1959.     rtn_code = PEM2DER_data_format(
  1960.                        pem_key,
  1961.                        strlen(pem_key),
  1962.                        Dec_Derkey,
  1963.             &Dec_DerkeySize);
  1964.     if(rtn_code != FUNC_OK){
  1965.         printf("PEM2DER_data_format error...n");
  1966.         return -1;
  1967.     }
  1968. rtn_code = iCrypt_Dec_Data_S(
  1969. Dec_Derkey,
  1970. Dec_DerkeySize,
  1971. DerPrikey,
  1972. DerPrikeyLen,
  1973. hashPasswd);
  1974. if(rtn_code != 0)
  1975. return -1;
  1976. return 0;
  1977. }
  1978. CRET Crypt_GetRSAPublicKey(
  1979.         char           *keyName,
  1980. char *passwd,
  1981.         unsigned char  *rsaPublicKey,
  1982.         int            *rsaPublicKeySize)
  1983. {
  1984.     int rtn_code;
  1985.     int keyNumber;
  1986. //    RSA_PUBLIC_KEY  cardRsaPublicKey;
  1987.     keyNumber = 0;
  1988. if(strcmp(keyName,"keypair.db1") == 0){
  1989.         keyNumber = 1;
  1990. }
  1991. if(strcmp(keyName,"keypair.db2") == 0){
  1992.         keyNumber = 2;
  1993. }
  1994. if(strcmp(keyName,"keypair.db3") == 0){
  1995.         keyNumber = 3;
  1996. }
  1997. if(strcmp(keyName,"keypair.db4") == 0){
  1998.         keyNumber = 4;
  1999. }
  2000. if(strcmp(keyName,"keypair.db5") == 0){
  2001.         keyNumber = 5;
  2002. }
  2003.     if(keyNumber == 0){
  2004. rtn_code = Crypt_RestorePublicKey(
  2005. keyName,
  2006. passwd,
  2007. rsaPublicKey,//&cardRsaPublicKey,
  2008. rsaPublicKeySize);
  2009.       if(rtn_code != 0)
  2010.             return FUNC_ERR;
  2011.         
  2012.     }
  2013. /*
  2014.     d2c_RSA_PUBLIC_KEY(rsaPublicKey,*rsaPublicKeySize,
  2015.                        &cardRsaPublicKey);
  2016.     c2d_RSA_PUBLIC_KEY(&cardRsaPublicKey,
  2017.                        rsaPublicKey,
  2018.            rsaPublicKeySize);
  2019. */
  2020.     return FUNC_OK;
  2021. }
  2022. CRET CA_Crypt_Gen_Signature( 
  2023.         int    uid_algorithm,
  2024. char   *keyName,
  2025. char   *password,
  2026.         unsigned char  *msg,
  2027.         int            msgSize,
  2028.         unsigned char  *sig,
  2029. int            *sigSize)
  2030. {
  2031. return     Crypt_Gen_Signature( 
  2032.     uid_algorithm,
  2033.     keyName,
  2034. password,
  2035. msg,
  2036.                 msgSize,
  2037. sig,
  2038. sigSize);
  2039. }
  2040. int Crypt_Gen_Signature( 
  2041.         int    uid_algorithm,
  2042. char   *keyName,
  2043. char   *password,
  2044.         unsigned char  *msg,
  2045.         int            msgSize,
  2046.         unsigned char  *sig,
  2047. int            *sigSize)
  2048. {
  2049.     int rtn_code;
  2050.     int keyNumber;
  2051. uint32 hashSize;
  2052.     UCHAR hash[64],output[128]; 
  2053.     ULONG outputLen;
  2054. unsigned char DerPrikey[1024];
  2055. int DerPrikeyLen;
  2056. //    RSA_PUBLIC_KEY  cardRsaPublicKey;
  2057.     RSA_PRIVATE_KEY  cardRsaPrivateKey;
  2058.     keyNumber = 0;
  2059. if(strcmp(keyName,"keypair.db1") == 0){
  2060.         keyNumber = 1;
  2061. }
  2062. if(strcmp(keyName,"keypair.db2") == 0){
  2063.         keyNumber = 2;
  2064. }
  2065. if(strcmp(keyName,"keypair.db3") == 0){
  2066.         keyNumber = 3;
  2067. }
  2068. if(strcmp(keyName,"keypair.db4") == 0){
  2069.         keyNumber = 4;
  2070. }
  2071. if(strcmp(keyName,"keypair.db5") == 0){
  2072.         keyNumber = 5;
  2073. }
  2074.     if(keyNumber == 0){
  2075. rtn_code = Crypt_RestorePriKey(
  2076. keyName,
  2077. password,
  2078. DerPrikey,//&cardRsaPrivateKey,
  2079. &DerPrikeyLen);
  2080. if(rtn_code != 0)
  2081. return -1;
  2082. //*
  2083. rtn_code = d2c_RSA_PRIVATE_KEY(
  2084. DerPrikey,
  2085. DerPrikeyLen,
  2086. &cardRsaPrivateKey);
  2087. if(rtn_code != 0)
  2088. return -1;
  2089. //*/
  2090.   }
  2091. /////////////  it is tmp codes, maybe modify ....... ///////////////////////////
  2092. // cardRsaPrivateKey.bits = 1024;
  2093. ////////////////////////////////////////////////////////////////////////////////
  2094. rtn_code = Crypt_Gen_Encoded_Hash(
  2095.            uid_algorithm,
  2096.                    msg,
  2097.                    msgSize,
  2098.    hash,
  2099.    &hashSize);
  2100.     if(rtn_code != FUNC_OK){
  2101.         return FUNC_ERR;
  2102.     }
  2103.     rtn_code = CSP_RSAPrivateEncrypt(
  2104.                (USHORT)keyNumber,
  2105.                    output, 
  2106.                    &outputLen,
  2107.                    hash,
  2108.                    (ULONG)hashSize, 
  2109.                    &cardRsaPrivateKey);
  2110.     if(rtn_code != RTN_OK){
  2111.         return FUNC_ERR;
  2112.     }
  2113.  
  2114.     memcpy(sig,output,outputLen);
  2115. *sigSize = (uint32)outputLen;
  2116.     return FUNC_OK;
  2117. }
  2118. CRET Crypt_Ver_Signature(
  2119.         int            uid_algorithm,
  2120.         unsigned char  *rsaPublicKey,
  2121.         int            rsaPublicKeySize,
  2122.         unsigned char  *msg,
  2123.         int            msgSize,
  2124.         unsigned char  *sig,
  2125. int            sigSize)
  2126. {
  2127.     int rtn_code;
  2128. uint32 hashSize;
  2129.     UCHAR hash[64],output[128]; 
  2130.     ULONG  outputLen;
  2131.     USHORT keyNumber;
  2132.     RSA_PUBLIC_KEY cardRsaPublicKey;
  2133.     d2c_RSA_PUBLIC_KEY(rsaPublicKey,rsaPublicKeySize,
  2134.                        &cardRsaPublicKey);
  2135. rtn_code = Crypt_Gen_Encoded_Hash(
  2136.            uid_algorithm,
  2137.                    msg,
  2138.                    msgSize,
  2139.    hash,
  2140.    &hashSize);
  2141.     if(rtn_code != FUNC_OK){
  2142.         return FUNC_ERR;
  2143.     }
  2144.     keyNumber = 0;
  2145.     rtn_code = CSP_RSAPublicDecrypt(
  2146.            (USHORT)keyNumber,
  2147.                     output,
  2148.                     &outputLen,
  2149.                     sig,
  2150.                     (ULONG)sigSize,
  2151.                     &cardRsaPublicKey); 
  2152.      if(rtn_code != RTN_OK){
  2153.         return FUNC_ERR;
  2154.     }
  2155.     if(memcmp(hash,output,hashSize) != 0){
  2156.         return FUNC_ERR;
  2157.     }
  2158.     return FUNC_OK;
  2159. }
  2160. CRET 
  2161. Crypt_Get_PubKey_FromCert(
  2162.     unsigned char   *der_cert,
  2163.     int             der_certSize,
  2164.     unsigned char   *subjectPublicKeyInfo,
  2165.     int             *subjectPublicKeyInfoSize)
  2166. {                          
  2167. /*
  2168.     int version,serialNumberSize,signatureAlgorithm,parametersSize;
  2169.     unsigned char  serialNumber[256],parameters[64];
  2170.     char issuer_countryName[64],issuer_organizationName[64];
  2171.     char issuer_organizationalUnitname1[64],issuer_organizationalUnitname2[64];
  2172.     char issuer_organizationalUnitname3[64],issuer_stateOrProvinceName[64];
  2173.     char issuer_commonName[64],issuer_localityName[64];
  2174.     char issuer_title[64],issuer_surname[64];
  2175.     char issuer_givenName[64],issuer_initials[64];
  2176.     char issuer_email[64],issuer_postalAddress[64];
  2177.     char issuer_postalCode[64],issuer_postalOfficeBox[64];
  2178.     char issuer_telephoneNumber[64],issuer_telexNumber[64];
  2179.     char notBefore[64],notAfter[64];
  2180.     char subject_countryName[64],subject_organizationName[64];
  2181.     char subject_organizationalUnitname1[64],subject_organizationalUnitname2[64];
  2182.     char subject_organizationalUnitname3[64],subject_stateOrProvinceName[64];
  2183.     char subject_commonName[64],subject_localityName[64];
  2184.     char subject_title[64],subject_surname[64];
  2185.     char subject_givenName[64],subject_initials[64];
  2186.     char subject_email[64],subject_postalAddress[64];
  2187.     char subject_postalCode[64],subject_postalOfficeBox[64];
  2188.     char subject_telephoneNumber[64],subject_telexNumber[64];
  2189.  
  2190.     unsigned char issuerUniqueID[64];
  2191.     unsigned char subjectUniqueID[64];
  2192.     int  issuerUniqueIDSize,subjectUniqueIDSize;
  2193.     unsigned int   extensionsFlag,criticalFlag;
  2194.  
  2195.     char authKeyId_countryName[64],authKeyId_organizationName[64];
  2196.     char authKeyId_organizationalUnitname1[64],authKeyId_organizationalUnitname2[64];
  2197.     char authKeyId_organizationalUnitname3[64],authKeyId_stateOrProvinceName[64];
  2198.     char authKeyId_commonName[64],authKeyId_localityName[64],authKeyId_title[64];
  2199.     char authKeyId_surname[64],authKeyId_givenName[64];
  2200.     char authKeyId_initials[64],authKeyId_email[64];
  2201.     char authKeyId_postalAddress[64],authKeyId_postalCode[64];
  2202.     char authKeyId_postalOfficeBox[64],authKeyId_telephoneNumber[64];
  2203.     char authKeyId_telexNumber[64];
  2204.     unsigned char authKeyId_serialNumber[64];
  2205.     int authKeyId_serialNumberSize;
  2206.     unsigned char policyId1[64],policyId2[64],policyId3[64],policyId4[64];
  2207.     int policyIdSize1,policyIdSize2,policyIdSize3,policyIdSize4;
  2208.     char policyUrl1[64],policyUrl2[64],policyUrl3[64],policyUrl4[64];
  2209.     char PrivateKeyUsagePeriod_notBefore[64];
  2210.     char PrivateKeyUsagePeriod_notAfter[64];
  2211.     int  BasicConstraints_cA,BasicConstraints_pathLenConstraint;
  2212.    
  2213.     unsigned int   KeyUsage,neCertType,setCertType;
  2214.     int  self_certClass;
  2215.     char revocationURL[64];
  2216.     unsigned char  hashedRootKey[64];
  2217.     int            hashedRootKeySize;
  2218. */
  2219.     int version,serialNumberSize,signatureAlgorithm,parametersSize;
  2220.     unsigned char  serialNumber[256],parameters[64];
  2221.     char issuer_countryName[64],issuer_organizationName[64];
  2222.     char issuer_organizationalUnitname1[64];
  2223.     char issuer_stateOrProvinceName[64];
  2224.     char issuer_commonName[64],issuer_localityName[64];
  2225.     char issuer_title[64],issuer_surname[64];
  2226.     char issuer_givenName[64],issuer_initials[64];
  2227.     char issuer_email[64],issuer_postalAddress[64];
  2228.     char issuer_postalCode[64],issuer_postalOfficeBox[64];
  2229.     char issuer_telephoneNumber[64],issuer_telexNumber[64];
  2230.     char notBefore[64],notAfter[64];
  2231.     char subject_countryName[64],subject_organizationName[64];
  2232.     char subject_organizationalUnitname1[64];
  2233.     char subject_stateOrProvinceName[64];
  2234.     char subject_commonName[64],subject_localityName[64];
  2235.     char subject_title[64],subject_surname[64];
  2236.     char subject_givenName[64],subject_initials[64];
  2237.     char subject_email[64],subject_postalAddress[64];
  2238.     char subject_postalCode[64],subject_postalOfficeBox[64];
  2239.     char subject_telephoneNumber[64],subject_telexNumber[64];
  2240.  
  2241.     unsigned char issuerUniqueID[64];
  2242.     unsigned char subjectUniqueID[64];
  2243.     int  issuerUniqueIDSize,subjectUniqueIDSize;
  2244.     unsigned int   extensionsFlag,criticalFlag;
  2245.  
  2246.     char authKeyId_countryName[64],authKeyId_organizationName[64];
  2247.     char authKeyId_organizationalUnitname1[64];
  2248.     char authKeyId_stateOrProvinceName[64];
  2249.     char authKeyId_commonName[64],authKeyId_localityName[64],authKeyId_title[64];
  2250.     char authKeyId_surname[64],authKeyId_givenName[64];
  2251.     char authKeyId_initials[64],authKeyId_email[64];
  2252.     char authKeyId_postalAddress[64],authKeyId_postalCode[64];
  2253.     char authKeyId_postalOfficeBox[64],authKeyId_telephoneNumber[64];
  2254.     char authKeyId_telexNumber[64];
  2255.     unsigned char authKeyId_serialNumber[64];
  2256.     int authKeyId_serialNumberSize;
  2257.     unsigned char policyId1[64],policyId2[64],policyId3[64],policyId4[64];
  2258.     int policyIdSize1,policyIdSize2,policyIdSize3,policyIdSize4;
  2259.     char policyUrl1[64],policyUrl2[64],policyUrl3[64],policyUrl4[64];
  2260.     char PrivateKeyUsagePeriod_notBefore[64];
  2261.     char PrivateKeyUsagePeriod_notAfter[64];
  2262.     int  BasicConstraints_cA,BasicConstraints_pathLenConstraint;
  2263.    
  2264.     unsigned int   KeyUsage,neCertType,setCertType;
  2265.     int  self_certClass;
  2266.     char revocationURL[64];
  2267.     unsigned char  hashedRootKey[64];
  2268.     int            hashedRootKeySize;
  2269.     int  rtn_code;
  2270.  
  2271.     rtn_code = Crypt_Decode_Cert(
  2272.                    der_cert,
  2273.                    der_certSize,
  2274.                    &version,
  2275.                    serialNumber,
  2276.                    &serialNumberSize,
  2277.                    &signatureAlgorithm,
  2278.                    parameters,
  2279.                    &parametersSize,
  2280.                    issuer_countryName,
  2281.                    issuer_organizationName,
  2282.                    issuer_organizationalUnitname1,
  2283. //                   issuer_organizationalUnitname2,
  2284. //                   issuer_organizationalUnitname3,
  2285.                    issuer_stateOrProvinceName,
  2286.                    issuer_commonName,
  2287.                    issuer_localityName,
  2288.                    issuer_title,
  2289.                    issuer_surname,
  2290.                    issuer_givenName,
  2291.                    issuer_initials,
  2292.                    issuer_email,
  2293.                    issuer_postalAddress,
  2294.                    issuer_postalCode,
  2295.                    issuer_postalOfficeBox,
  2296.                    issuer_telephoneNumber,
  2297.                    issuer_telexNumber,
  2298.                    notBefore,   
  2299.                    notAfter,     
  2300.                    subject_countryName,
  2301.                    subject_organizationName,
  2302.                    subject_organizationalUnitname1,
  2303. //                   subject_organizationalUnitname2,
  2304. //                   subject_organizationalUnitname3,
  2305.                    subject_stateOrProvinceName,
  2306.                    subject_commonName,
  2307.                    subject_localityName,
  2308.                    subject_title,
  2309.                    subject_surname,
  2310.                    subject_givenName,
  2311.                    subject_initials,
  2312.                    subject_email,
  2313.                    subject_postalAddress,
  2314.                    subject_postalCode,
  2315.                    subject_postalOfficeBox,
  2316.                    subject_telephoneNumber,
  2317.                    subject_telexNumber,
  2318.                    subjectPublicKeyInfo,
  2319.                    subjectPublicKeyInfoSize,
  2320.                    issuerUniqueID, 
  2321.                    &issuerUniqueIDSize,
  2322.                    subjectUniqueID,
  2323.                    &subjectUniqueIDSize,
  2324.                    &extensionsFlag,
  2325.                    &criticalFlag,
  2326.                    authKeyId_countryName,
  2327.                    authKeyId_organizationName,
  2328.                    authKeyId_organizationalUnitname1,
  2329. //                   authKeyId_organizationalUnitname2,
  2330. //                   authKeyId_organizationalUnitname3,
  2331.                    authKeyId_stateOrProvinceName,
  2332.                    authKeyId_commonName,
  2333.                    authKeyId_localityName,
  2334.                    authKeyId_title,
  2335.                    authKeyId_surname,
  2336.                    authKeyId_givenName,
  2337.                    authKeyId_initials,
  2338.                    authKeyId_email,
  2339.                    authKeyId_postalAddress,
  2340.                    authKeyId_postalCode,
  2341.                    authKeyId_postalOfficeBox,
  2342.                    authKeyId_telephoneNumber,
  2343.                    authKeyId_telexNumber,
  2344.                    authKeyId_serialNumber,
  2345.                    &authKeyId_serialNumberSize,
  2346.                    policyId1,
  2347.                    &policyIdSize1,
  2348.                    policyUrl1,
  2349.                    policyId2,
  2350.                    &policyIdSize2,
  2351.                    policyUrl2,
  2352.                    policyId3,
  2353.                    &policyIdSize3,
  2354.                    policyUrl3,
  2355.                    policyId4,
  2356.                    &policyIdSize4,
  2357.                    policyUrl4,
  2358.                    PrivateKeyUsagePeriod_notBefore,   
  2359.                    PrivateKeyUsagePeriod_notAfter,     
  2360.                    &BasicConstraints_cA,
  2361.                    &BasicConstraints_pathLenConstraint,
  2362.                    &KeyUsage,
  2363.                    &neCertType,
  2364.                    &setCertType,
  2365.                    hashedRootKey,
  2366.                    &hashedRootKeySize,
  2367.                    &self_certClass,
  2368.                    revocationURL);
  2369.     if(rtn_code != FUNC_OK){
  2370.         return FUNC_ERR;
  2371.     }
  2372.     return FUNC_OK; 
  2373. }
  2374.  
  2375. CRET 
  2376. Crypt_Ver_Signature_ByCert(
  2377.     int             uid_algorithm,
  2378.     unsigned char   *der_cert,
  2379.     int             der_certSize,
  2380.     unsigned char   *msg,
  2381.     int             msgSize,
  2382.     unsigned char   *sig,
  2383.     int             sigSize)
  2384. {
  2385.     unsigned char subjectPublicKeyInfo[512];
  2386.     int  subjectPublicKeyInfoSize;
  2387.     int  rtn_code;
  2388.     rtn_code = Crypt_Get_PubKey_FromCert(
  2389.                     der_cert,
  2390.                     der_certSize,
  2391.                     subjectPublicKeyInfo,
  2392.                     &subjectPublicKeyInfoSize);
  2393.     if(rtn_code != FUNC_OK){
  2394.         return FUNC_ERR;
  2395.     }
  2396.     rtn_code = Crypt_Ver_Signature(
  2397.                    uid_algorithm,
  2398.                    subjectPublicKeyInfo,
  2399.                    subjectPublicKeyInfoSize,
  2400.                    msg,
  2401.                    msgSize,
  2402.                    sig,
  2403.            sigSize);
  2404.     if(rtn_code != FUNC_OK){
  2405.         return FUNC_ERR;
  2406.     }
  2407.     return FUNC_OK;
  2408. }
  2409. int iCrypt_Enc_Data_S(
  2410. unsigned char  *indata,
  2411.     int            indataSize,
  2412.     unsigned char  *outdata,
  2413.     int            *outdataSize,
  2414. unsigned char  *sdbiKey)
  2415. {
  2416.     int rtn_code = CSP_SDBIEncrypt(
  2417.                    outdata,
  2418.                    outdataSize,
  2419.                    indata,
  2420.                    (ULONG)indataSize,
  2421.                    sdbiKey,
  2422.                    16);
  2423.     if(rtn_code != RTN_OK){
  2424.         return FUNC_ERR;
  2425.     }
  2426. return FUNC_OK;
  2427. }
  2428. CRET Crypt_Enc_Data_S(
  2429. unsigned char  *indata,
  2430.     int            indataSize,
  2431.     unsigned char  *outdata,
  2432.     int            *outdataSize,
  2433. unsigned char  *sdbiKey)
  2434. {
  2435.     int rtn_code = CSP_SDBIEncrypt(
  2436.                    outdata,
  2437.                    outdataSize,
  2438.                    indata,
  2439.                    (ULONG)indataSize,
  2440.                    sdbiKey,
  2441.                    16);
  2442.     if(rtn_code != RTN_OK){
  2443.         return FUNC_ERR;
  2444.     }
  2445. return FUNC_OK;
  2446. }
  2447. CRET
  2448. Crypt_Enc_Data(
  2449.     unsigned char  *indata,
  2450.     int            indataSize,
  2451.     unsigned char  *outdata,
  2452.     int            *outdataSize,
  2453.     unsigned char  *der_cert,
  2454.     int            der_certSize)
  2455. {
  2456.     unsigned char subjectPublicKeyInfo[512];
  2457.     int  subjectPublicKeyInfoSize;
  2458.     RSA_PUBLIC_KEY  rsaPublicKey;
  2459.     UCHAR  SDBIKey[32],seedValue[32];
  2460.     UCHAR  enc_SDBIKey[256];
  2461.     ULONG  SDBIKeyLen,seedLen;
  2462.     int rtn_code;
  2463.     rtn_code = Crypt_Get_PubKey_FromCert(
  2464.                     der_cert,
  2465.                     der_certSize,
  2466.                     subjectPublicKeyInfo,
  2467.                     &subjectPublicKeyInfoSize);
  2468.     if(rtn_code != FUNC_OK){
  2469.         return FUNC_ERR;
  2470.     }
  2471.     seedLen = 16;
  2472.     rtn_code = CSP_GenRandomBytes(
  2473.                    SDBIKey,
  2474.                    16,
  2475.                    seedValue,
  2476.                    seedLen);
  2477.     if(rtn_code != RTN_OK){
  2478.         return FUNC_ERR;
  2479.     }
  2480.                
  2481.     rtn_code = d2c_RSA_PUBLIC_KEY(
  2482.                    subjectPublicKeyInfo,
  2483.                    subjectPublicKeyInfoSize,
  2484.                    &rsaPublicKey);
  2485.     if(rtn_code != FUNC_OK){
  2486.         return FUNC_ERR;
  2487.     }
  2488.     rtn_code = CSP_RSAPublicEncrypt(
  2489.            0, 
  2490.                    enc_SDBIKey,
  2491.                    &SDBIKeyLen,
  2492.                    SDBIKey,
  2493.                    16,
  2494.                    &rsaPublicKey);
  2495.     if(rtn_code != RTN_OK){
  2496.         return FUNC_ERR;
  2497.     }
  2498.     memcpy(outdata,enc_SDBIKey,SDBIKeyLen);
  2499.     rtn_code = CSP_SDBIEncrypt(
  2500.                    &outdata[SDBIKeyLen],
  2501.                    &seedLen,
  2502.                    indata,
  2503.                    (ULONG)indataSize,
  2504.                    SDBIKey,
  2505.                    16);
  2506.     if(rtn_code != RTN_OK){
  2507.         return FUNC_ERR;
  2508.     }
  2509.     *outdataSize = SDBIKeyLen + seedLen;
  2510.    
  2511.     return FUNC_OK;
  2512. }
  2513. int iCrypt_Dec_Data_S(
  2514.       unsigned char  *indata,
  2515.       int           indataSize,
  2516.       unsigned char  *outdata,
  2517.       int           *outdataSize,
  2518.       unsigned char  *sdbiKey)
  2519. {
  2520.     int rtn_code = CSP_SDBIDecrypt(
  2521.                    outdata,
  2522.                    outdataSize,
  2523.                    indata,
  2524.                    indataSize,
  2525.                    sdbiKey,
  2526.                    16);
  2527.     if(rtn_code != RTN_OK){
  2528.         return FUNC_ERR;
  2529.     }
  2530.     return FUNC_OK;
  2531. }
  2532. CRET  Crypt_Dec_Data_S(
  2533.       unsigned char  *indata,
  2534.       int           indataSize,
  2535.       unsigned char  *outdata,
  2536.       int           *outdataSize,
  2537.       unsigned char  *sdbiKey)
  2538. {
  2539.     int rtn_code = CSP_SDBIDecrypt(
  2540.                    outdata,
  2541.                    outdataSize,
  2542.                    indata,
  2543.                    indataSize,
  2544.                    sdbiKey,
  2545.                    16);
  2546.     if(rtn_code != RTN_OK){
  2547.         return FUNC_ERR;
  2548.     }
  2549.     return FUNC_OK;
  2550. }
  2551. CRET  Crypt_Dec_Data(
  2552.       unsigned char  *indata,
  2553.       int           indataSize,
  2554.       unsigned char  *outdata,
  2555.       int           *outdataSize,
  2556.       char          *keyName,
  2557.       char          *password)
  2558. {
  2559.     int rtn_code;
  2560.     int keyNumber;
  2561.     UCHAR SDBIKey[128]; 
  2562.     ULONG outputLen;
  2563. unsigned char DerPrikey[1024];
  2564. int DerPrikeyLen;
  2565. //    RSA_PUBLIC_KEY  cardRsaPublicKey;
  2566.     RSA_PRIVATE_KEY  cardRsaPrivateKey;
  2567.     keyNumber = 0;
  2568. if(strcmp(keyName,"keypair.db1") == 0){
  2569.         keyNumber = 1;
  2570. }
  2571. if(strcmp(keyName,"keypair.db2") == 0){
  2572.         keyNumber = 2;
  2573. }
  2574. if(strcmp(keyName,"keypair.db3") == 0){
  2575.         keyNumber = 3;
  2576. }
  2577. if(strcmp(keyName,"keypair.db4") == 0){
  2578.         keyNumber = 4;
  2579. }
  2580. if(strcmp(keyName,"keypair.db5") == 0){
  2581.         keyNumber = 5;
  2582. }
  2583.     if(keyNumber == 0){
  2584. rtn_code = Crypt_RestorePriKey(
  2585. keyName,
  2586. password,
  2587. DerPrikey,//&cardRsaPrivateKey,
  2588. &DerPrikeyLen);
  2589. if(rtn_code != 0)
  2590. return -1;
  2591. //*
  2592. rtn_code = d2c_RSA_PRIVATE_KEY(
  2593. DerPrikey,
  2594. DerPrikeyLen,
  2595. &cardRsaPrivateKey);
  2596. if(rtn_code != 0)
  2597. return -1;
  2598. //*/  
  2599. }
  2600. ///////////////////////// tmp codes, maybe modify....................
  2601. // cardRsaPrivateKey.bits = 1024;
  2602. /////////////////////////////////////////////////////////////////////
  2603.     rtn_code = CSP_RSAPrivateDecrypt(
  2604.            keyNumber,
  2605.                    SDBIKey,
  2606.                    &outputLen,
  2607.                    indata,
  2608.                    128,
  2609.                    &cardRsaPrivateKey);
  2610.     if(rtn_code != RTN_OK){
  2611.         return FUNC_ERR;
  2612.     }
  2613.     
  2614.     rtn_code = CSP_SDBIDecrypt(
  2615.                    outdata,
  2616.                    &outputLen,
  2617.                    &indata[128],
  2618.                    indataSize-128,
  2619.                    SDBIKey,
  2620.                    16);
  2621.     if(rtn_code != RTN_OK){
  2622.         return FUNC_ERR;
  2623.     }
  2624.     *outdataSize = outputLen;
  2625.     return FUNC_OK;
  2626. }
  2627. CRET Crypt_Rsa_PrivateKeyDec(
  2628. char *keyName,
  2629. char *password,
  2630. unsigned char *cipher,
  2631. int cipherLen,
  2632. unsigned char *msg,
  2633. int *msgLen)
  2634. {
  2635.     int rtn_code;
  2636.     int keyNumber;
  2637.  
  2638. unsigned char DerPrikey[1024];
  2639. int DerPrikeyLen;
  2640. //    RSA_PUBLIC_KEY  cardRsaPublicKey;
  2641.     RSA_PRIVATE_KEY  cardRsaPrivateKey;
  2642. rtn_code = Crypt_RestorePriKey(
  2643. keyName,
  2644. password,
  2645. DerPrikey,//&cardRsaPrivateKey,
  2646. &DerPrikeyLen);
  2647. if(rtn_code != 0)
  2648. return -1;
  2649. rtn_code = d2c_RSA_PRIVATE_KEY(
  2650. DerPrikey,
  2651. DerPrikeyLen,
  2652. &cardRsaPrivateKey);
  2653. if(rtn_code != 0)
  2654. return -1;
  2655.     /*
  2656. rtn_code = CSP_RSAPrivateDecrypt(
  2657.            0,
  2658.                    msg,
  2659.                    &msgLen,
  2660.                    cipher,
  2661.                    cipherLen,
  2662.                    &cardRsaPrivateKey);
  2663. */
  2664. rtn_code = RSAPrivateBlock (msg, msgLen, cipher, cipherLen, &cardRsaPrivateKey);
  2665.     if(rtn_code != 0){
  2666.         return FUNC_ERR;
  2667.     }
  2668.     
  2669.     return FUNC_OK;
  2670. }
  2671. CRET Crypt_Cert_PubKeyEnc (
  2672. char *der_cert,
  2673. int der_certSize,
  2674. unsigned char *msg,
  2675. int msgLen,
  2676. unsigned char *cipher,
  2677. int *cipherLen)
  2678. {
  2679.     unsigned char subjectPublicKeyInfo[512];
  2680.     int  subjectPublicKeyInfoSize;
  2681.     RSA_PUBLIC_KEY  rsaPublicKey;
  2682.     int rtn_code;
  2683.     rtn_code = Crypt_Get_PubKey_FromCert(
  2684.                     der_cert,
  2685.                     der_certSize,
  2686.                     subjectPublicKeyInfo,
  2687.                     &subjectPublicKeyInfoSize);
  2688.     if(rtn_code != FUNC_OK){
  2689.         return FUNC_ERR;
  2690.     }
  2691.     rtn_code = d2c_RSA_PUBLIC_KEY(
  2692.                    subjectPublicKeyInfo,
  2693.                    subjectPublicKeyInfoSize,
  2694.                    &rsaPublicKey);
  2695.     if(rtn_code != FUNC_OK){
  2696.         return FUNC_ERR;
  2697.     }
  2698. /*
  2699.     rtn_code = CSP_RSAPublicEncrypt(
  2700.            0, 
  2701.                    cipher,
  2702.                    &cipherLen,
  2703.                    msg,
  2704.                    msgLen,
  2705.                    &rsaPublicKey);
  2706. */
  2707.  rtn_code =  RSAPublicBlock (cipher, cipherLen, msg, msgLen, &rsaPublicKey);
  2708.     if(rtn_code != 0){
  2709.         return FUNC_ERR;
  2710.     }
  2711. return 0;
  2712. }
  2713. char *del_rn(char *pbuf)
  2714. {
  2715.   char *p;
  2716.   if((p =strchr(pbuf, 'r')) !=NULL) *p =0;
  2717.   if((p =strchr(pbuf, 'n')) !=NULL) *p =0;
  2718.   return pbuf;
  2719. }