/src/gnutls/lib/x509/spki.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  | 0  | { | 
45  | 0  |   gnutls_x509_spki_t tmp;  | 
46  |  | 
  | 
47  | 0  |   *spki = NULL;  | 
48  | 0  |   FAIL_IF_LIB_ERROR;  | 
49  |  |  | 
50  | 0  |   tmp = gnutls_calloc(1, sizeof(gnutls_x509_spki_st));  | 
51  |  | 
  | 
52  | 0  |   if (!tmp)  | 
53  | 0  |     return GNUTLS_E_MEMORY_ERROR;  | 
54  |  |  | 
55  | 0  |   *spki = tmp;  | 
56  |  | 
  | 
57  | 0  |   return 0; /* success */  | 
58  | 0  | }  | 
59  |  |  | 
60  |  | /**  | 
61  |  |  * gnutls_x509_spki_deinit:  | 
62  |  |  * @spki: the SubjectPublicKeyInfo structure  | 
63  |  |  *  | 
64  |  |  * This function will deinitialize a SubjectPublicKeyInfo structure.  | 
65  |  |  *  | 
66  |  |  * Since: 3.6.0  | 
67  |  |  *  | 
68  |  |  **/  | 
69  |  | void gnutls_x509_spki_deinit(gnutls_x509_spki_t spki)  | 
70  | 0  | { | 
71  | 0  |   _gnutls_x509_spki_clear(spki);  | 
72  | 0  |   gnutls_free(spki);  | 
73  | 0  | }  | 
74  |  |  | 
75  |  | int _gnutls_x509_spki_copy(gnutls_x509_spki_st *dst,  | 
76  |  |          const gnutls_x509_spki_st *src)  | 
77  | 0  | { | 
78  | 0  |   memcpy(dst, src, sizeof(*src));  | 
79  | 0  |   return _gnutls_set_datum(&dst->rsa_oaep_label, src->rsa_oaep_label.data,  | 
80  | 0  |          src->rsa_oaep_label.size);  | 
81  | 0  | }  | 
82  |  |  | 
83  |  | void _gnutls_x509_spki_clear(gnutls_x509_spki_st *spki)  | 
84  | 0  | { | 
85  | 0  |   gnutls_free(spki->rsa_oaep_label.data);  | 
86  | 0  |   memset(spki, 0, sizeof(*spki));  | 
87  | 0  | }  | 
88  |  |  | 
89  |  | /**  | 
90  |  |  * gnutls_x509_spki_set_rsa_pss_params:  | 
91  |  |  * @spki: the SubjectPublicKeyInfo structure  | 
92  |  |  * @dig: a digest algorithm of type #gnutls_digest_algorithm_t  | 
93  |  |  * @salt_size: the size of salt string  | 
94  |  |  *  | 
95  |  |  * This function will set the public key parameters for  | 
96  |  |  * an RSA-PSS algorithm, in the SubjectPublicKeyInfo structure.  | 
97  |  |  *  | 
98  |  |  * Since: 3.6.0  | 
99  |  |  *  | 
100  |  |  **/  | 
101  |  | void gnutls_x509_spki_set_rsa_pss_params(gnutls_x509_spki_t spki,  | 
102  |  |            gnutls_digest_algorithm_t dig,  | 
103  |  |            unsigned int salt_size)  | 
104  | 0  | { | 
105  | 0  |   spki->pk = GNUTLS_PK_RSA_PSS;  | 
106  | 0  |   spki->rsa_pss_dig = dig;  | 
107  | 0  |   spki->salt_size = salt_size;  | 
108  | 0  | }  | 
109  |  |  | 
110  |  | /**  | 
111  |  |  * gnutls_x509_spki_get_rsa_pss_params:  | 
112  |  |  * @spki: the SubjectPublicKeyInfo structure  | 
113  |  |  * @dig: if non-NULL, it will hold the digest algorithm  | 
114  |  |  * @salt_size: if non-NULL, it will hold the salt size  | 
115  |  |  *  | 
116  |  |  * This function will get the public key algorithm parameters  | 
117  |  |  * of RSA-PSS type.  | 
118  |  |  *  | 
119  |  |  * Returns: zero if the parameters are present or a negative  | 
120  |  |  *     value on error.  | 
121  |  |  *  | 
122  |  |  * Since: 3.6.0  | 
123  |  |  *  | 
124  |  |  **/  | 
125  |  | int gnutls_x509_spki_get_rsa_pss_params(gnutls_x509_spki_t spki,  | 
126  |  |           gnutls_digest_algorithm_t *dig,  | 
127  |  |           unsigned int *salt_size)  | 
128  | 0  | { | 
129  | 0  |   if (spki->pk == 0)  | 
130  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
131  |  |  | 
132  | 0  |   if (spki->pk != GNUTLS_PK_RSA_PSS)  | 
133  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
134  |  |  | 
135  | 0  |   if (dig)  | 
136  | 0  |     *dig = spki->rsa_pss_dig;  | 
137  | 0  |   if (salt_size)  | 
138  | 0  |     *salt_size = spki->salt_size;  | 
139  |  | 
  | 
140  | 0  |   return 0;  | 
141  | 0  | }  | 
142  |  |  | 
143  |  | /**  | 
144  |  |  * gnutls_x509_spki_set_rsa_oaep_params:  | 
145  |  |  * @spki: the SubjectPublicKeyInfo structure  | 
146  |  |  * @dig: a digest algorithm of type #gnutls_digest_algorithm_t  | 
147  |  |  * @label: optional label  | 
148  |  |  *  | 
149  |  |  * This function will set the public key parameters for  | 
150  |  |  * an RSA-OAEP algorithm, in the SubjectPublicKeyInfo structure.  | 
151  |  |  *  | 
152  |  |  * Returns: zero if the parameters are present or a negative  | 
153  |  |  *     value on error.  | 
154  |  |  *  | 
155  |  |  * Since: 3.8.4  | 
156  |  |  *  | 
157  |  |  **/  | 
158  |  | int gnutls_x509_spki_set_rsa_oaep_params(gnutls_x509_spki_t spki,  | 
159  |  |            gnutls_digest_algorithm_t dig,  | 
160  |  |            const gnutls_datum_t *label)  | 
161  | 0  | { | 
162  | 0  |   spki->pk = GNUTLS_PK_RSA_OAEP;  | 
163  | 0  |   spki->rsa_oaep_dig = dig;  | 
164  | 0  |   if (label) { | 
165  | 0  |     int ret;  | 
166  |  | 
  | 
167  | 0  |     ret = _gnutls_set_datum(&spki->rsa_oaep_label, label->data,  | 
168  | 0  |           label->size);  | 
169  | 0  |     if (ret < 0)  | 
170  | 0  |       return gnutls_assert_val(ret);  | 
171  | 0  |   }  | 
172  | 0  |   return 0;  | 
173  | 0  | }  | 
174  |  |  | 
175  |  | /**  | 
176  |  |  * gnutls_x509_spki_get_rsa_oaep_params:  | 
177  |  |  * @spki: the SubjectPublicKeyInfo structure  | 
178  |  |  * @dig: if non-NULL, it will hold the digest algorithm  | 
179  |  |  * @label: if non-NULL, it will hold the pointer to label  | 
180  |  |  *  | 
181  |  |  * This function will get the public key algorithm parameters  | 
182  |  |  * of RSA-OAEP type.  | 
183  |  |  *  | 
184  |  |  * Returns: zero if the parameters are present or a negative  | 
185  |  |  *     value on error.  | 
186  |  |  *  | 
187  |  |  * Since: 3.8.4  | 
188  |  |  *  | 
189  |  |  **/  | 
190  |  | int gnutls_x509_spki_get_rsa_oaep_params(gnutls_x509_spki_t spki,  | 
191  |  |            gnutls_digest_algorithm_t *dig,  | 
192  |  |            gnutls_datum_t *label)  | 
193  | 0  | { | 
194  | 0  |   if (spki->pk == 0)  | 
195  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
196  |  |  | 
197  | 0  |   if (spki->pk != GNUTLS_PK_RSA_OAEP)  | 
198  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
199  |  |  | 
200  | 0  |   if (dig)  | 
201  | 0  |     *dig = spki->rsa_oaep_dig;  | 
202  | 0  |   if (label) { | 
203  | 0  |     int ret;  | 
204  |  | 
  | 
205  | 0  |     ret = _gnutls_set_datum(label, spki->rsa_oaep_label.data,  | 
206  | 0  |           spki->rsa_oaep_label.size);  | 
207  | 0  |     if (ret < 0)  | 
208  | 0  |       return gnutls_assert_val(ret);  | 
209  | 0  |   }  | 
210  |  |  | 
211  | 0  |   return 0;  | 
212  | 0  | }  |