summaryrefslogtreecommitdiff
path: root/lib/x509/sign.c
blob: 48992d4cfad95af7b629820bec17fe36ee4423e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (C) 2003-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

/* All functions which relate to X.509 certificate signing stuff are
 * included here
 */

#include "gnutls_int.h"

#include "errors.h"
#include <libtasn1.h>
#include <global.h>
#include <num.h> /* MAX */
#include <tls-sig.h>
#include <str.h>
#include <datum.h>
#include <x509_int.h>
#include <common.h>
#include <gnutls/abstract.h>
#include <pk.h>

/* This is the same as the _gnutls_x509_sign, but this one will decode
 * the asn1_node given, and sign the DER data. Actually used to get the DER
 * of the TBS and sign it on the fly.
 */
int _gnutls_x509_get_tbs(asn1_node cert, const char *tbs_name,
			 gnutls_datum_t *tbs)
{
	return _gnutls_x509_der_encode(cert, tbs_name, tbs, 0);
}

int _gnutls_x509_crt_get_spki_params(gnutls_x509_crt_t crt,
				     const gnutls_x509_spki_st *key_params,
				     gnutls_x509_spki_st *params)
{
	int result;
	gnutls_x509_spki_st crt_params;

	result = _gnutls_x509_crt_read_spki_params(crt, &crt_params);
	if (result < 0) {
		gnutls_assert();
		return result;
	}

	if (crt_params.pk == GNUTLS_PK_RSA_PSS) {
		if (key_params->pk == GNUTLS_PK_RSA_PSS) {
			if (crt_params.rsa_pss_dig != key_params->rsa_pss_dig) {
				gnutls_assert();
				return GNUTLS_E_CERTIFICATE_ERROR;
			}

			if (crt_params.salt_size < key_params->salt_size) {
				gnutls_assert();
				return GNUTLS_E_CERTIFICATE_ERROR;
			}
		} else if (key_params->pk != GNUTLS_PK_RSA &&
			   key_params->pk != GNUTLS_PK_UNKNOWN) {
			gnutls_assert();
			return GNUTLS_E_CERTIFICATE_ERROR;
		}
		memcpy(params, &crt_params, sizeof(gnutls_x509_spki_st));
	} else {
		memcpy(params, key_params, sizeof(gnutls_x509_spki_st));
	}

	return 0;
}

/*-
 * _gnutls_x509_pkix_sign - This function will sign a CRL or a certificate with a key
 * @src: should contain an asn1_node
 * @issuer: is the certificate of the certificate issuer
 * @issuer_key: holds the issuer's private key
 *
 * This function will sign a CRL or a certificate with the issuer's private key, and
 * will copy the issuer's information into the CRL or certificate.
 *
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
 *   negative error value.
 -*/
int _gnutls_x509_pkix_sign(asn1_node src, const char *src_name,
			   gnutls_digest_algorithm_t dig, unsigned int flags,
			   gnutls_x509_crt_t issuer,
			   gnutls_privkey_t issuer_key)
{
	int result;
	gnutls_datum_t signature;
	gnutls_datum_t tbs;
	char name[128];
	gnutls_pk_algorithm_t pk;
	gnutls_x509_spki_st key_params, params;
	const gnutls_sign_entry_st *se;

	pk = gnutls_x509_crt_get_pk_algorithm(issuer, NULL);
	if (pk == GNUTLS_PK_UNKNOWN)
		pk = gnutls_privkey_get_pk_algorithm(issuer_key, NULL);

	result = _gnutls_privkey_get_spki_params(issuer_key, &key_params);
	if (result < 0) {
		gnutls_assert();
		return result;
	}

	result = _gnutls_x509_crt_get_spki_params(issuer, &key_params, &params);
	if (result < 0) {
		gnutls_assert();
		return result;
	}

	result = _gnutls_privkey_update_spki_params(issuer_key, pk, dig, flags,
						    &params);
	if (result < 0) {
		gnutls_assert();
		return result;
	}

	/* Step 1. Copy the issuer's name into the certificate.
	 */
	_gnutls_str_cpy(name, sizeof(name), src_name);
	_gnutls_str_cat(name, sizeof(name), ".issuer");

	result = asn1_copy_node(src, name, issuer->cert,
				"tbsCertificate.subject");
	if (result != ASN1_SUCCESS) {
		gnutls_assert();
		return _gnutls_asn2err(result);
	}

	/* Step 1.5. Write the signature stuff in the tbsCertificate.
	 */
	_gnutls_str_cpy(name, sizeof(name), src_name);
	_gnutls_str_cat(name, sizeof(name), ".signature");

	se = _gnutls_pk_to_sign_entry(params.pk, dig);
	if (se == NULL)
		return gnutls_assert_val(
			GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);

	_gnutls_debug_log("signing structure using %s\n", se->name);

	result = _gnutls_x509_write_sign_params(src, name, se, &params);
	if (result < 0) {
		gnutls_assert();
		return result;
	}

	/* Step 2. Sign the certificate.
	 */
	result = _gnutls_x509_get_tbs(src, src_name, &tbs);

	if (result < 0) {
		gnutls_assert();
		return result;
	}

	FIX_SIGN_PARAMS(params, flags, dig);

	if (_gnutls_pk_is_not_prehashed(params.pk)) {
		result = privkey_sign_raw_data(issuer_key, se, &tbs, &signature,
					       &params);
	} else {
		result = privkey_sign_and_hash_data(issuer_key, se, &tbs,
						    &signature, &params);
	}
	gnutls_free(tbs.data);

	if (result < 0) {
		gnutls_assert();
		return result;
	}

	/* write the signature (bits)
	 */
	result = asn1_write_value(src, "signature", signature.data,
				  signature.size * 8);

	_gnutls_free_datum(&signature);

	if (result != ASN1_SUCCESS) {
		gnutls_assert();
		return _gnutls_asn2err(result);
	}

	/* Step 3. Move up and write the AlgorithmIdentifier, which is also
	 * the same. 
	 */

	result = _gnutls_x509_write_sign_params(src, "signatureAlgorithm", se,
						&params);
	if (result < 0) {
		gnutls_assert();
		return result;
	}

	return 0;
}