xref: /dflybsd-src/crypto/libressl/crypto/dsa/dsa_meth.c (revision 961e30ea7dc61d1112b778ea4981eac68129fb86)
1*de0e0e4dSAntonio Huete Jimenez /*	$OpenBSD: dsa_meth.c,v 1.5 2022/07/11 05:33:14 bcook Exp $	*/
272c33676SMaxim Ag /*
372c33676SMaxim Ag  * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
472c33676SMaxim Ag  *
572c33676SMaxim Ag  * Permission to use, copy, modify, and distribute this software for any
672c33676SMaxim Ag  * purpose with or without fee is hereby granted, provided that the above
772c33676SMaxim Ag  * copyright notice and this permission notice appear in all copies.
872c33676SMaxim Ag  *
972c33676SMaxim Ag  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1072c33676SMaxim Ag  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1172c33676SMaxim Ag  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1272c33676SMaxim Ag  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1372c33676SMaxim Ag  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1472c33676SMaxim Ag  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1572c33676SMaxim Ag  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1672c33676SMaxim Ag  */
1772c33676SMaxim Ag 
1872c33676SMaxim Ag #include <stdlib.h>
1972c33676SMaxim Ag #include <string.h>
2072c33676SMaxim Ag 
2172c33676SMaxim Ag #include <openssl/dsa.h>
2272c33676SMaxim Ag #include <openssl/err.h>
2372c33676SMaxim Ag 
24*de0e0e4dSAntonio Huete Jimenez #include "dsa_locl.h"
25*de0e0e4dSAntonio Huete Jimenez 
2672c33676SMaxim Ag DSA_METHOD *
DSA_meth_new(const char * name,int flags)2772c33676SMaxim Ag DSA_meth_new(const char *name, int flags)
2872c33676SMaxim Ag {
2972c33676SMaxim Ag 	DSA_METHOD *meth;
3072c33676SMaxim Ag 
3172c33676SMaxim Ag 	if ((meth = calloc(1, sizeof(*meth))) == NULL)
3272c33676SMaxim Ag 		return NULL;
3372c33676SMaxim Ag 	if ((meth->name = strdup(name)) == NULL) {
3472c33676SMaxim Ag 		free(meth);
3572c33676SMaxim Ag 		return NULL;
3672c33676SMaxim Ag 	}
3772c33676SMaxim Ag 	meth->flags = flags;
3872c33676SMaxim Ag 
3972c33676SMaxim Ag 	return meth;
4072c33676SMaxim Ag }
4172c33676SMaxim Ag 
4272c33676SMaxim Ag void
DSA_meth_free(DSA_METHOD * meth)4372c33676SMaxim Ag DSA_meth_free(DSA_METHOD *meth)
4472c33676SMaxim Ag {
45*de0e0e4dSAntonio Huete Jimenez 	if (meth == NULL)
46*de0e0e4dSAntonio Huete Jimenez 		return;
47*de0e0e4dSAntonio Huete Jimenez 
48*de0e0e4dSAntonio Huete Jimenez 	free(meth->name);
4972c33676SMaxim Ag 	free(meth);
5072c33676SMaxim Ag }
5172c33676SMaxim Ag 
5272c33676SMaxim Ag DSA_METHOD *
DSA_meth_dup(const DSA_METHOD * meth)5372c33676SMaxim Ag DSA_meth_dup(const DSA_METHOD *meth)
5472c33676SMaxim Ag {
5572c33676SMaxim Ag 	DSA_METHOD *copy;
5672c33676SMaxim Ag 
5772c33676SMaxim Ag 	if ((copy = calloc(1, sizeof(*copy))) == NULL)
5872c33676SMaxim Ag 		return NULL;
5972c33676SMaxim Ag 	memcpy(copy, meth, sizeof(*copy));
6072c33676SMaxim Ag 	if ((copy->name = strdup(meth->name)) == NULL) {
6172c33676SMaxim Ag 		free(copy);
6272c33676SMaxim Ag 		return NULL;
6372c33676SMaxim Ag 	}
6472c33676SMaxim Ag 
6572c33676SMaxim Ag 	return copy;
6672c33676SMaxim Ag }
6772c33676SMaxim Ag 
68*de0e0e4dSAntonio Huete Jimenez const char *
DSA_meth_get0_name(const DSA_METHOD * meth)69*de0e0e4dSAntonio Huete Jimenez DSA_meth_get0_name(const DSA_METHOD *meth)
70*de0e0e4dSAntonio Huete Jimenez {
71*de0e0e4dSAntonio Huete Jimenez 	return meth->name;
72*de0e0e4dSAntonio Huete Jimenez }
73*de0e0e4dSAntonio Huete Jimenez 
74*de0e0e4dSAntonio Huete Jimenez int
DSA_meth_set1_name(DSA_METHOD * meth,const char * name)75*de0e0e4dSAntonio Huete Jimenez DSA_meth_set1_name(DSA_METHOD *meth, const char *name)
76*de0e0e4dSAntonio Huete Jimenez {
77*de0e0e4dSAntonio Huete Jimenez 	char *new_name;
78*de0e0e4dSAntonio Huete Jimenez 
79*de0e0e4dSAntonio Huete Jimenez 	if ((new_name = strdup(name)) == NULL) {
80*de0e0e4dSAntonio Huete Jimenez 		DSAerror(ERR_R_MALLOC_FAILURE);
81*de0e0e4dSAntonio Huete Jimenez 		return 0;
82*de0e0e4dSAntonio Huete Jimenez 	}
83*de0e0e4dSAntonio Huete Jimenez 
84*de0e0e4dSAntonio Huete Jimenez 	free(meth->name);
85*de0e0e4dSAntonio Huete Jimenez 	meth->name = new_name;
86*de0e0e4dSAntonio Huete Jimenez 
87*de0e0e4dSAntonio Huete Jimenez 	return 1;
88*de0e0e4dSAntonio Huete Jimenez }
89*de0e0e4dSAntonio Huete Jimenez 
9072c33676SMaxim Ag int
DSA_meth_set_sign(DSA_METHOD * meth,DSA_SIG * (* sign)(const unsigned char *,int,DSA *))9172c33676SMaxim Ag DSA_meth_set_sign(DSA_METHOD *meth,
9272c33676SMaxim Ag     DSA_SIG *(*sign)(const unsigned char *, int, DSA *))
9372c33676SMaxim Ag {
9472c33676SMaxim Ag 	meth->dsa_do_sign = sign;
9572c33676SMaxim Ag 	return 1;
9672c33676SMaxim Ag }
9772c33676SMaxim Ag 
9872c33676SMaxim Ag int
DSA_meth_set_finish(DSA_METHOD * meth,int (* finish)(DSA *))9972c33676SMaxim Ag DSA_meth_set_finish(DSA_METHOD *meth, int (*finish)(DSA *))
10072c33676SMaxim Ag {
10172c33676SMaxim Ag 	meth->finish = finish;
10272c33676SMaxim Ag 	return 1;
10372c33676SMaxim Ag }
104