Skip to content

Commit cec0a47

Browse files
davidbenandrewhop
authored andcommitted
Use a stub fopen implementation when OPENSSL_NO_FILESYSTEM is set
Detecting errors (i.e. fs-less platforms using fs-only APIs) at compile time is generally preferable to doing so at runtime, so https://boringssl-review.googlesource.com/c/boringssl/+/61726 opted to remove the APIs altogether on applicable targets. However, Trusty uses rust-openssl somewhere and rust-openssl binds a bunch of filesystem-dependent APIs unconditionally. To keep that working, switch to a stub fopen when OPENSSL_NO_FILESYSTEM is set. We effectively model a platform where the filesystem "exists", but is empty. Upstream OpenSSL similarly has OPENSSL_NO_STDIO still define the file BIO (unlike the socket BIO, which is excluded), but in a stub form. As part of this, I've gone ahead and resolved one of the Trusty TODOs. It does produce a duplicate symbol with [1], but things seem to link fine in treehugger. In case it does break, I've bumped BORINGSSL_API_VERSION, so we can go in and condition it if needed. [1] https://android.googlesource.com/trusty/lib/+/refs/heads/main/lib/openssl-stubs/bio.c Bug: 629 Change-Id: I4f20d872a7cde863d21c78090f270b77b03545fa Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/61925 Commit-Queue: Bob Beck <[email protected]> Auto-Submit: David Benjamin <[email protected]> Reviewed-by: Bob Beck <[email protected]> (cherry picked from commit 0ffd3658dcdc21a6c56d234cf2a6008487dcfaa7)
1 parent d9816d6 commit cec0a47

File tree

12 files changed

+11
-51
lines changed

12 files changed

+11
-51
lines changed

crypto/bio/file.c

+10-13
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@
7373

7474
#include <openssl/bio.h>
7575

76-
// TODO(crbug.com/boringssl/629): Remove this in favor of the more fine-grained
77-
// OPENSSL_NO_FILESYSTEM ifdef.
78-
#if !defined(OPENSSL_TRUSTY)
79-
8076
#include <errno.h>
8177
#include <stdio.h>
8278
#include <string.h>
@@ -97,11 +93,19 @@
9793
#define BIO_FP_APPEND 0x08
9894

9995
#if !defined(OPENSSL_NO_FILESYSTEM)
96+
#define fopen_if_available fopen
97+
#else
98+
static FILE *fopen_if_available(const char *path, const char *mode) {
99+
errno = ENOENT;
100+
return NULL;
101+
}
102+
#endif
103+
100104
BIO *BIO_new_file(const char *filename, const char *mode) {
101105
BIO *ret;
102106
FILE *file;
103107

104-
file = fopen(filename, mode);
108+
file = fopen_if_available(filename, mode);
105109
if (file == NULL) {
106110
OPENSSL_PUT_SYSTEM_ERROR();
107111

@@ -122,7 +126,6 @@ BIO *BIO_new_file(const char *filename, const char *mode) {
122126

123127
return ret;
124128
}
125-
#endif // !OPENSSL_NO_FILESYSTEM
126129

127130
BIO *BIO_new_fp(FILE *stream, int close_flag) {
128131
BIO *ret = BIO_new(BIO_s_file());
@@ -209,7 +212,6 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
209212
_setmode(_fileno(b->ptr), num & BIO_FP_TEXT ? _O_TEXT : _O_BINARY);
210213
#endif
211214
break;
212-
#if !defined(OPENSSL_NO_FILESYSTEM)
213215
case BIO_C_SET_FILENAME:
214216
file_free(b);
215217
b->shutdown = (int)num & BIO_CLOSE;
@@ -231,7 +233,7 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
231233
ret = 0;
232234
break;
233235
}
234-
fp = fopen(ptr, mode);
236+
fp = fopen_if_available(ptr, mode);
235237
if (fp == NULL) {
236238
OPENSSL_PUT_SYSTEM_ERROR();
237239
ERR_add_error_data(5, "fopen('", ptr, "','", mode, "')");
@@ -242,7 +244,6 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
242244
b->ptr = fp;
243245
b->init = 1;
244246
break;
245-
#endif // !OPENSSL_NO_FILESYSTEM
246247
case BIO_C_GET_FILE_PTR:
247248
// the ptr parameter is actually a FILE ** in this case.
248249
if (ptr != NULL) {
@@ -302,7 +303,6 @@ int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
302303
return (int)BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *)file);
303304
}
304305

305-
#if !defined(OPENSSL_NO_FILESYSTEM)
306306
int BIO_read_filename(BIO *bio, const char *filename) {
307307
return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
308308
(char *)filename);
@@ -323,12 +323,9 @@ int BIO_rw_filename(BIO *bio, const char *filename) {
323323
BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE,
324324
(char *)filename);
325325
}
326-
#endif // !OPENSSL_NO_FILESYSTEM
327326

328327
long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); }
329328

330329
long BIO_seek(BIO *bio, long offset) {
331330
return BIO_ctrl(bio, BIO_C_FILE_SEEK, offset, NULL);
332331
}
333-
334-
#endif // OPENSSL_TRUSTY

crypto/conf/conf.c

-2
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ int NCONF_load_bio(CONF *conf, BIO *in, long *out_error_line) {
585585
return 0;
586586
}
587587

588-
#if !defined(OPENSSL_NO_FILESYSTEM)
589588
int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
590589
BIO *in = BIO_new_file(filename, "rb");
591590
int ret;
@@ -600,7 +599,6 @@ int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
600599

601600
return ret;
602601
}
603-
#endif // !OPENSSL_NO_FILESYSTEM
604602

605603
int CONF_parse_list(const char *list, char sep, int remove_whitespace,
606604
int (*list_cb)(const char *elem, size_t len, void *usr),

crypto/x509/by_dir.c

-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464
#include <openssl/thread.h>
6565
#include <openssl/x509.h>
6666

67-
#if !defined(OPENSSL_NO_FILESYSTEM)
68-
6967
#include "../internal.h"
7068
#include "internal.h"
7169

@@ -402,5 +400,3 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
402400
BUF_MEM_free(b);
403401
return ok;
404402
}
405-
406-
#endif // OPENSSL_NO_FILESYSTEM

crypto/x509/by_file.c

-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262

6363
#include "internal.h"
6464

65-
#if !defined(OPENSSL_NO_FILESYSTEM)
6665

6766
static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
6867
char **ret);
@@ -278,5 +277,3 @@ int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) {
278277
sk_X509_INFO_pop_free(inf, X509_INFO_free);
279278
return count;
280279
}
281-
282-
#endif // !OPENSSL_NO_FILESYSTEM

crypto/x509/x509_d2.c

-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#include <openssl/err.h>
5858
#include <openssl/x509.h>
5959

60-
#if !defined(OPENSSL_NO_FILESYSTEM)
6160

6261
int X509_STORE_set_default_paths(X509_STORE *ctx) {
6362
X509_LOOKUP *lookup;
@@ -107,5 +106,3 @@ int X509_STORE_load_locations(X509_STORE *ctx, const char *file,
107106
}
108107
return 1;
109108
}
110-
111-
#endif // !OPENSSL_NO_FILESYSTEM

include/openssl/base.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ extern "C" {
113113
// A consumer may use this symbol in the preprocessor to temporarily build
114114
// against multiple revisions of BoringSSL at the same time. It is not
115115
// recommended to do so for longer than is necessary.
116-
117-
#define AWSLC_API_VERSION 25
116+
#define AWSLC_API_VERSION 26
118117

119118
// This string tracks the most current production release version on Github
120119
// https://github.com/aws/aws-lc/releases.

include/openssl/bio.h

-4
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,9 @@ OPENSSL_EXPORT int BIO_get_fd(BIO *bio, int *out_fd);
513513
// BIO_s_file returns a BIO_METHOD that wraps a |FILE|.
514514
OPENSSL_EXPORT const BIO_METHOD *BIO_s_file(void);
515515

516-
#if !defined(OPENSSL_NO_FILESYSTEM)
517516
// BIO_new_file creates a file BIO by opening |filename| with the given mode.
518517
// See the |fopen| manual page for details of the mode argument.
519518
OPENSSL_EXPORT BIO *BIO_new_file(const char *filename, const char *mode);
520-
#endif
521519

522520
// BIO_new_fp creates a new file BIO that wraps the given |FILE|. If
523521
// |close_flag| is |BIO_CLOSE|, then |fclose| will be called on |stream| when
@@ -535,7 +533,6 @@ OPENSSL_EXPORT int BIO_get_fp(BIO *bio, FILE **out_file);
535533
// returns one on success and zero otherwise.
536534
OPENSSL_EXPORT int BIO_set_fp(BIO *bio, FILE *file, int close_flag);
537535

538-
#if !defined(OPENSSL_NO_FILESYSTEM)
539536
// BIO_read_filename opens |filename| for reading and sets the result as the
540537
// |FILE| for |bio|. It returns one on success and zero otherwise. The |FILE|
541538
// will be closed when |bio| is freed.
@@ -555,7 +552,6 @@ OPENSSL_EXPORT int BIO_append_filename(BIO *bio, const char *filename);
555552
// as the |FILE| for |bio|. It returns one on success and zero otherwise. The
556553
// |FILE| will be closed when |bio| is freed.
557554
OPENSSL_EXPORT int BIO_rw_filename(BIO *bio, const char *filename);
558-
#endif // OPENSSL_NO_FILESYSTEM
559555

560556
// BIO_tell returns the file offset of |bio|, or a negative number on error or
561557
// if |bio| does not support the operation.

include/openssl/conf.h

-2
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ OPENSSL_EXPORT CONF *NCONF_new(void *method);
9999
// NCONF_free frees all the data owned by |conf| and then |conf| itself.
100100
OPENSSL_EXPORT void NCONF_free(CONF *conf);
101101

102-
#if !defined(OPENSSL_NO_FILESYSTEM)
103102
// NCONF_load parses the file named |filename| and adds the values found to
104103
// |conf|. It returns one on success and zero on error. In the event of an
105104
// error, if |out_error_line| is not NULL, |*out_error_line| is set to the
106105
// number of the line that contained the error.
107106
OPENSSL_EXPORT int NCONF_load(CONF *conf, const char *filename,
108107
long *out_error_line);
109-
#endif
110108

111109
// NCONF_load_bio acts like |NCONF_load| but reads from |bio| rather than from
112110
// a named file.

include/openssl/ssl.h

-8
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,6 @@ OPENSSL_EXPORT int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der,
13171317
#define SSL_FILETYPE_PEM 1
13181318
#define SSL_FILETYPE_ASN1 2
13191319

1320-
#if !defined(OPENSSL_NO_FILESYSTEM)
13211320
OPENSSL_EXPORT int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx,
13221321
const char *file, int type);
13231322
OPENSSL_EXPORT int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file,
@@ -1339,7 +1338,6 @@ OPENSSL_EXPORT int SSL_use_PrivateKey_file(SSL *ssl, const char *file,
13391338
// success and zero on failure.
13401339
OPENSSL_EXPORT int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx,
13411340
const char *file);
1342-
#endif // !OPENSSL_NO_FILESYSTEM
13431341

13441342
// SSL_CTX_use_certificate_chain_file configures certificates for |ssl|. It
13451343
// reads the contents of |file| as a PEM-encoded leaf certificate followed
@@ -2954,7 +2952,6 @@ OPENSSL_EXPORT void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store);
29542952
// SSL_CTX_get_cert_store returns |ctx|'s certificate store.
29552953
OPENSSL_EXPORT X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx);
29562954

2957-
#if !defined(OPENSSL_NO_FILESYSTEM)
29582955
// SSL_CTX_set_default_verify_paths loads the OpenSSL system-default trust
29592956
// anchors into |ctx|'s store. It returns one on success and zero on failure.
29602957
OPENSSL_EXPORT int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx);
@@ -2971,7 +2968,6 @@ OPENSSL_EXPORT int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx);
29712968
OPENSSL_EXPORT int SSL_CTX_load_verify_locations(SSL_CTX *ctx,
29722969
const char *ca_file,
29732970
const char *ca_dir);
2974-
#endif // !OPENSSL_NO_FILESYSTEM
29752971

29762972
// SSL_get_verify_result returns the result of certificate verification. It is
29772973
// either |X509_V_OK| or a |X509_V_ERR_*| value.
@@ -3133,24 +3129,20 @@ OPENSSL_EXPORT int SSL_add_client_CA(SSL *ssl, X509 *x509);
31333129
// ownership of |x509|.
31343130
OPENSSL_EXPORT int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509);
31353131

3136-
#if !defined(OPENSSL_NO_FILESYSTEM)
31373132
// SSL_load_client_CA_file opens |file| and reads PEM-encoded certificates from
31383133
// it. It returns a newly-allocated stack of the certificate subjects or NULL
31393134
// on error. Duplicates in |file| are ignored.
31403135
OPENSSL_EXPORT STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file);
3141-
#endif
31423136

31433137
// SSL_dup_CA_list makes a deep copy of |list|. It returns the new list on
31443138
// success or NULL on allocation error.
31453139
OPENSSL_EXPORT STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list);
31463140

3147-
#if !defined(OPENSSL_NO_FILESYSTEM)
31483141
// SSL_add_file_cert_subjects_to_stack behaves like |SSL_load_client_CA_file|
31493142
// but appends the result to |out|. It returns one on success or zero on
31503143
// error.
31513144
OPENSSL_EXPORT int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *out,
31523145
const char *file);
3153-
#endif
31543146

31553147
// SSL_add_bio_cert_subjects_to_stack behaves like
31563148
// |SSL_add_file_cert_subjects_to_stack| but reads from |bio|.

include/openssl/x509.h

-6
Original file line numberDiff line numberDiff line change
@@ -2905,25 +2905,21 @@ OPENSSL_EXPORT X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx);
29052905
OPENSSL_EXPORT X509_LOOKUP *X509_STORE_add_lookup(X509_STORE *v,
29062906
X509_LOOKUP_METHOD *m);
29072907

2908-
#if !defined(OPENSSL_NO_FILESYSTEM)
29092908
OPENSSL_EXPORT X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void);
29102909
OPENSSL_EXPORT X509_LOOKUP_METHOD *X509_LOOKUP_file(void);
2911-
#endif
29122910

29132911
OPENSSL_EXPORT int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
29142912
OPENSSL_EXPORT int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
29152913

29162914
OPENSSL_EXPORT int X509_LOOKUP_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
29172915
long argl, char **ret);
29182916

2919-
#if !defined(OPENSSL_NO_FILESYSTEM)
29202917
OPENSSL_EXPORT int X509_load_cert_file(X509_LOOKUP *ctx, const char *file,
29212918
int type);
29222919
OPENSSL_EXPORT int X509_load_crl_file(X509_LOOKUP *ctx, const char *file,
29232920
int type);
29242921
OPENSSL_EXPORT int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file,
29252922
int type);
2926-
#endif
29272923

29282924
OPENSSL_EXPORT X509_LOOKUP *X509_LOOKUP_new(X509_LOOKUP_METHOD *method);
29292925
OPENSSL_EXPORT void X509_LOOKUP_free(X509_LOOKUP *ctx);
@@ -2932,11 +2928,9 @@ OPENSSL_EXPORT int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type,
29322928
X509_NAME *name, X509_OBJECT *ret);
29332929
OPENSSL_EXPORT int X509_LOOKUP_shutdown(X509_LOOKUP *ctx);
29342930

2935-
#if !defined(OPENSSL_NO_FILESYSTEM)
29362931
OPENSSL_EXPORT int X509_STORE_load_locations(X509_STORE *ctx, const char *file,
29372932
const char *dir);
29382933
OPENSSL_EXPORT int X509_STORE_set_default_paths(X509_STORE *ctx);
2939-
#endif
29402934
OPENSSL_EXPORT int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
29412935
OPENSSL_EXPORT void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int s);
29422936
OPENSSL_EXPORT int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);

ssl/ssl_file.cc

-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ int SSL_add_bio_cert_subjects_to_stack(STACK_OF(X509_NAME) *out, BIO *bio) {
203203
return add_bio_cert_subjects_to_stack(out, bio, /*allow_empty=*/true);
204204
}
205205

206-
#if !defined(OPENSSL_NO_FILESYSTEM)
207206
STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) {
208207
bssl::UniquePtr<BIO> in(BIO_new_file(file, "r"));
209208
if (in == nullptr) {
@@ -561,7 +560,6 @@ static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
561560

562561
return ret;
563562
}
564-
#endif // !OPENSSL_NO_FILESYSTEM
565563

566564

567565
int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file) {

ssl/ssl_x509.cc

-2
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
734734
X509_VERIFY_PARAM_set_depth(ctx->param, depth);
735735
}
736736

737-
#if !defined(OPENSSL_NO_FILESYSTEM)
738737
int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
739738
check_ssl_ctx_x509_method(ctx);
740739
return X509_STORE_set_default_paths(ctx->cert_store);
@@ -745,7 +744,6 @@ int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
745744
check_ssl_ctx_x509_method(ctx);
746745
return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
747746
}
748-
#endif // !OPENSSL_NO_FILESYSTEM
749747

750748
long SSL_get_verify_result(const SSL *ssl) {
751749
check_ssl_x509_method(ssl);

0 commit comments

Comments
 (0)