callbacks.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. INCLUDES = """
  6. #include <string.h>
  7. """
  8. TYPES = """
  9. typedef struct {
  10. char *password;
  11. int length;
  12. int called;
  13. int error;
  14. int maxsize;
  15. } CRYPTOGRAPHY_PASSWORD_DATA;
  16. """
  17. FUNCTIONS = """
  18. int Cryptography_pem_password_cb(char *, int, int, void *);
  19. """
  20. CUSTOMIZATIONS = """
  21. typedef struct {
  22. char *password;
  23. int length;
  24. int called;
  25. int error;
  26. int maxsize;
  27. } CRYPTOGRAPHY_PASSWORD_DATA;
  28. int Cryptography_pem_password_cb(char *buf, int size,
  29. int rwflag, void *userdata) {
  30. /* The password cb is only invoked if OpenSSL decides the private
  31. key is encrypted. So this path only occurs if it needs a password */
  32. CRYPTOGRAPHY_PASSWORD_DATA *st = (CRYPTOGRAPHY_PASSWORD_DATA *)userdata;
  33. st->called += 1;
  34. st->maxsize = size;
  35. if (st->length == 0) {
  36. st->error = -1;
  37. return 0;
  38. } else if (st->length < size) {
  39. memcpy(buf, st->password, st->length);
  40. return st->length;
  41. } else {
  42. st->error = -2;
  43. return 0;
  44. }
  45. }
  46. """