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