1 /*	$OpenBSD: rsa_meth.c,v 1.2 2018/09/12 06:35:38 djm Exp $	*/
2 /*
3  * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include <openssl/err.h>
22 #include <openssl/rsa.h>
23 
24 RSA_METHOD *
25 RSA_meth_new(const char *name, int flags)
26 {
27 	RSA_METHOD *meth;
28 
29 	if ((meth = calloc(1, sizeof(*meth))) == NULL)
30 		return NULL;
31 	if ((meth->name = strdup(name)) == NULL) {
32 		free(meth);
33 		return NULL;
34 	}
35 	meth->flags = flags;
36 
37 	return meth;
38 }
39 
40 void
41 RSA_meth_free(RSA_METHOD *meth)
42 {
43 	if (meth != NULL) {
44 		free((char *)meth->name);
45 		free(meth);
46 	}
47 }
48 
49 RSA_METHOD *
50 RSA_meth_dup(const RSA_METHOD *meth)
51 {
52 	RSA_METHOD *copy;
53 
54 	if ((copy = calloc(1, sizeof(*copy))) == NULL)
55 		return NULL;
56 	memcpy(copy, meth, sizeof(*copy));
57 	if ((copy->name = strdup(meth->name)) == NULL) {
58 		free(copy);
59 		return NULL;
60 	}
61 
62 	return copy;
63 }
64 
65 int
66 RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
67 {
68 	char *copy;
69 
70 	if ((copy = strdup(name)) == NULL)
71 		return 0;
72 	free((char *)meth->name);
73 	meth->name = copy;
74 	return 1;
75 }
76 
77 int
78 (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa)
79 {
80 	return meth->finish;
81 }
82 
83 int
84 RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
85     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
86 {
87 	meth->rsa_priv_enc = priv_enc;
88 	return 1;
89 }
90 
91 int
92 RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
93     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
94 {
95 	meth->rsa_priv_dec = priv_dec;
96 	return 1;
97 }
98 
99 int
100 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa))
101 {
102 	meth->finish = finish;
103 	return 1;
104 }
105