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

Crypto-Plugin: ref counting for library initialization (WIP) #419

Merged
merged 9 commits into from
Dec 12, 2015

Conversation

petermax2
Copy link
Member

I prepared the library initialization using a simple ref counter (protected by a mutex) as preparation for the planned OpenSSL support.

  • investigate crypto API based ref counting
  • check if gcrypt has been initialized (possibly by an application)

@markus2330
Copy link
Contributor

Thank you!

I think the abstraction can be improved: the ref counting schema is needed for openssl, but for others there might be a better solution (atomic refcounting already as part of a crypto-API).

So why not put the refcounting to the gcrypt and openssl provider of elektraCryptoInit? (No library has such ref counting is a valid answer: then the choice of abstraction is correct.)

@markus2330
Copy link
Contributor

Btw. a way to avoid init/cleanup might be needed to. (e.g. If application already configured openssl)

@petermax2 petermax2 changed the title Crypto-Plugin: ref counting for library initialization Crypto-Plugin: ref counting for library initialization (WIP) Dec 11, 2015
@petermax2
Copy link
Member Author

gcrypt does not seem to have any ref counting built into its public API. They might be using it internally (I read some old discussions about it in developer forums). Libssl uses ref counting internally but they don't seem to make it public either. Libcrypto does not use any ref counting at all but provides a lock mechanism through its API. [1, 2] However this is the part of the API that we try to initialize here. ;)

[1] The Libgcrypt Reference Manual
[2] Viega, Messier and Chandra. Network Security With OpenSSL. O'Reilly. 2002.

markus2330 pushed a commit that referenced this pull request Dec 12, 2015
Crypto-Plugin: ref counting for library initialization (WIP)
@markus2330 markus2330 merged commit 207318f into ElektraInitiative:master Dec 12, 2015
@markus2330
Copy link
Contributor

Ok, then thank you for the PR! Some plugin options and maybe also as metadata in kdbOpen()/kdbClose() to enable/disable tearup/down should be added later, too. (and the check if gcrypt has been initialized)

I merged it, because it was a nicely coupled unit, please create a new PR then.

@petermax2
Copy link
Member Author

Thank you!

OK, I noted the metadata check. Checking if gcrypt has already been set up is not supported by the API. In general the manual suggests to leave all of the initialization stuff to the application developer. So OpenSSL will make some things easier for us.

@markus2330
Copy link
Contributor

An additional remark to the internal ref counting in gcrypt/libssl: If they do it correctly, it should not matter if you reopen/reclose it as long as the number of open and close matches?

What happens if the crypto libs have multiple init/teardown? Did you test it?
The default behaviour has to be chosen carefully based on that.

For plugin configuration we should,however, agree to a naming convention in #418 first.

@petermax2
Copy link
Member Author

For now it does not matter if the setup routines have been called multiple times. I tested it for both Gcrypt and libcrypto. That may change in the future. The gcrypt folks are planning on dropping the initialization alltogether in the future but they didn't make it clear when that will be.

@markus2330
Copy link
Contributor

That sounds good! Did you also test the case with complete shutdown and restart? That caused problems in python.

So the ref counting introduced in this PR is actually not needed?

@petermax2
Copy link
Member Author

gcrypt does not have to shutdown, I'm not sure about OpenSSL yet. The one thing I did not test yet is how these libraries behave when multiple threads are running and the library gets re-initialized in between operations. OpenSSL shouldn't have any problems as they internally just update a function pointer.

The thing with ref counting is: I want to comply to the guidelines and manuals of the crypto APIs as much as possible. And they all suggest to initializing the libraries as early as possible and call these routines only once. So maybe in a future update something breaks if we call the setup routines multiple times. Also if libgcrypt starts in FIPS-enabled mode the library does additional setup, that is supposed to run only once in the lifetime of the application. The same is true for libcrypto. I think the self tests in FIPS-mode may throw errors if executed multiple times. It all depends on versions and compile/startup options and we can not be careful enough.

@markus2330
Copy link
Contributor

Ok, thank you! So better we have the ref-counting in place!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants