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

     1  package x509ca
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  )
     7  
     8  //// 格式化csr文件
     9  //func readCertificateRequestFromPem(certPem []byte) (*x509.CertificateRequest, error) {
    10  //	block, _ := pem.Decode(certPem)
    11  //	if block == nil {
    12  //		return nil, errors.New("failed to decode certificate request")
    13  //	}
    14  //	return x509.ParseCertificateRequest(block.Bytes)
    15  //}
    16  
    17  // 获取根证书
    18  func GetRootCaCert() string {
    19  	// 默认不传参数,根据接口名称区分
    20  	paramStr := ""
    21  	cert := httpPost(REQ_TYPE_STRING, "getRootCaCert", paramStr, nil)
    22  	fmt.Println("获取到根证书:  " + cert)
    23  	//certByte, _ := base64.StdEncoding.DecodeString(cert)
    24  	//x509cert, _ := x509.ParseGmx509DerToX509(certByte)
    25  	//sujectName := x509cert.Subject.String()
    26  	//fmt.Println(sujectName)
    27  	return cert
    28  }
    29  
    30  func GetIntermediateCaCert() string {
    31  	// 默认不传参数,根据接口名称区分
    32  	paramStr := ""
    33  	cert := httpPost(REQ_TYPE_STRING, "getIntermediateCaCert", paramStr, nil)
    34  	fmt.Println("获取到中间证书:  " + cert)
    35  	//certByte, _ := base64.StdEncoding.DecodeString(cert)
    36  	//x509cert, _ := x509.ParseGmx509DerToX509(certByte)
    37  	//sujectName := x509cert.Subject.String()
    38  	//fmt.Println(sujectName)
    39  	return cert
    40  }
    41  
    42  func ApplySignCert(csr string, certType string) string {
    43  
    44  	//csrByte, _ := base64.StdEncoding.DecodeString(csr)
    45  	//cerReq, _ := readCertificateRequestFromPem(csrByte)
    46  
    47  	// 解析解析扩展项
    48  	//var extendIds []string
    49  	//var extendValues []string
    50  	//if len(cerReq.ExtraExtensions) != 0 {
    51  	//	for i := 0; i < len(cerReq.ExtraExtensions); i++ {
    52  	//		extendIds[i] = cerReq.ExtraExtensions[i].Id.String()
    53  	//		extendValues[i] = hex.EncodeToString(cerReq.ExtraExtensions[i].Value)
    54  	//	}
    55  	//}
    56  
    57  	// 组织参数
    58  	param := url.Values{
    59  		"csr":      []string{csr},
    60  		"certType": []string{certType},
    61  		//"extendIds":    extendIds,
    62  		//"extendValues": extendValues,
    63  	}
    64  	cert := httpPost(REQ_TYPE_FORM, "applySignCert", "", param)
    65  	//fmt.Println("通过CA签发证书:  " + cert)
    66  	//certByte, _ := base64.StdEncoding.DecodeString(cert)
    67  	//x509cert, _ := x509.ParseGmx509DerToX509(certByte)
    68  	//sujectName := x509cert.Subject.String()
    69  	//fmt.Println(sujectName)
    70  	return cert
    71  }