-
Notifications
You must be signed in to change notification settings - Fork 90
Add support for loading issuer certs from AAMVA VICAL in the verifier service #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,7 @@ import ( | |||||
| var ( | ||||||
| port = flag.String("port", ":8888", "Listening port") | ||||||
| certs = flag.String("cacerts", "certs.pem", "File containing issuer CA certs") | ||||||
| vicalUrl = flag.String("vical_url", "https://vical.dts.aamva.org/vical/vc", "URL to fetch AAMVA VICAL from") | ||||||
| circuitDir = flag.String("circuit_dir", "circuits", "Directory from which to load circuits") | ||||||
| ) | ||||||
|
|
||||||
|
|
@@ -76,6 +77,11 @@ func main() { | |||||
| os.Exit(1) | ||||||
| } | ||||||
|
|
||||||
| if err := zk.LoadVICAL(*vicalUrl); err != nil { | ||||||
| logger.Error("could not load VICAL", "url", *vicalUrl, "err", err) | ||||||
| // We decide not to exit here, as the server might still be useful with just local certs | ||||||
|
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.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| server := NewServer(*port, logger) | ||||||
|
|
||||||
| mux := http.NewServeMux() | ||||||
|
|
||||||
Binary file not shown.
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,8 @@ | ||
| package zk | ||
|
|
||
| import "crypto/x509" | ||
|
|
||
| var ( | ||
| // IssuerRoots is a pool of trusted root certificate authorities. | ||
| IssuerRoots = x509.NewCertPool() | ||
| ) |
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,75 @@ | ||
| package zk | ||
|
|
||
| import ( | ||
| "crypto/x509" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
|
|
||
| "github.com/fxamacker/cbor/v2" | ||
| ) | ||
|
|
||
| // LoadVICAL fetches the VICAL from the given URL and adds the certificates to the IssuerRoots pool. | ||
| func LoadVICAL(url string) error { | ||
| log.Printf("Fetching VICAL from %s", url) | ||
| resp, err := http.Get(url) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to fetch VICAL: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return fmt.Errorf("failed to fetch VICAL: status %s", resp.Status) | ||
| } | ||
|
|
||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read VICAL body: %w", err) | ||
| } | ||
|
|
||
| var rawItems []interface{} | ||
| if err := cbor.Unmarshal(data, &rawItems); err != nil { | ||
| return fmt.Errorf("failed to unmarshal VICAL CBOR: %w", err) | ||
| } | ||
|
|
||
| count := 0 | ||
| var findCerts func(item interface{}, depth int) | ||
| findCerts = func(item interface{}, depth int) { | ||
| if depth > 10 { | ||
| return // Avoid infinite recursion | ||
| } | ||
| switch v := item.(type) { | ||
| case []byte: | ||
| // Try to parse as certificate first | ||
| if len(v) > 0 && v[0] == 0x30 { | ||
| cert, err := x509.ParseCertificate(v) | ||
| if err == nil { | ||
| IssuerRoots.AddCert(cert) | ||
| count++ | ||
| return // Found a cert, stop digging in this branch | ||
| } | ||
| } | ||
| // If not a cert or cert parse failed, try treating as CBOR | ||
| var child interface{} | ||
| if err := cbor.Unmarshal(v, &child); err == nil { | ||
| findCerts(child, depth+1) | ||
| } | ||
| case []interface{}: | ||
| for _, child := range v { | ||
| findCerts(child, depth+1) | ||
| } | ||
| case map[interface{}]interface{}: | ||
| for _, val := range v { | ||
| findCerts(val, depth+1) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, item := range rawItems { | ||
| findCerts(item, 0) | ||
| } | ||
|
|
||
| log.Printf("Loaded %d certificates from VICAL", count) | ||
| return nil | ||
| } |
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,36 @@ | ||
| package zk | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestLoadVICAL(t *testing.T) { | ||
| // Load real test data | ||
| cborData, err := os.ReadFile("../vical.cbor") | ||
| if err != nil { | ||
| t.Fatalf("Failed to read vical.cbor: %v", err) | ||
| } | ||
|
|
||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Content-Type", "application/cbor") | ||
| w.Write(cborData) | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| // Check initial count | ||
| initialCount := len(IssuerRoots.Subjects()) | ||
|
|
||
| err = LoadVICAL(ts.URL) | ||
siriscac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| t.Fatalf("LoadVICAL failed: %v", err) | ||
| } | ||
|
|
||
| // Check final count | ||
| finalCount := len(IssuerRoots.Subjects()) | ||
| if finalCount <= initialCount { | ||
| t.Errorf("Expected to load certificates, but count did not increase. Initial: %d, Final: %d", initialCount, finalCount) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.