proxygen
fizz::JavaCryptoPeerCert Class Reference

#include <JavaCryptoPeerCert.h>

Inheritance diagram for fizz::JavaCryptoPeerCert:
fizz::PeerCert folly::AsyncTransportCertificate

Public Member Functions

 JavaCryptoPeerCert (Buf certData)
 
 ~JavaCryptoPeerCert () override=default
 
std::string getIdentity () const override
 
void verify (SignatureScheme scheme, CertificateVerifyContext context, folly::ByteRange toBeSigned, folly::ByteRange signature) const override
 
folly::ssl::X509UniquePtr getX509 () const override
 
- Public Member Functions inherited from fizz::PeerCert
virtual ~PeerCert ()=default
 
- Public Member Functions inherited from folly::AsyncTransportCertificate
virtual ~AsyncTransportCertificate ()=default
 

Static Public Member Functions

static void onLoad (JNIEnv *env)
 

Private Attributes

jobject jobject_
 

Detailed Description

Definition at line 21 of file JavaCryptoPeerCert.h.

Constructor & Destructor Documentation

fizz::JavaCryptoPeerCert::JavaCryptoPeerCert ( Buf  certData)
explicit

Definition at line 30 of file JavaCryptoPeerCert.cpp.

References fizz::jni::createByteArray(), fizz::jni::getEnv(), jobject_, fizz::jni::maybeThrowException(), folly::gen::move, and fizz::jni::releaseEnv().

30  {
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 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void maybeThrowException(JNIEnv *env, bool shouldDetach)
Definition: JniUtils.cpp:63
void releaseEnv(bool shouldDetach)
Definition: JniUtils.cpp:40
JNIEnv * getEnv(bool *shouldDetach)
Definition: JniUtils.cpp:23
jbyteArray createByteArray(JNIEnv *env, folly::ByteRange byteRange)
Definition: JniUtils.cpp:72
fizz::JavaCryptoPeerCert::~JavaCryptoPeerCert ( )
overridedefault

Member Function Documentation

std::string fizz::JavaCryptoPeerCert::getIdentity ( ) const
overridevirtual

Returns the identity this certificate conveys.

An identity is an opaque string that may be used by the application for authentication or authorization purposes. The exact structure and semantics of the identity string are determined by concrete implementations of AsyncTransport.

Implements folly::AsyncTransportCertificate.

Definition at line 42 of file JavaCryptoPeerCert.cpp.

References fizz::jni::getEnv(), jobject_, fizz::jni::maybeThrowException(), fizz::jni::releaseEnv(), and string.

42  {
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 }
void maybeThrowException(JNIEnv *env, bool shouldDetach)
Definition: JniUtils.cpp:63
void releaseEnv(bool shouldDetach)
Definition: JniUtils.cpp:40
JNIEnv * getEnv(bool *shouldDetach)
Definition: JniUtils.cpp:23
const char * string
Definition: Conv.cpp:212
folly::ssl::X509UniquePtr fizz::JavaCryptoPeerCert::getX509 ( ) const
overridevirtual

Returns an X509 structure associated with this Certificate. This may be null.

Implements folly::AsyncTransportCertificate.

Definition at line 88 of file JavaCryptoPeerCert.cpp.

88  {
89  return nullptr;
90 }
void fizz::JavaCryptoPeerCert::onLoad ( JNIEnv *  env)
static

Definition at line 21 of file JavaCryptoPeerCert.cpp.

References fizz::jni::getClass(), and fizz::jni::getMethodID().

Referenced by JNI_OnLoad().

21  {
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 }
jclass getClass(JNIEnv *env, const std::string &name)
Definition: JniUtils.cpp:46
jmethodID getMethodID(JNIEnv *env, jclass clazz, const std::string &name, const std::string &signature)
Definition: JniUtils.cpp:53
void fizz::JavaCryptoPeerCert::verify ( SignatureScheme  scheme,
CertificateVerifyContext  context,
folly::ByteRange  toBeSigned,
folly::ByteRange  signature 
) const
overridevirtual

Verifies that signature is a valid signature of toBeSigned. Throws if it's not.

Implements fizz::PeerCert.

Definition at line 56 of file JavaCryptoPeerCert.cpp.

References fizz::jni::createByteArray(), fizz::ecdsa_secp256r1_sha256, fizz::jni::getEnv(), jobject_, fizz::jni::maybeThrowException(), folly::gen::move, fizz::CertUtils::prepareSignData(), fizz::jni::releaseEnv(), and string.

60  {
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 }
context
Definition: CMakeCache.txt:563
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void maybeThrowException(JNIEnv *env, bool shouldDetach)
Definition: JniUtils.cpp:63
void releaseEnv(bool shouldDetach)
Definition: JniUtils.cpp:40
JNIEnv * getEnv(bool *shouldDetach)
Definition: JniUtils.cpp:23
const char * string
Definition: Conv.cpp:212
static Buf prepareSignData(CertificateVerifyContext context, folly::ByteRange toBeSigned)
Definition: Certificate.cpp:23
jbyteArray createByteArray(JNIEnv *env, folly::ByteRange byteRange)
Definition: JniUtils.cpp:72

Member Data Documentation

jobject fizz::JavaCryptoPeerCert::jobject_
private

Definition at line 41 of file JavaCryptoPeerCert.h.

Referenced by getIdentity(), JavaCryptoPeerCert(), and verify().


The documentation for this class was generated from the following files: