LoadAndQueryCertificateForAccount wraps LoadAndQueryPEMForAccount and tls.X509KeyPair
(ctx context.Context, cctx client.Context, fin io.Reader)
| 16 | |
| 17 | // LoadAndQueryCertificateForAccount wraps LoadAndQueryPEMForAccount and tls.X509KeyPair |
| 18 | func LoadAndQueryCertificateForAccount(ctx context.Context, cctx client.Context, fin io.Reader) (tls.Certificate, error) { |
| 19 | kpm, err := NewKeyPairManager(cctx, cctx.FromAddress) |
| 20 | if err != nil { |
| 21 | return tls.Certificate{}, err |
| 22 | } |
| 23 | |
| 24 | x509cert, tlsCert, err := kpm.ReadX509KeyPair(fin) |
| 25 | if err != nil { |
| 26 | return tls.Certificate{}, err |
| 27 | } |
| 28 | |
| 29 | // Check if valid according to time |
| 30 | if x509cert.NotBefore.After(time.Now().UTC()) { |
| 31 | return tls.Certificate{}, fmt.Errorf("%w: certificate is not yet active, start ts %s", certerrors.ErrCertificate, x509cert.NotBefore) |
| 32 | } |
| 33 | |
| 34 | if time.Now().UTC().After(x509cert.NotAfter) { |
| 35 | return tls.Certificate{}, fmt.Errorf("%w: certificate has been expired since %s", certerrors.ErrCertificate, x509cert.NotAfter) |
| 36 | } |
| 37 | |
| 38 | params := &ctypes.QueryCertificatesRequest{ |
| 39 | Filter: ctypes.CertificateFilter{ |
| 40 | Owner: x509cert.Subject.CommonName, |
| 41 | Serial: x509cert.SerialNumber.String(), |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | certs, err := ctypes.NewQueryClient(cctx).Certificates(ctx, params) |
| 46 | if err != nil { |
| 47 | return tls.Certificate{}, err |
| 48 | } |
| 49 | |
| 50 | if len(certs.Certificates) == 0 { |
| 51 | return tls.Certificate{}, fmt.Errorf("%w: certificate has not been committed to blockchain", certerrors.ErrCertificate) |
| 52 | } |
| 53 | |
| 54 | foundCert := certs.Certificates[0] |
| 55 | if foundCert.GetCertificate().State != ctypes.CertificateValid { |
| 56 | return tls.Certificate{}, fmt.Errorf("%w: certificate is not valid", certerrors.ErrCertificate) |
| 57 | } |
| 58 | |
| 59 | return tlsCert, nil |
| 60 | } |
nothing calls this directly
no test coverage detected