LCOV - code coverage report
Current view: top level - libpkg - rsa.c (source / functions) Hit Total Coverage
Test: cov.info Lines: 3 151 2.0 %
Date: 2015-08-15 Functions: 1 10 10.0 %

          Line data    Source code
       1             : /*-
       2             :  * Copyright (c) 2011-2013 Baptiste Daroussin <bapt@FreeBSD.org>
       3             :  * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
       4             :  * All rights reserved.
       5             :  * 
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  * 1. Redistributions of source code must retain the above copyright
      10             :  *    notice, this list of conditions and the following disclaimer
      11             :  *    in this position and unchanged.
      12             :  * 2. Redistributions in binary form must reproduce the above copyright
      13             :  *    notice, this list of conditions and the following disclaimer in the
      14             :  *    documentation and/or other materials provided with the distribution.
      15             :  * 
      16             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      17             :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      18             :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      19             :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      20             :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      21             :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      22             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      23             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      24             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      25             :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      26             :  */
      27             : 
      28             : #include <sys/stat.h>
      29             : #include <sys/param.h>
      30             : 
      31             : #include <fcntl.h>
      32             : 
      33             : #include <openssl/err.h>
      34             : #include <openssl/rsa.h>
      35             : #include <openssl/ssl.h>
      36             : 
      37             : 
      38             : #include "pkg.h"
      39             : #include "private/event.h"
      40             : #include "private/pkg.h"
      41             : 
      42             : static int
      43           0 : _load_rsa_private_key(struct rsa_key *rsa)
      44             : {
      45             :         FILE *fp;
      46             : 
      47           0 :         if ((fp = fopen(rsa->path, "r")) == NULL)
      48           0 :                 return (EPKG_FATAL);
      49             : 
      50           0 :         if ((rsa->key = RSA_new()) == NULL) {
      51           0 :                 fclose(fp);
      52           0 :                 return (EPKG_FATAL);
      53             :         }
      54             : 
      55           0 :         rsa->key = PEM_read_RSAPrivateKey(fp, 0, rsa->pw_cb, rsa->path);
      56           0 :         if (rsa->key == NULL) {
      57           0 :                 fclose(fp);
      58           0 :                 return (EPKG_FATAL);
      59             :         }
      60             : 
      61           0 :         fclose(fp);
      62           0 :         return (EPKG_OK);
      63             : }
      64             : 
      65             : static RSA *
      66           0 : _load_rsa_public_key(const char *rsa_key_path)
      67             : {
      68             :         FILE *fp;
      69           0 :         RSA *rsa = NULL;
      70             :         char errbuf[1024];
      71             : 
      72           0 :         if ((fp = fopen(rsa_key_path, "rb")) == NULL) {
      73           0 :                 pkg_emit_errno("fopen", rsa_key_path);
      74           0 :                 return (NULL);
      75             :         }
      76             : 
      77           0 :         if (!PEM_read_RSA_PUBKEY(fp, &rsa, NULL, NULL)) {
      78           0 :                 pkg_emit_error("error reading public key(%s): %s", rsa_key_path,
      79             :                     ERR_error_string(ERR_get_error(), errbuf));
      80           0 :                 fclose(fp);
      81           0 :                 return (NULL);
      82             :         }
      83             : 
      84           0 :         fclose(fp);
      85           0 :         return (rsa);
      86             : }
      87             : 
      88             : static RSA *
      89           0 : _load_rsa_public_key_buf(unsigned char *cert, int certlen)
      90             : {
      91           0 :         RSA *rsa = NULL;
      92             :         BIO *bp;
      93             :         char errbuf[1024];
      94             : 
      95           0 :         bp = BIO_new_mem_buf((void *)cert, certlen);
      96           0 :         if (!PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) {
      97           0 :                 pkg_emit_error("error reading public key: %s",
      98             :                     ERR_error_string(ERR_get_error(), errbuf));
      99           0 :                 BIO_free(bp);
     100           0 :                 return (NULL);
     101             :         }
     102           0 :         BIO_free(bp);
     103           0 :         return (rsa);
     104             : }
     105             : 
     106             : struct rsa_verify_cbdata {
     107             :         unsigned char *key;
     108             :         size_t keylen;
     109             :         unsigned char *sig;
     110             :         size_t siglen;
     111             : };
     112             : 
     113             : static int
     114           0 : rsa_verify_cert_cb(int fd, void *ud)
     115             : {
     116           0 :         struct rsa_verify_cbdata *cbdata = ud;
     117             :         char *sha256;
     118             :         char *hash;
     119             :         char errbuf[1024];
     120           0 :         RSA *rsa = NULL;
     121             :         int ret;
     122             : 
     123           0 :         sha256 = pkg_checksum_fd(fd, PKG_HASH_TYPE_SHA256_HEX);
     124           0 :         if (sha256 == NULL)
     125           0 :                 return (EPKG_FATAL);
     126             : 
     127           0 :         hash = pkg_checksum_data(sha256, strlen(sha256),
     128             :             PKG_HASH_TYPE_SHA256_RAW);
     129           0 :         free(sha256);
     130             : 
     131           0 :         rsa = _load_rsa_public_key_buf(cbdata->key, cbdata->keylen);
     132           0 :         if (rsa == NULL) {
     133           0 :                 free(hash);
     134           0 :                 return (EPKG_FATAL);
     135             :         }
     136           0 :         ret = RSA_verify(NID_sha256, hash,
     137           0 :             pkg_checksum_type_size(PKG_HASH_TYPE_SHA256_RAW), cbdata->sig,
     138           0 :             cbdata->siglen, rsa);
     139           0 :         free(hash);
     140           0 :         if (ret == 0) {
     141           0 :                 pkg_emit_error("rsa verify failed: %s",
     142             :                                 ERR_error_string(ERR_get_error(), errbuf));
     143           0 :                 RSA_free(rsa);
     144           0 :                 return (EPKG_FATAL);
     145             :         }
     146             : 
     147           0 :         RSA_free(rsa);
     148             : 
     149           0 :         return (EPKG_OK);
     150             : }
     151             : 
     152             : int
     153           0 : rsa_verify_cert(const char *path, unsigned char *key, int keylen,
     154             :     unsigned char *sig, int siglen, int fd)
     155             : {
     156             :         int ret;
     157           0 :         bool need_close = false;
     158             :         struct rsa_verify_cbdata cbdata;
     159             : 
     160           0 :         if (fd == -1) {
     161           0 :                 if ((fd = open(path, O_RDONLY)) == -1) {
     162           0 :                         pkg_emit_errno("fopen", path);
     163           0 :                         return (EPKG_FATAL);
     164             :                 }
     165           0 :                 need_close = true;
     166             :         }
     167           0 :         (void)lseek(fd, 0, SEEK_SET);
     168             : 
     169           0 :         cbdata.key = key;
     170           0 :         cbdata.keylen = keylen;
     171           0 :         cbdata.sig = sig;
     172           0 :         cbdata.siglen = siglen;
     173             : 
     174           0 :         SSL_load_error_strings();
     175           0 :         OpenSSL_add_all_algorithms();
     176           0 :         OpenSSL_add_all_ciphers();
     177             : 
     178           0 :         ret = pkg_emit_sandbox_call(rsa_verify_cert_cb, fd, &cbdata);
     179           0 :         if (need_close)
     180           0 :                 close(fd);
     181             : 
     182           0 :         return (ret);
     183             : }
     184             : 
     185             : static int
     186           0 : rsa_verify_cb(int fd, void *ud)
     187             : {
     188           0 :         struct rsa_verify_cbdata *cbdata = ud;
     189             :         char *sha256;
     190             :         char errbuf[1024];
     191           0 :         RSA *rsa = NULL;
     192             :         int ret;
     193             : 
     194           0 :         sha256 = pkg_checksum_fd(fd, PKG_HASH_TYPE_SHA256_HEX);
     195           0 :         if (sha256 == NULL)
     196           0 :                 return (EPKG_FATAL);
     197             : 
     198           0 :         rsa = _load_rsa_public_key_buf(cbdata->key, cbdata->keylen);
     199           0 :         if (rsa == NULL) {
     200           0 :                 free(sha256);
     201           0 :                 return(EPKG_FATAL);
     202             :         }
     203             : 
     204           0 :         ret = RSA_verify(NID_sha1, sha256, sizeof(sha256), cbdata->sig,
     205           0 :             cbdata->siglen, rsa);
     206           0 :         free(sha256);
     207           0 :         if (ret == 0) {
     208           0 :                 pkg_emit_error("%s: %s", cbdata->key,
     209             :                     ERR_error_string(ERR_get_error(), errbuf));
     210           0 :                 RSA_free(rsa);
     211           0 :                 return (EPKG_FATAL);
     212             :         }
     213             : 
     214           0 :         RSA_free(rsa);
     215             : 
     216           0 :         return (EPKG_OK);
     217             : }
     218             : 
     219             : int
     220           0 : rsa_verify(const char *path, const char *key, unsigned char *sig,
     221             :     unsigned int sig_len, int fd)
     222             : {
     223             :         int ret;
     224           0 :         bool need_close = false;
     225             :         struct rsa_verify_cbdata cbdata;
     226             :         char *key_buf;
     227             :         off_t key_len;
     228             : 
     229           0 :         if (file_to_buffer(key, (char**)&key_buf, &key_len) != EPKG_OK) {
     230           0 :                 pkg_emit_errno("rsa_verify", "cannot read key");
     231           0 :                 return (EPKG_FATAL);
     232             :         }
     233             : 
     234           0 :         if (fd == -1) {
     235           0 :                 if ((fd = open(path, O_RDONLY)) == -1) {
     236           0 :                         pkg_emit_errno("fopen", path);
     237           0 :                         free(key_buf);
     238           0 :                         return (EPKG_FATAL);
     239             :                 }
     240           0 :                 need_close = true;
     241             :         }
     242           0 :         (void)lseek(fd, 0, SEEK_SET);
     243             : 
     244           0 :         cbdata.key = key_buf;
     245           0 :         cbdata.keylen = key_len;
     246           0 :         cbdata.sig = sig;
     247           0 :         cbdata.siglen = sig_len;
     248             : 
     249           0 :         SSL_load_error_strings();
     250           0 :         OpenSSL_add_all_algorithms();
     251           0 :         OpenSSL_add_all_ciphers();
     252             : 
     253           0 :         ret = pkg_emit_sandbox_call(rsa_verify_cb, fd, &cbdata);
     254           0 :         if (need_close)
     255           0 :                 close(fd);
     256             : 
     257           0 :         free(key_buf);
     258             : 
     259           0 :         return (ret);
     260             : }
     261             : 
     262             : int
     263           0 : rsa_sign(char *path, struct rsa_key *rsa, unsigned char **sigret, unsigned int *siglen)
     264             : {
     265             :         char errbuf[1024];
     266           0 :         int max_len = 0, ret;
     267             :         char *sha256;
     268             : 
     269           0 :         if (access(rsa->path, R_OK) == -1) {
     270           0 :                 pkg_emit_errno("access", rsa->path);
     271           0 :                 return (EPKG_FATAL);
     272             :         }
     273             : 
     274           0 :         if (rsa->key == NULL && _load_rsa_private_key(rsa) != EPKG_OK) {
     275           0 :                 pkg_emit_error("can't load key from %s", rsa->path);
     276           0 :                 return (EPKG_FATAL);
     277             :         }
     278             : 
     279           0 :         max_len = RSA_size(rsa->key);
     280           0 :         *sigret = calloc(1, max_len + 1);
     281             : 
     282           0 :         sha256 = pkg_checksum_file(path, PKG_HASH_TYPE_SHA256_HEX);
     283           0 :         if (sha256 == NULL)
     284           0 :                 return (EPKG_FATAL);
     285             : 
     286           0 :         ret = RSA_sign(NID_sha1, sha256, sizeof(sha256), *sigret, siglen, rsa->key);
     287           0 :         free(sha256);
     288           0 :         if (ret == 0) {
     289             :                 /* XXX pass back RSA errors correctly */
     290           0 :                 pkg_emit_error("%s: %s", rsa->path,
     291             :                    ERR_error_string(ERR_get_error(), errbuf));
     292           0 :                 return (EPKG_FATAL);
     293             :         }
     294             : 
     295           0 :         return (EPKG_OK);
     296             : }
     297             : 
     298             : int
     299           0 : rsa_new(struct rsa_key **rsa, pem_password_cb *cb, char *path)
     300             : {
     301           0 :         assert(*rsa == NULL);
     302             : 
     303           0 :         *rsa = calloc(1, sizeof(struct rsa_key));
     304           0 :         (*rsa)->path = path;
     305           0 :         (*rsa)->pw_cb = cb;
     306             : 
     307           0 :         SSL_load_error_strings();
     308             : 
     309           0 :         OpenSSL_add_all_algorithms();
     310           0 :         OpenSSL_add_all_ciphers();
     311             : 
     312           0 :         return (EPKG_OK);
     313             : }
     314             : 
     315             : void
     316          11 : rsa_free(struct rsa_key *rsa)
     317             : {
     318          11 :         if (rsa == NULL)
     319          22 :                 return;
     320             : 
     321           0 :         if (rsa->key != NULL)
     322           0 :                 RSA_free(rsa->key);
     323             : 
     324           0 :         free(rsa);
     325           0 :         ERR_free_strings();
     326             : }
     327             : 

Generated by: LCOV version 1.10