This repository was archived by the owner on Dec 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
add OCSP status worker #336
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed5e893
Added OCSP worker to retrieve certificate status
9834c9d
Retrieves certs from worker input
94bc909
Improved worker error handling
d6f078d
Fixed build errors
8e9352e
Update OCSP worker, store results, add analysis printer
adamdecaf 48a9f1e
vendor x/crypto/ocsp
adamdecaf 24aa920
Merge branch 'master' into ocspStatus
jvehent 908f3bc
Merge branch 'master' into ocspStatus
jvehent c1228a0
Merge branch 'master' into ocspStatus
jvehent d92cc6c
Merge branch 'master' into ocspStatus
jvehent 8246015
worker/ocsp: make output a single line
adamdecaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package ocspStatus | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto" | ||
| "crypto/x509" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/mozilla/tls-observatory/logger" | ||
| "github.com/mozilla/tls-observatory/worker" | ||
| "golang.org/x/crypto/ocsp" | ||
| ) | ||
|
|
||
| var ( | ||
| workerName = "ocspStatus" | ||
| workerDesc = "Determines a certificate's status via OCSP'" | ||
|
|
||
| httpClient = &http.Client{ | ||
| Transport: &http.Transport{ | ||
| TLSHandshakeTimeout: 30 * time.Second, | ||
| }, | ||
| Timeout: 60 * time.Second, | ||
| } | ||
|
|
||
| log = logger.GetLogger() | ||
| ) | ||
|
|
||
| func init() { | ||
| runner := new(ocspStatus) | ||
|
|
||
| worker.RegisterPrinter(workerName, worker.Info{Runner: runner, Description: workerDesc}) | ||
| worker.RegisterWorker(workerName, worker.Info{Runner: runner, Description: workerDesc}) | ||
| } | ||
|
|
||
| type Result struct { | ||
| Status int `json:"status"` | ||
| RevokedAt time.Time `json:"revoked_at,omitempty"` | ||
| } | ||
|
|
||
| type ocspStatus struct{} | ||
|
|
||
| func (w ocspStatus) Run(in worker.Input, resChan chan worker.Result) { | ||
| res := worker.Result{ | ||
| WorkerName: workerName, | ||
| Success: false, | ||
| } | ||
|
|
||
| x509Cert, err := in.Certificate.ToX509() | ||
| if err != nil { | ||
| resChan <- worker.Result{ | ||
| Success: false, | ||
| WorkerName: workerName, | ||
| Errors: []string{fmt.Sprintf("%s for certificate %d", err.Error(), in.Certificate.ID)}, | ||
| Result: nil, | ||
| } | ||
| return | ||
| } | ||
|
|
||
| issuerEncoded := in.CertificateChain.Certs[len(in.CertificateChain.Certs)-1] // last cert in chain | ||
| x509IssuerCertEncoded, err := base64.StdEncoding.DecodeString(issuerEncoded) | ||
| if err != nil { | ||
| resChan <- worker.Result{ | ||
| Success: false, | ||
| WorkerName: workerName, | ||
| Errors: []string{fmt.Sprintf("%s decoding issuer for certificate %d", err.Error(), in.Certificate.ID)}, | ||
| Result: nil, | ||
| } | ||
| return | ||
| } | ||
| x509Issuer, err := x509.ParseCertificate(x509IssuerCertEncoded) | ||
| if err != nil { | ||
| resChan <- worker.Result{ | ||
| Success: false, | ||
| WorkerName: workerName, | ||
| Errors: []string{fmt.Sprintf("%s parsing issuer for certificate %d", err.Error(), in.Certificate.ID)}, | ||
| Result: nil, | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // grab OCSP response | ||
| opts := &ocsp.RequestOptions{ | ||
| Hash: crypto.SHA256, | ||
| } | ||
| req, err := ocsp.CreateRequest(x509Cert, x509Issuer, opts) | ||
| if err != nil { | ||
| resChan <- worker.Result{ | ||
| Success: false, | ||
| WorkerName: workerName, | ||
| Errors: []string{fmt.Sprintf("%s creating OCSP response for certificate %d", err.Error(), in.Certificate.ID)}, | ||
| Result: nil, | ||
| } | ||
| return | ||
| } | ||
| httpResponse, err := http.Post(x509Cert.OCSPServer[0], "application/ocsp-request", bytes.NewReader(req)) | ||
| if err != nil { | ||
| resChan <- worker.Result{ | ||
| Success: false, | ||
| WorkerName: workerName, | ||
| Errors: []string{fmt.Sprintf("%s making OCSP response for certificate %d", err.Error(), in.Certificate.ID)}, | ||
| Result: nil, | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // parse response | ||
| output, err := ioutil.ReadAll(httpResponse.Body) | ||
| if err != nil { | ||
| resChan <- worker.Result{ | ||
| Success: false, | ||
| WorkerName: workerName, | ||
| Errors: []string{fmt.Sprintf("%s reading OCSP response for certificate %d", err.Error(), in.Certificate.ID)}, | ||
| Result: nil, | ||
| } | ||
| return | ||
| } | ||
| resp, err := ocsp.ParseResponse(output, x509Issuer) | ||
| if err != nil { | ||
| resChan <- worker.Result{ | ||
| Success: false, | ||
| WorkerName: workerName, | ||
| Errors: []string{fmt.Sprintf("%s parsing OCSP response for certificate %d", err.Error(), in.Certificate.ID)}, | ||
| Result: nil, | ||
| } | ||
| return | ||
| } | ||
|
|
||
| status := Result{ | ||
| Status: resp.Status, | ||
| } | ||
| if resp.Status == ocsp.Revoked { | ||
| status.RevokedAt = resp.RevokedAt | ||
| } | ||
|
|
||
| out, err := json.Marshal(status) | ||
| if err != nil { | ||
| res.Success = false | ||
| res.Errors = append(res.Errors, err.Error()) | ||
| } else { | ||
| res.Success = true | ||
| res.Result = out | ||
| } | ||
| resChan <- res | ||
| } | ||
|
|
||
| func (ocspStatus) AnalysisPrinter(input []byte, printAll interface{}) (results []string, err error) { | ||
| var result Result | ||
| if err = json.Unmarshal(input, &result); err != nil { | ||
| return nil, fmt.Errorf("ocspStatus Worker: failed to parse results: err=%v", err) | ||
| } | ||
|
|
||
| results = []string{"* OCSP Status: "} | ||
| switch result.Status { | ||
| case ocsp.Good: | ||
| results = append(results, fmt.Sprintf(" - Not revoked")) | ||
| case ocsp.Revoked: | ||
| results = append(results, fmt.Sprintf(" - Revoked at %s\n", result.RevokedAt.Format(time.RFC3339))) | ||
| default: | ||
| results = append(results, fmt.Sprintf(" - Unknown status code %d\n", result.Status)) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets put everything on a single line like we did for the CAA and CRL workers. The output should be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jvehent Done! |
||
| return results, nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
revoked_atfield is still stored here, but it looks to be detected by.IsZero(), which doesn't really make sense as a revocation time anyway.The status field is always checked in the printer anyway.