int ConnectOperation::mysqlCertValidator()

in squangle/mysql_client/Operation.cpp [900:932]


int ConnectOperation::mysqlCertValidator(
    X509* server_cert,
    const void* context,
    const char** errptr) {
  ConnectOperation* self =
      reinterpret_cast<ConnectOperation*>(const_cast<void*>(context));
  CHECK_NOTNULL(self);

  // Hold a shared pointer to the Operation object while running the callback
  auto weak_self = self->weak_from_this();
  if (weak_self.expired()) {
    LOG(ERROR) << "ConnectOperation object " << self
               << " is already deallocated";
    return 0;
  }
  auto guard = weak_self.lock();

  const CertValidatorCallback callback =
      self->conn_options_.getCertValidationCallback();
  CHECK(callback);
  const void* callbackContext = self->conn_options_.isOpPtrAsValidationContext()
      ? self
      : self->conn_options_.getCertValidationContext();
  folly::StringPiece errorMessage;

  // "libmysql" expects this callback to return "0" if the cert validation was
  // successful, and return "1" if validation failed.
  int result = callback(server_cert, callbackContext, errorMessage) ? 0 : 1;
  if (!errorMessage.empty()) {
    *errptr = errorMessage.data();
  }
  return result;
}