MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / ParseCert

Function ParseCert

service/certificate/service.go:181–224  ·  view source on GitHub ↗

func parseCert(crt []byte) (*x509.Certificate, error) { //获取下一个pem格式证书数据 -----BEGIN CERTIFICATE----- -----END CERTIFICATE----- certDERBlock, _ := pem.Decode(crt) if certDERBlock == nil { return nil, fmt.Errorf("pem.Decode failed") } //第一个叶子证书就是我们https中使用的证书 x509Cert, err := x509.ParseCer

(privateKey, pemValue string)

Source from the content-addressed store, hash-verified

179//}
180
181func ParseCert(privateKey, pemValue string) (*tls.Certificate, error) {
182 var cert tls.Certificate
183 //获取下一个pem格式证书数据 -----BEGIN CERTIFICATE----- -----END CERTIFICATE-----
184 certDERBlock, restPEMBlock := pem.Decode([]byte(pemValue))
185 if certDERBlock == nil {
186 return nil, errors.New("证书解析失败")
187 }
188 //附加数字证书到返回
189 cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
190 //继续解析Certificate Chan,这里要明白证书链的概念
191 certDERBlockChain, _ := pem.Decode(restPEMBlock)
192 if certDERBlockChain != nil {
193 //追加证书链证书到返回
194 cert.Certificate = append(cert.Certificate, certDERBlockChain.Bytes)
195 }
196
197 //解码pem格式的私钥------BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----
198 keyDERBlock, _ := pem.Decode([]byte(privateKey))
199 if keyDERBlock == nil {
200 return nil, errors.New("证书解析失败")
201 }
202 var key interface{}
203 var errParsePK error
204 if keyDERBlock.Type == "RSA PRIVATE KEY" {
205 //RSA PKCS1
206 key, errParsePK = x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
207 } else if keyDERBlock.Type == "PRIVATE KEY" {
208 //pkcs8格式的私钥解析
209 key, errParsePK = x509.ParsePKCS8PrivateKey(keyDERBlock.Bytes)
210 }
211
212 if errParsePK != nil {
213 return nil, errors.New("证书解析失败")
214 } else {
215 cert.PrivateKey = key
216 }
217 //第一个叶子证书就是我们https中使用的证书
218 x509Cert, err := x509.ParseCertificate(certDERBlock.Bytes)
219 if err != nil {
220 return nil, err
221 }
222 cert.Leaf = x509Cert
223 return &cert, nil
224}

Callers 1

SaveMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected