Skip to content
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

Implement crypto backend for openssl #363

Merged
merged 22 commits into from
Jan 14, 2022

Conversation

qmuntal
Copy link
Contributor

@qmuntal qmuntal commented Jan 12, 2022

Important: This PR will be rebased to microsoft/dev.boringcrypto.go1.17 once #361 is merged.

This PR contains two major changes:

  • 0235b1a: crypto/internal/backend/internal/openssl does not have an init nor Enabled function anymore. It exposes instead three new functions: Init() error, FIPS() bool, SetFIPS(bool) error. It is now up to the caller to initialize the package and track if OpenSSL is enabled or not. Tests and documentation has changed accordingly.
  • 2cc8bd5: Creates crypto/internal/backend. This package acts as a façade between Go crypto and whatever crypto backend we want to implement, at the moment just OpenSSL. It was a linux-only init function that decides is OpenSSL and FIPS should be used or not using the criteria previously implemented.

There are also some minor refactors:

  • a18476b: Unexport NewOpenSSLError() as it is not useful externally.
  • 949b294 and 7d486e9: Cleans up build tags. The openssl package no longer needs so many build tags as part of the logic has been moved to backend. Header files don't need build tags as they are only included if a .go or .c imports them.

)

// Test that func init does not panic.
func TestInit(t *testing.T) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to call Init in this test function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this test is a leftover and does not make sense because init is called before executing the test. I'll remove it.

}
return strings.TrimSpace(string(buf)) == "1"
// FIPS returns true if OpenSSL is running in FIPS mode, else returns false.
func FIPS() bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the diff between FIPS() and Enabled after this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openssl.FIPS() just check if OpenSSL is running on FIPS mode by calling the appropriate OpenSSL function. It can return either because FIPS is enabled system-wide by default or because SetFIPS has been called successfully. So the FIPS state does not belong to the openssl Go package but to OpenSSL itself.

backend.Enabled means that OpenSSL has been initialized and is running in FIPS mode. In other words, the init function has successfully called openssl.Init() and either openssl.FIPS() == true or openssl.SetFIPS(true) == nil.

func VerifyRSAPSS(pub *PublicKeyRSA, h crypto.Hash, hashed, sig []byte, saltLen int) error {
panic("boringcrypto: not available")
}
// Copyright 2017 The Go Authors. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this file changed from LF to CRLF line endings. (That's why the diff doesn't find any similar lines. Go explicitly turns off autocrlf in .gitattributes, so need to be careful with editors. The .gitattributes file suggests there's some logic in the CL submission tool that verifies LF endings.)

These files currently have CRLF as of this PR:

$ find src/crypto -not -type d -exec file "{}" ";" | grep CRLF
src/crypto/internal/backend/openssl_linux.go: ASCII text, with CRLF line terminators
src/crypto/internal/backend/backend_test.go: ASCII text, with CRLF line terminators
src/crypto/internal/backend/nobackend.go: ASCII text, with CRLF line terminators
src/crypto/internal/backend/internal/openssl/goopenssl.c: C source, ASCII text, with CRLF line terminators
src/crypto/internal/backend/internal/openssl/apibridge_1_1.h: ASCII text, with CRLF line terminators
src/crypto/internal/backend/internal/openssl/openssl_funcs.h: C source, ASCII text, with CRLF line terminators
src/crypto/internal/backend/internal/openssl/apibridge_1_1.c: C source, ASCII text, with CRLF line terminators

Turning on GitHub's "ignore whitespace" option does work to make this reviewable, though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's unexpected. I'll fix it 👍

src/crypto/internal/backend/internal/openssl/openssl.go Outdated Show resolved Hide resolved
src/crypto/internal/backend/openssl_linux.go Outdated Show resolved Hide resolved
}
}

func hasSuffix(s, t string) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we use strings.HasSuffix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial answer was going to be:

Go is very conscious about binary sizes and they don't want to add bloat to final apps is not strictly necessary. strings imports unicode/utf8, which is huge, so better just copy the HasSuffix implementation and avoid that dependency for apps that use crypto but do not import strings (or anything that ends up importing it). In fact we are importing os when we shouldn't: #360

But I then realized that I already introduced strings some lines above to call strings.TrimSpace, which was an oversight, but my argument falls apart.

Anyway, I'll keep it as is for now as when I remove the os dependency will no longer need strings.TrimSpace, and I plan to do so once the active FIPS PRs are merged.

@qmuntal qmuntal changed the base branch from dev/qmuntal/port-openssl to microsoft/dev.boringcrypto.go1.17 January 13, 2022 18:14
@qmuntal qmuntal merged commit 49f49ae into microsoft/dev.boringcrypto.go1.17 Jan 14, 2022
@dagood
Copy link
Member

dagood commented Jan 18, 2022

It looks like a lot of commits got duplicated. (Pull/merge rather than force push after a rebase?)

@dagood dagood deleted the dev/qmuntal/crypto-backend branch January 24, 2022 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants