3 * Read public or secret key.
5 * Copyright (c) 2005 Marko Kreen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * contrib/pgcrypto/pgp-pubkey.c
38 pgp_key_alloc(PGP_PubKey
**pk_p
)
42 pk
= palloc0(sizeof(*pk
));
48 pgp_key_free(PGP_PubKey
*pk
)
55 case PGP_PUB_ELG_ENCRYPT
:
56 pgp_mpi_free(pk
->pub
.elg
.p
);
57 pgp_mpi_free(pk
->pub
.elg
.g
);
58 pgp_mpi_free(pk
->pub
.elg
.y
);
59 pgp_mpi_free(pk
->sec
.elg
.x
);
61 case PGP_PUB_RSA_SIGN
:
62 case PGP_PUB_RSA_ENCRYPT
:
63 case PGP_PUB_RSA_ENCRYPT_SIGN
:
64 pgp_mpi_free(pk
->pub
.rsa
.n
);
65 pgp_mpi_free(pk
->pub
.rsa
.e
);
66 pgp_mpi_free(pk
->sec
.rsa
.d
);
67 pgp_mpi_free(pk
->sec
.rsa
.p
);
68 pgp_mpi_free(pk
->sec
.rsa
.q
);
69 pgp_mpi_free(pk
->sec
.rsa
.u
);
71 case PGP_PUB_DSA_SIGN
:
72 pgp_mpi_free(pk
->pub
.dsa
.p
);
73 pgp_mpi_free(pk
->pub
.dsa
.q
);
74 pgp_mpi_free(pk
->pub
.dsa
.g
);
75 pgp_mpi_free(pk
->pub
.dsa
.y
);
76 pgp_mpi_free(pk
->sec
.dsa
.x
);
79 px_memset(pk
, 0, sizeof(*pk
));
84 calc_key_id(PGP_PubKey
*pk
)
92 res
= pgp_load_digest(PGP_DIGEST_SHA1
, &md
);
99 case PGP_PUB_ELG_ENCRYPT
:
100 len
+= 2 + pk
->pub
.elg
.p
->bytes
;
101 len
+= 2 + pk
->pub
.elg
.g
->bytes
;
102 len
+= 2 + pk
->pub
.elg
.y
->bytes
;
104 case PGP_PUB_RSA_SIGN
:
105 case PGP_PUB_RSA_ENCRYPT
:
106 case PGP_PUB_RSA_ENCRYPT_SIGN
:
107 len
+= 2 + pk
->pub
.rsa
.n
->bytes
;
108 len
+= 2 + pk
->pub
.rsa
.e
->bytes
;
110 case PGP_PUB_DSA_SIGN
:
111 len
+= 2 + pk
->pub
.dsa
.p
->bytes
;
112 len
+= 2 + pk
->pub
.dsa
.q
->bytes
;
113 len
+= 2 + pk
->pub
.dsa
.g
->bytes
;
114 len
+= 2 + pk
->pub
.dsa
.y
->bytes
;
121 px_md_update(md
, hdr
, 3);
123 px_md_update(md
, &pk
->ver
, 1);
124 px_md_update(md
, pk
->time
, 4);
125 px_md_update(md
, &pk
->algo
, 1);
129 case PGP_PUB_ELG_ENCRYPT
:
130 pgp_mpi_hash(md
, pk
->pub
.elg
.p
);
131 pgp_mpi_hash(md
, pk
->pub
.elg
.g
);
132 pgp_mpi_hash(md
, pk
->pub
.elg
.y
);
134 case PGP_PUB_RSA_SIGN
:
135 case PGP_PUB_RSA_ENCRYPT
:
136 case PGP_PUB_RSA_ENCRYPT_SIGN
:
137 pgp_mpi_hash(md
, pk
->pub
.rsa
.n
);
138 pgp_mpi_hash(md
, pk
->pub
.rsa
.e
);
140 case PGP_PUB_DSA_SIGN
:
141 pgp_mpi_hash(md
, pk
->pub
.dsa
.p
);
142 pgp_mpi_hash(md
, pk
->pub
.dsa
.q
);
143 pgp_mpi_hash(md
, pk
->pub
.dsa
.g
);
144 pgp_mpi_hash(md
, pk
->pub
.dsa
.y
);
148 px_md_finish(md
, hash
);
151 memcpy(pk
->key_id
, hash
+ 12, 8);
152 px_memset(hash
, 0, 20);
158 _pgp_read_public_key(PullFilter
*pkt
, PGP_PubKey
**pk_p
)
163 res
= pgp_key_alloc(&pk
);
168 GETBYTE(pkt
, pk
->ver
);
171 res
= PXE_PGP_NOT_V4_KEYPKT
;
176 res
= pullf_read_fixed(pkt
, 4, pk
->time
);
180 /* pubkey algorithm */
181 GETBYTE(pkt
, pk
->algo
);
185 case PGP_PUB_DSA_SIGN
:
186 res
= pgp_mpi_read(pkt
, &pk
->pub
.dsa
.p
);
189 res
= pgp_mpi_read(pkt
, &pk
->pub
.dsa
.q
);
192 res
= pgp_mpi_read(pkt
, &pk
->pub
.dsa
.g
);
195 res
= pgp_mpi_read(pkt
, &pk
->pub
.dsa
.y
);
199 res
= calc_key_id(pk
);
202 case PGP_PUB_RSA_SIGN
:
203 case PGP_PUB_RSA_ENCRYPT
:
204 case PGP_PUB_RSA_ENCRYPT_SIGN
:
205 res
= pgp_mpi_read(pkt
, &pk
->pub
.rsa
.n
);
208 res
= pgp_mpi_read(pkt
, &pk
->pub
.rsa
.e
);
212 res
= calc_key_id(pk
);
214 if (pk
->algo
!= PGP_PUB_RSA_SIGN
)
218 case PGP_PUB_ELG_ENCRYPT
:
219 res
= pgp_mpi_read(pkt
, &pk
->pub
.elg
.p
);
222 res
= pgp_mpi_read(pkt
, &pk
->pub
.elg
.g
);
225 res
= pgp_mpi_read(pkt
, &pk
->pub
.elg
.y
);
229 res
= calc_key_id(pk
);
235 px_debug("unknown public algo: %d", pk
->algo
);
236 res
= PXE_PGP_UNKNOWN_PUBALGO
;
249 #define HIDE_CKSUM 255
250 #define HIDE_SHA1 254
253 check_key_sha1(PullFilter
*src
, PGP_PubKey
*pk
)
260 res
= pullf_read_fixed(src
, 20, got_sha1
);
264 res
= pgp_load_digest(PGP_DIGEST_SHA1
, &md
);
269 case PGP_PUB_ELG_ENCRYPT
:
270 pgp_mpi_hash(md
, pk
->sec
.elg
.x
);
272 case PGP_PUB_RSA_SIGN
:
273 case PGP_PUB_RSA_ENCRYPT
:
274 case PGP_PUB_RSA_ENCRYPT_SIGN
:
275 pgp_mpi_hash(md
, pk
->sec
.rsa
.d
);
276 pgp_mpi_hash(md
, pk
->sec
.rsa
.p
);
277 pgp_mpi_hash(md
, pk
->sec
.rsa
.q
);
278 pgp_mpi_hash(md
, pk
->sec
.rsa
.u
);
280 case PGP_PUB_DSA_SIGN
:
281 pgp_mpi_hash(md
, pk
->sec
.dsa
.x
);
284 px_md_finish(md
, my_sha1
);
287 if (memcmp(my_sha1
, got_sha1
, 20) != 0)
289 px_debug("key sha1 check failed");
290 res
= PXE_PGP_KEYPKT_CORRUPT
;
293 px_memset(got_sha1
, 0, 20);
294 px_memset(my_sha1
, 0, 20);
299 check_key_cksum(PullFilter
*src
, PGP_PubKey
*pk
)
306 res
= pullf_read_fixed(src
, 2, buf
);
310 got_cksum
= ((unsigned) buf
[0] << 8) + buf
[1];
313 case PGP_PUB_ELG_ENCRYPT
:
314 my_cksum
= pgp_mpi_cksum(0, pk
->sec
.elg
.x
);
316 case PGP_PUB_RSA_SIGN
:
317 case PGP_PUB_RSA_ENCRYPT
:
318 case PGP_PUB_RSA_ENCRYPT_SIGN
:
319 my_cksum
= pgp_mpi_cksum(0, pk
->sec
.rsa
.d
);
320 my_cksum
= pgp_mpi_cksum(my_cksum
, pk
->sec
.rsa
.p
);
321 my_cksum
= pgp_mpi_cksum(my_cksum
, pk
->sec
.rsa
.q
);
322 my_cksum
= pgp_mpi_cksum(my_cksum
, pk
->sec
.rsa
.u
);
324 case PGP_PUB_DSA_SIGN
:
325 my_cksum
= pgp_mpi_cksum(0, pk
->sec
.dsa
.x
);
328 if (my_cksum
!= got_cksum
)
330 px_debug("key cksum check failed");
331 return PXE_PGP_KEYPKT_CORRUPT
;
337 process_secret_key(PullFilter
*pkt
, PGP_PubKey
**pk_p
,
338 const uint8
*key
, int key_len
)
345 PullFilter
*pf_decrypt
= NULL
,
351 /* first read public key part */
352 res
= _pgp_read_public_key(pkt
, &pk
);
357 * is secret key encrypted?
359 GETBYTE(pkt
, hide_type
);
360 if (hide_type
== HIDE_SHA1
|| hide_type
== HIDE_CKSUM
)
363 return PXE_PGP_NEED_SECRET_PSW
;
364 GETBYTE(pkt
, cipher_algo
);
365 res
= pgp_s2k_read(pkt
, &s2k
);
369 res
= pgp_s2k_process(&s2k
, cipher_algo
, key
, key_len
);
373 bs
= pgp_get_cipher_block_size(cipher_algo
);
376 px_debug("unknown cipher algo=%d", cipher_algo
);
377 return PXE_PGP_UNSUPPORTED_CIPHER
;
379 res
= pullf_read_fixed(pkt
, bs
, iv
);
384 * create decrypt filter
386 res
= pgp_cfb_create(&cfb
, cipher_algo
, s2k
.key
, s2k
.key_len
, 0, iv
);
389 res
= pullf_create(&pf_decrypt
, &pgp_decrypt_filter
, cfb
, pkt
);
394 else if (hide_type
== HIDE_CLEAR
)
400 px_debug("unknown hide type");
401 return PXE_PGP_KEYPKT_CORRUPT
;
404 /* read secret key */
407 case PGP_PUB_RSA_SIGN
:
408 case PGP_PUB_RSA_ENCRYPT
:
409 case PGP_PUB_RSA_ENCRYPT_SIGN
:
410 res
= pgp_mpi_read(pf_key
, &pk
->sec
.rsa
.d
);
413 res
= pgp_mpi_read(pf_key
, &pk
->sec
.rsa
.p
);
416 res
= pgp_mpi_read(pf_key
, &pk
->sec
.rsa
.q
);
419 res
= pgp_mpi_read(pf_key
, &pk
->sec
.rsa
.u
);
423 case PGP_PUB_ELG_ENCRYPT
:
424 res
= pgp_mpi_read(pf_key
, &pk
->sec
.elg
.x
);
426 case PGP_PUB_DSA_SIGN
:
427 res
= pgp_mpi_read(pf_key
, &pk
->sec
.dsa
.x
);
430 px_debug("unknown public algo: %d", pk
->algo
);
431 res
= PXE_PGP_KEYPKT_CORRUPT
;
433 /* read checksum / sha1 */
436 if (hide_type
== HIDE_SHA1
)
437 res
= check_key_sha1(pf_key
, pk
);
439 res
= check_key_cksum(pf_key
, pk
);
442 res
= pgp_expect_packet_end(pf_key
);
445 pullf_free(pf_decrypt
);
458 internal_read_key(PullFilter
*src
, PGP_PubKey
**pk_p
,
459 const uint8
*psw
, int psw_len
, int pubtype
)
461 PullFilter
*pkt
= NULL
;
465 PGP_PubKey
*enc_key
= NULL
;
466 PGP_PubKey
*pk
= NULL
;
467 int got_main_key
= 0;
470 * Search for encryption key.
472 * Error out on anything fancy.
476 res
= pgp_parse_pkt_hdr(src
, &tag
, &len
, 0);
479 res
= pgp_create_pkt_reader(&pkt
, src
, len
, res
, NULL
);
485 case PGP_PKT_PUBLIC_KEY
:
486 case PGP_PKT_SECRET_KEY
:
489 res
= PXE_PGP_MULTIPLE_KEYS
;
493 res
= pgp_skip_packet(pkt
);
496 case PGP_PKT_PUBLIC_SUBKEY
:
498 res
= PXE_PGP_EXPECT_SECRET_KEY
;
500 res
= _pgp_read_public_key(pkt
, &pk
);
503 case PGP_PKT_SECRET_SUBKEY
:
505 res
= PXE_PGP_EXPECT_PUBLIC_KEY
;
507 res
= process_secret_key(pkt
, &pk
, psw
, psw_len
);
510 case PGP_PKT_SIGNATURE
:
513 case PGP_PKT_USER_ID
:
514 case PGP_PKT_USER_ATTR
:
515 case PGP_PKT_PRIV_61
:
516 res
= pgp_skip_packet(pkt
);
519 px_debug("unknown/unexpected packet: %d", tag
);
520 res
= PXE_PGP_UNEXPECTED_PKT
;
527 if (res
>= 0 && pk
->can_encrypt
)
535 res
= PXE_PGP_MULTIPLE_SUBKEYS
;
553 pgp_key_free(enc_key
);
558 res
= PXE_PGP_NO_USABLE_KEY
;
565 pgp_set_pubkey(PGP_Context
*ctx
, MBuf
*keypkt
,
566 const uint8
*key
, int key_len
, int pubtype
)
570 PGP_PubKey
*pk
= NULL
;
572 res
= pullf_create_mbuf_reader(&src
, keypkt
);
576 res
= internal_read_key(src
, &pk
, key
, key_len
, pubtype
);
582 return res
< 0 ? res
: 0;