|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Andreas Steffen, strongSec GmbH |
| 3 | + * |
| 4 | + * Copyright (C) secunet Security Networks AG |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License as published by the |
| 8 | + * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * for more details. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <library.h> |
| 18 | +#include <utils/debug.h> |
| 19 | + |
| 20 | +#include "openxpki_ocsp_responder.h" |
| 21 | + |
| 22 | +typedef struct private_openxpki_ocsp_responder_t private_openxpki_ocsp_responder_t; |
| 23 | + |
| 24 | +/** |
| 25 | + * Private Data of a openxpki_ocsp_responder_t object. |
| 26 | + */ |
| 27 | +struct private_openxpki_ocsp_responder_t { |
| 28 | + |
| 29 | + /** |
| 30 | + * Public data |
| 31 | + */ |
| 32 | + openxpki_ocsp_responder_t public; |
| 33 | + |
| 34 | + /** |
| 35 | + * OpenXPKI certificate database |
| 36 | + */ |
| 37 | + database_t *db; |
| 38 | +}; |
| 39 | + |
| 40 | +METHOD(ocsp_responder_t, get_status, cert_validation_t, |
| 41 | + private_openxpki_ocsp_responder_t *this, certificate_t *cacert, |
| 42 | + chunk_t serial_number, time_t *revocation_time, crl_reason_t *reason) |
| 43 | +{ |
| 44 | + cert_validation_t validation = VALIDATION_FAILED; |
| 45 | + int rev_time; |
| 46 | + const int max_decimals = 49; |
| 47 | + const int max_bytes = max_decimals / 2.4083; |
| 48 | + char serial[max_decimals + 1], authKeyId[60]; |
| 49 | + char *status, *reason_code; |
| 50 | + chunk_t subjectKeyId; |
| 51 | + public_key_t *public; |
| 52 | + enumerator_t *e; |
| 53 | + bool success; |
| 54 | + |
| 55 | + /* convert serialNumber from binary to decimal as required for the DB query. |
| 56 | + * check for a potential overflow since the database table field supports |
| 57 | + * up to 49 decimal digits, only. |
| 58 | + */ |
| 59 | + if (serial_number.len > max_bytes) |
| 60 | + { |
| 61 | + DBG1(DBG_LIB, "serialNumber conversion exceeds %d decimals", max_decimals); |
| 62 | + return VALIDATION_FAILED; |
| 63 | + |
| 64 | + } |
| 65 | + chunk_to_dec(serial_number, serial); |
| 66 | + |
| 67 | + /* additionally use the subjectyKeyId of the issuing CA for the DB query */ |
| 68 | + public = cacert->get_public_key(cacert); |
| 69 | + success = public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &subjectKeyId); |
| 70 | + public->destroy(public); |
| 71 | + |
| 72 | + if (!success) |
| 73 | + { |
| 74 | + DBG1(DBG_LIB, "failed to extract subjectKeyId from CA certificate"); |
| 75 | + return VALIDATION_FAILED; |
| 76 | + } |
| 77 | + |
| 78 | + /* the authKeyId of the certificate is the subjectKeyId of the issuing CA */ |
| 79 | + snprintf(authKeyId, sizeof(authKeyId), "%#B", &subjectKeyId); |
| 80 | + |
| 81 | + /* query the OpenXPKI certificate database */ |
| 82 | + e = this->db->query(this->db, "SELECT status, reason_code, revocation_time " |
| 83 | + "FROM certificate WHERE cert_key = ? " |
| 84 | + "AND authority_key_identifier = ?", DB_TEXT, serial, |
| 85 | + DB_TEXT, authKeyId, DB_TEXT, DB_TEXT, DB_INT); |
| 86 | + if (e && e->enumerate(e, &status, &reason_code, &rev_time)) |
| 87 | + { |
| 88 | + if (streq(status, "ISSUED") || streq(status, "ISSUANCE_PENDING")) |
| 89 | + { |
| 90 | + validation = VALIDATION_GOOD; |
| 91 | + } |
| 92 | + else if (streq(status, "UNKNOWN")) |
| 93 | + { |
| 94 | + validation = VALIDATION_FAILED; |
| 95 | + } |
| 96 | + else if (streq(status, "REVOKED") || streq(status, "HOLD") || |
| 97 | + streq(status, "CRL_ISSUANCE_PENDING")) |
| 98 | + { |
| 99 | + validation = VALIDATION_REVOKED; |
| 100 | + |
| 101 | + if (revocation_time) |
| 102 | + { |
| 103 | + *revocation_time = rev_time; |
| 104 | + } |
| 105 | + if (reason) |
| 106 | + { |
| 107 | + if (streq(reason_code, "unspecified")) |
| 108 | + { |
| 109 | + *reason = CRL_REASON_UNSPECIFIED; |
| 110 | + } |
| 111 | + else if (streq(reason_code, "keyCompromise")) |
| 112 | + { |
| 113 | + *reason = CRL_REASON_KEY_COMPROMISE; |
| 114 | + } |
| 115 | + else if (streq(reason_code, "CACompromise")) |
| 116 | + { |
| 117 | + *reason = CRL_REASON_CA_COMPROMISE; |
| 118 | + } |
| 119 | + else if (streq(reason_code, "affiliationChanged")) |
| 120 | + { |
| 121 | + *reason = CRL_REASON_AFFILIATION_CHANGED; |
| 122 | + } |
| 123 | + else if (streq(reason_code, "superseded")) |
| 124 | + { |
| 125 | + *reason = CRL_REASON_SUPERSEDED; |
| 126 | + } |
| 127 | + else if (streq(reason_code, "cessationOfOperation")) |
| 128 | + { |
| 129 | + *reason = CRL_REASON_CESSATION_OF_OPERATION; |
| 130 | + } |
| 131 | + else if (streq(reason_code, "certificateHold")) |
| 132 | + { |
| 133 | + *reason = CRL_REASON_CERTIFICATE_HOLD; |
| 134 | + validation = VALIDATION_ON_HOLD; |
| 135 | + } |
| 136 | + else if (streq(reason_code, "removeFromCRL")) |
| 137 | + { |
| 138 | + *reason = CRL_REASON_REMOVE_FROM_CRL; |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + *reason = CRL_REASON_UNSPECIFIED; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + DESTROY_IF(e); |
| 148 | + |
| 149 | + return validation; |
| 150 | +} |
| 151 | + |
| 152 | +METHOD(ocsp_responder_t, destroy, void, |
| 153 | + private_openxpki_ocsp_responder_t *this) |
| 154 | +{ |
| 155 | + this->db->destroy(this->db); |
| 156 | + free(this); |
| 157 | +} |
| 158 | + |
| 159 | +/* |
| 160 | + * See header |
| 161 | + */ |
| 162 | +ocsp_responder_t *openxpki_ocsp_responder_create() |
| 163 | +{ |
| 164 | + private_openxpki_ocsp_responder_t *this; |
| 165 | + database_t *db; |
| 166 | + char *uri; |
| 167 | + |
| 168 | + uri = lib->settings->get_str(lib->settings, |
| 169 | + "%s.plugins.openxpki.database", NULL, lib->ns); |
| 170 | + if (!uri) |
| 171 | + { |
| 172 | + DBG1(DBG_CFG, "openxpki database URI missing"); |
| 173 | + return NULL; |
| 174 | + } |
| 175 | + db = lib->db->create(lib->db, uri); |
| 176 | + if (!db) |
| 177 | + { |
| 178 | + DBG1(DBG_CFG, "opening openxpki database failed"); |
| 179 | + return NULL; |
| 180 | + } |
| 181 | + INIT(this, |
| 182 | + .public = { |
| 183 | + .interface = { |
| 184 | + .get_status = _get_status, |
| 185 | + .destroy = _destroy, |
| 186 | + }, |
| 187 | + }, |
| 188 | + .db = db, |
| 189 | + ); |
| 190 | + |
| 191 | + return &this->public.interface; |
| 192 | +} |
0 commit comments