1 /*
2  * Copyright (C) 2017 Red Hat, Inc.
3  *
4  * Authors: Daiki Ueno
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>
20  *
21  */
22 
23 #include "gnutls_int.h"
24 #include "errors.h"
25 #include <common.h>
26 #include <x509.h>
27 #include <x509_int.h>
28 
29 /**
30  * gnutls_x509_spki_init:
31  * @spki: A pointer to the type to be initialized
32  *
33  * This function will initialize a SubjectPublicKeyInfo structure used
34  * in PKIX. The structure is used to set additional parameters
35  * in the public key information field of a certificate.
36  *
37  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
38  *   negative error value.
39  *
40  * Since: 3.6.0
41  *
42  **/
43 int
gnutls_x509_spki_init(gnutls_x509_spki_t * spki)44 gnutls_x509_spki_init(gnutls_x509_spki_t *spki)
45 {
46 	gnutls_x509_spki_t tmp;
47 
48 	FAIL_IF_LIB_ERROR;
49 
50 	tmp =
51 	    gnutls_calloc(1, sizeof(gnutls_x509_spki_st));
52 
53 	if (!tmp)
54 		return GNUTLS_E_MEMORY_ERROR;
55 
56 	*spki = tmp;
57 
58 	return 0;		/* success */
59 }
60 
61 /**
62  * gnutls_x509_spki_deinit:
63  * @spki: the SubjectPublicKeyInfo structure
64  *
65  * This function will deinitialize a SubjectPublicKeyInfo structure.
66  *
67  * Since: 3.6.0
68  *
69  **/
70 void
gnutls_x509_spki_deinit(gnutls_x509_spki_t spki)71 gnutls_x509_spki_deinit(gnutls_x509_spki_t spki)
72 {
73 	gnutls_free(spki);
74 }
75 
76 /**
77  * gnutls_x509_spki_set_rsa_pss_params:
78  * @spki: the SubjectPublicKeyInfo structure
79  * @dig: a digest algorithm of type #gnutls_digest_algorithm_t
80  * @salt_size: the size of salt string
81  *
82  * This function will set the public key parameters for
83  * an RSA-PSS algorithm, in the SubjectPublicKeyInfo structure.
84  *
85  * Since: 3.6.0
86  *
87  **/
88 void
gnutls_x509_spki_set_rsa_pss_params(gnutls_x509_spki_t spki,gnutls_digest_algorithm_t dig,unsigned int salt_size)89 gnutls_x509_spki_set_rsa_pss_params(gnutls_x509_spki_t spki,
90 				    gnutls_digest_algorithm_t dig,
91 				    unsigned int salt_size)
92 {
93 	spki->pk = GNUTLS_PK_RSA_PSS;
94 	spki->rsa_pss_dig = dig;
95 	spki->salt_size = salt_size;
96 }
97 
98 /**
99  * gnutls_x509_spki_get_rsa_pss_params:
100  * @spki: the SubjectPublicKeyInfo structure
101  * @dig: if non-NULL, it will hold the digest algorithm
102  * @salt_size: if non-NULL, it will hold the salt size
103  *
104  * This function will get the public key algorithm parameters
105  * of RSA-PSS type.
106  *
107  * Returns: zero if the parameters are present or a negative
108  *     value on error.
109  *
110  * Since: 3.6.0
111  *
112  **/
113 int
gnutls_x509_spki_get_rsa_pss_params(gnutls_x509_spki_t spki,gnutls_digest_algorithm_t * dig,unsigned int * salt_size)114 gnutls_x509_spki_get_rsa_pss_params(gnutls_x509_spki_t spki,
115 				    gnutls_digest_algorithm_t *dig,
116 				    unsigned int *salt_size)
117 {
118 	if (spki->pk == 0)
119 		return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
120 
121 	if (spki->pk != GNUTLS_PK_RSA_PSS)
122 		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
123 
124 	if (dig)
125 		*dig = spki->rsa_pss_dig;
126 	if (salt_size)
127 		*salt_size = spki->salt_size;
128 
129 	return 0;
130 }
131