| 1 | /* |
| 2 | * Copyright 2019 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 | #include <openssl/core_numbers.h> |
| 11 | #include <openssl/pem.h> |
| 12 | #include <openssl/dh.h> |
| 13 | #include <openssl/types.h> |
| 14 | #include <openssl/params.h> |
| 15 | #include "prov/bio.h" |
| 16 | #include "prov/implementations.h" |
| 17 | #include "prov/providercommonerr.h" |
| 18 | #include "serializer_local.h" |
| 19 | |
| 20 | static OSSL_OP_serializer_newctx_fn dh_param_newctx; |
| 21 | static OSSL_OP_serializer_freectx_fn dh_param_freectx; |
| 22 | static OSSL_OP_serializer_serialize_data_fn dh_param_der_data; |
| 23 | static OSSL_OP_serializer_serialize_object_fn dh_param_der; |
| 24 | static OSSL_OP_serializer_serialize_data_fn dh_param_pem_data; |
| 25 | static OSSL_OP_serializer_serialize_object_fn dh_param_pem; |
| 26 | |
| 27 | static OSSL_OP_serializer_serialize_data_fn dh_param_print_data; |
| 28 | static OSSL_OP_serializer_serialize_object_fn dh_param_print; |
| 29 | |
| 30 | /* Parameters : context */ |
| 31 | |
| 32 | /* |
| 33 | * There's no specific implementation context, so we use the provider context |
| 34 | */ |
| 35 | static void *dh_param_newctx(void *provctx) |
| 36 | { |
| 37 | return provctx; |
| 38 | } |
| 39 | |
| 40 | static void dh_param_freectx(void *ctx) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | /* Public key : DER */ |
| 45 | static int dh_param_der_data(void *ctx, const OSSL_PARAM params[], BIO *out, |
| 46 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
| 47 | { |
| 48 | OSSL_OP_keymgmt_importkey_fn *dh_importkey = |
| 49 | ossl_prov_get_dh_importkey(); |
| 50 | int ok = 0; |
| 51 | |
| 52 | if (dh_importkey != NULL) { |
| 53 | DH *dh = dh_importkey(ctx, params); /* ctx == provctx */ |
| 54 | |
| 55 | ok = dh_param_der(ctx, dh, out, cb, cbarg); |
| 56 | DH_free(dh); |
| 57 | } |
| 58 | return ok; |
| 59 | } |
| 60 | |
| 61 | static int dh_param_der(void *ctx, void *dh, BIO *out, |
| 62 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
| 63 | { |
| 64 | return i2d_DHparams_bio(out, dh); |
| 65 | } |
| 66 | |
| 67 | /* Public key : PEM */ |
| 68 | static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out, |
| 69 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
| 70 | { |
| 71 | OSSL_OP_keymgmt_importkey_fn *dh_importkey = |
| 72 | ossl_prov_get_dh_importkey(); |
| 73 | int ok = 0; |
| 74 | |
| 75 | if (dh_importkey != NULL) { |
| 76 | DH *dh = dh_importkey(ctx, params); /* ctx == provctx */ |
| 77 | |
| 78 | ok = dh_param_pem(ctx, dh, out, cb, cbarg); |
| 79 | DH_free(dh); |
| 80 | } |
| 81 | return ok; |
| 82 | } |
| 83 | |
| 84 | static int dh_param_pem(void *ctx, void *dh, BIO *out, |
| 85 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
| 86 | { |
| 87 | return PEM_write_bio_DHparams(out, dh); |
| 88 | } |
| 89 | |
| 90 | static int dh_param_print_data(void *ctx, const OSSL_PARAM params[], BIO *out, |
| 91 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
| 92 | { |
| 93 | OSSL_OP_keymgmt_importkey_fn *dh_importkey = |
| 94 | ossl_prov_get_dh_importkey(); |
| 95 | int ok = 0; |
| 96 | |
| 97 | if (dh_importkey != NULL) { |
| 98 | DH *dh = dh_importkey(ctx, params); /* ctx == provctx */ |
| 99 | |
| 100 | ok = dh_param_print(ctx, dh, out, cb, cbarg); |
| 101 | DH_free(dh); |
| 102 | } |
| 103 | return ok; |
| 104 | } |
| 105 | |
| 106 | static int dh_param_print(void *ctx, void *dh, BIO *out, |
| 107 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
| 108 | { |
| 109 | return ossl_prov_print_dh(out, dh, dh_print_params); |
| 110 | } |
| 111 | |
| 112 | const OSSL_DISPATCH dh_param_der_serializer_functions[] = { |
| 113 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx }, |
| 114 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx }, |
| 115 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_der_data }, |
| 116 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_der }, |
| 117 | { 0, NULL } |
| 118 | }; |
| 119 | |
| 120 | const OSSL_DISPATCH dh_param_pem_serializer_functions[] = { |
| 121 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx }, |
| 122 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx }, |
| 123 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_pem_data }, |
| 124 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_pem }, |
| 125 | { 0, NULL } |
| 126 | }; |
| 127 | |
| 128 | const OSSL_DISPATCH dh_param_text_serializer_functions[] = { |
| 129 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx }, |
| 130 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx }, |
| 131 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_print }, |
| 132 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, |
| 133 | (void (*)(void))dh_param_print_data }, |
| 134 | { 0, NULL } |
| 135 | }; |
| 136 | |