Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example of using password callback #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ def get_method(index):
)[index]


# Callback for wolfssl/certs/client-keyEnc.pem (yassl123 password)
# sz is the max size of password allowed
# rw is type i.e PEM_PASS_READ
# userdata is a void pointer potentially set by user
def password_cb(sz, rw, userdata):
return "yassl123"


def main():
args = build_arg_parser().parse_args()

Expand All @@ -110,6 +118,7 @@ def main():

context = wolfssl.SSLContext(get_method(args.v))

context.set_passwd_cb(password_cb)
context.load_cert_chain(args.c, args.k)

if args.d:
Expand Down
8 changes: 8 additions & 0 deletions src/wolfssl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,14 @@ def callback(self):
def _get_passwd(self, passwd, sz, rw, userdata):
try:
result = self._passwd_wrapper(sz, rw, userdata)

# if the result returned is a string then try to convert it
try:
if isinstance(result, str):
result = str.encode(result)
except Exception as e:
pass

if not isinstance(result, bytes):
raise ValueError("Problem, expected String, not bytes")
if len(result) > sz:
Expand Down