LCOV - code coverage report
Current view: top level - libpkg - pkg_key.c (source / functions) Hit Total Coverage
Test: plop Lines: 25 40 62.5 %
Date: 2024-12-28 18:40:32 Functions: 5 6 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 5 14 35.7 %

           Branch data     Line data    Source code
       1                 :            : /*-
       2                 :            :  * Copyright (c) 2021 Kyle Evans <kevans@FreeBSD.org>
       3                 :            :  *
       4                 :            :  * Redistribution and use in source and binary forms, with or without
       5                 :            :  * modification, are permitted provided that the following conditions
       6                 :            :  * are met:
       7                 :            :  * 1. Redistributions of source code must retain the above copyright
       8                 :            :  *    notice, this list of conditions and the following disclaimer
       9                 :            :  *    in this position and unchanged.
      10                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      11                 :            :  *    notice, this list of conditions and the following disclaimer in the
      12                 :            :  *    documentation and/or other materials provided with the distribution.
      13                 :            :  *
      14                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      15                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      16                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      17                 :            :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      18                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      19                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      20                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      21                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      22                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      23                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      24                 :            :  */
      25                 :            : 
      26                 :            : #include <sys/cdefs.h>
      27                 :            : 
      28                 :            : #include <assert.h>
      29                 :            : #include <errno.h>
      30                 :            : #include <stdlib.h>
      31                 :            : #include <string.h>
      32                 :            : 
      33                 :            : #include "pkg.h"
      34                 :            : #include "xmalloc.h"
      35                 :            : #include "private/pkg.h"
      36                 :            : #include "private/pkgsign.h"
      37                 :            : 
      38                 :            : int
      39                 :         15 : pkg_key_new(struct pkg_key **key, const char *keytype, const char *keypath,
      40                 :            :     pkg_password_cb *cb)
      41                 :            : {
      42                 :            :         struct pkg_key *nkey;
      43                 :         15 :         struct pkgsign_ctx *ctx = NULL;
      44                 :            :         int ret;
      45                 :            : 
      46         [ +  - ]:         15 :         assert(*key == NULL);
      47         [ +  - ]:         15 :         assert(keytype != NULL);        /* XXX for now. */
      48         [ +  - ]:         15 :         if (*keypath == '\0')
      49                 :          0 :                 return (EPKG_FATAL);
      50                 :            : 
      51                 :         15 :         ret = pkgsign_new_sign(keytype, &ctx);
      52         [ -  + ]:         15 :         if (ret != 0)
      53                 :          0 :                 return (EPKG_FATAL);
      54                 :            : 
      55                 :         15 :         pkgsign_set(ctx, cb, keypath);
      56                 :            : 
      57                 :         15 :         nkey = xcalloc(1, sizeof(*nkey));
      58                 :         15 :         nkey->ctx = ctx;
      59                 :            : 
      60                 :         15 :         *key = nkey;
      61                 :         15 :         return (EPKG_OK);
      62                 :         15 : }
      63                 :            : 
      64                 :            : void
      65                 :         15 : pkg_key_free(struct pkg_key *key)
      66                 :            : {
      67                 :            : 
      68                 :         15 :         pkgsign_free(key->ctx);
      69                 :         15 :         free(key);
      70                 :         15 : }
      71                 :            : 
      72                 :            : /*
      73                 :            :  * Key generation callbacks may take any number of options, so we handle those
      74                 :            :  * with an iovec.  The pkg_key layer does not discriminate, beyond enforcing
      75                 :            :  * that options come in pairs.  The intention is that the first option in every
      76                 :            :  * pair names the option.
      77                 :            :  */
      78                 :            : int
      79                 :         12 : pkg_key_create(struct pkg_key *key, const struct iovec *iov, int niov)
      80                 :            : {
      81                 :            : 
      82                 :            :         /* Malformed arguments; must come in pairs. */
      83         [ -  + ]:         12 :         if ((niov % 2) != 0)
      84                 :          0 :                 return (EPKG_FATAL);
      85                 :            : 
      86                 :         12 :         return (pkgsign_generate(key->ctx, iov, niov));
      87                 :         12 : }
      88                 :            : 
      89                 :            : int
      90                 :          2 : pkg_key_sign_data(struct pkg_key *key, const unsigned char *msg, size_t msgsz,
      91                 :            :     unsigned char **sig, size_t *siglen)
      92                 :            : {
      93                 :            : 
      94                 :          2 :         return (pkgsign_sign_data(key->ctx, msg, msgsz, sig, siglen));
      95                 :            : }
      96                 :            : 
      97                 :            : int
      98                 :          0 : pkg_key_info(struct pkg_key *key, struct iovec **iov, int *niov)
      99                 :            : {
     100                 :            :         int rc;
     101                 :            :         struct iovec *kiov;
     102                 :            :         int nkiov;
     103                 :            : 
     104                 :          0 :         kiov = NULL;
     105                 :          0 :         rc = pkgsign_keyinfo(key->ctx, &kiov, &nkiov);
     106         [ #  # ]:          0 :         if (rc != EPKG_OK)
     107                 :          0 :                 return (rc);
     108         [ #  # ]:          0 :         if ((nkiov % 2) != 0) {
     109                 :          0 :                 free(kiov);
     110                 :          0 :                 return (EPKG_FATAL);
     111                 :            :         }
     112                 :            : 
     113                 :          0 :         *iov = kiov;
     114                 :          0 :         *niov = nkiov;
     115                 :            : 
     116                 :          0 :         return (EPKG_OK);
     117                 :          0 : }
     118                 :            : 
     119                 :            : int
     120                 :         13 : pkg_key_pubkey(struct pkg_key *key, char **pubkey, size_t *len)
     121                 :            : {
     122                 :            : 
     123                 :         13 :         return (pkgsign_pubkey(key->ctx, pubkey, len));
     124                 :            : }

Generated by: LCOV version 1.15