cmp_status.c 11 KB

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