Repository navigation
create credential private key files with mode 0600 #197
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -409,6 +409,7 @@ do_unit_tests(void) | |
| *chain; // Cert chain data | ||
| size_t chainlen; // Length of cert chain data | ||
| http_trust_t trust; // Trust in credentials | ||
| struct stat fileinfo; // Key file information | ||
| static const char * const alt_names[] = | ||
| { // subjectAltName values | ||
| "printer.example.com", | ||
|
|
@@ -442,6 +443,9 @@ do_unit_tests(void) | |
| data = cupsCopyCredentialsKey(TEST_CERT_PATH, "_site_"); | ||
| testEnd(data != NULL); | ||
| free(data); | ||
|
|
||
| testBegin("_site_.key permissions"); | ||
| testEnd(!stat(TEST_CERT_PATH "/_site_.key", &fileinfo) && !(fileinfo.st_mode & 077)); | ||
|
Member
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. Would be useful to separate the mode check from the stat check, e.g.:
|
||
| } | ||
| else | ||
| { | ||
|
|
@@ -520,6 +524,9 @@ do_unit_tests(void) | |
| testEnd(data != NULL); | ||
| free(data); | ||
|
|
||
| testBegin("altprinter.ktm permissions"); | ||
| testEnd(!stat(TEST_CERT_PATH "/altprinter.ktm", &fileinfo) && !(fileinfo.st_mode & 077)); | ||
|
Member
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. Same test code as above. |
||
|
|
||
| testBegin("cupsCopyCredentialsRequest(altprinter w/alt names)"); | ||
| data = cupsCopyCredentialsRequest(TEST_CERT_PATH, "altprinter"); | ||
| testEnd(data != NULL); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ static X509_EXTENSION *openssl_create_san(const char *common_name, size_t num_al | |
| static time_t openssl_get_date(X509 *cert, int which); | ||
| //static void openssl_load_crl(void); | ||
| static STACK_OF(X509 *) openssl_load_x509(const char *credentials); | ||
| static BIO *openssl_open_key(const char *keyfile); | ||
|
Member
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. Maybe just add a permissions argument and make this |
||
| static bool openssl_validate_name(X509 *cert, const char *common_name); | ||
|
|
||
|
|
||
|
|
@@ -370,7 +371,7 @@ cupsCreateCredentials( | |
| http_make_path(keyfile, sizeof(keyfile), path, common_name, "key"); | ||
| http_make_path(pubfile, sizeof(pubfile), path, common_name, "pub"); | ||
|
|
||
| if ((bio = BIO_new_file(keyfile, "wb")) == NULL) | ||
| if ((bio = openssl_open_key(keyfile)) == NULL) | ||
| { | ||
| _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); | ||
| goto done; | ||
|
|
@@ -606,7 +607,7 @@ cupsCreateCredentialsRequest( | |
| sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); | ||
|
|
||
| // Save them... | ||
| if ((bio = BIO_new_file(keyfile, "wb")) == NULL) | ||
| if ((bio = openssl_open_key(keyfile)) == NULL) | ||
| { | ||
| _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); | ||
| goto done; | ||
|
|
@@ -2609,6 +2610,27 @@ openssl_load_x509( | |
| } | ||
|
|
||
|
|
||
| // | ||
| // 'openssl_open_key()' - Create a private key file that only the owner can read. | ||
| // | ||
|
|
||
| static BIO * // O - Key file BIO or `NULL` on error | ||
| openssl_open_key(const char *keyfile) // I - Key filename | ||
|
Member
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. Think it would be good to have a mode argument so that the caller can choose the perms. I'm not sure what OpenSSL uses for the default... |
||
| { | ||
| int fd; // File descriptor | ||
| BIO *bio; // Key file BIO | ||
|
|
||
|
|
||
| if ((fd = open(keyfile, O_CREAT | O_WRONLY | O_TRUNC, 0600)) < 0) | ||
| return (NULL); | ||
|
|
||
| if ((bio = BIO_new_fd(fd, BIO_CLOSE)) == NULL) | ||
| close(fd); | ||
|
|
||
| return (bio); | ||
| } | ||
|
|
||
|
|
||
| // | ||
| // 'openssl_validate_name()' - Check that the certificate is valid for the given name. | ||
| // | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -811,7 +811,7 @@ http_save_file(const char *path, // I - Directory path for certificate/key store | |
| return (true); | ||
| } | ||
|
|
||
| if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0) | ||
| if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, strcmp(ext, "key") ? 0644 : 0600)) < 0) | ||
|
Member
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. This also needs to handle .ktm files. |
||
| return (false); | ||
|
|
||
| if (write(fd, value, strlen(value)) < 0) | ||
|
|
||
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.
As noted in my initial comment, please leave this change out of the PR. Thanks!