ocsp_common.c
Go to the documentation of this file.
1 /**
2  * @file ocsp_common.c
3  * @brief OCSP common definitions
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL OCSP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ocsp/ocsp_common.h"
36 #include "encoding/oid.h"
37 
38 //Check crypto library configuration
39 #if (OCSP_SUPPORT == ENABLED)
40 
41 //PKIX OCSP Basic OID (1.3.6.1.5.5.7.48.1.1)
42 const uint8_t PKIX_OCSP_BASIC_OID[9] = {0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01};
43 //PKIX OCSP Nonce OID (1.3.6.1.5.5.7.48.1.2)
44 const uint8_t PKIX_OCSP_NONCE_OID[9] = {0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x02};
45 
46 
47 /**
48  * @brief Hash algorithm selection
49  * @return Pointer to the preferred hash algorithm
50  **/
51 
53 {
54  const HashAlgo *hashAlgo;
55 
56 #if (OCSP_SHA1_SUPPORT == ENABLED && SHA1_SUPPORT == ENABLED)
57  //Select SHA-1 hash algorithm
58  hashAlgo = SHA1_HASH_ALGO;
59 #elif (OCSP_SHA256_SUPPORT == ENABLED && SHA256_SUPPORT == ENABLED)
60  //Select SHA-256 hash algorithm
61  hashAlgo = SHA256_HASH_ALGO;
62 #elif (OCSP_SHA384_SUPPORT == ENABLED && SHA384_SUPPORT == ENABLED)
63  //Select SHA-384 hash algorithm
64  hashAlgo = SHA384_HASH_ALGO;
65 #elif (OCSP_SHA512_SUPPORT == ENABLED && SHA512_SUPPORT == ENABLED)
66  //Select SHA-512 hash algorithm
67  hashAlgo = SHA512_HASH_ALGO;
68 #else
69  //Just for sanity
70  hashAlgo = NULL;
71 #endif
72 
73  //Return the preferred hash algorithm
74  return hashAlgo;
75 }
76 
77 
78 /**
79  * @brief Get the hash algorithm that matches the specified identifier
80  * @param[in] oid Hash algorithm OID
81  * @param[in] length Length of the hash algorithm OID, in bytes
82  * @return Pointer to the hash algorithm
83  **/
84 
85 const HashAlgo *ocspGetHashAlgo(const uint8_t *oid, size_t length)
86 {
87  const HashAlgo *hashAlgo;
88 
89 #if (OCSP_SHA1_SUPPORT == ENABLED && SHA1_SUPPORT == ENABLED)
90  //SHA-1 hash algorithm identifier?
91  if(!oidComp(oid, length, SHA1_OID, sizeof(SHA1_OID)))
92  {
93  hashAlgo = SHA1_HASH_ALGO;
94  }
95  else
96 #endif
97 #if (OCSP_SHA256_SUPPORT == ENABLED && SHA256_SUPPORT == ENABLED)
98  //SHA-256 hash algorithm identifier?
99  if(!oidComp(oid, length, SHA256_OID, sizeof(SHA256_OID)))
100  {
101  hashAlgo = SHA256_HASH_ALGO;
102  }
103  else
104 #endif
105 #if (OCSP_SHA384_SUPPORT == ENABLED && SHA384_SUPPORT == ENABLED)
106  //SHA-384 hash algorithm identifier?
107  if(!oidComp(oid, length, SHA384_OID, sizeof(SHA384_OID)))
108  {
109  hashAlgo = SHA384_HASH_ALGO;
110  }
111  else
112 #endif
113 #if (OCSP_SHA512_SUPPORT == ENABLED && SHA512_SUPPORT == ENABLED)
114  //SHA-512 hash algorithm identifier?
115  if(!oidComp(oid, length, SHA512_OID, sizeof(SHA512_OID)))
116  {
117  hashAlgo = SHA512_HASH_ALGO;
118  }
119  else
120 #endif
121  //Unknown hash algorithm identifier?
122  {
123  hashAlgo = NULL;
124  }
125 
126  //Return the hash algorithm that matches the specified OID
127  return hashAlgo;
128 }
129 
130 #endif
uint8_t oid[]
Definition: lldp_tlv.h:300
const uint8_t PKIX_OCSP_BASIC_OID[9]
Definition: ocsp_common.c:42
const uint8_t PKIX_OCSP_NONCE_OID[9]
Definition: ocsp_common.c:44
const HashAlgo * ocspGetHashAlgo(const uint8_t *oid, size_t length)
Get the hash algorithm that matches the specified identifier.
Definition: ocsp_common.c:85
const HashAlgo * ocspSelectHashAlgo(void)
Hash algorithm selection.
Definition: ocsp_common.c:52
OCSP common definitions.
int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2)
Compare object identifiers.
Definition: oid.c:103
OID (Object Identifier)
const uint8_t SHA1_OID[5]
Definition: sha1.c:73
#define SHA1_HASH_ALGO
Definition: sha1.h:49
const uint8_t SHA256_OID[9]
Definition: sha256.c:80
#define SHA256_HASH_ALGO
Definition: sha256.h:49
const uint8_t SHA384_OID[9]
Definition: sha384.c:47
#define SHA384_HASH_ALGO
Definition: sha384.h:45
const uint8_t SHA512_OID[9]
Definition: sha512.c:97
#define SHA512_HASH_ALGO
Definition: sha512.h:49
Common interface for hash algorithms.
Definition: crypto.h:1014
uint8_t length
Definition: tcp.h:368