gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/txsec/xt_sec_win.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package txsec
     5  
     6  /*
     7  #include <stdlib.h>
     8  */
     9  import "C"
    10  import (
    11  	"encoding/hex"
    12  	"fmt"
    13  	"github.com/pkg/errors"
    14  	"syscall"
    15  	"unsafe"
    16  )
    17  
    18  type XTSecWin struct {
    19  }
    20  
    21  var (
    22  	// go env -w GOARCH=386 CGO_ENABLED=1
    23  	XTSecdll       *syscall.LazyDLL  = syscall.NewLazyDLL("XTSec.dll")
    24  	XtGenkeypair   *syscall.LazyProc = XTSecdll.NewProc("XT_GenKeyPair")
    25  	XtMakep10      *syscall.LazyProc = XTSecdll.NewProc("XT_MakeP10")
    26  	XtSavecert     *syscall.LazyProc = XTSecdll.NewProc("XT_SaveCert")
    27  	XtGetpubkey    *syscall.LazyProc = XTSecdll.NewProc("XT_GetPubKey")
    28  	XtSign         *syscall.LazyProc = XTSecdll.NewProc("XT_Sign")
    29  	XtVerifysign   *syscall.LazyProc = XTSecdll.NewProc("XT_VerifySign")
    30  	XtEncrypt      *syscall.LazyProc = XTSecdll.NewProc("XT_Encrypt")
    31  	XtDecrypt      *syscall.LazyProc = XTSecdll.NewProc("XT_Decrypt")
    32  	XtChangeContId *syscall.LazyProc = XTSecdll.NewProc("XT_ChangeContId")
    33  )
    34  
    35  func (sec *XTSecWin) GenKeyPair(keyId string) ([]byte, error) {
    36  
    37  	userLable := C.CString(keyId)
    38  	defer C.free(unsafe.Pointer(userLable))
    39  
    40  	cPublicKeyTemp := [256]byte{}
    41  	puiPubLen := 0
    42  
    43  	ret, _, _ := XtGenkeypair.Call(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&cPublicKeyTemp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
    44  	fmt.Println("XtGenkeypair ret:", ret)
    45  	//fmt.Println("XtGetpubkey puiPubLen:", puiPubLen)
    46  	// 公钥
    47  	cPublicKey := []byte{}
    48  	for i := 0; i < puiPubLen; i++ {
    49  		cPublicKey = append(cPublicKey, cPublicKeyTemp[i])
    50  	}
    51  	fmt.Println("XtGetpubkey cPublicKey:", hex.EncodeToString(cPublicKey))
    52  	return cPublicKey, nil
    53  	//// 公钥x
    54  	//cPublicKeyX := []byte{}
    55  	//for i := 1; i < 33; i++ {
    56  	//	cPublicKeyX = append(cPublicKeyX, cPublicKey[i])
    57  	//}
    58  	////fmt.Println("XtGetpubkey cPublicKeyX:", hex.EncodeToString(cPublicKeyX))
    59  	//
    60  	//// 公钥y
    61  	//cPublicKeyY := []byte{}
    62  	//for i := 33; i < len(cPublicKey); i++ {
    63  	//	cPublicKeyY = append(cPublicKeyY, cPublicKey[i])
    64  	//}
    65  	////fmt.Println("XtGetpubkey cPublicKeyY:", hex.EncodeToString(cPublicKeyY))
    66  	//
    67  	//publicKey := new(sm2.PublicKey)
    68  	//publicKey.X = new(big.Int).SetBytes(cPublicKeyX)
    69  	//publicKey.Y = new(big.Int).SetBytes(cPublicKeyY)
    70  	//publicKey.Curve = sm2.P256Sm2()
    71  	//return publicKey, nil
    72  }
    73  
    74  func (sec *XTSecWin) MakeP10(keyId string, dn string) ([]byte, error) {
    75  
    76  	userLable := C.CString(keyId)
    77  	defer C.free(unsafe.Pointer(userLable))
    78  
    79  	userDn := C.CString(dn)
    80  	defer C.free(unsafe.Pointer(userDn))
    81  
    82  	pkcs10Temp := [256]byte{}
    83  	puiPubLen := 0
    84  
    85  	//typedef int(WINAPI *PFN_XT_MakeP10)(IN const char *szContId, IN const char *szDn, OUT unsigned char *pucP10, OUT unsigned int *puiP10Len);
    86  	ret, _, _ := XtMakep10.Call(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(userDn)), uintptr(unsafe.Pointer(&pkcs10Temp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
    87  	fmt.Println("XtMakep10 ret : ", ret)
    88  
    89  	// 证书申请文件
    90  	pkcs10 := []byte{}
    91  	for i := 0; i < puiPubLen; i++ {
    92  		pkcs10 = append(pkcs10, pkcs10Temp[i])
    93  	}
    94  	fmt.Println("XtGetpubkey pkcs10:", hex.EncodeToString(pkcs10))
    95  
    96  	return pkcs10, nil
    97  
    98  }
    99  
   100  func (sec *XTSecWin) SaveCert(keyId string, signCert []byte, signCertLen int) error {
   101  
   102  	userLable := C.CString(keyId)
   103  	defer C.free(unsafe.Pointer(userLable))
   104  
   105  	empty := []byte{0}
   106  
   107  	//typedef int(WINAPI *PFN_XT_SaveCert)(IN const char *szContId, IN unsigned char *pucSignCert, IN unsigned int uiSignCertLen, IN unsigned char *pucEncKey, IN unsigned int uiEncKeyLen, IN unsigned char *pucEncCert, IN unsigned int uiEncCertLen);
   108  	ret, _, _ := XtSavecert.Call(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&signCert[0])),
   109  		uintptr(signCertLen), uintptr(unsafe.Pointer(&empty[0])), 0, uintptr(unsafe.Pointer(&empty[0])), 0)
   110  
   111  	if ret != 0 {
   112  		return errors.New("XtSavecert save error")
   113  	}
   114  	fmt.Println("XtSavecert ret : ", ret)
   115  	return nil
   116  }
   117  
   118  func (sec *XTSecWin) GetPubKey(keyId string) ([]byte, error) {
   119  
   120  	userLable := C.CString(keyId)
   121  	defer C.free(unsafe.Pointer(userLable))
   122  
   123  	cPublicKeyTemp := [256]byte{}
   124  	puiPubLen := 0
   125  
   126  	//typedef int(WINAPI *PFN_XT_GetPubKey)(IN const char *szContId, IN int iType, OUT unsigned char *pucPub, OUT unsigned int *puiPubLen);
   127  	ret, _, _ := XtGetpubkey.Call(uintptr(unsafe.Pointer(userLable)), uintptr(0), uintptr(unsafe.Pointer(&cPublicKeyTemp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
   128  	fmt.Println("XtGetpubkey ret:", ret)
   129  	//fmt.Println("XtGetpubkey puiPubLen:", puiPubLen)
   130  	// 公钥
   131  	cPublicKey := []byte{}
   132  	for i := 0; i < puiPubLen; i++ {
   133  		cPublicKey = append(cPublicKey, cPublicKeyTemp[i])
   134  	}
   135  	fmt.Println("XtGetpubkey cPublicKey:", hex.EncodeToString(cPublicKey))
   136  
   137  	return cPublicKey, nil
   138  
   139  	//// 公钥x
   140  	//cPublicKeyX := []byte{}
   141  	//for i := 1; i < 33; i++ {
   142  	//	cPublicKeyX = append(cPublicKeyX, cPublicKey[i])
   143  	//}
   144  	////fmt.Println("XtGetpubkey cPublicKeyX:", hex.EncodeToString(cPublicKeyX))
   145  	//
   146  	//// 公钥y
   147  	//cPublicKeyY := []byte{}
   148  	//for i := 33; i < len(cPublicKey); i++ {
   149  	//	cPublicKeyY = append(cPublicKeyY, cPublicKey[i])
   150  	//}
   151  	////fmt.Println("XtGetpubkey cPublicKeyY:", hex.EncodeToString(cPublicKeyY))
   152  	//
   153  	//publicKey := new(sm2.PublicKey)
   154  	//publicKey.X = new(big.Int).SetBytes(cPublicKeyX)
   155  	//publicKey.Y = new(big.Int).SetBytes(cPublicKeyY)
   156  	//publicKey.Curve = sm2.P256Sm2()
   157  
   158  	//return publicKey, nil
   159  
   160  }
   161  
   162  func (sec *XTSecWin) Sign(keyId string, msg []byte) ([]byte, error) {
   163  
   164  	userLable := C.CString(keyId)
   165  	defer C.free(unsafe.Pointer(userLable))
   166  
   167  	signDataTemp := [256]byte{}
   168  	signDataLen := 0
   169  
   170  	fmt.Println("XtSign keyId:", keyId)
   171  
   172  	//typedef int(WINAPI *PFN_XT_Sign)(IN const char *szContId, IN unsigned char *pucIn, IN unsigned int uiInLen, OUT unsigned char *pucSign, OUT unsigned int *puiSignLen);
   173  	ret, _, _ := XtSign.Call(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&msg[0])), uintptr(len(msg)),
   174  		uintptr(unsafe.Pointer(&signDataTemp[0])), uintptr(unsafe.Pointer(&signDataLen)))
   175  	fmt.Println("XtSign ret:", ret)
   176  	fmt.Println("XtSign signDataLen:", signDataLen)
   177  
   178  	// 签名值
   179  	signData := []byte{}
   180  	for i := 0; i < signDataLen; i++ {
   181  		signData = append(signData, signDataTemp[i])
   182  	}
   183  	fmt.Println("XtSign signData:", hex.EncodeToString(signData))
   184  
   185  	return signData, nil
   186  }
   187  
   188  func (sec *XTSecWin) VerifySign(publicKey []byte, msg []byte, signData []byte) bool {
   189  
   190  	//pucPub := []byte{4}
   191  	//pucPub = append(pucPub, publicKey.X.Bytes()...)
   192  	//pucPub = append(pucPub, publicKey.Y.Bytes()...)
   193  
   194  	//typedef int(WINAPI *PFN_XT_VerifySign)(IN unsigned char *pucPub, IN unsigned int uiPubLen, IN unsigned char *pucIn, IN unsigned int uiInLen, IN unsigned char *pucSign, IN unsigned int uiSignLen);
   195  	ret, _, _ := XtVerifysign.Call(uintptr(unsafe.Pointer(&publicKey[0])), uintptr(len(publicKey)), uintptr(unsafe.Pointer(&msg[0])), uintptr(len(msg)), uintptr(unsafe.Pointer(&signData[0])), uintptr(len(signData)))
   196  	fmt.Println("XtVerifysign ret:", ret)
   197  	return ret == 0
   198  }
   199  
   200  func (sec *XTSecWin) Encrypt(publicKey []byte, msg []byte) ([]byte, error) {
   201  
   202  	//pucPub := []byte{4}
   203  	//pucPub = append(pucPub, publicKey.X.Bytes()...)
   204  	//pucPub = append(pucPub, publicKey.Y.Bytes()...)
   205  
   206  	encDataTemp := [10000]byte{}
   207  	encDataLen := 10000
   208  
   209  	//typedef int(WINAPI *PFN_XT_Encrypt)(IN unsigned char *pucPub, IN unsigned int uiPubLen, IN unsigned char *pucIn, IN unsigned int uiInLen, OUT unsigned char *pucOut, OUT unsigned int *puiOutLen);
   210  	ret, _, _ := XtEncrypt.Call(uintptr(unsafe.Pointer(&publicKey[0])), uintptr(len(publicKey)), uintptr(unsafe.Pointer(&msg[0])), uintptr(len(msg)), uintptr(unsafe.Pointer(&encDataTemp[0])), uintptr(unsafe.Pointer(&encDataLen)))
   211  	fmt.Println("XtEncrypt ret:", ret)
   212  	fmt.Println("XtEncrypt encDataLen:", encDataLen)
   213  
   214  	// 密文数据
   215  	encData := []byte{}
   216  	for i := 0; i < encDataLen; i++ {
   217  		encData = append(encData, encDataTemp[i])
   218  	}
   219  	fmt.Println("XtEncrypt encData:", hex.EncodeToString(encData))
   220  
   221  	return encData, nil
   222  
   223  }
   224  
   225  func (sec *XTSecWin) Decrypt(keyId string, encData []byte) ([]byte, error) {
   226  
   227  	userLable := C.CString(keyId)
   228  	defer C.free(unsafe.Pointer(userLable))
   229  
   230  	msgTemp := [10000]byte{}
   231  	msgLen := 10000
   232  
   233  	//typedef int(WINAPI *PFN_XT_Decrypt)(IN const char *szContId, IN unsigned char *pucIn, IN unsigned int uiInLen, OUT unsigned char *pucOut, OUT unsigned int *puiOutLen);
   234  	ret, _, _ := XtDecrypt.Call(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&encData[0])), uintptr(len(encData)), uintptr(unsafe.Pointer(&msgTemp[0])), uintptr(unsafe.Pointer(&msgLen)))
   235  	fmt.Println("XtDecrypt ret:", ret)
   236  	fmt.Println("XtDecrypt msgLen:", msgLen)
   237  	// 解密后数据
   238  	msg := []byte{}
   239  	for i := 0; i < msgLen; i++ {
   240  		msg = append(msg, msgTemp[i])
   241  	}
   242  	fmt.Println("XtDecrypt msg:", hex.EncodeToString(msg))
   243  	return msg, nil
   244  }
   245  
   246  func (sec *XTSecWin) ChangeContId(keyId string, newKeyId string) error {
   247  
   248  	userLable := C.CString(keyId)
   249  	defer C.free(unsafe.Pointer(userLable))
   250  
   251  	newUserLable := C.CString(newKeyId)
   252  	defer C.free(unsafe.Pointer(newUserLable))
   253  
   254  	//XT_API int XT_CALL XT_ChangeContId(IN const char *szContId, IN const char *szNewContId);
   255  	ret, _, _ := XtChangeContId.Call(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(newUserLable)))
   256  	fmt.Println("XtChangeContId ret:", ret)
   257  
   258  	if ret != 0 {
   259  		return errors.New("change contId error")
   260  	}
   261  	return nil
   262  }