]>
Commit | Line | Data |
---|---|---|
f864a939 | 1 | /* |
0f84cbc3 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
f864a939 RL |
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 | /* | |
11 | * TODO(v3.0): the IMPLEMENT macros in include/openssl/pem.h should be | |
12 | * moved here. | |
13 | */ | |
14 | ||
15 | #include <openssl/pem.h> | |
ece9304c | 16 | #include <openssl/encoder.h> |
f864a939 | 17 | |
ece9304c | 18 | /* Alternative IMPLEMENT macros for provided encoders */ |
f864a939 RL |
19 | |
20 | # define IMPLEMENT_PEM_provided_write_body_vars(type, asn1) \ | |
21 | int ret = 0; \ | |
ece9304c RL |
22 | const char *pq = OSSL_ENCODER_##asn1##_TO_PEM_PQ; \ |
23 | OSSL_ENCODER_CTX *ctx = OSSL_ENCODER_CTX_new_by_##type(x, pq); \ | |
f864a939 | 24 | \ |
ece9304c RL |
25 | if (ctx != NULL && OSSL_ENCODER_CTX_get_encoder(ctx) == NULL) { \ |
26 | OSSL_ENCODER_CTX_free(ctx); \ | |
f864a939 RL |
27 | goto legacy; \ |
28 | } | |
29 | # define IMPLEMENT_PEM_provided_write_body_pass() \ | |
30 | ret = 1; \ | |
31 | if (kstr == NULL && cb == NULL) { \ | |
32 | if (u != NULL) { \ | |
33 | kstr = u; \ | |
34 | klen = strlen(u); \ | |
35 | } else { \ | |
36 | cb = PEM_def_callback; \ | |
37 | } \ | |
38 | } \ | |
39 | if (enc != NULL) { \ | |
40 | ret = 0; \ | |
ece9304c RL |
41 | if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), \ |
42 | NULL)) { \ | |
f864a939 RL |
43 | ret = 1; \ |
44 | if (kstr != NULL \ | |
ece9304c | 45 | && !OSSL_ENCODER_CTX_set_passphrase(ctx, kstr, klen)) \ |
f864a939 RL |
46 | ret = 0; \ |
47 | else if (cb != NULL \ | |
ece9304c RL |
48 | && !OSSL_ENCODER_CTX_set_passphrase_cb(ctx, \ |
49 | cb, u)) \ | |
f864a939 RL |
50 | ret = 0; \ |
51 | } \ | |
52 | } \ | |
53 | if (!ret) { \ | |
ece9304c | 54 | OSSL_ENCODER_CTX_free(ctx); \ |
f864a939 RL |
55 | return 0; \ |
56 | } | |
57 | # define IMPLEMENT_PEM_provided_write_body_main(type, outtype) \ | |
ece9304c RL |
58 | ret = OSSL_ENCODER_to_##outtype(ctx, out); \ |
59 | OSSL_ENCODER_CTX_free(ctx); \ | |
f864a939 RL |
60 | return ret |
61 | # define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \ | |
62 | writename) \ | |
63 | legacy: \ | |
64 | return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \ | |
ece9304c | 65 | x, NULL, NULL, 0, NULL, NULL) |
f864a939 RL |
66 | # define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \ |
67 | writename) \ | |
68 | legacy: \ | |
69 | return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \ | |
70 | x, enc, kstr, klen, cb, u) | |
71 | ||
72 | # define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, \ | |
73 | OUTTYPE, outtype, writename) \ | |
74 | PEM_write_fnsig(name, type, OUTTYPE, writename) \ | |
75 | { \ | |
76 | IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \ | |
77 | IMPLEMENT_PEM_provided_write_body_main(type, outtype); \ | |
78 | IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \ | |
79 | writename); \ | |
80 | } | |
81 | ||
82 | ||
83 | # define IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, \ | |
84 | OUTTYPE, outtype, writename) \ | |
85 | PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \ | |
86 | { \ | |
87 | IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \ | |
88 | IMPLEMENT_PEM_provided_write_body_pass(); \ | |
89 | IMPLEMENT_PEM_provided_write_body_main(type, outtype); \ | |
90 | IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \ | |
91 | writename); \ | |
92 | } | |
93 | ||
94 | # ifdef OPENSSL_NO_STDIO | |
95 | ||
96 | # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) | |
97 | # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) | |
98 | ||
99 | # else | |
100 | ||
101 | # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) \ | |
102 | IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, FILE, fp, write) | |
103 | # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) \ | |
104 | IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, FILE, fp, write) | |
105 | ||
106 | # endif | |
107 | ||
108 | # define IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \ | |
109 | IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, BIO, bio, write_bio) | |
110 | # define IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \ | |
111 | IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, BIO, bio, write_bio) | |
112 | ||
113 | # define IMPLEMENT_PEM_provided_write(name, type, str, asn1) \ | |
114 | IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \ | |
115 | IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) | |
116 | ||
ece9304c RL |
117 | # define IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1) \ |
118 | IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \ | |
f864a939 RL |
119 | IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) |
120 | ||
ece9304c | 121 | # define IMPLEMENT_PEM_provided_rw(name, type, str, asn1) \ |
f864a939 RL |
122 | IMPLEMENT_PEM_read(name, type, str, asn1) \ |
123 | IMPLEMENT_PEM_provided_write(name, type, str, asn1) | |
124 | ||
ece9304c | 125 | # define IMPLEMENT_PEM_provided_rw_cb(name, type, str, asn1) \ |
f864a939 RL |
126 | IMPLEMENT_PEM_read(name, type, str, asn1) \ |
127 | IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1) | |
128 |