This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 714
Expand file tree
/
Copy pathauthentication_oci_client_plugin.cc
More file actions
147 lines (123 loc) · 4.65 KB
/
Copy pathauthentication_oci_client_plugin.cc
File metadata and controls
147 lines (123 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright (c) 2021, 2022, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "my_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <ostream>
#include <my_compiler.h>
#include <mysql.h>
#include <mysql/client_plugin.h>
#include "sql-common/oci/signing_key.h"
#include "sql-common/oci/ssl.h"
#include "sql-common/oci/utilities.h"
static const char *s_oci_config_location = nullptr;
static oci::OCI_config_file *s_oci_config_file = nullptr;
// Helper functions.
/**
* Parse the system variables for the location of the ~/.oci/config file.
* Extract the key_file= value from the ~/.oci/config file.
*/
oci::OCI_config_file parse_oci_config_file() {
return oci::parse_oci_config_file(
oci::get_oci_config_file_location(s_oci_config_location));
}
/**
client auth function
* read stuff via the VIO. try to *read first*
* get (login) data from the MYSQL handle: mysql->user, mysql->passwd
* return CR_OK on success, CR_ERROR on failure
*/
static int oci_authenticate_client_plugin(MYSQL_PLUGIN_VIO *vio,
MYSQL * /*mysql*/) {
/**
* Step 1: Receive the nonce from the server.
*/
unsigned char *server_nonce = nullptr;
int server_nonce_length = vio->read_packet(vio, &server_nonce);
if (server_nonce_length <= 0) {
return CR_AUTH_HANDSHAKE;
}
/**
* Step 2: Sign the nonce with the private key.
*/
oci::Signing_Key signer{s_oci_config_file->key_file};
if (!signer) {
return CR_AUTH_PLUGIN_ERROR;
}
auto encoded = signer.sign(server_nonce, server_nonce_length);
if (encoded.size() == 0) {
return CR_AUTH_PLUGIN_ERROR;
}
/**
* Step 3: Prepare the response.
*/
auto response = oci::prepare_response(s_oci_config_file->fingerprint,
oci::ssl::base64_encode(encoded));
/**
* Step 4: Send the encrypted nonce back to the server for verification.
*/
if (vio->write_packet(
vio, reinterpret_cast<const unsigned char *>(response.c_str()),
response.length())) {
return CR_AUTH_HANDSHAKE;
}
return CR_OK;
}
/**
Try to parse and assess the currently effective config file.
@retval 0 : success
@retval 1 : parsing failed or not initialized
*/
static int try_parse_and_set_config_file() {
auto config = parse_oci_config_file();
if (!config.key_file.empty() && !config.fingerprint.empty() &&
s_oci_config_file != nullptr) {
s_oci_config_file->key_file.assign(config.key_file);
s_oci_config_file->fingerprint.assign(config.fingerprint);
return 0;
}
return 1;
}
static int initialize_plugin(char *, size_t, int, va_list) {
s_oci_config_file = new (std::nothrow) oci::OCI_config_file{};
if (s_oci_config_file == nullptr) return 1;
try_parse_and_set_config_file();
return 0;
}
static int deinitialize_plugin() {
if (s_oci_config_file != nullptr) delete s_oci_config_file;
return 0;
}
/**
oci_authenticate_client_option plugin API to allow server to pass optional
data for plugin to process
*/
static int oci_authenticate_client_option(const char *option, const void *val) {
if (strcmp(option, "oci-config-file") == 0 && val != nullptr) {
s_oci_config_location = static_cast<const char *>(val);
return try_parse_and_set_config_file();
}
return 1;
}
mysql_declare_client_plugin(AUTHENTICATION) "authentication_oci_client",
MYSQL_CLIENT_PLUGIN_AUTHOR_ORACLE, "OCI Client Authentication Plugin",
{0, 1, 0}, "COMMUNITY", nullptr, initialize_plugin, deinitialize_plugin,
oci_authenticate_client_option, nullptr, oci_authenticate_client_plugin,
nullptr mysql_end_client_plugin;