1 /* sig-check.c - Check a signature
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GNUPG.
6 * GNUPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNUPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
35 static int do_check( PKT_public_cert *pkc, PKT_signature *sig,
40 * Check the signature which is contained in the rsa_integer.
41 * The md5handle should be currently open, so that this function
42 * is able to append some data, before getting the digest.
45 signature_check( PKT_signature *sig, MD_HANDLE digest )
47 PKT_public_cert *pkc = m_alloc_clear( sizeof *pkc );
50 if( get_pubkey( pkc, sig->keyid ) )
51 rc = G10ERR_NO_PUBKEY;
53 rc = do_check( pkc, sig, digest );
55 free_public_cert( pkc );
61 do_check( PKT_public_cert *pkc, PKT_signature *sig, MD_HANDLE digest )
66 if( pkc->version == 4 && pkc->pubkey_algo == PUBKEY_ALGO_ELGAMAL )
67 log_info("WARNING: This is probably a PGP generated "
68 "ElGamal key which is NOT secure for signatures!\n");
70 if( pkc->timestamp > sig->timestamp )
71 return G10ERR_TIME_CONFLICT; /* pubkey newer that signature */
73 if( pkc->pubkey_algo == PUBKEY_ALGO_ELGAMAL ) {
74 if( (rc=check_digest_algo(sig->digest_algo)) )
76 /* make sure the digest algo is enabled (in case of a detached
78 md_enable( digest, sig->digest_algo );
79 /* complete the digest */
80 md_putc( digest, sig->sig_class );
81 { u32 a = sig->timestamp;
82 md_putc( digest, (a >> 24) & 0xff );
83 md_putc( digest, (a >> 16) & 0xff );
84 md_putc( digest, (a >> 8) & 0xff );
85 md_putc( digest, a & 0xff );
88 result = encode_md_value( digest, mpi_get_nbits(pkc->d.elg.p));
89 if( !elg_verify( sig->d.elg.a, sig->d.elg.b, result, &pkc->d.elg ) )
92 else if( pkc->pubkey_algo == PUBKEY_ALGO_DSA ) {
93 if( (rc=check_digest_algo(sig->digest_algo)) )
95 /* make sure the digest algo is enabled (in case of a detached
97 md_enable( digest, sig->digest_algo );
99 /* complete the digest */
100 if( sig->version >= 4 )
101 md_putc( digest, sig->version );
102 md_putc( digest, sig->sig_class );
103 if( sig->version < 4 ) {
104 u32 a = sig->timestamp;
105 md_putc( digest, (a >> 24) & 0xff );
106 md_putc( digest, (a >> 16) & 0xff );
107 md_putc( digest, (a >> 8) & 0xff );
108 md_putc( digest, a & 0xff );
113 md_putc( digest, sig->pubkey_algo );
114 md_putc( digest, sig->digest_algo );
115 if( sig->hashed_data ) {
116 n = (sig->hashed_data[0] << 8) | sig->hashed_data[1];
117 md_write( digest, sig->hashed_data, n+2 );
123 buf[0] = sig->version;
129 md_write( digest, buf, 6 );
132 result = mpi_alloc( (md_digest_length(sig->digest_algo)
133 +BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
134 mpi_set_buffer( result, md_read(digest, sig->digest_algo),
135 md_digest_length(sig->digest_algo), 0 );
137 log_mpidump("calc sig frame: ", result);
138 if( !dsa_verify( sig->d.dsa.r, sig->d.dsa.s, result, &pkc->d.dsa ) )
139 rc = G10ERR_BAD_SIGN;
141 #ifdef HAVE_RSA_CIPHER
142 else if( pkc->pubkey_algo == PUBKEY_ALGO_RSA ) {
143 int i, j, c, old_enc;
146 size_t mdlen, asnlen;
148 result = mpi_alloc(40);
149 rsa_public( result, sig->d.rsa.rsa_integer, &pkc->d.rsa );
152 for(i=j=0; (c=mpi_getbyte(result, i)) != -1; i++ ) {
156 else if( i && c == 0xff )
157 ; /* skip the padding */
163 else if( ++j == 18 && c != 1 )
165 else if( j == 19 && c == 0 ) {
171 log_error("old encoding scheme is not supported\n");
176 if( (rc=check_digest_algo(sig->digest_algo)) )
177 goto leave; /* unsupported algo */
178 md_enable( digest, sig->digest_algo );
179 asn = md_asn_oid( sig->digest_algo, &asnlen, &mdlen );
181 for(i=mdlen,j=asnlen-1; (c=mpi_getbyte(result, i)) != -1 && j >= 0;
185 if( j != -1 || mpi_getbyte(result, i) ) { /* ASN is wrong */
186 rc = G10ERR_BAD_PUBKEY;
189 for(i++; (c=mpi_getbyte(result, i)) != -1; i++ )
193 if( c != sig->digest_algo || mpi_getbyte(result, i) ) {
194 /* Padding or leading bytes in signature is wrong */
195 rc = G10ERR_BAD_PUBKEY;
198 if( mpi_getbyte(result, mdlen-1) != sig->digest_start[0]
199 || mpi_getbyte(result, mdlen-2) != sig->digest_start[1] ) {
200 /* Wrong key used to check the signature */
201 rc = G10ERR_BAD_PUBKEY;
205 /* complete the digest */
206 md_putc( digest, sig->sig_class );
207 { u32 a = sig->timestamp;
208 md_putc( digest, (a >> 24) & 0xff );
209 md_putc( digest, (a >> 16) & 0xff );
210 md_putc( digest, (a >> 8) & 0xff );
211 md_putc( digest, a & 0xff );
214 dp = md_read( digest, sig->digest_algo );
215 for(i=mdlen-1; i >= 0; i--, dp++ ) {
216 if( mpi_getbyte( result, i ) != *dp ) {
217 rc = G10ERR_BAD_SIGN;
222 #endif/*HAVE_RSA_CIPHER*/
224 /*log_debug("signature_check: unsupported pubkey algo %d\n",
225 pkc->pubkey_algo );*/
226 rc = G10ERR_PUBKEY_ALGO;
238 * check the signature pointed to by NODE. This is a key signature.
239 * If the function detects a self-signature, it uses the PKC from
240 * NODE and does not read any public key.
243 check_key_signature( KBNODE root, KBNODE node, int *is_selfsig )
246 PKT_public_cert *pkc;
253 assert( node->pkt->pkttype == PKT_SIGNATURE );
254 assert( root->pkt->pkttype == PKT_PUBLIC_CERT );
256 pkc = root->pkt->pkt.public_cert;
257 sig = node->pkt->pkt.signature;
259 if( sig->pubkey_algo == PUBKEY_ALGO_ELGAMAL )
260 algo = sig->digest_algo;
261 else if( sig->pubkey_algo == PUBKEY_ALGO_DSA )
262 algo = sig->digest_algo;
263 else if(sig->pubkey_algo == PUBKEY_ALGO_RSA )
264 algo = sig->digest_algo;
266 return G10ERR_PUBKEY_ALGO;
267 if( (rc=check_digest_algo(algo)) )
270 if( sig->sig_class == 0x20 ) {
271 md = md_open( algo, 0 );
272 hash_public_cert( md, pkc );
273 rc = do_check( pkc, sig, md );
276 else if( sig->sig_class == 0x18 ) {
277 KBNODE snode = find_prev_kbnode( root, node, PKT_PUBKEY_SUBCERT );
280 md = md_open( algo, 0 );
281 hash_public_cert( md, pkc );
282 hash_public_cert( md, snode->pkt->pkt.public_cert );
283 rc = do_check( pkc, sig, md );
287 log_error("no subkey for key signature packet\n");
288 rc = G10ERR_SIG_CLASS;
292 KBNODE unode = find_prev_kbnode( root, node, PKT_USER_ID );
295 PKT_user_id *uid = unode->pkt->pkt.user_id;
298 keyid_from_pkc( pkc, keyid );
299 md = md_open( algo, 0 );
300 hash_public_cert( md, pkc );
301 if( sig->version >=4 ) {
303 buf[0] = 0xb4; /* indicates a userid packet */
304 buf[1] = uid->len >> 24; /* always use 4 length bytes */
305 buf[2] = uid->len >> 16;
306 buf[3] = uid->len >> 8;
308 md_write( md, buf, 5 );
310 md_write( md, uid->name, uid->len );
311 if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] ) {
314 rc = do_check( pkc, sig, md );
317 rc = signature_check( sig, md );
321 log_error("no user id for key signature packet\n");
322 rc = G10ERR_SIG_CLASS;