FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sec_negotiate_openssl.h
1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 2015 Frediano Ziglio
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 #include <openssl/rand.h>
21 #include <openssl/bio.h>
22 #include <openssl/pem.h>
23 #include <openssl/err.h>
24 
36 #ifndef HAVE_OPENSSL
37 #error HAVE_OPENSSL not defines, this file should not be included
38 #endif
39 
40 static void*
41 tds5_rsa_encrypt(const void *key, size_t key_len, const void *nonce, size_t nonce_len, const char *pwd, size_t *em_size)
42 {
43  RSA *rsa = NULL;
44  BIO *keybio;
45 
46  TDS_UCHAR *message = NULL;
47  size_t message_len, pwd_len;
48  TDS_UCHAR *em = NULL;
49 
50  int result;
51 
52  keybio = BIO_new_mem_buf((void*) key, key_len);
53  if (keybio == NULL)
54  goto error;
55 
56  rsa = PEM_read_bio_RSAPublicKey(keybio, &rsa, NULL, NULL);
57  if (!rsa)
58  goto error;
59 
60  pwd_len = strlen(pwd);
61  message_len = nonce_len + pwd_len;
62  message = tds_new(TDS_UCHAR, message_len);
63  if (!message)
64  goto error;
65  memcpy(message, nonce, nonce_len);
66  memcpy(message + nonce_len, pwd, pwd_len);
67 
68  em = tds_new(TDS_UCHAR, BN_num_bytes(rsa->n));
69  if (!em)
70  goto error;
71 
72  result = RSA_public_encrypt(message_len, message, em, rsa, RSA_PKCS1_OAEP_PADDING);
73  if (result < 0)
74  goto error;
75 
76  free(message);
77  RSA_free(rsa);
78  BIO_free(keybio);
79 
80  *em_size = result;
81  return em;
82 
83 error:
84  free(message);
85  free(em);
86  RSA_free(rsa);
87  BIO_free(keybio);
88  return NULL;
89 }
90