1 | /*
|
---|
2 | * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifndef OSSL_INTERNAL_PASSPHRASE_H
|
---|
11 | # define OSSL_INTERNAL_PASSPHRASE_H
|
---|
12 | # ifndef RT_WITHOUT_PRAGMA_ONCE /* VBOX */
|
---|
13 | # pragma once
|
---|
14 | # endif /* VBOX */
|
---|
15 |
|
---|
16 | /*
|
---|
17 | * This is a passphrase reader bridge with bells and whistles.
|
---|
18 | *
|
---|
19 | * On one hand, an API may wish to offer all sorts of passphrase callback
|
---|
20 | * possibilities to users, or may have to do so for historical reasons.
|
---|
21 | * On the other hand, that same API may have demands from other interfaces,
|
---|
22 | * notably from the libcrypto <-> provider interface, which uses
|
---|
23 | * OSSL_PASSPHRASE_CALLBACK consistently.
|
---|
24 | *
|
---|
25 | * The structure and functions below are the fundaments for bridging one
|
---|
26 | * passphrase callback form to another.
|
---|
27 | *
|
---|
28 | * In addition, extra features are included (this may be a growing list):
|
---|
29 | *
|
---|
30 | * - password caching. This is to be used by APIs where it's likely
|
---|
31 | * that the same passphrase may be asked for more than once, but the
|
---|
32 | * user shouldn't get prompted more than once. For example, this is
|
---|
33 | * useful for OSSL_DECODER, which may have to use a passphrase while
|
---|
34 | * trying to find out what input it has.
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Structure to hold whatever the calling user may specify. This structure
|
---|
39 | * is intended to be integrated into API specific structures or to be used
|
---|
40 | * as a local on-stack variable type. Therefore, no functions to allocate
|
---|
41 | * or freed it on the heap is offered.
|
---|
42 | */
|
---|
43 | struct ossl_passphrase_data_st {
|
---|
44 | enum {
|
---|
45 | is_expl_passphrase = 1, /* Explicit passphrase given by user */
|
---|
46 | is_pem_password, /* pem_password_cb given by user */
|
---|
47 | is_ossl_passphrase, /* OSSL_PASSPHRASE_CALLBACK given by user */
|
---|
48 | is_ui_method /* UI_METHOD given by user */
|
---|
49 | } type;
|
---|
50 | union {
|
---|
51 | struct {
|
---|
52 | char *passphrase_copy;
|
---|
53 | size_t passphrase_len;
|
---|
54 | } expl_passphrase;
|
---|
55 |
|
---|
56 | struct {
|
---|
57 | pem_password_cb *password_cb;
|
---|
58 | void *password_cbarg;
|
---|
59 | } pem_password;
|
---|
60 |
|
---|
61 | struct {
|
---|
62 | OSSL_PASSPHRASE_CALLBACK *passphrase_cb;
|
---|
63 | void *passphrase_cbarg;
|
---|
64 | } ossl_passphrase;
|
---|
65 |
|
---|
66 | struct {
|
---|
67 | const UI_METHOD *ui_method;
|
---|
68 | void *ui_method_data;
|
---|
69 | } ui_method;
|
---|
70 | } _;
|
---|
71 |
|
---|
72 | /*-
|
---|
73 | * Flags section
|
---|
74 | */
|
---|
75 |
|
---|
76 | /* Set to indicate that caching should be done */
|
---|
77 | unsigned int flag_cache_passphrase:1;
|
---|
78 |
|
---|
79 | /*-
|
---|
80 | * Misc section: caches and other
|
---|
81 | */
|
---|
82 |
|
---|
83 | char *cached_passphrase;
|
---|
84 | size_t cached_passphrase_len;
|
---|
85 | };
|
---|
86 |
|
---|
87 | /* Structure manipulation */
|
---|
88 |
|
---|
89 | void ossl_pw_clear_passphrase_data(struct ossl_passphrase_data_st *data);
|
---|
90 | void ossl_pw_clear_passphrase_cache(struct ossl_passphrase_data_st *data);
|
---|
91 |
|
---|
92 | int ossl_pw_set_passphrase(struct ossl_passphrase_data_st *data,
|
---|
93 | const unsigned char *passphrase,
|
---|
94 | size_t passphrase_len);
|
---|
95 | int ossl_pw_set_pem_password_cb(struct ossl_passphrase_data_st *data,
|
---|
96 | pem_password_cb *cb, void *cbarg);
|
---|
97 | int ossl_pw_set_ossl_passphrase_cb(struct ossl_passphrase_data_st *data,
|
---|
98 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg);
|
---|
99 | int ossl_pw_set_ui_method(struct ossl_passphrase_data_st *data,
|
---|
100 | const UI_METHOD *ui_method, void *ui_data);
|
---|
101 |
|
---|
102 | int ossl_pw_enable_passphrase_caching(struct ossl_passphrase_data_st *data);
|
---|
103 | int ossl_pw_disable_passphrase_caching(struct ossl_passphrase_data_st *data);
|
---|
104 |
|
---|
105 | /* Central function for direct calls */
|
---|
106 |
|
---|
107 | int ossl_pw_get_passphrase(char *pass, size_t pass_size, size_t *pass_len,
|
---|
108 | const OSSL_PARAM params[], int verify,
|
---|
109 | struct ossl_passphrase_data_st *data);
|
---|
110 |
|
---|
111 | /* Callback functions */
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * All of these callback expect that the callback argument is a
|
---|
115 | * struct ossl_passphrase_data_st
|
---|
116 | */
|
---|
117 |
|
---|
118 | pem_password_cb ossl_pw_pem_password;
|
---|
119 | pem_password_cb ossl_pw_pvk_password;
|
---|
120 | /* One callback for encoding (verification prompt) and one for decoding */
|
---|
121 | OSSL_PASSPHRASE_CALLBACK ossl_pw_passphrase_callback_enc;
|
---|
122 | OSSL_PASSPHRASE_CALLBACK ossl_pw_passphrase_callback_dec;
|
---|
123 |
|
---|
124 | #endif
|
---|