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

     1  //go:build dragonfly || freebsd || linux || netbsd || openbsd
     2  // +build dragonfly freebsd linux netbsd openbsd
     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  	"github.com/rainycape/dl"
    15  	"unsafe"
    16  )
    17  
    18  var (
    19  	lib *dl.DL = getXTSexLib()
    20  )
    21  
    22  func getXTSexLib() *dl.DL {
    23  	lib, err := dl.Open("/opt/XTSec/lib/libXTSec.so", 0)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	defer lib.Close()
    28  	return lib
    29  }
    30  
    31  type XTSecLinux struct {
    32  }
    33  
    34  func (sec *XTSecLinux) GenKeyPair(keyId string) ([]byte, error) {
    35  
    36  	userLable := C.CString(keyId)
    37  	defer C.free(unsafe.Pointer(userLable))
    38  
    39  	cPublicKeyTemp := [256]byte{}
    40  	puiPubLen := 0
    41  
    42  	var genKeyPair func(szContId uintptr, pucPub uintptr, puiPubLen uintptr) uintptr
    43  	lib.Sym("XT_GenKeyPair", &genKeyPair)
    44  	ret := genKeyPair(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&cPublicKeyTemp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
    45  	fmt.Println("XtGenkeypair ret:", ret)
    46  	//fmt.Println("XtGetpubkey puiPubLen:", puiPubLen)
    47  	// 公钥
    48  	cPublicKey := []byte{}
    49  	for i := 0; i < puiPubLen; i++ {
    50  		cPublicKey = append(cPublicKey, cPublicKeyTemp[i])
    51  	}
    52  	fmt.Println("XtGetpubkey cPublicKey:", hex.EncodeToString(cPublicKey))
    53  
    54  	return cPublicKey, nil
    55  }
    56  
    57  func (sec *XTSecLinux) MakeP10(keyId string, dn string) ([]byte, error) {
    58  
    59  	userLable := C.CString(keyId)
    60  	defer C.free(unsafe.Pointer(userLable))
    61  
    62  	userDn := C.CString(dn)
    63  	defer C.free(unsafe.Pointer(userDn))
    64  
    65  	pkcs10Temp := [256]byte{}
    66  	puiPubLen := 0
    67  
    68  	//typedef int(WINAPI *PFN_XT_MakeP10)(IN const char *szContId, IN const char *szDn, OUT unsigned char *pucP10, OUT unsigned int *puiP10Len);
    69  	var makeP10 func(szContId uintptr, szDn uintptr, pucP10 uintptr, puiP10Len uintptr) uintptr
    70  	lib.Sym("XT_MakeP10", &makeP10)
    71  	ret := makeP10(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(userDn)), uintptr(unsafe.Pointer(&pkcs10Temp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
    72  	fmt.Println("XtMakep10 ret : ", ret)
    73  
    74  	// 证书申请文件
    75  	pkcs10 := []byte{}
    76  	for i := 0; i < puiPubLen; i++ {
    77  		pkcs10 = append(pkcs10, pkcs10Temp[i])
    78  	}
    79  	fmt.Println("XtGetpubkey pkcs10:", hex.EncodeToString(pkcs10))
    80  
    81  	return pkcs10, nil
    82  
    83  }
    84  
    85  func (sec *XTSecLinux) SaveCert(keyId string, signCert []byte, signCertLen int) error {
    86  
    87  	userLable := C.CString(keyId)
    88  	defer C.free(unsafe.Pointer(userLable))
    89  
    90  	empty := []byte{0}
    91  
    92  	//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);
    93  	var saveCert func(szContId uintptr, pucSignCert uintptr, uiSignCertLen uintptr, pucEncKey uintptr, uiEncKeyLen uintptr, pucEncCert uintptr, uiEncCertLen uintptr) uintptr
    94  	lib.Sym("XT_SaveCert", &saveCert)
    95  	ret := saveCert(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&signCert[0])),
    96  		uintptr(signCertLen), uintptr(unsafe.Pointer(&empty[0])), 0, uintptr(unsafe.Pointer(&empty[0])), 0)
    97  
    98  	if ret != 0 {
    99  		return errors.New("XtSavecert save error")
   100  	}
   101  	fmt.Println("XtSavecert ret : ", ret)
   102  	return nil
   103  }
   104  
   105  func (sec *XTSecLinux) GetPubKey(keyId string) ([]byte, error) {
   106  
   107  	userLable := C.CString(keyId)
   108  	defer C.free(unsafe.Pointer(userLable))
   109  
   110  	cPublicKeyTemp := [256]byte{}
   111  	puiPubLen := 0
   112  
   113  	//typedef int(WINAPI *PFN_XT_GetPubKey)(IN const char *szContId, IN int iType, OUT unsigned char *pucPub, OUT unsigned int *puiPubLen);
   114  	var getPubKey func(szContId uintptr, iType uintptr, pucPub uintptr, puiPubLen uintptr) uintptr
   115  	lib.Sym("XT_GetPubKey", &getPubKey)
   116  	ret := getPubKey(uintptr(unsafe.Pointer(userLable)), uintptr(0), uintptr(unsafe.Pointer(&cPublicKeyTemp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
   117  	fmt.Println("XtGetpubkey ret:", ret)
   118  	//fmt.Println("XtGetpubkey puiPubLen:", puiPubLen)
   119  	// 公钥
   120  	cPublicKey := []byte{}
   121  	for i := 0; i < puiPubLen; i++ {
   122  		cPublicKey = append(cPublicKey, cPublicKeyTemp[i])
   123  	}
   124  	fmt.Println("XtGetpubkey cPublicKey:", hex.EncodeToString(cPublicKey))
   125  
   126  	return cPublicKey, nil
   127  
   128  }
   129  
   130  func (sec *XTSecLinux) Sign(keyId string, msg []byte) ([]byte, error) {
   131  
   132  	userLable := C.CString(keyId)
   133  	defer C.free(unsafe.Pointer(userLable))
   134  
   135  	signDataTemp := [256]byte{}
   136  	signDataLen := 0
   137  
   138  	//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);
   139  	var sign func(szContId uintptr, pucIn uintptr, uiInLen uintptr, pucSign uintptr, puiSignLen uintptr) uintptr
   140  	lib.Sym("XT_Sign", &sign)
   141  	ret := sign(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&msg[0])), uintptr(len(msg)),
   142  		uintptr(unsafe.Pointer(&signDataTemp[0])), uintptr(unsafe.Pointer(&signDataLen)))
   143  
   144  	fmt.Println("XtSign ret:", ret)
   145  	fmt.Println("XtSign signDataLen:", signDataLen)
   146  
   147  	// 签名值
   148  	signData := []byte{}
   149  	for i := 0; i < signDataLen; i++ {
   150  		signData = append(signData, signDataTemp[i])
   151  	}
   152  	fmt.Println("XtSign signData:", hex.EncodeToString(signData))
   153  
   154  	return signData, nil
   155  }
   156  
   157  func (sec *XTSecLinux) VerifySign(publicKey []byte, msg []byte, signData []byte) bool {
   158  
   159  	//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);
   160  	var verifySign func(pucPub uintptr, uiPubLen uintptr, pucIn uintptr, uiInLen uintptr, pucSign uintptr, uiSignLen uintptr) uintptr
   161  	lib.Sym("XT_VerifySign", &verifySign)
   162  	ret := verifySign(uintptr(unsafe.Pointer(&publicKey[0])), uintptr(len(publicKey)), uintptr(unsafe.Pointer(&msg[0])), uintptr(len(msg)), uintptr(unsafe.Pointer(&signData[0])), uintptr(len(signData)))
   163  	fmt.Println("XtVerifysign ret:", ret)
   164  	return ret == 0
   165  }
   166  
   167  func (sec *XTSecLinux) Encrypt(publicKey []byte, msg []byte) ([]byte, error) {
   168  
   169  	encDataTemp := [10000]byte{}
   170  	encDataLen := 10000
   171  
   172  	//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);
   173  	var encrypt func(pucPub uintptr, uiPubLen uintptr, pucIn uintptr, uiInLen uintptr, pucOut uintptr, puiOutLen uintptr) uintptr
   174  	lib.Sym("XT_Encrypt", &encrypt)
   175  	ret := encrypt(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)))
   176  	fmt.Println("XtEncrypt ret:", ret)
   177  	fmt.Println("XtEncrypt encDataLen:", encDataLen)
   178  
   179  	// 密文数据
   180  	encData := []byte{}
   181  	for i := 0; i < encDataLen; i++ {
   182  		encData = append(encData, encDataTemp[i])
   183  	}
   184  	fmt.Println("XtEncrypt encData:", hex.EncodeToString(encData))
   185  
   186  	return encData, nil
   187  
   188  }
   189  
   190  func (sec *XTSecLinux) Decrypt(keyId string, encData []byte) ([]byte, error) {
   191  
   192  	userLable := C.CString(keyId)
   193  
   194  	msgTemp := [10000]byte{}
   195  	msgLen := 10000
   196  
   197  	//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);
   198  	var decrypt func(szContId uintptr, pucIn uintptr, uiInLen uintptr, pucOut uintptr, puiOutLen uintptr) uintptr
   199  	lib.Sym("XT_Decrypt", &decrypt)
   200  	ret := decrypt(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&encData[0])), uintptr(len(encData)), uintptr(unsafe.Pointer(&msgTemp[0])), uintptr(unsafe.Pointer(&msgLen)))
   201  	fmt.Println("XtDecrypt ret:", ret)
   202  	fmt.Println("XtDecrypt msgLen:", msgLen)
   203  	// 解密后数据
   204  	msg := []byte{}
   205  	for i := 0; i < msgLen; i++ {
   206  		msg = append(msg, msgTemp[i])
   207  	}
   208  	fmt.Println("XtDecrypt msg:", hex.EncodeToString(msg))
   209  	return msg, nil
   210  }
   211  
   212  func (sec *XTSecLinux) ChangeContId(keyId string, newKeyId string) error {
   213  
   214  	userLable := C.CString(keyId)
   215  	defer C.free(unsafe.Pointer(userLable))
   216  
   217  	newUserLable := C.CString(newKeyId)
   218  	defer C.free(unsafe.Pointer(newUserLable))
   219  
   220  	//XT_API int XT_CALL XT_ChangeContId(IN const char *szContId, IN const char *szNewContId);
   221  	var changeContId func(szContId uintptr, pucIn uintptr) uintptr
   222  	lib.Sym("XT_ChangeContId", &changeContId)
   223  	ret := changeContId(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(newUserLable)))
   224  	fmt.Println("XtChangeContId ret:", ret)
   225  
   226  	if ret != 0 {
   227  		return errors.New("change contId error")
   228  	}
   229  	return nil
   230  }