| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 |
| 19 | #include <openssl/dh.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 |
| 23 | #include "internal.h" |
| 24 |
| 25 |
| 26 | namespace { |
| 27 | typedef struct dh_pkey_ctx_st { |
| 28 | int pad; |
| 29 | } DH_PKEY_CTX; |
| 30 | } // namespace |
| 31 |
| 32 | static int pkey_dh_init(EVP_PKEY_CTX *ctx) { |
| 33 | DH_PKEY_CTX *dctx = |
| 34 | reinterpret_cast<DH_PKEY_CTX *>(OPENSSL_zalloc(sizeof(DH_PKEY_CTX))); |
| 35 | if (dctx == NULL) { |
| 36 | return 0; |
| 37 | } |
| 38 |
| 39 | ctx->data = dctx; |
| 40 | return 1; |
| 41 | } |
| 42 |
| 43 | static int pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { |
| 44 | if (!pkey_dh_init(dst)) { |
| 45 | return 0; |
| 46 | } |
| 47 |
| 48 | const DH_PKEY_CTX *sctx = reinterpret_cast<DH_PKEY_CTX *>(src->data); |
| 49 | DH_PKEY_CTX *dctx = reinterpret_cast<DH_PKEY_CTX *>(dst->data); |
| 50 | dctx->pad = sctx->pad; |
| 51 | return 1; |
| 52 | } |
| 53 |
| 54 | static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx) { |
| 55 | OPENSSL_free(ctx->data); |
| 56 | ctx->data = NULL; |
| 57 | } |
| 58 |
| 59 | static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { |
| 60 | DH *dh = DH_new(); |
| 61 | if (dh == NULL || !EVP_PKEY_assign_DH(pkey, dh)) { |
| 62 | DH_free(dh); |
| 63 | return 0; |
| 64 | } |
| 65 |
| 66 | if (ctx->pkey != NULL && !EVP_PKEY_copy_parameters(pkey, ctx->pkey)) { |
| 67 | return 0; |
| 68 | } |
| 69 |
| 70 | return DH_generate_key(dh); |
| 71 | } |
| 72 |
| 73 | static int pkey_dh_derive(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len) { |
| 74 | DH_PKEY_CTX *dctx = reinterpret_cast<DH_PKEY_CTX *>(ctx->data); |
| 75 | if (ctx->pkey == NULL || ctx->peerkey == NULL) { |
| 76 | OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); |
| 77 | return 0; |
| 78 | } |
| 79 |
| 80 | DH *our_key = reinterpret_cast<DH *>(ctx->pkey->pkey); |
| 81 | DH *peer_key = reinterpret_cast<DH *>(ctx->peerkey->pkey); |
| 82 | if (our_key == NULL || peer_key == NULL) { |
| 83 | OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); |
| 84 | return 0; |
| 85 | } |
| 86 |
| 87 | const BIGNUM *pub_key = DH_get0_pub_key(peer_key); |
| 88 | if (pub_key == NULL) { |
| 89 | OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); |
| 90 | return 0; |
| 91 | } |
| 92 |
| 93 | if (out == NULL) { |
| 94 | *out_len = DH_size(our_key); |
| 95 | return 1; |
| 96 | } |
| 97 |
| 98 | if (*out_len < (size_t)DH_size(our_key)) { |
| 99 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 100 | return 0; |
| 101 | } |
| 102 |
| 103 | int ret = dctx->pad ? DH_compute_key_padded(out, pub_key, our_key) |
| 104 | : DH_compute_key(out, pub_key, our_key); |
| 105 | if (ret < 0) { |
| 106 | return 0; |
| 107 | } |
| 108 |
| 109 | assert(ret <= DH_size(our_key)); |
| 110 | *out_len = (size_t)ret; |
| 111 | return 1; |
| 112 | } |
| 113 |
| 114 | static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { |
| 115 | DH_PKEY_CTX *dctx = reinterpret_cast<DH_PKEY_CTX *>(ctx->data); |
| 116 | switch (type) { |
| 117 | case EVP_PKEY_CTRL_PEER_KEY: |
| 118 | // |EVP_PKEY_derive_set_peer| requires the key implement this command, |
| 119 | // even if it is a no-op. |
| 120 | return 1; |
| 121 |
| 122 | case EVP_PKEY_CTRL_DH_PAD: |
| 123 | dctx->pad = p1; |
| 124 | return 1; |
| 125 |
| 126 | default: |
| 127 | OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 |
| 132 | const EVP_PKEY_METHOD dh_pkey_meth = { |
| 133 | /*pkey_id=*/EVP_PKEY_DH, |
| 134 | /*init=*/pkey_dh_init, |
| 135 | /*copy=*/pkey_dh_copy, |
| 136 | /*cleanup=*/pkey_dh_cleanup, |
| 137 | /*keygen=*/pkey_dh_keygen, |
| 138 | /*sign=*/nullptr, |
| 139 | /*sign_message=*/nullptr, |
| 140 | /*verify=*/nullptr, |
| 141 | /*verify_message=*/nullptr, |
| 142 | /*verify_recover=*/nullptr, |
| 143 | /*encrypt=*/nullptr, |
| 144 | /*decrypt=*/nullptr, |
| 145 | /*derive=*/pkey_dh_derive, |
| 146 | /*paramgen=*/nullptr, |
| 147 | /*ctrl=*/pkey_dh_ctrl, |
| 148 | }; |
| 149 |
| 150 | int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad) { |
| 151 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, |
| 152 | EVP_PKEY_CTRL_DH_PAD, pad, NULL); |
| 153 | } |
| 154 |