cmp_status.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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
  154. char *snprint_PKIStatusInfo_parts(int status, int fail_info,
  155. const OSSL_CMP_PKIFREETEXT *status_strings,
  156. char *buf, size_t bufsize)
  157. {
  158. int failure;
  159. const char *status_string, *failure_string;
  160. ASN1_UTF8STRING *text;
  161. int i;
  162. int printed_chars;
  163. int failinfo_found = 0;
  164. int n_status_strings;
  165. char *write_ptr = buf;
  166. if (buf == NULL
  167. || status < 0
  168. || (status_string = ossl_cmp_PKIStatus_to_string(status)) == NULL)
  169. return NULL;
  170. #define ADVANCE_BUFFER \
  171. if (printed_chars < 0 || (size_t)printed_chars >= bufsize) \
  172. return NULL; \
  173. write_ptr += printed_chars; \
  174. bufsize -= printed_chars;
  175. printed_chars = BIO_snprintf(write_ptr, bufsize, "%s", status_string);
  176. ADVANCE_BUFFER;
  177. /*
  178. * failInfo is optional and may be empty;
  179. * if present, print failInfo before statusString because it is more concise
  180. */
  181. if (fail_info != -1 && fail_info != 0) {
  182. printed_chars = BIO_snprintf(write_ptr, bufsize, "; PKIFailureInfo: ");
  183. ADVANCE_BUFFER;
  184. for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
  185. if ((fail_info & (1 << failure)) != 0) {
  186. failure_string = CMP_PKIFAILUREINFO_to_string(failure);
  187. if (failure_string != NULL) {
  188. printed_chars = BIO_snprintf(write_ptr, bufsize, "%s%s",
  189. failinfo_found ? ", " : "",
  190. failure_string);
  191. ADVANCE_BUFFER;
  192. failinfo_found = 1;
  193. }
  194. }
  195. }
  196. }
  197. if (!failinfo_found && status != OSSL_CMP_PKISTATUS_accepted
  198. && status != OSSL_CMP_PKISTATUS_grantedWithMods) {
  199. printed_chars = BIO_snprintf(write_ptr, bufsize, "; <no failure info>");
  200. ADVANCE_BUFFER;
  201. }
  202. /* statusString sequence is optional and may be empty */
  203. n_status_strings = sk_ASN1_UTF8STRING_num(status_strings);
  204. if (n_status_strings > 0) {
  205. printed_chars = BIO_snprintf(write_ptr, bufsize, "; StatusString%s: ",
  206. n_status_strings > 1 ? "s" : "");
  207. ADVANCE_BUFFER;
  208. for (i = 0; i < n_status_strings; i++) {
  209. text = sk_ASN1_UTF8STRING_value(status_strings, i);
  210. printed_chars = BIO_snprintf(write_ptr, bufsize, "\"%.*s\"%s",
  211. ASN1_STRING_length(text),
  212. ASN1_STRING_get0_data(text),
  213. i < n_status_strings - 1 ? ", " : "");
  214. ADVANCE_BUFFER;
  215. }
  216. }
  217. #undef ADVANCE_BUFFER
  218. return buf;
  219. }
  220. char *OSSL_CMP_snprint_PKIStatusInfo(const OSSL_CMP_PKISI *statusInfo,
  221. char *buf, size_t bufsize)
  222. {
  223. int failure_info;
  224. if (statusInfo == NULL) {
  225. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  226. return NULL;
  227. }
  228. failure_info = ossl_cmp_pkisi_get_pkifailureinfo(statusInfo);
  229. return snprint_PKIStatusInfo_parts(ASN1_INTEGER_get(statusInfo->status),
  230. failure_info,
  231. statusInfo->statusString, buf, bufsize);
  232. }
  233. char *OSSL_CMP_CTX_snprint_PKIStatus(const OSSL_CMP_CTX *ctx, char *buf,
  234. size_t bufsize)
  235. {
  236. if (ctx == NULL) {
  237. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  238. return NULL;
  239. }
  240. return snprint_PKIStatusInfo_parts(OSSL_CMP_CTX_get_status(ctx),
  241. OSSL_CMP_CTX_get_failInfoCode(ctx),
  242. OSSL_CMP_CTX_get0_statusString(ctx),
  243. buf, bufsize);
  244. }
  245. /*-
  246. * Creates a new PKIStatusInfo structure and fills it in
  247. * returns a pointer to the structure on success, NULL on error
  248. * note: strongly overlaps with TS_RESP_CTX_set_status_info()
  249. * and TS_RESP_CTX_add_failure_info() in ../ts/ts_rsp_sign.c
  250. */
  251. OSSL_CMP_PKISI *OSSL_CMP_STATUSINFO_new(int status, int fail_info,
  252. const char *text)
  253. {
  254. OSSL_CMP_PKISI *si = OSSL_CMP_PKISI_new();
  255. ASN1_UTF8STRING *utf8_text = NULL;
  256. int failure;
  257. if (si == NULL)
  258. goto err;
  259. if (!ASN1_INTEGER_set(si->status, status))
  260. goto err;
  261. if (text != NULL) {
  262. if ((utf8_text = ASN1_UTF8STRING_new()) == NULL
  263. || !ASN1_STRING_set(utf8_text, text, -1))
  264. goto err;
  265. if ((si->statusString = sk_ASN1_UTF8STRING_new_null()) == NULL)
  266. goto err;
  267. if (!sk_ASN1_UTF8STRING_push(si->statusString, utf8_text))
  268. goto err;
  269. /* Ownership is lost. */
  270. utf8_text = NULL;
  271. }
  272. for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
  273. if ((fail_info & (1 << failure)) != 0) {
  274. if (si->failInfo == NULL
  275. && (si->failInfo = ASN1_BIT_STRING_new()) == NULL)
  276. goto err;
  277. if (!ASN1_BIT_STRING_set_bit(si->failInfo, failure, 1))
  278. goto err;
  279. }
  280. }
  281. return si;
  282. err:
  283. OSSL_CMP_PKISI_free(si);
  284. ASN1_UTF8STRING_free(utf8_text);
  285. return NULL;
  286. }