3 * Decrypt public-key encrypted session 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-pubdec.c
37 * padded msg = 02 || PS || 00 || M
42 check_eme_pkcs1_v15(uint8
*data
, int len
)
44 uint8
*data_end
= data
+ len
;
54 while (p
< data_end
&& *p
)
70 * secret message: 1 byte algo, sesskey, 2 byte cksum
71 * ignore algo in cksum
74 control_cksum(uint8
*msg
, int msglen
)
81 return PXE_PGP_WRONG_KEY
;
84 for (i
= 1; i
< msglen
- 2; i
++)
87 got_cksum
= ((unsigned) (msg
[msglen
- 2]) << 8) + msg
[msglen
- 1];
88 if (my_cksum
!= got_cksum
)
90 px_debug("pubenc cksum failed");
91 return PXE_PGP_WRONG_KEY
;
97 decrypt_elgamal(PGP_PubKey
*pk
, PullFilter
*pkt
, PGP_MPI
**m_p
)
103 if (pk
->algo
!= PGP_PUB_ELG_ENCRYPT
)
104 return PXE_PGP_WRONG_KEY
;
106 /* read elgamal encrypted data */
107 res
= pgp_mpi_read(pkt
, &c1
);
110 res
= pgp_mpi_read(pkt
, &c2
);
115 res
= pgp_elgamal_decrypt(pk
, c1
, c2
, m_p
);
124 decrypt_rsa(PGP_PubKey
*pk
, PullFilter
*pkt
, PGP_MPI
**m_p
)
129 if (pk
->algo
!= PGP_PUB_RSA_ENCRYPT
130 && pk
->algo
!= PGP_PUB_RSA_ENCRYPT_SIGN
)
131 return PXE_PGP_WRONG_KEY
;
133 /* read rsa encrypted data */
134 res
= pgp_mpi_read(pkt
, &c
);
139 res
= pgp_rsa_decrypt(pk
, c
, m_p
);
145 /* key id is missing - user is expected to try all keys */
147 any_key
[] = {0, 0, 0, 0, 0, 0, 0, 0};
150 pgp_parse_pubenc_sesskey(PGP_Context
*ctx
, PullFilter
*pkt
)
164 px_debug("no pubkey?");
171 px_debug("unknown pubenc_sesskey pkt ver=%d", ver
);
172 return PXE_PGP_CORRUPT_DATA
;
176 * check if keyid's match - user-friendly msg
178 res
= pullf_read_fixed(pkt
, 8, key_id
);
181 if (memcmp(key_id
, any_key
, 8) != 0
182 && memcmp(key_id
, pk
->key_id
, 8) != 0)
184 px_debug("key_id's does not match");
185 return PXE_PGP_WRONG_KEY
;
194 case PGP_PUB_ELG_ENCRYPT
:
195 res
= decrypt_elgamal(pk
, pkt
, &m
);
197 case PGP_PUB_RSA_ENCRYPT
:
198 case PGP_PUB_RSA_ENCRYPT_SIGN
:
199 res
= decrypt_rsa(pk
, pkt
, &m
);
202 res
= PXE_PGP_UNKNOWN_PUBALGO
;
210 msg
= check_eme_pkcs1_v15(m
->data
, m
->bytes
);
213 px_debug("check_eme_pkcs1_v15 failed");
214 res
= PXE_PGP_WRONG_KEY
;
217 msglen
= m
->bytes
- (msg
- m
->data
);
219 res
= control_cksum(msg
, msglen
);
226 ctx
->cipher_algo
= *msg
;
227 ctx
->sess_key_len
= msglen
- 3;
228 memcpy(ctx
->sess_key
, msg
+ 1, ctx
->sess_key_len
);
234 return pgp_expect_packet_end(pkt
);