MySQL 8.0.39
Source Code Documentation
i_sha2_password_common.h
Go to the documentation of this file.
1/*
2Copyright (c) 2017, 2024, Oracle and/or its affiliates.
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License, version 2.0,
6as published by the Free Software Foundation.
7
8This program is designed to work with certain software (including
9but not limited to OpenSSL) that is licensed under separate terms,
10as designated in a particular file or component or in included license
11documentation. The authors of MySQL hereby grant you an additional
12permission to link the program and your derivative works with the
13separately licensed software that they have either included with
14the program or referenced in the documentation.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License, version 2.0, for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef I_SHA2_PASSWORD_COMMON_INCLUDED
26#define I_SHA2_PASSWORD_COMMON_INCLUDED
27
28#include <openssl/evp.h>
29#include "openssl/ossl_typ.h"
30#include "sha2.h" /* SHA256_DIGEST_LENGTH */
31
32#include <string>
33
34/**
35 @file sql/auth/i_sha2_password_common.h
36 Classes for caching_sha2_authentication plugin
37*/
38
39/**
40 @defgroup auth_caching_sha2_auth caching_sha2_authentication information
41 @{
42*/
43namespace sha2_password {
44/* Digest length for caching_sha2_authentication plugin */
45const unsigned int CACHING_SHA2_DIGEST_LENGTH = SHA256_DIGEST_LENGTH;
46
47/**
48 Supported digest information
49*/
50
52
53/**
54 Interface for cryptographic digest generation
55*/
56
58 public:
59 virtual bool update_digest(const void *src, unsigned int length) = 0;
60 virtual bool retrieve_digest(unsigned char *digest, unsigned int length) = 0;
61 virtual void scrub() = 0;
62 virtual ~Generate_digest() = default;
63};
64
65/**
66 SHA256 digest generator
67 @sa Generate_digest
68 @sa Digest_info
69*/
70
72 public:
74 ~SHA256_digest() override;
75
76 bool update_digest(const void *src, unsigned int length) override;
77 bool retrieve_digest(unsigned char *digest, unsigned int length) override;
78 void scrub() override;
79 bool all_ok() { return m_ok; }
80
81 private:
82 void init();
83 void deinit();
84
85 private:
86 /** Digest output buffer */
88 /** Digest context */
89 EVP_MD_CTX *md_context;
90 /** Status */
91 bool m_ok;
92};
93
94/**
95 Scramble generator
96 Responsible for generating scramble of following format:
97 XOR(SHA2(m_src), SHA2(SHA2(SHA2(m_src)), m_rnd))
98 @sa SHA256_digest
99 @sa Digest_info
100*/
101
103 public:
104 Generate_scramble(const std::string source, const std::string rnd,
106
108
109 bool scramble(unsigned char *scramble, unsigned int scramble_length);
110
111 private:
112 /** plaintext source string */
113 std::string m_src;
114 /** random string */
115 std::string m_rnd;
116 /** Type of digest */
118 /** Digest generator class */
120 /** length of the digest */
121 unsigned int m_digest_length;
122};
123
124/**
125 Scramble validator
126 Expects scramble to be:
127 XOR(SHA2(m_src), SHA2(SHA2(SHA2(m_src)), m_rnd))
128 Validates it against:
129 SHA2(SHA2(m_src)) and random string
130
131 @sa Generate_scramble
132 @sa SHA256_digest
133 @sa Digest_info
134*/
135
137 public:
138 Validate_scramble(const unsigned char *scramble, const unsigned char *known,
139 const unsigned char *rnd, unsigned int rnd_length,
141
143
144 bool validate();
145
146 private:
147 /** scramble to be validated */
148 const unsigned char *m_scramble;
149 /** SHA2(SHA2(plaintext_password)) */
150 const unsigned char *m_known;
151 /** random string */
152 const unsigned char *m_rnd;
153 /** random string length*/
154 unsigned int m_rnd_length;
155 /** Type of digest */
157 /** Digest generator class */
159 /** length of the digest */
160 unsigned int m_digest_length;
161};
162} // namespace sha2_password
163
164/** @} (end of auth_caching_sha2_auth) */
165
166#endif // !I_SHA2_PASSWORD_INCLUDED
Interface for cryptographic digest generation.
Definition: i_sha2_password_common.h:57
virtual bool retrieve_digest(unsigned char *digest, unsigned int length)=0
virtual ~Generate_digest()=default
virtual bool update_digest(const void *src, unsigned int length)=0
Scramble generator Responsible for generating scramble of following format: XOR(SHA2(m_src),...
Definition: i_sha2_password_common.h:102
Generate_digest * m_digest_generator
Digest generator class.
Definition: i_sha2_password_common.h:119
std::string m_rnd
random string
Definition: i_sha2_password_common.h:115
~Generate_scramble()
Generate_scramble destructor.
Definition: sha2_password_common.cc:184
std::string m_src
plaintext source string
Definition: i_sha2_password_common.h:113
bool scramble(unsigned char *scramble, unsigned int scramble_length)
Scramble generation.
Definition: sha2_password_common.cc:206
Digest_info m_digest_type
Type of digest.
Definition: i_sha2_password_common.h:117
unsigned int m_digest_length
length of the digest
Definition: i_sha2_password_common.h:121
Generate_scramble(const std::string source, const std::string rnd, Digest_info digest_type=Digest_info::SHA256_DIGEST)
Generate_scramble constructor.
Definition: sha2_password_common.cc:165
SHA256 digest generator.
Definition: i_sha2_password_common.h:71
bool update_digest(const void *src, unsigned int length) override
Update digest with plaintext.
Definition: sha2_password_common.cc:72
void deinit()
Release allocated memory for digest context.
Definition: sha2_password_common.cc:152
void scrub() override
Cleanup and reinit.
Definition: sha2_password_common.cc:118
SHA256_digest()
SHA256 digest generator constructor.
Definition: sha2_password_common.cc:53
unsigned char m_digest[CACHING_SHA2_DIGEST_LENGTH]
Digest output buffer.
Definition: i_sha2_password_common.h:87
bool m_ok
Status.
Definition: i_sha2_password_common.h:91
bool retrieve_digest(unsigned char *digest, unsigned int length) override
Retrieve generated digest.
Definition: sha2_password_common.cc:96
bool all_ok()
Definition: i_sha2_password_common.h:79
~SHA256_digest() override
Release acquired memory.
Definition: sha2_password_common.cc:59
void init()
Initialize digest context.
Definition: sha2_password_common.cc:130
EVP_MD_CTX * md_context
Digest context.
Definition: i_sha2_password_common.h:89
Scramble validator Expects scramble to be: XOR(SHA2(m_src), SHA2(SHA2(SHA2(m_src)),...
Definition: i_sha2_password_common.h:136
~Validate_scramble()
Validate_scramble destructor.
Definition: sha2_password_common.cc:298
const unsigned char * m_rnd
random string
Definition: i_sha2_password_common.h:152
Generate_digest * m_digest_generator
Digest generator class.
Definition: i_sha2_password_common.h:158
const unsigned char * m_known
SHA2(SHA2(plaintext_password))
Definition: i_sha2_password_common.h:150
bool validate()
Validate the scramble.
Definition: sha2_password_common.cc:317
unsigned int m_digest_length
length of the digest
Definition: i_sha2_password_common.h:160
unsigned int m_rnd_length
random string length
Definition: i_sha2_password_common.h:154
Digest_info m_digest_type
Type of digest.
Definition: i_sha2_password_common.h:156
Validate_scramble(const unsigned char *scramble, const unsigned char *known, const unsigned char *rnd, unsigned int rnd_length, Digest_info digest_type=Digest_info::SHA256_DIGEST)
Validate scramble constructor.
Definition: sha2_password_common.cc:275
const unsigned char * m_scramble
scramble to be validated
Definition: i_sha2_password_common.h:148
static int rnd(int max_value)
Definition: hp_test2.cc:548
void scramble(char *to, const char *message, const char *password)
Produce an obscure octet sequence from password and random string, received from the server.
Definition: password.cc:271
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: i_sha2_password.h:45
const unsigned int CACHING_SHA2_DIGEST_LENGTH
Definition: i_sha2_password_common.h:45
Digest_info
Supported digest information.
Definition: i_sha2_password_common.h:51
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42