cmp_status.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. /* CMP functions for PKIStatusInfo handling and PKIMessage decomposition */
  12. #include <string.h>
  13. #include "cmp_local.h"
  14. /* explicit #includes not strictly needed since implied by the above: */
  15. #include <time.h>
  16. #include <openssl/cmp.h>
  17. #include <openssl/crmf.h>
  18. #include <openssl/err.h> /* needed in case config no-deprecated */
  19. #include <openssl/engine.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/objects.h>
  22. #include <openssl/x509.h>
  23. #include <openssl/asn1err.h> /* for ASN1_R_TOO_SMALL and ASN1_R_TOO_LARGE */
  24. /* CMP functions related to PKIStatus */
  25. int ossl_cmp_pkisi_get_status(const OSSL_CMP_PKISI *si)
  26. {
  27. int res;
  28. if (!ossl_assert(si != NULL && si->status != NULL))
  29. return -1;
  30. res = ossl_cmp_asn1_get_int(si->status);
  31. return res == -2 ? -1 : res;
  32. }
  33. const char *ossl_cmp_PKIStatus_to_string(int status)
  34. {
  35. switch (status) {
  36. case OSSL_CMP_PKISTATUS_accepted:
  37. return "PKIStatus: accepted";
  38. case OSSL_CMP_PKISTATUS_grantedWithMods:
  39. return "PKIStatus: granted with modifications";
  40. case OSSL_CMP_PKISTATUS_rejection:
  41. return "PKIStatus: rejection";
  42. case OSSL_CMP_PKISTATUS_waiting:
  43. return "PKIStatus: waiting";
  44. case OSSL_CMP_PKISTATUS_revocationWarning:
  45. return "PKIStatus: revocation warning - a revocation of the cert is imminent";
  46. case OSSL_CMP_PKISTATUS_revocationNotification:
  47. return "PKIStatus: revocation notification - a revocation of the cert has occurred";
  48. case OSSL_CMP_PKISTATUS_keyUpdateWarning:
  49. return "PKIStatus: key update warning - update already done for the cert";
  50. default:
  51. ERR_raise_data(ERR_LIB_CMP, CMP_R_ERROR_PARSING_PKISTATUS,
  52. "PKIStatus: invalid=%d", status);
  53. return NULL;
  54. }
  55. }
  56. OSSL_CMP_PKIFREETEXT *ossl_cmp_pkisi_get0_statusString(const OSSL_CMP_PKISI *si)
  57. {
  58. if (!ossl_assert(si != NULL))
  59. return NULL;
  60. return si->statusString;
  61. }
  62. int ossl_cmp_pkisi_get_pkifailureinfo(const OSSL_CMP_PKISI *si)
  63. {
  64. int i;
  65. int res = 0;
  66. if (!ossl_assert(si != NULL))
  67. return -1;
  68. if (si->failInfo != NULL)
  69. for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++)
  70. if (ASN1_BIT_STRING_get_bit(si->failInfo, i))
  71. res |= 1 << i;
  72. return res;
  73. }
  74. /*-
  75. * convert PKIFailureInfo number to human-readable string
  76. * returns pointer to static string, or NULL on error
  77. */
  78. static const char *CMP_PKIFAILUREINFO_to_string(int number)
  79. {
  80. switch (number) {
  81. case OSSL_CMP_PKIFAILUREINFO_badAlg:
  82. return "badAlg";
  83. case OSSL_CMP_PKIFAILUREINFO_badMessageCheck:
  84. return "badMessageCheck";
  85. case OSSL_CMP_PKIFAILUREINFO_badRequest:
  86. return "badRequest";
  87. case OSSL_CMP_PKIFAILUREINFO_badTime:
  88. return "badTime";
  89. case OSSL_CMP_PKIFAILUREINFO_badCertId:
  90. return "badCertId";
  91. case OSSL_CMP_PKIFAILUREINFO_badDataFormat:
  92. return "badDataFormat";
  93. case OSSL_CMP_PKIFAILUREINFO_wrongAuthority:
  94. return "wrongAuthority";
  95. case OSSL_CMP_PKIFAILUREINFO_incorrectData:
  96. return "incorrectData";
  97. case OSSL_CMP_PKIFAILUREINFO_missingTimeStamp:
  98. return "missingTimeStamp";
  99. case OSSL_CMP_PKIFAILUREINFO_badPOP:
  100. return "badPOP";
  101. case OSSL_CMP_PKIFAILUREINFO_certRevoked:
  102. return "certRevoked";
  103. case OSSL_CMP_PKIFAILUREINFO_certConfirmed:
  104. return "certConfirmed";
  105. case OSSL_CMP_PKIFAILUREINFO_wrongIntegrity:
  106. return "wrongIntegrity";
  107. case OSSL_CMP_PKIFAILUREINFO_badRecipientNonce:
  108. return "badRecipientNonce";
  109. case OSSL_CMP_PKIFAILUREINFO_timeNotAvailable:
  110. return "timeNotAvailable";
  111. case OSSL_CMP_PKIFAILUREINFO_unacceptedPolicy:
  112. return "unacceptedPolicy";
  113. case OSSL_CMP_PKIFAILUREINFO_unacceptedExtension:
  114. return "unacceptedExtension";
  115. case OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable:
  116. return "addInfoNotAvailable";
  117. case OSSL_CMP_PKIFAILUREINFO_badSenderNonce:
  118. return "badSenderNonce";
  119. case OSSL_CMP_PKIFAILUREINFO_badCertTemplate:
  120. return "badCertTemplate";
  121. case OSSL_CMP_PKIFAILUREINFO_signerNotTrusted:
  122. return "signerNotTrusted";
  123. case OSSL_CMP_PKIFAILUREINFO_transactionIdInUse:
  124. return "transactionIdInUse";
  125. case OSSL_CMP_PKIFAILUREINFO_unsupportedVersion:
  126. return "unsupportedVersion";
  127. case OSSL_CMP_PKIFAILUREINFO_notAuthorized:
  128. return "notAuthorized";
  129. case OSSL_CMP_PKIFAILUREINFO_systemUnavail:
  130. return "systemUnavail";
  131. case OSSL_CMP_PKIFAILUREINFO_systemFailure:
  132. return "systemFailure";
  133. case OSSL_CMP_PKIFAILUREINFO_duplicateCertReq:
  134. return "duplicateCertReq";
  135. default:
  136. return NULL; /* illegal failure number */
  137. }
  138. }
  139. int ossl_cmp_pkisi_check_pkifailureinfo(const OSSL_CMP_PKISI *si, int bit_index)
  140. {
  141. if (!ossl_assert(si != NULL && si->failInfo != NULL))
  142. return -1;
  143. if (bit_index < 0 || bit_index > OSSL_CMP_PKIFAILUREINFO_MAX) {
  144. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  145. return -1;
  146. }
  147. return ASN1_BIT_STRING_get_bit(si->failInfo, bit_index);
  148. }
  149. /*-
  150. * place human-readable error string created from PKIStatusInfo in given buffer
  151. * returns pointer to the same buffer containing the string, or NULL on error
  152. */
  153. static char *snprint_PKIStatusInfo_parts(int status, int fail_info,
  154. const OSSL_CMP_PKIFREETEXT *status_strings,
  155. char *buf, size_t bufsize)
  156. {
  157. int failure;
  158. const char *status_string, *failure_string;
  159. ASN1_UTF8STRING *text;
  160. int i;
  161. int printed_chars;
  162. int failinfo_found = 0;
  163. int n_status_strings;
  164. char *write_ptr = buf;
  165. if (buf == NULL
  166. || status < 0
  167. || (status_string = ossl_cmp_PKIStatus_to_string(status)) == NULL)
  168. return NULL;
  169. #define ADVANCE_BUFFER \
  170. if (printed_chars < 0 || (size_t)printed_chars >= bufsize) \
  171. return NULL; \
  172. write_ptr += printed_chars; \
  173. bufsize -= printed_chars;
  174. printed_chars = BIO_snprintf(write_ptr, bufsize, "%s", status_string);
  175. ADVANCE_BUFFER;
  176. /*
  177. * failInfo is optional and may be empty;
  178. * if present, print failInfo before statusString because it is more concise
  179. */
  180. if (fail_info != -1 && fail_info != 0) {
  181. printed_chars = BIO_snprintf(write_ptr, bufsize, "; PKIFailureInfo: ");
  182. ADVANCE_BUFFER;
  183. for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
  184. if ((fail_info & (1 << failure)) != 0) {
  185. failure_string = CMP_PKIFAILUREINFO_to_string(failure);
  186. if (failure_string != NULL) {
  187. printed_chars = BIO_snprintf(write_ptr, bufsize, "%s%s",
  188. failinfo_found ? ", " : "",
  189. failure_string);
  190. ADVANCE_BUFFER;
  191. failinfo_found = 1;
  192. }
  193. }
  194. }
  195. }
  196. if (!failinfo_found && status != OSSL_CMP_PKISTATUS_accepted
  197. && status != OSSL_CMP_PKISTATUS_grantedWithMods) {
  198. printed_chars = BIO_snprintf(write_ptr, bufsize, "; <no failure info>");
  199. ADVANCE_BUFFER;
  200. }
  201. /* statusString sequence is optional and may be empty */
  202. n_status_strings = sk_ASN1_UTF8STRING_num(status_strings);
  203. if (n_status_strings > 0) {
  204. printed_chars = BIO_snprintf(write_ptr, bufsize, "; StatusString%s: ",
  205. n_status_strings > 1 ? "s" : "");
  206. ADVANCE_BUFFER;
  207. for (i = 0; i < n_status_strings; i++) {
  208. text = sk_ASN1_UTF8STRING_value(status_strings, i);
  209. printed_chars = BIO_snprintf(write_ptr, bufsize, "\"%.*s\"%s",
  210. ASN1_STRING_length(text),
  211. ASN1_STRING_get0_data(text),
  212. i < n_status_strings - 1 ? ", " : "");
  213. ADVANCE_BUFFER;
  214. }
  215. }
  216. #undef ADVANCE_BUFFER
  217. return buf;
  218. }
  219. char *OSSL_CMP_snprint_PKIStatusInfo(const OSSL_CMP_PKISI *statusInfo,
  220. char *buf, size_t bufsize)
  221. {
  222. int failure_info;
  223. if (statusInfo == NULL) {
  224. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  225. return NULL;
  226. }
  227. failure_info = ossl_cmp_pkisi_get_pkifailureinfo(statusInfo);
  228. return snprint_PKIStatusInfo_parts(ASN1_INTEGER_get(statusInfo->status),
  229. failure_info,
  230. statusInfo->statusString, buf, bufsize);
  231. }
  232. char *OSSL_CMP_CTX_snprint_PKIStatus(const OSSL_CMP_CTX *ctx, char *buf,
  233. size_t bufsize)
  234. {
  235. if (ctx == NULL) {
  236. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  237. return NULL;
  238. }
  239. return snprint_PKIStatusInfo_parts(OSSL_CMP_CTX_get_status(ctx),
  240. OSSL_CMP_CTX_get_failInfoCode(ctx),
  241. OSSL_CMP_CTX_get0_statusString(ctx),
  242. buf, bufsize);
  243. }
  244. /*-
  245. * Creates a new PKIStatusInfo structure and fills it in
  246. * returns a pointer to the structure on success, NULL on error
  247. * note: strongly overlaps with TS_RESP_CTX_set_status_info()
  248. * and TS_RESP_CTX_add_failure_info() in ../ts/ts_rsp_sign.c
  249. */
  250. OSSL_CMP_PKISI *OSSL_CMP_STATUSINFO_new(int status, int fail_info,
  251. const char *text)
  252. {
  253. OSSL_CMP_PKISI *si = OSSL_CMP_PKISI_new();
  254. ASN1_UTF8STRING *utf8_text = NULL;
  255. int failure;
  256. if (si == NULL)
  257. goto err;
  258. if (!ASN1_INTEGER_set(si->status, status))
  259. goto err;
  260. if (text != NULL) {
  261. if ((utf8_text = ASN1_UTF8STRING_new()) == NULL
  262. || !ASN1_STRING_set(utf8_text, text, -1))
  263. goto err;
  264. if ((si->statusString = sk_ASN1_UTF8STRING_new_null()) == NULL)
  265. goto err;
  266. if (!sk_ASN1_UTF8STRING_push(si->statusString, utf8_text))
  267. goto err;
  268. /* Ownership is lost. */
  269. utf8_text = NULL;
  270. }
  271. for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
  272. if ((fail_info & (1 << failure)) != 0) {
  273. if (si->failInfo == NULL
  274. && (si->failInfo = ASN1_BIT_STRING_new()) == NULL)
  275. goto err;
  276. if (!ASN1_BIT_STRING_set_bit(si->failInfo, failure, 1))
  277. goto err;
  278. }
  279. }
  280. return si;
  281. err:
  282. OSSL_CMP_PKISI_free(si);
  283. ASN1_UTF8STRING_free(utf8_text);
  284. return NULL;
  285. }