1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CONSCRYPT_BIO_INPUT_STREAM_H_
18#define CONSCRYPT_BIO_INPUT_STREAM_H_
19
20#include <jni.h>
21#include <openssl/ssl.h>
22
23#include <conscrypt/bio_stream.h>
24#include <nativehelper/ScopedLocalRef.h>
25
26namespace conscrypt {
27
28class BioInputStream : public BioStream {
29 public:
30    BioInputStream(jobject stream, bool isFinite) : BioStream(stream), isFinite_(isFinite) {}
31
32    int read(char *buf, int len) {
33        return read_internal(buf, len, jniutil::inputStream_readMethod);
34    }
35
36    int gets(char *buf, int len) {
37        if (len > PEM_LINE_LENGTH) {
38            len = PEM_LINE_LENGTH;
39        }
40
41        int read = read_internal(buf, len - 1, jniutil::openSslInputStream_readLineMethod);
42        buf[read] = '\0';
43        JNI_TRACE("BIO::gets \"%s\"", buf);
44        return read;
45    }
46
47    bool isFinite() const {
48        return isFinite_;
49    }
50
51 private:
52    const bool isFinite_;
53
54    int read_internal(char *buf, int len, jmethodID method) {
55        JNIEnv *env = jniutil::getJNIEnv();
56        if (env == nullptr) {
57            JNI_TRACE("BioInputStream::read could not get JNIEnv");
58            return -1;
59        }
60
61        if (env->ExceptionCheck()) {
62            JNI_TRACE("BioInputStream::read called with pending exception");
63            return -1;
64        }
65
66        ScopedLocalRef<jbyteArray> javaBytes(env, env->NewByteArray(len));
67        if (javaBytes.get() == nullptr) {
68            JNI_TRACE("BioInputStream::read failed call to NewByteArray");
69            return -1;
70        }
71
72        jint read = env->CallIntMethod(getStream(), method, javaBytes.get());
73        if (env->ExceptionCheck()) {
74            JNI_TRACE("BioInputStream::read failed call to InputStream#read");
75            return -1;
76        }
77
78        /* Java uses -1 to indicate EOF condition. */
79        if (read == -1) {
80            setEof(true);
81            read = 0;
82        } else if (read > 0) {
83            env->GetByteArrayRegion(javaBytes.get(), 0, read, reinterpret_cast<jbyte *>(buf));
84        }
85
86        return read;
87    }
88
89 public:
90    /** Length of PEM-encoded line (64) plus CR plus NUL */
91    static const int PEM_LINE_LENGTH = 66;
92};
93
94}  // namespace conscrypt
95
96#endif  // CONSCRYPT_BIO_INPUT_STREAM_H_
97