LCOV - differential code coverage report
Current view: top level - contrib/pgcrypto - pgp-pubkey.c (source / functions) Coverage Total Hit UBC CBC
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 72.1 % 333 240 93 240
Current Date: 2026-08-27 14:31:44 +0300 Functions: 100.0 % 9 9 9
Baseline: lcov-20260827-baseline Branches: 58.5 % 159 93 66 93
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 1 1 1
(360..) days: 71.9 % 331 238 93 238
Function coverage date bins:
(360..) days: 100.0 % 9 9 9
Branch coverage date bins:
(360..) days: 58.5 % 159 93 66 93

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * pgp-pubkey.c
                                  3                 :                :  *    Read public or secret key.
                                  4                 :                :  *
                                  5                 :                :  * Copyright (c) 2005 Marko Kreen
                                  6                 :                :  * All rights reserved.
                                  7                 :                :  *
                                  8                 :                :  * Redistribution and use in source and binary forms, with or without
                                  9                 :                :  * modification, are permitted provided that the following conditions
                                 10                 :                :  * are met:
                                 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.
                                 16                 :                :  *
                                 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
                                 27                 :                :  * SUCH DAMAGE.
                                 28                 :                :  *
                                 29                 :                :  * contrib/pgcrypto/pgp-pubkey.c
                                 30                 :                :  */
                                 31                 :                : #include "postgres.h"
                                 32                 :                : 
                                 33                 :                : #include "mbuf.h"
                                 34                 :                : #include "pgp.h"
                                 35                 :                : #include "px.h"
                                 36                 :                : 
                                 37                 :                : int
 6286 bruce@momjian.us           38                 :CBC          37 : pgp_key_alloc(PGP_PubKey **pk_p)
                                 39                 :                : {
                                 40                 :                :     PGP_PubKey *pk;
                                 41                 :                : 
  265 michael@paquier.xyz        42                 :             37 :     pk = palloc0_object(PGP_PubKey);
 7718 bruce@momjian.us           43                 :             37 :     *pk_p = pk;
                                 44                 :             37 :     return 0;
                                 45                 :                : }
                                 46                 :                : 
                                 47                 :                : void
 6286                            48                 :             34 : pgp_key_free(PGP_PubKey *pk)
                                 49                 :                : {
 7684                            50         [ -  + ]:             34 :     if (pk == NULL)
 7684 bruce@momjian.us           51                 :UBC           0 :         return;
                                 52                 :                : 
 7684 bruce@momjian.us           53   [ +  +  -  - ]:CBC          34 :     switch (pk->algo)
                                 54                 :                :     {
                                 55                 :             25 :         case PGP_PUB_ELG_ENCRYPT:
                                 56                 :             25 :             pgp_mpi_free(pk->pub.elg.p);
                                 57                 :             25 :             pgp_mpi_free(pk->pub.elg.g);
                                 58                 :             25 :             pgp_mpi_free(pk->pub.elg.y);
                                 59                 :             25 :             pgp_mpi_free(pk->sec.elg.x);
                                 60                 :             25 :             break;
                                 61                 :              9 :         case PGP_PUB_RSA_SIGN:
                                 62                 :                :         case PGP_PUB_RSA_ENCRYPT:
                                 63                 :                :         case PGP_PUB_RSA_ENCRYPT_SIGN:
                                 64                 :              9 :             pgp_mpi_free(pk->pub.rsa.n);
                                 65                 :              9 :             pgp_mpi_free(pk->pub.rsa.e);
                                 66                 :              9 :             pgp_mpi_free(pk->sec.rsa.d);
                                 67                 :              9 :             pgp_mpi_free(pk->sec.rsa.p);
                                 68                 :              9 :             pgp_mpi_free(pk->sec.rsa.q);
                                 69                 :              9 :             pgp_mpi_free(pk->sec.rsa.u);
                                 70                 :              9 :             break;
 7684 bruce@momjian.us           71                 :UBC           0 :         case PGP_PUB_DSA_SIGN:
                                 72                 :              0 :             pgp_mpi_free(pk->pub.dsa.p);
                                 73                 :              0 :             pgp_mpi_free(pk->pub.dsa.q);
                                 74                 :              0 :             pgp_mpi_free(pk->pub.dsa.g);
                                 75                 :              0 :             pgp_mpi_free(pk->pub.dsa.y);
                                 76                 :              0 :             pgp_mpi_free(pk->sec.dsa.x);
                                 77                 :              0 :             break;
                                 78                 :                :     }
 4515 bruce@momjian.us           79                 :CBC          34 :     px_memset(pk, 0, sizeof(*pk));
 2162 michael@paquier.xyz        80                 :             34 :     pfree(pk);
                                 81                 :                : }
                                 82                 :                : 
                                 83                 :                : static int
 6286 bruce@momjian.us           84                 :             37 : calc_key_id(PGP_PubKey *pk)
                                 85                 :                : {
                                 86                 :                :     int         res;
                                 87                 :                :     PX_MD      *md;
                                 88                 :                :     int         len;
                                 89                 :                :     uint8       hdr[3];
                                 90                 :                :     uint8       hash[20];
                                 91                 :                : 
 7718                            92                 :             37 :     res = pgp_load_digest(PGP_DIGEST_SHA1, &md);
                                 93         [ -  + ]:             37 :     if (res < 0)
 7718 bruce@momjian.us           94                 :UBC           0 :         return res;
                                 95                 :                : 
 7718 bruce@momjian.us           96                 :CBC          37 :     len = 1 + 4 + 1;
                                 97   [ +  +  -  - ]:             37 :     switch (pk->algo)
                                 98                 :                :     {
                                 99                 :             28 :         case PGP_PUB_ELG_ENCRYPT:
 7684                           100                 :             28 :             len += 2 + pk->pub.elg.p->bytes;
                                101                 :             28 :             len += 2 + pk->pub.elg.g->bytes;
                                102                 :             28 :             len += 2 + pk->pub.elg.y->bytes;
                                103                 :             28 :             break;
                                104                 :              9 :         case PGP_PUB_RSA_SIGN:
                                105                 :                :         case PGP_PUB_RSA_ENCRYPT:
                                106                 :                :         case PGP_PUB_RSA_ENCRYPT_SIGN:
                                107                 :              9 :             len += 2 + pk->pub.rsa.n->bytes;
                                108                 :              9 :             len += 2 + pk->pub.rsa.e->bytes;
                                109                 :              9 :             break;
 7684 bruce@momjian.us          110                 :UBC           0 :         case PGP_PUB_DSA_SIGN:
                                111                 :              0 :             len += 2 + pk->pub.dsa.p->bytes;
                                112                 :              0 :             len += 2 + pk->pub.dsa.q->bytes;
                                113                 :              0 :             len += 2 + pk->pub.dsa.g->bytes;
                                114                 :              0 :             len += 2 + pk->pub.dsa.y->bytes;
 7718                           115                 :              0 :             break;
                                116                 :                :     }
                                117                 :                : 
 7718 bruce@momjian.us          118                 :CBC          37 :     hdr[0] = 0x99;
                                119                 :             37 :     hdr[1] = len >> 8;
                                120                 :             37 :     hdr[2] = len & 0xFF;
                                121                 :             37 :     px_md_update(md, hdr, 3);
                                122                 :                : 
                                123                 :             37 :     px_md_update(md, &pk->ver, 1);
                                124                 :             37 :     px_md_update(md, pk->time, 4);
                                125                 :             37 :     px_md_update(md, &pk->algo, 1);
                                126                 :                : 
                                127   [ +  +  -  - ]:             37 :     switch (pk->algo)
                                128                 :                :     {
                                129                 :             28 :         case PGP_PUB_ELG_ENCRYPT:
 7684                           130                 :             28 :             pgp_mpi_hash(md, pk->pub.elg.p);
                                131                 :             28 :             pgp_mpi_hash(md, pk->pub.elg.g);
                                132                 :             28 :             pgp_mpi_hash(md, pk->pub.elg.y);
                                133                 :             28 :             break;
                                134                 :              9 :         case PGP_PUB_RSA_SIGN:
                                135                 :                :         case PGP_PUB_RSA_ENCRYPT:
                                136                 :                :         case PGP_PUB_RSA_ENCRYPT_SIGN:
                                137                 :              9 :             pgp_mpi_hash(md, pk->pub.rsa.n);
                                138                 :              9 :             pgp_mpi_hash(md, pk->pub.rsa.e);
                                139                 :              9 :             break;
 7684 bruce@momjian.us          140                 :UBC           0 :         case PGP_PUB_DSA_SIGN:
                                141                 :              0 :             pgp_mpi_hash(md, pk->pub.dsa.p);
                                142                 :              0 :             pgp_mpi_hash(md, pk->pub.dsa.q);
                                143                 :              0 :             pgp_mpi_hash(md, pk->pub.dsa.g);
                                144                 :              0 :             pgp_mpi_hash(md, pk->pub.dsa.y);
 7718                           145                 :              0 :             break;
                                146                 :                :     }
                                147                 :                : 
 7718 bruce@momjian.us          148                 :CBC          37 :     px_md_finish(md, hash);
                                149                 :             37 :     px_md_free(md);
                                150                 :                : 
                                151                 :             37 :     memcpy(pk->key_id, hash + 12, 8);
 4515                           152                 :             37 :     px_memset(hash, 0, 20);
                                153                 :                : 
 7718                           154                 :             37 :     return 0;
                                155                 :                : }
                                156                 :                : 
                                157                 :                : int
 6286                           158                 :             37 : _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)
                                159                 :                : {
                                160                 :                :     int         res;
                                161                 :                :     PGP_PubKey *pk;
                                162                 :                : 
 7684                           163                 :             37 :     res = pgp_key_alloc(&pk);
                                164         [ -  + ]:             37 :     if (res < 0)
 7684 bruce@momjian.us          165                 :UBC           0 :         return res;
                                166                 :                : 
                                167                 :                :     /* get version */
 7718 bruce@momjian.us          168         [ -  + ]:CBC          37 :     GETBYTE(pkt, pk->ver);
 7621                           169         [ -  + ]:             37 :     if (pk->ver != 4)
                                170                 :                :     {
 7684 bruce@momjian.us          171                 :UBC           0 :         res = PXE_PGP_NOT_V4_KEYPKT;
                                172                 :              0 :         goto out;
                                173                 :                :     }
                                174                 :                : 
                                175                 :                :     /* read time */
 7718 bruce@momjian.us          176                 :CBC          37 :     res = pullf_read_fixed(pkt, 4, pk->time);
                                177         [ -  + ]:             37 :     if (res < 0)
 7684 bruce@momjian.us          178                 :UBC           0 :         goto out;
                                179                 :                : 
                                180                 :                :     /* pubkey algorithm */
 7718 bruce@momjian.us          181         [ -  + ]:CBC          37 :     GETBYTE(pkt, pk->algo);
                                182                 :                : 
 7621                           183   [ -  +  +  - ]:             37 :     switch (pk->algo)
                                184                 :                :     {
 7718 bruce@momjian.us          185                 :UBC           0 :         case PGP_PUB_DSA_SIGN:
 7684                           186                 :              0 :             res = pgp_mpi_read(pkt, &pk->pub.dsa.p);
 7621                           187         [ #  # ]:              0 :             if (res < 0)
                                188                 :              0 :                 break;
 7684                           189                 :              0 :             res = pgp_mpi_read(pkt, &pk->pub.dsa.q);
 7621                           190         [ #  # ]:              0 :             if (res < 0)
                                191                 :              0 :                 break;
 7684                           192                 :              0 :             res = pgp_mpi_read(pkt, &pk->pub.dsa.g);
 7621                           193         [ #  # ]:              0 :             if (res < 0)
                                194                 :              0 :                 break;
 7684                           195                 :              0 :             res = pgp_mpi_read(pkt, &pk->pub.dsa.y);
 7621                           196         [ #  # ]:              0 :             if (res < 0)
                                197                 :              0 :                 break;
                                198                 :                : 
 7684                           199                 :              0 :             res = calc_key_id(pk);
 7718                           200                 :              0 :             break;
                                201                 :                : 
 7684 bruce@momjian.us          202                 :CBC           9 :         case PGP_PUB_RSA_SIGN:
                                203                 :                :         case PGP_PUB_RSA_ENCRYPT:
                                204                 :                :         case PGP_PUB_RSA_ENCRYPT_SIGN:
                                205                 :              9 :             res = pgp_mpi_read(pkt, &pk->pub.rsa.n);
 7621                           206         [ -  + ]:              9 :             if (res < 0)
 7621 bruce@momjian.us          207                 :UBC           0 :                 break;
 7684 bruce@momjian.us          208                 :CBC           9 :             res = pgp_mpi_read(pkt, &pk->pub.rsa.e);
 7621                           209         [ -  + ]:              9 :             if (res < 0)
 7621 bruce@momjian.us          210                 :UBC           0 :                 break;
                                211                 :                : 
 7684 bruce@momjian.us          212                 :CBC           9 :             res = calc_key_id(pk);
                                213                 :                : 
                                214         [ +  - ]:              9 :             if (pk->algo != PGP_PUB_RSA_SIGN)
                                215                 :              9 :                 pk->can_encrypt = 1;
                                216                 :              9 :             break;
                                217                 :                : 
 7718                           218                 :             28 :         case PGP_PUB_ELG_ENCRYPT:
 7684                           219                 :             28 :             res = pgp_mpi_read(pkt, &pk->pub.elg.p);
 7621                           220         [ -  + ]:             28 :             if (res < 0)
 7621 bruce@momjian.us          221                 :UBC           0 :                 break;
 7684 bruce@momjian.us          222                 :CBC          28 :             res = pgp_mpi_read(pkt, &pk->pub.elg.g);
 7621                           223         [ -  + ]:             28 :             if (res < 0)
 7621 bruce@momjian.us          224                 :UBC           0 :                 break;
 7684 bruce@momjian.us          225                 :CBC          28 :             res = pgp_mpi_read(pkt, &pk->pub.elg.y);
 7621                           226         [ -  + ]:             28 :             if (res < 0)
 7621 bruce@momjian.us          227                 :UBC           0 :                 break;
                                228                 :                : 
 7718 bruce@momjian.us          229                 :CBC          28 :             res = calc_key_id(pk);
                                230                 :                : 
 7684                           231                 :             28 :             pk->can_encrypt = 1;
 7718                           232                 :             28 :             break;
                                233                 :                : 
 7718 bruce@momjian.us          234                 :UBC           0 :         default:
                                235                 :              0 :             px_debug("unknown public algo: %d", pk->algo);
                                236                 :              0 :             res = PXE_PGP_UNKNOWN_PUBALGO;
                                237                 :                :     }
                                238                 :                : 
 7684 bruce@momjian.us          239                 :CBC          37 : out:
                                240         [ -  + ]:             37 :     if (res < 0)
 7684 bruce@momjian.us          241                 :UBC           0 :         pgp_key_free(pk);
                                242                 :                :     else
 7684 bruce@momjian.us          243                 :CBC          37 :         *pk_p = pk;
                                244                 :                : 
 7718                           245                 :             37 :     return res;
                                246                 :                : }
                                247                 :                : 
                                248                 :                : #define HIDE_CLEAR 0
                                249                 :                : #define HIDE_CKSUM 255
                                250                 :                : #define HIDE_SHA1 254
                                251                 :                : 
                                252                 :                : static int
 6286                           253                 :              2 : check_key_sha1(PullFilter *src, PGP_PubKey *pk)
                                254                 :                : {
                                255                 :                :     int         res;
                                256                 :                :     uint8       got_sha1[20];
                                257                 :                :     uint8       my_sha1[20];
                                258                 :                :     PX_MD      *md;
                                259                 :                : 
 7718                           260                 :              2 :     res = pullf_read_fixed(src, 20, got_sha1);
                                261         [ -  + ]:              2 :     if (res < 0)
 7718 bruce@momjian.us          262                 :UBC           0 :         return res;
                                263                 :                : 
 7718 bruce@momjian.us          264                 :CBC           2 :     res = pgp_load_digest(PGP_DIGEST_SHA1, &md);
                                265         [ -  + ]:              2 :     if (res < 0)
 7718 bruce@momjian.us          266                 :UBC           0 :         goto err;
 7718 bruce@momjian.us          267   [ +  +  -  - ]:CBC           2 :     switch (pk->algo)
                                268                 :                :     {
                                269                 :              1 :         case PGP_PUB_ELG_ENCRYPT:
 7684                           270                 :              1 :             pgp_mpi_hash(md, pk->sec.elg.x);
                                271                 :              1 :             break;
                                272                 :              1 :         case PGP_PUB_RSA_SIGN:
                                273                 :                :         case PGP_PUB_RSA_ENCRYPT:
                                274                 :                :         case PGP_PUB_RSA_ENCRYPT_SIGN:
                                275                 :              1 :             pgp_mpi_hash(md, pk->sec.rsa.d);
                                276                 :              1 :             pgp_mpi_hash(md, pk->sec.rsa.p);
                                277                 :              1 :             pgp_mpi_hash(md, pk->sec.rsa.q);
                                278                 :              1 :             pgp_mpi_hash(md, pk->sec.rsa.u);
                                279                 :              1 :             break;
 7684 bruce@momjian.us          280                 :UBC           0 :         case PGP_PUB_DSA_SIGN:
                                281                 :              0 :             pgp_mpi_hash(md, pk->sec.dsa.x);
 7718                           282                 :              0 :             break;
                                283                 :                :     }
 7718 bruce@momjian.us          284                 :CBC           2 :     px_md_finish(md, my_sha1);
                                285                 :              2 :     px_md_free(md);
                                286                 :                : 
                                287         [ +  - ]:              2 :     if (memcmp(my_sha1, got_sha1, 20) != 0)
                                288                 :                :     {
 7718 bruce@momjian.us          289                 :UBC           0 :         px_debug("key sha1 check failed");
                                290                 :              0 :         res = PXE_PGP_KEYPKT_CORRUPT;
                                291                 :                :     }
 7718 bruce@momjian.us          292                 :CBC           2 : err:
 4515                           293                 :              2 :     px_memset(got_sha1, 0, 20);
                                294                 :              2 :     px_memset(my_sha1, 0, 20);
 7718                           295                 :              2 :     return res;
                                296                 :                : }
                                297                 :                : 
                                298                 :                : static int
 6286                           299                 :             16 : check_key_cksum(PullFilter *src, PGP_PubKey *pk)
                                300                 :                : {
                                301                 :                :     int         res;
                                302                 :                :     unsigned    got_cksum,
 7621                           303                 :             16 :                 my_cksum = 0;
                                304                 :                :     uint8       buf[2];
                                305                 :                : 
 7718                           306                 :             16 :     res = pullf_read_fixed(src, 2, buf);
                                307         [ -  + ]:             16 :     if (res < 0)
 7718 bruce@momjian.us          308                 :UBC           0 :         return res;
                                309                 :                : 
 7621 bruce@momjian.us          310                 :CBC          16 :     got_cksum = ((unsigned) buf[0] << 8) + buf[1];
 7718                           311   [ +  +  -  - ]:             16 :     switch (pk->algo)
                                312                 :                :     {
                                313                 :             12 :         case PGP_PUB_ELG_ENCRYPT:
 7684                           314                 :             12 :             my_cksum = pgp_mpi_cksum(0, pk->sec.elg.x);
                                315                 :             12 :             break;
                                316                 :              4 :         case PGP_PUB_RSA_SIGN:
                                317                 :                :         case PGP_PUB_RSA_ENCRYPT:
                                318                 :                :         case PGP_PUB_RSA_ENCRYPT_SIGN:
                                319                 :              4 :             my_cksum = pgp_mpi_cksum(0, pk->sec.rsa.d);
                                320                 :              4 :             my_cksum = pgp_mpi_cksum(my_cksum, pk->sec.rsa.p);
                                321                 :              4 :             my_cksum = pgp_mpi_cksum(my_cksum, pk->sec.rsa.q);
                                322                 :              4 :             my_cksum = pgp_mpi_cksum(my_cksum, pk->sec.rsa.u);
                                323                 :              4 :             break;
 7684 bruce@momjian.us          324                 :UBC           0 :         case PGP_PUB_DSA_SIGN:
                                325                 :              0 :             my_cksum = pgp_mpi_cksum(0, pk->sec.dsa.x);
 7718                           326                 :              0 :             break;
                                327                 :                :     }
 7718 bruce@momjian.us          328         [ -  + ]:CBC          16 :     if (my_cksum != got_cksum)
                                329                 :                :     {
 7718 bruce@momjian.us          330                 :UBC           0 :         px_debug("key cksum check failed");
                                331                 :              0 :         return PXE_PGP_KEYPKT_CORRUPT;
                                332                 :                :     }
 7718 bruce@momjian.us          333                 :CBC          16 :     return 0;
                                334                 :                : }
                                335                 :                : 
                                336                 :                : static int
 6286                           337                 :             21 : process_secret_key(PullFilter *pkt, PGP_PubKey **pk_p,
                                338                 :                :                    const uint8 *key, int key_len)
                                339                 :                : {
                                340                 :                :     int         res;
                                341                 :                :     int         hide_type;
                                342                 :                :     int         cipher_algo;
                                343                 :                :     int         bs;
                                344                 :                :     uint8       iv[512];
 7621                           345                 :             21 :     PullFilter *pf_decrypt = NULL,
                                346                 :                :                *pf_key;
                                347                 :             21 :     PGP_CFB    *cfb = NULL;
                                348                 :                :     PGP_S2K     s2k;
                                349                 :                :     PGP_PubKey *pk;
                                350                 :                : 
                                351                 :                :     /* first read public key part */
 7684                           352                 :             21 :     res = _pgp_read_public_key(pkt, &pk);
 7718                           353         [ -  + ]:             21 :     if (res < 0)
 7718 bruce@momjian.us          354                 :UBC           0 :         return res;
                                355                 :                : 
                                356                 :                :     /*
                                357                 :                :      * is secret key encrypted?
                                358                 :                :      */
 7718 bruce@momjian.us          359         [ -  + ]:CBC          21 :     GETBYTE(pkt, hide_type);
 7621                           360   [ +  +  -  + ]:             21 :     if (hide_type == HIDE_SHA1 || hide_type == HIDE_CKSUM)
                                361                 :                :     {
 7718                           362         [ +  + ]:              5 :         if (key == NULL)
                                363                 :              1 :             return PXE_PGP_NEED_SECRET_PSW;
                                364         [ -  + ]:              4 :         GETBYTE(pkt, cipher_algo);
                                365                 :              4 :         res = pgp_s2k_read(pkt, &s2k);
                                366         [ -  + ]:              4 :         if (res < 0)
 7718 bruce@momjian.us          367                 :UBC           0 :             return res;
                                368                 :                : 
 7718 bruce@momjian.us          369                 :CBC           4 :         res = pgp_s2k_process(&s2k, cipher_algo, key, key_len);
                                370         [ -  + ]:              4 :         if (res < 0)
 7718 bruce@momjian.us          371                 :UBC           0 :             return res;
                                372                 :                : 
 7718 bruce@momjian.us          373                 :CBC           4 :         bs = pgp_get_cipher_block_size(cipher_algo);
 7621                           374         [ -  + ]:              4 :         if (bs == 0)
                                375                 :                :         {
 7718 bruce@momjian.us          376                 :UBC           0 :             px_debug("unknown cipher algo=%d", cipher_algo);
                                377                 :              0 :             return PXE_PGP_UNSUPPORTED_CIPHER;
                                378                 :                :         }
 7718 bruce@momjian.us          379                 :CBC           4 :         res = pullf_read_fixed(pkt, bs, iv);
                                380         [ -  + ]:              4 :         if (res < 0)
 7718 bruce@momjian.us          381                 :UBC           0 :             return res;
                                382                 :                : 
                                383                 :                :         /*
                                384                 :                :          * create decrypt filter
                                385                 :                :          *
                                386                 :                :          * ignore-cipher-failure doesn't apply here; pgcrypto didn't encrypt
                                387                 :                :          * the secret key to begin with, and any stored encrypted data was
                                388                 :                :          * generated using the public key, so users don't have a reason to
                                389                 :                :          * want to incorrectly decrypt this. We'll ignore failures during
                                390                 :                :          * decryption with the session key, instead.
                                391                 :                :          */
   17 jchampion@postgresql      392                 :CBC           4 :         res = pgp_cfb_create(&cfb, cipher_algo, s2k.key, s2k.key_len, 0, iv,
                                393                 :                :                              0 /* don't ignore cipher failures */ );
 7718 bruce@momjian.us          394         [ -  + ]:              4 :         if (res < 0)
 7718 bruce@momjian.us          395                 :UBC           0 :             return res;
 7718 bruce@momjian.us          396                 :CBC           4 :         res = pullf_create(&pf_decrypt, &pgp_decrypt_filter, cfb, pkt);
                                397         [ -  + ]:              4 :         if (res < 0)
 7718 bruce@momjian.us          398                 :UBC           0 :             return res;
 7718 bruce@momjian.us          399                 :CBC           4 :         pf_key = pf_decrypt;
                                400                 :                :     }
 7621                           401         [ +  - ]:             16 :     else if (hide_type == HIDE_CLEAR)
                                402                 :                :     {
 7718                           403                 :             16 :         pf_key = pkt;
                                404                 :                :     }
                                405                 :                :     else
                                406                 :                :     {
 7718 bruce@momjian.us          407                 :UBC           0 :         px_debug("unknown hide type");
                                408                 :              0 :         return PXE_PGP_KEYPKT_CORRUPT;
                                409                 :                :     }
                                410                 :                : 
                                411                 :                :     /* read secret key */
 7621 bruce@momjian.us          412   [ +  +  -  - ]:CBC          20 :     switch (pk->algo)
                                413                 :                :     {
 7718                           414                 :              6 :         case PGP_PUB_RSA_SIGN:
                                415                 :                :         case PGP_PUB_RSA_ENCRYPT:
                                416                 :                :         case PGP_PUB_RSA_ENCRYPT_SIGN:
 4857 tgl@sss.pgh.pa.us         417                 :              6 :             res = pgp_mpi_read(pf_key, &pk->sec.rsa.d);
 7621 bruce@momjian.us          418         [ -  + ]:              6 :             if (res < 0)
 7621 bruce@momjian.us          419                 :UBC           0 :                 break;
 4857 tgl@sss.pgh.pa.us         420                 :CBC           6 :             res = pgp_mpi_read(pf_key, &pk->sec.rsa.p);
 7621 bruce@momjian.us          421         [ +  + ]:              6 :             if (res < 0)
                                422                 :              1 :                 break;
 4857 tgl@sss.pgh.pa.us         423                 :              5 :             res = pgp_mpi_read(pf_key, &pk->sec.rsa.q);
 7621 bruce@momjian.us          424         [ -  + ]:              5 :             if (res < 0)
 7621 bruce@momjian.us          425                 :UBC           0 :                 break;
 4857 tgl@sss.pgh.pa.us         426                 :CBC           5 :             res = pgp_mpi_read(pf_key, &pk->sec.rsa.u);
 7621 bruce@momjian.us          427         [ -  + ]:              5 :             if (res < 0)
 7621 bruce@momjian.us          428                 :UBC           0 :                 break;
 7718 bruce@momjian.us          429                 :CBC           5 :             break;
                                430                 :             14 :         case PGP_PUB_ELG_ENCRYPT:
 7684                           431                 :             14 :             res = pgp_mpi_read(pf_key, &pk->sec.elg.x);
                                432                 :             14 :             break;
 7684 bruce@momjian.us          433                 :UBC           0 :         case PGP_PUB_DSA_SIGN:
                                434                 :              0 :             res = pgp_mpi_read(pf_key, &pk->sec.dsa.x);
 7718                           435                 :              0 :             break;
                                436                 :              0 :         default:
                                437                 :              0 :             px_debug("unknown public algo: %d", pk->algo);
                                438                 :              0 :             res = PXE_PGP_KEYPKT_CORRUPT;
                                439                 :                :     }
                                440                 :                :     /* read checksum / sha1 */
 7718 bruce@momjian.us          441         [ +  + ]:CBC          20 :     if (res >= 0)
                                442                 :                :     {
                                443         [ +  + ]:             18 :         if (hide_type == HIDE_SHA1)
                                444                 :              2 :             res = check_key_sha1(pf_key, pk);
                                445                 :                :         else
                                446                 :             16 :             res = check_key_cksum(pf_key, pk);
                                447                 :                :     }
                                448         [ +  + ]:             20 :     if (res >= 0)
                                449                 :             18 :         res = pgp_expect_packet_end(pf_key);
                                450                 :                : 
                                451         [ +  + ]:             20 :     if (pf_decrypt)
                                452                 :              4 :         pullf_free(pf_decrypt);
                                453         [ +  + ]:             20 :     if (cfb)
                                454                 :              4 :         pgp_cfb_free(cfb);
                                455                 :                : 
 7684                           456         [ +  + ]:             20 :     if (res < 0)
                                457                 :              2 :         pgp_key_free(pk);
                                458                 :                :     else
                                459                 :             18 :         *pk_p = pk;
                                460                 :                : 
 7718                           461                 :             20 :     return res;
                                462                 :                : }
                                463                 :                : 
                                464                 :                : static int
 6286                           465                 :             30 : internal_read_key(PullFilter *src, PGP_PubKey **pk_p,
                                466                 :                :                   const uint8 *psw, int psw_len, int pubtype)
                                467                 :                : {
 7718                           468                 :             30 :     PullFilter *pkt = NULL;
                                469                 :                :     int         res;
                                470                 :                :     uint8       tag;
                                471                 :                :     int         len;
 7684                           472                 :             30 :     PGP_PubKey *enc_key = NULL;
 7718                           473                 :             30 :     PGP_PubKey *pk = NULL;
 7621                           474                 :             30 :     int         got_main_key = 0;
                                475                 :                : 
                                476                 :                :     /*
                                477                 :                :      * Search for encryption key.
                                478                 :                :      *
                                479                 :                :      * Error out on anything fancy.
                                480                 :                :      */
                                481                 :                :     while (1)
                                482                 :                :     {
 7718                           483                 :            164 :         res = pgp_parse_pkt_hdr(src, &tag, &len, 0);
                                484         [ +  + ]:            164 :         if (res <= 0)
                                485                 :             26 :             break;
                                486                 :            138 :         res = pgp_create_pkt_reader(&pkt, src, len, res, NULL);
                                487         [ -  + ]:            138 :         if (res < 0)
 7718 bruce@momjian.us          488                 :UBC           0 :             break;
                                489                 :                : 
 7621 bruce@momjian.us          490   [ +  +  +  +  :CBC         138 :         switch (tag)
                                                 - ]
                                491                 :                :         {
 7684                           492                 :             29 :             case PGP_PKT_PUBLIC_KEY:
                                493                 :                :             case PGP_PKT_SECRET_KEY:
                                494         [ -  + ]:             29 :                 if (got_main_key)
                                495                 :                :                 {
 7718 bruce@momjian.us          496                 :UBC           0 :                     res = PXE_PGP_MULTIPLE_KEYS;
                                497                 :              0 :                     break;
                                498                 :                :                 }
 7684 bruce@momjian.us          499                 :CBC          29 :                 got_main_key = 1;
                                500                 :             29 :                 res = pgp_skip_packet(pkt);
                                501                 :             29 :                 break;
                                502                 :                : 
                                503                 :              6 :             case PGP_PKT_PUBLIC_SUBKEY:
                                504         [ -  + ]:              6 :                 if (pubtype != 0)
 7684 bruce@momjian.us          505                 :UBC           0 :                     res = PXE_PGP_EXPECT_SECRET_KEY;
                                506                 :                :                 else
 7684 bruce@momjian.us          507                 :CBC           6 :                     res = _pgp_read_public_key(pkt, &pk);
 7718                           508                 :              6 :                 break;
                                509                 :                : 
 7684                           510                 :             22 :             case PGP_PKT_SECRET_SUBKEY:
                                511         [ +  + ]:             22 :                 if (pubtype != 1)
                                512                 :              1 :                     res = PXE_PGP_EXPECT_PUBLIC_KEY;
                                513                 :                :                 else
                                514                 :             21 :                     res = process_secret_key(pkt, &pk, psw, psw_len);
 7718                           515                 :             22 :                 break;
                                516                 :                : 
                                517                 :             81 :             case PGP_PKT_SIGNATURE:
                                518                 :                :             case PGP_PKT_MARKER:
                                519                 :                :             case PGP_PKT_TRUST:
                                520                 :                :             case PGP_PKT_USER_ID:
                                521                 :                :             case PGP_PKT_USER_ATTR:
                                522                 :                :             case PGP_PKT_PRIV_61:
                                523                 :             81 :                 res = pgp_skip_packet(pkt);
                                524                 :             81 :                 break;
 7718 bruce@momjian.us          525                 :UBC           0 :             default:
                                526                 :              0 :                 px_debug("unknown/unexpected packet: %d", tag);
                                527                 :              0 :                 res = PXE_PGP_UNEXPECTED_PKT;
                                528                 :                :         }
 7718 bruce@momjian.us          529                 :CBC         138 :         pullf_free(pkt);
 7621                           530                 :            138 :         pkt = NULL;
                                531                 :                : 
 7684                           532         [ +  + ]:            138 :         if (pk != NULL)
                                533                 :                :         {
                                534   [ +  -  +  - ]:             24 :             if (res >= 0 && pk->can_encrypt)
                                535                 :                :             {
                                536         [ +  - ]:             24 :                 if (enc_key == NULL)
                                537                 :                :                 {
                                538                 :             24 :                     enc_key = pk;
                                539                 :             24 :                     pk = NULL;
                                540                 :                :                 }
                                541                 :                :                 else
 7684 bruce@momjian.us          542                 :UBC           0 :                     res = PXE_PGP_MULTIPLE_SUBKEYS;
                                543                 :                :             }
                                544                 :                : 
 7684 bruce@momjian.us          545         [ -  + ]:CBC          24 :             if (pk)
 7684 bruce@momjian.us          546                 :UBC           0 :                 pgp_key_free(pk);
 7684 bruce@momjian.us          547                 :CBC          24 :             pk = NULL;
                                548                 :                :         }
                                549                 :                : 
                                550         [ +  + ]:            138 :         if (res < 0)
 7718                           551                 :              4 :             break;
                                552                 :                :     }
                                553                 :                : 
                                554         [ -  + ]:             30 :     if (pkt)
 7718 bruce@momjian.us          555                 :UBC           0 :         pullf_free(pkt);
                                556                 :                : 
 7718 bruce@momjian.us          557         [ +  + ]:CBC          30 :     if (res < 0)
                                558                 :                :     {
 7684                           559         [ -  + ]:              4 :         if (enc_key)
 7684 bruce@momjian.us          560                 :UBC           0 :             pgp_key_free(enc_key);
 7684 bruce@momjian.us          561                 :CBC           4 :         return res;
                                562                 :                :     }
                                563                 :                : 
                                564         [ +  + ]:             26 :     if (!enc_key)
                                565                 :              2 :         res = PXE_PGP_NO_USABLE_KEY;
                                566                 :                :     else
                                567                 :             24 :         *pk_p = enc_key;
                                568                 :             26 :     return res;
                                569                 :                : }
                                570                 :                : 
                                571                 :                : int
 6286                           572                 :             30 : pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt,
                                573                 :                :                const uint8 *key, int key_len, int pubtype)
                                574                 :                : {
                                575                 :                :     int         res;
                                576                 :                :     PullFilter *src;
 7718                           577                 :             30 :     PGP_PubKey *pk = NULL;
                                578                 :                : 
                                579                 :             30 :     res = pullf_create_mbuf_reader(&src, keypkt);
                                580         [ -  + ]:             30 :     if (res < 0)
 7718 bruce@momjian.us          581                 :UBC           0 :         return res;
                                582                 :                : 
 7718 bruce@momjian.us          583                 :CBC          30 :     res = internal_read_key(src, &pk, key, key_len, pubtype);
                                584                 :             30 :     pullf_free(src);
                                585                 :                : 
                                586         [ +  + ]:             30 :     if (res >= 0)
                                587                 :             24 :         ctx->pub_key = pk;
                                588                 :                : 
                                589                 :             30 :     return res < 0 ? res : 0;
                                590                 :                : }
        

Generated by: LCOV version 2.0-1