| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #if defined(__linux) || defined(__sun) || defined(__hpux) |
| 16 | // Following definition aliases fopen to fopen64 on above mentioned |
| 17 | // platforms. This makes it possible to open and sequentially access |
| 18 | // files larger than 2GB from 32-bit application. It does not allow to |
| 19 | // traverse them beyond 2GB with fseek/ftell, but on the other hand *no* |
| 20 | // 32-bit platform permits that, not with fseek/ftell. Not to mention |
| 21 | // that breaking 2GB limit for seeking would require surgery to *our* |
| 22 | // API. But sequential access suffices for practical cases when you |
| 23 | // can run into large files, such as fingerprinting, so we can let API |
| 24 | // alone. For reference, the list of 32-bit platforms which allow for |
| 25 | // sequential access of large files without extra "magic" comprise *BSD, |
| 26 | // Darwin, IRIX... |
| 27 | #ifndef _FILE_OFFSET_BITS |
| 28 | #define _FILE_OFFSET_BITS 64 |
| 29 | #endif |
| 30 | #endif |
| 31 |
| 32 | #include <openssl/bio.h> |
| 33 |
| 34 | #include <assert.h> |
| 35 | #include <errno.h> |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 |
| 39 | #include <openssl/err.h> |
| 40 | #include <openssl/mem.h> |
| 41 |
| 42 | #include "../internal.h" |
| 43 |
| 44 | #if defined(OPENSSL_WINDOWS) |
| 45 | #include <fcntl.h> |
| 46 | #include <io.h> |
| 47 | #endif |
| 48 |
| 49 | #define BIO_FP_READ 0x02 |
| 50 | #define BIO_FP_WRITE 0x04 |
| 51 | #define BIO_FP_APPEND 0x08 |
| 52 |
| 53 | #if !defined(OPENSSL_NO_FILESYSTEM) |
| 54 | #define fopen_if_available fopen |
| 55 | #else |
| 56 | static FILE *fopen_if_available(const char *path, const char *mode) { |
| 57 | errno = ENOENT; |
| 58 | return NULL; |
| 59 | } |
| 60 | #endif |
| 61 |
| 62 | BIO *BIO_new_file(const char *filename, const char *mode) { |
| 63 | BIO *ret; |
| 64 | FILE *file; |
| 65 |
| 66 | file = fopen_if_available(filename, mode); |
| 67 | if (file == NULL) { |
| 68 | OPENSSL_PUT_SYSTEM_ERROR(); |
| 69 |
| 70 | ERR_add_error_data(5, "fopen('", filename, "','", mode, "')"); |
| 71 | if (errno == ENOENT) { |
| 72 | OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE); |
| 73 | } else { |
| 74 | OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); |
| 75 | } |
| 76 | return NULL; |
| 77 | } |
| 78 |
| 79 | ret = BIO_new_fp(file, BIO_CLOSE); |
| 80 | if (ret == NULL) { |
| 81 | fclose(file); |
| 82 | return NULL; |
| 83 | } |
| 84 |
| 85 | return ret; |
| 86 | } |
| 87 |
| 88 | BIO *BIO_new_fp(FILE *stream, int flags) { |
| 89 | BIO *ret = BIO_new(BIO_s_file()); |
| 90 | if (ret == NULL) { |
| 91 | return NULL; |
| 92 | } |
| 93 |
| 94 | BIO_set_fp(ret, stream, flags); |
| 95 | return ret; |
| 96 | } |
| 97 |
| 98 | static int file_free(BIO *bio) { |
| 99 | if (!bio->shutdown) { |
| 100 | return 1; |
| 101 | } |
| 102 |
| 103 | if (bio->init && bio->ptr != NULL) { |
| 104 | fclose(reinterpret_cast<FILE *>(bio->ptr)); |
| 105 | bio->ptr = NULL; |
| 106 | } |
| 107 | bio->init = 0; |
| 108 |
| 109 | return 1; |
| 110 | } |
| 111 |
| 112 | static int file_read(BIO *b, char *out, int outl) { |
| 113 | if (!b->init) { |
| 114 | return 0; |
| 115 | } |
| 116 |
| 117 | size_t ret = fread(out, 1, outl, (FILE *)b->ptr); |
| 118 | if (ret == 0 && ferror((FILE *)b->ptr)) { |
| 119 | OPENSSL_PUT_SYSTEM_ERROR(); |
| 120 | OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); |
| 121 | return -1; |
| 122 | } |
| 123 |
| 124 | // fread reads at most |outl| bytes, so |ret| fits in an int. |
| 125 | return (int)ret; |
| 126 | } |
| 127 |
| 128 | static int file_write(BIO *b, const char *in, int inl) { |
| 129 | if (!b->init) { |
| 130 | return 0; |
| 131 | } |
| 132 |
| 133 | int ret = (int)fwrite(in, inl, 1, (FILE *)b->ptr); |
| 134 | if (ret > 0) { |
| 135 | ret = inl; |
| 136 | } |
| 137 | return ret; |
| 138 | } |
| 139 |
| 140 | static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { |
| 141 | long ret = 1; |
| 142 | FILE *fp = (FILE *)b->ptr; |
| 143 | FILE **fpp; |
| 144 |
| 145 | switch (cmd) { |
| 146 | case BIO_CTRL_RESET: |
| 147 | num = 0; |
| 148 | [[fallthrough]]; |
| 149 | case BIO_C_FILE_SEEK: |
| 150 | ret = (long)fseek(fp, num, 0); |
| 151 | break; |
| 152 | case BIO_CTRL_EOF: |
| 153 | ret = (long)feof(fp); |
| 154 | break; |
| 155 | case BIO_C_FILE_TELL: |
| 156 | case BIO_CTRL_INFO: |
| 157 | ret = ftell(fp); |
| 158 | break; |
| 159 | case BIO_C_SET_FILE_PTR: |
| 160 | file_free(b); |
| 161 | static_assert((BIO_CLOSE & BIO_FP_TEXT) == 0, |
| 162 | "BIO_CLOSE and BIO_FP_TEXT must not collide"); |
| 163 | #if defined(OPENSSL_WINDOWS) |
| 164 | // If |BIO_FP_TEXT| is not set, OpenSSL will switch the file to binary |
| 165 | // mode. BoringSSL intentionally diverges here because it means code |
| 166 | // tested under POSIX will inadvertently change the state of |FILE| |
| 167 | // objects when wrapping them in a |BIO|. |
| 168 | if (num & BIO_FP_TEXT) { |
| 169 | _setmode(_fileno(reinterpret_cast<FILE *>(ptr)), _O_TEXT); |
| 170 | } |
| 171 | #endif |
| 172 | b->shutdown = (int)num & BIO_CLOSE; |
| 173 | b->ptr = ptr; |
| 174 | b->init = 1; |
| 175 | break; |
| 176 | case BIO_C_SET_FILENAME: |
| 177 | file_free(b); |
| 178 | b->shutdown = (int)num & BIO_CLOSE; |
| 179 | const char *mode; |
| 180 | if (num & BIO_FP_APPEND) { |
| 181 | if (num & BIO_FP_READ) { |
| 182 | mode = "ab+"; |
| 183 | } else { |
| 184 | mode = "ab"; |
| 185 | } |
| 186 | } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) { |
| 187 | mode = "rb+"; |
| 188 | } else if (num & BIO_FP_WRITE) { |
| 189 | mode = "wb"; |
| 190 | } else if (num & BIO_FP_READ) { |
| 191 | mode = "rb"; |
| 192 | } else { |
| 193 | OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE); |
| 194 | ret = 0; |
| 195 | break; |
| 196 | } |
| 197 | fp = fopen_if_available(reinterpret_cast<const char *>(ptr), mode); |
| 198 | if (fp == NULL) { |
| 199 | OPENSSL_PUT_SYSTEM_ERROR(); |
| 200 | ERR_add_error_data(5, "fopen('", ptr, "','", mode, "')"); |
| 201 | OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); |
| 202 | ret = 0; |
| 203 | break; |
| 204 | } |
| 205 | b->ptr = fp; |
| 206 | b->init = 1; |
| 207 | break; |
| 208 | case BIO_C_GET_FILE_PTR: |
| 209 | // the ptr parameter is actually a FILE ** in this case. |
| 210 | if (ptr != NULL) { |
| 211 | fpp = (FILE **)ptr; |
| 212 | *fpp = (FILE *)b->ptr; |
| 213 | } |
| 214 | break; |
| 215 | case BIO_CTRL_GET_CLOSE: |
| 216 | ret = (long)b->shutdown; |
| 217 | break; |
| 218 | case BIO_CTRL_SET_CLOSE: |
| 219 | b->shutdown = (int)num; |
| 220 | break; |
| 221 | case BIO_CTRL_FLUSH: |
| 222 | ret = 0 == fflush((FILE *)b->ptr); |
| 223 | break; |
| 224 | case BIO_CTRL_WPENDING: |
| 225 | case BIO_CTRL_PENDING: |
| 226 | default: |
| 227 | ret = 0; |
| 228 | break; |
| 229 | } |
| 230 | return ret; |
| 231 | } |
| 232 |
| 233 | static int file_gets(BIO *bp, char *buf, int size) { |
| 234 | if (size == 0) { |
| 235 | return 0; |
| 236 | } |
| 237 |
| 238 | if (!fgets(buf, size, (FILE *)bp->ptr)) { |
| 239 | buf[0] = 0; |
| 240 | // TODO(davidben): This doesn't distinguish error and EOF. This should check |
| 241 | // |ferror| as in |file_read|. |
| 242 | return 0; |
| 243 | } |
| 244 |
| 245 | return (int)strlen(buf); |
| 246 | } |
| 247 |
| 248 | static const BIO_METHOD methods_filep = { |
| 249 | BIO_TYPE_FILE, "FILE pointer", |
| 250 | file_write, file_read, |
| 251 | NULL /* puts */, file_gets, |
| 252 | file_ctrl, NULL /* create */, |
| 253 | file_free, NULL /* callback_ctrl */, |
| 254 | }; |
| 255 |
| 256 | const BIO_METHOD *BIO_s_file(void) { return &methods_filep; } |
| 257 |
| 258 |
| 259 | int BIO_get_fp(BIO *bio, FILE **out_file) { |
| 260 | return (int)BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char *)out_file); |
| 261 | } |
| 262 |
| 263 | int BIO_set_fp(BIO *bio, FILE *file, int flags) { |
| 264 | return (int)BIO_ctrl(bio, BIO_C_SET_FILE_PTR, flags, (char *)file); |
| 265 | } |
| 266 |
| 267 | int BIO_read_filename(BIO *bio, const char *filename) { |
| 268 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ, |
| 269 | (char *)filename); |
| 270 | } |
| 271 |
| 272 | int BIO_write_filename(BIO *bio, const char *filename) { |
| 273 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE, |
| 274 | (char *)filename); |
| 275 | } |
| 276 |
| 277 | int BIO_append_filename(BIO *bio, const char *filename) { |
| 278 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND, |
| 279 | (char *)filename); |
| 280 | } |
| 281 |
| 282 | int BIO_rw_filename(BIO *bio, const char *filename) { |
| 283 | return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, |
| 284 | BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, |
| 285 | (char *)filename); |
| 286 | } |
| 287 |
| 288 | long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); } |
| 289 |
| 290 | long BIO_seek(BIO *bio, long offset) { |
| 291 | return BIO_ctrl(bio, BIO_C_FILE_SEEK, offset, NULL); |
| 292 | } |
| 293 |