| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2020-04-23 20:55:52 +08:00
										 |  |  |  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							|  |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 05:23:39 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * DSA low level APIs are deprecated for public use, but still ok for | 
					
						
							|  |  |  |  * internal use. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include "internal/deprecated.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 07:21:19 +08:00
										 |  |  | #include <openssl/core_dispatch.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | #include <openssl/pem.h>
 | 
					
						
							|  |  |  | #include <openssl/dsa.h>
 | 
					
						
							|  |  |  | #include <openssl/types.h>
 | 
					
						
							|  |  |  | #include <openssl/params.h>
 | 
					
						
							|  |  |  | #include "prov/bio.h"
 | 
					
						
							|  |  |  | #include "prov/implementations.h"
 | 
					
						
							|  |  |  | #include "prov/providercommonerr.h"
 | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  | #include "prov/provider_ctx.h"
 | 
					
						
							| 
									
										
										
										
											2020-08-17 03:25:08 +08:00
										 |  |  | #include "encoder_local.h"
 | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 03:25:08 +08:00
										 |  |  | static OSSL_FUNC_encoder_newctx_fn dsa_param_newctx; | 
					
						
							|  |  |  | static OSSL_FUNC_encoder_freectx_fn dsa_param_freectx; | 
					
						
							|  |  |  | static OSSL_FUNC_encoder_encode_data_fn dsa_param_der_data; | 
					
						
							|  |  |  | static OSSL_FUNC_encoder_encode_object_fn dsa_param_der; | 
					
						
							|  |  |  | static OSSL_FUNC_encoder_encode_data_fn dsa_param_pem_data; | 
					
						
							|  |  |  | static OSSL_FUNC_encoder_encode_object_fn dsa_param_pem; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 03:25:08 +08:00
										 |  |  | static OSSL_FUNC_encoder_encode_data_fn dsa_param_print_data; | 
					
						
							|  |  |  | static OSSL_FUNC_encoder_encode_object_fn dsa_param_print; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Parameters : context */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * There's no specific implementation context, so we use the provider context | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void *dsa_param_newctx(void *provctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return provctx; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void dsa_param_freectx(void *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Public key : DER */ | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  | static int dsa_param_der_data(void *ctx, const OSSL_PARAM params[], | 
					
						
							|  |  |  |                               OSSL_CORE_BIO *out, | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-21 07:19:16 +08:00
										 |  |  |     OSSL_FUNC_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); | 
					
						
							|  |  |  |     OSSL_FUNC_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); | 
					
						
							|  |  |  |     OSSL_FUNC_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     int ok = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |     if (dsa_import != NULL) { | 
					
						
							|  |  |  |         DSA *dsa; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |         /* ctx == provctx */ | 
					
						
							|  |  |  |         if ((dsa = dsa_new(ctx)) != NULL | 
					
						
							| 
									
										
										
										
											2020-08-21 09:14:34 +08:00
										 |  |  |             && dsa_import(dsa, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, params) | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |             && dsa_param_der(ctx, dsa, out, cb, cbarg)) | 
					
						
							|  |  |  |             ok = 1; | 
					
						
							|  |  |  |         dsa_free(dsa); | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return ok; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  | static int dsa_param_der(void *ctx, void *dsa, OSSL_CORE_BIO *cout, | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  |     BIO *out = bio_new_from_core_bio(ctx, cout); | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (out == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = i2d_DSAparams_bio(out, dsa); | 
					
						
							|  |  |  |     BIO_free(out); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Public key : PEM */ | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  | static int dsa_param_pem_data(void *ctx, const OSSL_PARAM params[], | 
					
						
							|  |  |  |                               OSSL_CORE_BIO *out, | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-21 07:19:16 +08:00
										 |  |  |     OSSL_FUNC_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); | 
					
						
							|  |  |  |     OSSL_FUNC_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); | 
					
						
							|  |  |  |     OSSL_FUNC_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     int ok = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |     if (dsa_import != NULL) { | 
					
						
							|  |  |  |         DSA *dsa; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |         /* ctx == provctx */ | 
					
						
							|  |  |  |         if ((dsa = dsa_new(ctx)) != NULL | 
					
						
							| 
									
										
										
										
											2020-08-21 09:14:34 +08:00
										 |  |  |             && dsa_import(dsa, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, params) | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |             && dsa_param_pem(ctx, dsa, out, cb, cbarg)) | 
					
						
							|  |  |  |             ok = 1; | 
					
						
							|  |  |  |         dsa_free(dsa); | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return ok; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  | static int dsa_param_pem(void *ctx, void *dsa, OSSL_CORE_BIO *cout, | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  |     BIO *out = bio_new_from_core_bio(ctx, cout); | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (out == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = PEM_write_bio_DSAparams(out, dsa); | 
					
						
							|  |  |  |     BIO_free(out); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  | static int dsa_param_print_data(void *ctx, const OSSL_PARAM params[], | 
					
						
							|  |  |  |                                 OSSL_CORE_BIO *out, | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |                                 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-21 07:19:16 +08:00
										 |  |  |     OSSL_FUNC_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); | 
					
						
							|  |  |  |     OSSL_FUNC_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); | 
					
						
							|  |  |  |     OSSL_FUNC_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     int ok = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |     if (dsa_import != NULL) { | 
					
						
							|  |  |  |         DSA *dsa; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |         /* ctx == provctx */ | 
					
						
							|  |  |  |         if ((dsa = dsa_new(ctx)) != NULL | 
					
						
							| 
									
										
										
										
											2020-08-21 09:14:34 +08:00
										 |  |  |             && dsa_import(dsa, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, params) | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |             && dsa_param_print(ctx, dsa, out, cb, cbarg)) | 
					
						
							|  |  |  |             ok = 1; | 
					
						
							|  |  |  |         dsa_free(dsa); | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return ok; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  | static int dsa_param_print(void *ctx, void *dsa, OSSL_CORE_BIO *cout, | 
					
						
							| 
									
										
										
										
											2020-02-03 23:36:24 +08:00
										 |  |  |                            OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-06 19:29:57 +08:00
										 |  |  |     BIO *out = bio_new_from_core_bio(ctx, cout); | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (out == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = ossl_prov_print_dsa(out, dsa, dsa_print_params); | 
					
						
							|  |  |  |     BIO_free(out); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 03:25:08 +08:00
										 |  |  | const OSSL_DISPATCH dsa_param_der_encoder_functions[] = { | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dsa_param_newctx }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dsa_param_freectx }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dsa_param_der_data }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dsa_param_der }, | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     { 0, NULL } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 03:25:08 +08:00
										 |  |  | const OSSL_DISPATCH dsa_param_pem_encoder_functions[] = { | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dsa_param_newctx }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dsa_param_freectx }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dsa_param_pem_data }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dsa_param_pem }, | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |     { 0, NULL } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 03:25:08 +08:00
										 |  |  | const OSSL_DISPATCH dsa_param_text_encoder_functions[] = { | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dsa_param_newctx }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dsa_param_freectx }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dsa_param_print }, | 
					
						
							|  |  |  |     { OSSL_FUNC_ENCODER_ENCODE_DATA, | 
					
						
							| 
									
										
										
										
											2019-11-18 09:01:13 +08:00
										 |  |  |       (void (*)(void))dsa_param_print_data }, | 
					
						
							|  |  |  |     { 0, NULL } | 
					
						
							|  |  |  | }; |