proxygen
JavaCryptoPeerCert.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
11 
12 namespace fizz {
13 
14 namespace {
15 jclass clazz;
16 jmethodID constructor;
17 jmethodID getIdentityMethod;
18 jmethodID verifyMethod;
19 } // namespace
20 
21 void JavaCryptoPeerCert::onLoad(JNIEnv* env) {
22  clazz = jni::getClass(env, "com/facebook/fizz/JavaCryptoPeerCert");
23  constructor = jni::getMethodID(env, clazz, "<init>", "([B)V");
24  getIdentityMethod =
25  jni::getMethodID(env, clazz, "getIdentity", "()Ljava/lang/String;");
26  verifyMethod =
27  jni::getMethodID(env, clazz, "verify", "(Ljava/lang/String;[B[B)V");
28 }
29 
31  bool shouldDetach;
32  auto env = jni::getEnv(&shouldDetach);
33 
34  auto byteArray = jni::createByteArray(env, std::move(certData));
35  jobject_ = env->NewObject(clazz, constructor, byteArray);
36  env->DeleteLocalRef(byteArray);
37 
38  jni::maybeThrowException(env, shouldDetach);
39  jni::releaseEnv(shouldDetach);
40 }
41 
43  bool shouldDetach;
44  auto env = jni::getEnv(&shouldDetach);
45 
46  auto jIdentity = (jstring)env->CallObjectMethod(jobject_, getIdentityMethod);
47  auto cIdentity = env->GetStringUTFChars(jIdentity, JNI_FALSE /* isCopy */);
48  std::string identity{cIdentity};
49  env->ReleaseStringUTFChars(jIdentity, cIdentity);
50 
51  jni::maybeThrowException(env, shouldDetach);
52  jni::releaseEnv(shouldDetach);
53  return identity;
54 }
55 
57  SignatureScheme scheme,
59  folly::ByteRange toBeSigned,
60  folly::ByteRange signature) const {
61  bool shouldDetach;
62  auto env = jni::getEnv(&shouldDetach);
63 
64  std::string algorithm;
65  switch (scheme) {
67  algorithm = "SHA256withECDSA";
68  break;
69  default:
70  throw std::runtime_error("Unsupported signature scheme");
71  }
72  auto jAlgorithm = env->NewStringUTF(algorithm.c_str());
73  auto signData = CertUtils::prepareSignData(context, toBeSigned);
74  auto jSignData = jni::createByteArray(env, std::move(signData));
75  auto jSignature = jni::createByteArray(env, signature);
76 
77  env->CallObjectMethod(
78  jobject_, verifyMethod, jAlgorithm, jSignData, jSignature);
79 
80  env->DeleteLocalRef(jSignature);
81  env->DeleteLocalRef(jSignData);
82  env->DeleteLocalRef(jAlgorithm);
83 
84  jni::maybeThrowException(env, shouldDetach);
85  jni::releaseEnv(shouldDetach);
86 }
87 
89  return nullptr;
90 }
91 
92 } // namespace fizz
std::unique_ptr< X509, X509Deleter > X509UniquePtr
jclass getClass(JNIEnv *env, const std::string &name)
Definition: JniUtils.cpp:46
context
Definition: CMakeCache.txt:563
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
SignatureScheme
Definition: Types.h:257
void maybeThrowException(JNIEnv *env, bool shouldDetach)
Definition: JniUtils.cpp:63
void releaseEnv(bool shouldDetach)
Definition: JniUtils.cpp:40
Definition: Actions.h:16
JNIEnv * getEnv(bool *shouldDetach)
Definition: JniUtils.cpp:23
CertificateVerifyContext
Definition: Certificate.h:20
jmethodID getMethodID(JNIEnv *env, jclass clazz, const std::string &name, const std::string &signature)
Definition: JniUtils.cpp:53
void verify(SignatureScheme scheme, CertificateVerifyContext context, folly::ByteRange toBeSigned, folly::ByteRange signature) const override
const char * string
Definition: Conv.cpp:212
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
std::string getIdentity() const override
static Buf prepareSignData(CertificateVerifyContext context, folly::ByteRange toBeSigned)
Definition: Certificate.cpp:23
jbyteArray createByteArray(JNIEnv *env, folly::ByteRange byteRange)
Definition: JniUtils.cpp:72
static void onLoad(JNIEnv *env)
folly::ssl::X509UniquePtr getX509() const override