x_req.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include "crypto/x509.h"
  14. /*-
  15. * X509_REQ_INFO is handled in an unusual way to get round
  16. * invalid encodings. Some broken certificate requests don't
  17. * encode the attributes field if it is empty. This is in
  18. * violation of PKCS#10 but we need to tolerate it. We do
  19. * this by making the attributes field OPTIONAL then using
  20. * the callback to initialise it to an empty STACK.
  21. *
  22. * This means that the field will be correctly encoded unless
  23. * we NULL out the field.
  24. *
  25. * As a result we no longer need the req_kludge field because
  26. * the information is now contained in the attributes field:
  27. * 1. If it is NULL then it's the invalid omission.
  28. * 2. If it is empty it is the correct encoding.
  29. * 3. If it is not empty then some attributes are present.
  30. *
  31. */
  32. static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  33. void *exarg)
  34. {
  35. X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval;
  36. if (operation == ASN1_OP_NEW_POST) {
  37. rinf->attributes = sk_X509_ATTRIBUTE_new_null();
  38. if (!rinf->attributes)
  39. return 0;
  40. }
  41. return 1;
  42. }
  43. static int req_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  44. void *exarg)
  45. {
  46. X509_REQ *ret = (X509_REQ *)*pval;
  47. switch (operation) {
  48. case ASN1_OP_D2I_PRE:
  49. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  50. /* fall through */
  51. case ASN1_OP_NEW_POST:
  52. ret->distinguishing_id = NULL;
  53. break;
  54. case ASN1_OP_FREE_POST:
  55. ASN1_OCTET_STRING_free(ret->distinguishing_id);
  56. OPENSSL_free(ret->propq);
  57. break;
  58. case ASN1_OP_DUP_POST:
  59. {
  60. X509_REQ *old = exarg;
  61. if (!ossl_x509_req_set0_libctx(ret, old->libctx, old->propq))
  62. return 0;
  63. if (old->req_info.pubkey != NULL) {
  64. EVP_PKEY *pkey = X509_PUBKEY_get0(old->req_info.pubkey);
  65. if (pkey != NULL) {
  66. pkey = EVP_PKEY_dup(pkey);
  67. if (pkey == NULL) {
  68. ERR_raise(ERR_LIB_X509, ERR_R_EVP_LIB);
  69. return 0;
  70. }
  71. if (!X509_PUBKEY_set(&ret->req_info.pubkey, pkey)) {
  72. EVP_PKEY_free(pkey);
  73. ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
  74. return 0;
  75. }
  76. EVP_PKEY_free(pkey);
  77. }
  78. }
  79. }
  80. break;
  81. case ASN1_OP_GET0_LIBCTX:
  82. {
  83. OSSL_LIB_CTX **libctx = exarg;
  84. *libctx = ret->libctx;
  85. }
  86. break;
  87. case ASN1_OP_GET0_PROPQ:
  88. {
  89. const char **propq = exarg;
  90. *propq = ret->propq;
  91. }
  92. break;
  93. }
  94. return 1;
  95. }
  96. ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = {
  97. ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER),
  98. ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME),
  99. ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY),
  100. /* This isn't really OPTIONAL but it gets round invalid
  101. * encodings
  102. */
  103. ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0)
  104. } ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO)
  105. IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO)
  106. ASN1_SEQUENCE_ref(X509_REQ, req_cb) = {
  107. ASN1_EMBED(X509_REQ, req_info, X509_REQ_INFO),
  108. ASN1_EMBED(X509_REQ, sig_alg, X509_ALGOR),
  109. ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING)
  110. } ASN1_SEQUENCE_END_ref(X509_REQ, X509_REQ)
  111. IMPLEMENT_ASN1_FUNCTIONS(X509_REQ)
  112. IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ)
  113. void X509_REQ_set0_distinguishing_id(X509_REQ *x, ASN1_OCTET_STRING *d_id)
  114. {
  115. ASN1_OCTET_STRING_free(x->distinguishing_id);
  116. x->distinguishing_id = d_id;
  117. }
  118. ASN1_OCTET_STRING *X509_REQ_get0_distinguishing_id(X509_REQ *x)
  119. {
  120. return x->distinguishing_id;
  121. }
  122. /*
  123. * This should only be used if the X509_REQ object was embedded inside another
  124. * asn1 object and it needs a libctx to operate.
  125. * Use X509_REQ_new_ex() instead if possible.
  126. */
  127. int ossl_x509_req_set0_libctx(X509_REQ *x, OSSL_LIB_CTX *libctx,
  128. const char *propq)
  129. {
  130. if (x != NULL) {
  131. x->libctx = libctx;
  132. OPENSSL_free(x->propq);
  133. x->propq = NULL;
  134. if (propq != NULL) {
  135. x->propq = OPENSSL_strdup(propq);
  136. if (x->propq == NULL)
  137. return 0;
  138. }
  139. }
  140. return 1;
  141. }
  142. X509_REQ *X509_REQ_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
  143. {
  144. X509_REQ *req = NULL;
  145. req = (X509_REQ *)ASN1_item_new((X509_REQ_it()));
  146. if (!ossl_x509_req_set0_libctx(req, libctx, propq)) {
  147. X509_REQ_free(req);
  148. req = NULL;
  149. }
  150. return req;
  151. }