LCOV - code coverage report
Current view: top level - ocsp - ocsp_srv.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 0 76 0.0 %
Date: 2014-08-02 Functions: 0 9 0.0 %
Branches: 0 86 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* ocsp_srv.c */
       2                 :            : /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
       3                 :            :  * project 2001.
       4                 :            :  */
       5                 :            : /* ====================================================================
       6                 :            :  * Copyright (c) 1998-2001 The OpenSSL Project.  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                 :            :  *
      12                 :            :  * 1. Redistributions of source code must retain the above copyright
      13                 :            :  *    notice, this list of conditions and the following disclaimer. 
      14                 :            :  *
      15                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      16                 :            :  *    notice, this list of conditions and the following disclaimer in
      17                 :            :  *    the documentation and/or other materials provided with the
      18                 :            :  *    distribution.
      19                 :            :  *
      20                 :            :  * 3. All advertising materials mentioning features or use of this
      21                 :            :  *    software must display the following acknowledgment:
      22                 :            :  *    "This product includes software developed by the OpenSSL Project
      23                 :            :  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
      24                 :            :  *
      25                 :            :  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
      26                 :            :  *    endorse or promote products derived from this software without
      27                 :            :  *    prior written permission. For written permission, please contact
      28                 :            :  *    openssl-core@openssl.org.
      29                 :            :  *
      30                 :            :  * 5. Products derived from this software may not be called "OpenSSL"
      31                 :            :  *    nor may "OpenSSL" appear in their names without prior written
      32                 :            :  *    permission of the OpenSSL Project.
      33                 :            :  *
      34                 :            :  * 6. Redistributions of any form whatsoever must retain the following
      35                 :            :  *    acknowledgment:
      36                 :            :  *    "This product includes software developed by the OpenSSL Project
      37                 :            :  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
      38                 :            :  *
      39                 :            :  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
      40                 :            :  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      41                 :            :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
      42                 :            :  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
      43                 :            :  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      44                 :            :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      45                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      46                 :            :  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      47                 :            :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
      48                 :            :  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      49                 :            :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
      50                 :            :  * OF THE POSSIBILITY OF SUCH DAMAGE.
      51                 :            :  * ====================================================================
      52                 :            :  *
      53                 :            :  * This product includes cryptographic software written by Eric Young
      54                 :            :  * (eay@cryptsoft.com).  This product includes software written by Tim
      55                 :            :  * Hudson (tjh@cryptsoft.com).
      56                 :            :  *
      57                 :            :  */
      58                 :            : 
      59                 :            : #include <stdio.h>
      60                 :            : #include <cryptlib.h>
      61                 :            : #include <openssl/objects.h>
      62                 :            : #include <openssl/rand.h>
      63                 :            : #include <openssl/x509.h>
      64                 :            : #include <openssl/pem.h>
      65                 :            : #include <openssl/x509v3.h>
      66                 :            : #include <openssl/ocsp.h>
      67                 :            : 
      68                 :            : /* Utility functions related to sending OCSP responses and extracting
      69                 :            :  * relevant information from the request.
      70                 :            :  */
      71                 :            : 
      72                 :          0 : int OCSP_request_onereq_count(OCSP_REQUEST *req)
      73                 :            :         {
      74                 :          0 :         return sk_OCSP_ONEREQ_num(req->tbsRequest->requestList);
      75                 :            :         }
      76                 :            : 
      77                 :          0 : OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
      78                 :            :         {
      79                 :          0 :         return sk_OCSP_ONEREQ_value(req->tbsRequest->requestList, i);
      80                 :            :         }
      81                 :            : 
      82                 :          0 : OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
      83                 :            :         {
      84                 :          0 :         return one->reqCert;
      85                 :            :         }
      86                 :            : 
      87                 :          0 : int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
      88                 :            :                         ASN1_OCTET_STRING **pikeyHash,
      89                 :            :                         ASN1_INTEGER **pserial, OCSP_CERTID *cid)
      90                 :            :         {
      91         [ #  # ]:          0 :         if (!cid) return 0;
      92         [ #  # ]:          0 :         if (pmd) *pmd = cid->hashAlgorithm->algorithm;
      93         [ #  # ]:          0 :         if(piNameHash) *piNameHash = cid->issuerNameHash;
      94         [ #  # ]:          0 :         if (pikeyHash) *pikeyHash = cid->issuerKeyHash;
      95         [ #  # ]:          0 :         if (pserial) *pserial = cid->serialNumber;
      96                 :            :         return 1;
      97                 :            :         }
      98                 :            : 
      99                 :          0 : int OCSP_request_is_signed(OCSP_REQUEST *req)
     100                 :            :         {
     101         [ #  # ]:          0 :         if(req->optionalSignature) return 1;
     102                 :          0 :         return 0;
     103                 :            :         }
     104                 :            : 
     105                 :            : /* Create an OCSP response and encode an optional basic response */
     106                 :          0 : OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
     107                 :            :         {
     108                 :          0 :         OCSP_RESPONSE *rsp = NULL;
     109                 :            : 
     110         [ #  # ]:          0 :         if (!(rsp = OCSP_RESPONSE_new())) goto err;
     111         [ #  # ]:          0 :         if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) goto err;
     112         [ #  # ]:          0 :         if (!bs) return rsp;
     113         [ #  # ]:          0 :         if (!(rsp->responseBytes = OCSP_RESPBYTES_new())) goto err;
     114                 :          0 :         rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
     115         [ #  # ]:          0 :         if (!ASN1_item_pack(bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
     116                 :            :                                 goto err;
     117                 :            :         return rsp;
     118                 :            : err:
     119         [ #  # ]:          0 :         if (rsp) OCSP_RESPONSE_free(rsp);
     120                 :            :         return NULL;
     121                 :            :         }
     122                 :            : 
     123                 :            : 
     124                 :          0 : OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
     125                 :            :                                                 OCSP_CERTID *cid,
     126                 :            :                                                 int status, int reason,
     127                 :            :                                                 ASN1_TIME *revtime,
     128                 :            :                                         ASN1_TIME *thisupd, ASN1_TIME *nextupd)
     129                 :            :         {
     130                 :          0 :         OCSP_SINGLERESP *single = NULL;
     131                 :            :         OCSP_CERTSTATUS *cs;
     132                 :            :         OCSP_REVOKEDINFO *ri;
     133                 :            : 
     134   [ #  #  #  # ]:          0 :         if(!rsp->tbsResponseData->responses &&
     135                 :          0 :             !(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new_null()))
     136                 :            :                 goto err;
     137                 :            : 
     138         [ #  # ]:          0 :         if (!(single = OCSP_SINGLERESP_new()))
     139                 :            :                 goto err;
     140                 :            : 
     141                 :            : 
     142                 :            : 
     143         [ #  # ]:          0 :         if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
     144                 :            :                 goto err;
     145   [ #  #  #  # ]:          0 :         if (nextupd &&
     146                 :          0 :                 !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
     147                 :            :                 goto err;
     148                 :            : 
     149                 :          0 :         OCSP_CERTID_free(single->certId);
     150                 :            : 
     151         [ #  # ]:          0 :         if(!(single->certId = OCSP_CERTID_dup(cid)))
     152                 :            :                 goto err;
     153                 :            : 
     154                 :          0 :         cs = single->certStatus;
     155   [ #  #  #  # ]:          0 :         switch(cs->type = status)
     156                 :            :                 {
     157                 :            :         case V_OCSP_CERTSTATUS_REVOKED:
     158         [ #  # ]:          0 :                 if (!revtime)
     159                 :            :                         {
     160                 :          0 :                         OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS,OCSP_R_NO_REVOKED_TIME);
     161                 :          0 :                         goto err;
     162                 :            :                         }
     163         [ #  # ]:          0 :                 if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new())) goto err;
     164         [ #  # ]:          0 :                 if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
     165                 :            :                         goto err;       
     166         [ #  # ]:          0 :                 if (reason != OCSP_REVOKED_STATUS_NOSTATUS)
     167                 :            :                         {
     168         [ #  # ]:          0 :                         if (!(ri->revocationReason = ASN1_ENUMERATED_new())) 
     169                 :            :                                 goto err;
     170         [ #  # ]:          0 :                         if (!(ASN1_ENUMERATED_set(ri->revocationReason, 
     171                 :            :                                                   reason)))
     172                 :            :                                 goto err;       
     173                 :            :                         }
     174                 :            :                 break;
     175                 :            : 
     176                 :            :         case V_OCSP_CERTSTATUS_GOOD:
     177                 :          0 :                 cs->value.good = ASN1_NULL_new();
     178                 :          0 :                 break;
     179                 :            : 
     180                 :            :         case V_OCSP_CERTSTATUS_UNKNOWN:
     181                 :          0 :                 cs->value.unknown = ASN1_NULL_new();
     182                 :          0 :                 break;
     183                 :            : 
     184                 :            :         default:
     185                 :            :                 goto err;
     186                 :            : 
     187                 :            :                 }
     188         [ #  # ]:          0 :         if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses, single)))
     189                 :            :                 goto err;
     190                 :            :         return single;
     191                 :            : err:
     192                 :          0 :         OCSP_SINGLERESP_free(single);
     193                 :          0 :         return NULL;
     194                 :            :         }
     195                 :            : 
     196                 :            : /* Add a certificate to an OCSP request */
     197                 :            : 
     198                 :          0 : int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
     199                 :            :         {
     200 [ #  # ][ #  # ]:          0 :         if (!resp->certs && !(resp->certs = sk_X509_new_null()))
     201                 :            :                 return 0;
     202                 :            : 
     203         [ #  # ]:          0 :         if(!sk_X509_push(resp->certs, cert)) return 0;
     204                 :          0 :         CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
     205                 :          0 :         return 1;
     206                 :            :         }
     207                 :            : 
     208                 :          0 : int OCSP_basic_sign(OCSP_BASICRESP *brsp, 
     209                 :            :                         X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
     210                 :            :                         STACK_OF(X509) *certs, unsigned long flags)
     211                 :            :         {
     212                 :            :         int i;
     213                 :            :         OCSP_RESPID *rid;
     214                 :            : 
     215         [ #  # ]:          0 :         if (!X509_check_private_key(signer, key))
     216                 :            :                 {
     217                 :          0 :                 OCSPerr(OCSP_F_OCSP_BASIC_SIGN, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
     218                 :          0 :                 goto err;
     219                 :            :                 }
     220                 :            : 
     221         [ #  # ]:          0 :         if(!(flags & OCSP_NOCERTS))
     222                 :            :                 {
     223         [ #  # ]:          0 :                 if(!OCSP_basic_add1_cert(brsp, signer))
     224                 :            :                         goto err;
     225         [ #  # ]:          0 :                 for (i = 0; i < sk_X509_num(certs); i++)
     226                 :            :                         {
     227                 :          0 :                         X509 *tmpcert = sk_X509_value(certs, i);
     228         [ #  # ]:          0 :                         if(!OCSP_basic_add1_cert(brsp, tmpcert))
     229                 :            :                                 goto err;
     230                 :            :                         }
     231                 :            :                 }
     232                 :            : 
     233                 :          0 :         rid = brsp->tbsResponseData->responderId;
     234         [ #  # ]:          0 :         if (flags & OCSP_RESPID_KEY)
     235                 :            :                 {
     236                 :            :                 unsigned char md[SHA_DIGEST_LENGTH];
     237                 :          0 :                 X509_pubkey_digest(signer, EVP_sha1(), md, NULL);
     238         [ #  # ]:          0 :                 if (!(rid->value.byKey = ASN1_OCTET_STRING_new()))
     239                 :            :                         goto err;
     240         [ #  # ]:          0 :                 if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH)))
     241                 :            :                                 goto err;
     242                 :          0 :                 rid->type = V_OCSP_RESPID_KEY;
     243                 :            :                 }
     244                 :            :         else
     245                 :            :                 {
     246         [ #  # ]:          0 :                 if (!X509_NAME_set(&rid->value.byName,
     247                 :            :                                         X509_get_subject_name(signer)))
     248                 :            :                                 goto err;
     249                 :          0 :                 rid->type = V_OCSP_RESPID_NAME;
     250                 :            :                 }
     251                 :            : 
     252   [ #  #  #  # ]:          0 :         if (!(flags & OCSP_NOTIME) &&
     253                 :          0 :                 !X509_gmtime_adj(brsp->tbsResponseData->producedAt, 0))
     254                 :            :                 goto err;
     255                 :            : 
     256                 :            :         /* Right now, I think that not doing double hashing is the right
     257                 :            :            thing.       -- Richard Levitte */
     258                 :            : 
     259         [ #  # ]:          0 :         if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0)) goto err;
     260                 :            : 
     261                 :            :         return 1;
     262                 :            : err:
     263                 :            :         return 0;
     264                 :            :         }

Generated by: LCOV version 1.9