You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

81 lines
1.8 KiB

  1. /**
  2. * xrdp: A Remote Desktop Protocol server.
  3. *
  4. * Copyright (C) Koichiro Iwao 2017
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Base64 encoder / decoder
  19. */
  20. #if defined(HAVE_CONFIG_H)
  21. #include <config_ac.h>
  22. #endif
  23. #include "os_calls.h"
  24. #include <openssl/bio.h>
  25. #include <openssl/evp.h>
  26. size_t
  27. base64_decoded_bytes(const char *src)
  28. {
  29. size_t len;
  30. size_t padding;
  31. len = g_strlen(src);
  32. padding = 0;
  33. if (src[len - 1] == '=')
  34. {
  35. padding++;
  36. if (src[len - 2] == '=')
  37. {
  38. padding++;
  39. }
  40. }
  41. return len * 3 / 4 - padding;
  42. }
  43. /*****************************************************************************/
  44. char *
  45. base64_decode(char *dst, const char *src, size_t len)
  46. {
  47. BIO *b64;
  48. BIO *bio;
  49. char *b64str;
  50. size_t estimated_decoded_bytes;
  51. size_t decoded_bytes;
  52. b64str = g_strdup(src);
  53. estimated_decoded_bytes = base64_decoded_bytes(b64str);
  54. dst[estimated_decoded_bytes] = '\0';
  55. b64 = BIO_new(BIO_f_base64());
  56. bio = BIO_new_mem_buf(b64str, len);
  57. bio = BIO_push(b64, bio);
  58. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  59. decoded_bytes = BIO_read(bio , dst, len);
  60. BIO_free_all(bio);
  61. /* if input is corrupt, return empty string */
  62. if (estimated_decoded_bytes != decoded_bytes)
  63. {
  64. g_strncpy(dst, "", sizeof(""));
  65. }
  66. return dst;
  67. }