• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5from __future__ import absolute_import, division, print_function
6
7INCLUDES = """
8#include <string.h>
9"""
10
11TYPES = """
12typedef struct {
13    char *password;
14    int length;
15    int called;
16    int error;
17    int maxsize;
18} CRYPTOGRAPHY_PASSWORD_DATA;
19"""
20
21FUNCTIONS = """
22int Cryptography_pem_password_cb(char *, int, int, void *);
23"""
24
25CUSTOMIZATIONS = """
26typedef struct {
27    char *password;
28    int length;
29    int called;
30    int error;
31    int maxsize;
32} CRYPTOGRAPHY_PASSWORD_DATA;
33
34int Cryptography_pem_password_cb(char *buf, int size,
35                                  int rwflag, void *userdata) {
36    /* The password cb is only invoked if OpenSSL decides the private
37       key is encrypted. So this path only occurs if it needs a password */
38    CRYPTOGRAPHY_PASSWORD_DATA *st = (CRYPTOGRAPHY_PASSWORD_DATA *)userdata;
39    st->called += 1;
40    st->maxsize = size;
41    if (st->length == 0) {
42        st->error = -1;
43        return 0;
44    } else if (st->length < size) {
45        memcpy(buf, st->password, st->length);
46        return st->length;
47    } else {
48        st->error = -2;
49        return 0;
50    }
51}
52"""
53