v3_akid.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 1999-2022 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/conf.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509v3.h>
  15. #include "crypto/x509.h"
  16. #include "ext_dat.h"
  17. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  18. AUTHORITY_KEYID *akeyid,
  19. STACK_OF(CONF_VALUE)
  20. *extlist);
  21. static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  22. X509V3_CTX *ctx,
  23. STACK_OF(CONF_VALUE) *values);
  24. const X509V3_EXT_METHOD ossl_v3_akey_id = {
  25. NID_authority_key_identifier,
  26. X509V3_EXT_MULTILINE, ASN1_ITEM_ref(AUTHORITY_KEYID),
  27. 0, 0, 0, 0,
  28. 0, 0,
  29. (X509V3_EXT_I2V) i2v_AUTHORITY_KEYID,
  30. (X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
  31. 0, 0,
  32. NULL
  33. };
  34. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  35. AUTHORITY_KEYID *akeyid,
  36. STACK_OF(CONF_VALUE)
  37. *extlist)
  38. {
  39. char *tmp = NULL;
  40. STACK_OF(CONF_VALUE) *origextlist = extlist, *tmpextlist;
  41. if (akeyid->keyid) {
  42. tmp = i2s_ASN1_OCTET_STRING(NULL, akeyid->keyid);
  43. if (tmp == NULL) {
  44. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  45. return NULL;
  46. }
  47. if (!X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL,
  48. tmp, &extlist)) {
  49. OPENSSL_free(tmp);
  50. ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB);
  51. goto err;
  52. }
  53. OPENSSL_free(tmp);
  54. }
  55. if (akeyid->issuer) {
  56. tmpextlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
  57. if (tmpextlist == NULL) {
  58. ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB);
  59. goto err;
  60. }
  61. extlist = tmpextlist;
  62. }
  63. if (akeyid->serial) {
  64. tmp = i2s_ASN1_OCTET_STRING(NULL, akeyid->serial);
  65. if (tmp == NULL) {
  66. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  67. goto err;
  68. }
  69. if (!X509V3_add_value("serial", tmp, &extlist)) {
  70. OPENSSL_free(tmp);
  71. goto err;
  72. }
  73. OPENSSL_free(tmp);
  74. }
  75. return extlist;
  76. err:
  77. if (origextlist == NULL)
  78. sk_CONF_VALUE_pop_free(extlist, X509V3_conf_free);
  79. return NULL;
  80. }
  81. /*-
  82. * Three explicit tags may be given, where 'keyid' and 'issuer' may be combined:
  83. * 'none': do not add any authority key identifier.
  84. * 'keyid': use the issuer's subject keyid; the option 'always' means its is
  85. * an error if the issuer certificate doesn't have a subject key id.
  86. * 'issuer': use the issuer's cert issuer and serial number. The default is
  87. * to only use this if 'keyid' is not present. With the option 'always'
  88. * this is always included.
  89. */
  90. static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  91. X509V3_CTX *ctx,
  92. STACK_OF(CONF_VALUE) *values)
  93. {
  94. char keyid = 0, issuer = 0;
  95. int i, n = sk_CONF_VALUE_num(values);
  96. CONF_VALUE *cnf;
  97. ASN1_OCTET_STRING *ikeyid = NULL;
  98. X509_NAME *isname = NULL;
  99. GENERAL_NAMES *gens = NULL;
  100. GENERAL_NAME *gen = NULL;
  101. ASN1_INTEGER *serial = NULL;
  102. X509_EXTENSION *ext;
  103. X509 *issuer_cert;
  104. int same_issuer, ss;
  105. AUTHORITY_KEYID *akeyid = AUTHORITY_KEYID_new();
  106. if (akeyid == NULL)
  107. goto err;
  108. if (n == 1 && strcmp(sk_CONF_VALUE_value(values, 0)->name, "none") == 0) {
  109. return akeyid;
  110. }
  111. for (i = 0; i < n; i++) {
  112. cnf = sk_CONF_VALUE_value(values, i);
  113. if (cnf->value != NULL && strcmp(cnf->value, "always") != 0) {
  114. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION,
  115. "name=%s option=%s", cnf->name, cnf->value);
  116. goto err;
  117. }
  118. if (strcmp(cnf->name, "keyid") == 0 && keyid == 0) {
  119. keyid = 1;
  120. if (cnf->value != NULL)
  121. keyid = 2;
  122. } else if (strcmp(cnf->name, "issuer") == 0 && issuer == 0) {
  123. issuer = 1;
  124. if (cnf->value != NULL)
  125. issuer = 2;
  126. } else if (strcmp(cnf->name, "none") == 0
  127. || strcmp(cnf->name, "keyid") == 0
  128. || strcmp(cnf->name, "issuer") == 0) {
  129. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_VALUE,
  130. "name=%s", cnf->name);
  131. goto err;
  132. } else {
  133. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_VALUE,
  134. "name=%s", cnf->name);
  135. goto err;
  136. }
  137. }
  138. if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
  139. return akeyid;
  140. if (ctx == NULL) {
  141. ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
  142. goto err;
  143. }
  144. if ((issuer_cert = ctx->issuer_cert) == NULL) {
  145. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE);
  146. goto err;
  147. }
  148. same_issuer = ctx->subject_cert == ctx->issuer_cert;
  149. ERR_set_mark();
  150. if (ctx->issuer_pkey != NULL)
  151. ss = X509_check_private_key(ctx->subject_cert, ctx->issuer_pkey);
  152. else
  153. ss = same_issuer;
  154. ERR_pop_to_mark();
  155. /* unless forced with "always", AKID is suppressed for self-signed certs */
  156. if (keyid == 2 || (keyid == 1 && !ss)) {
  157. /*
  158. * prefer any pre-existing subject key identifier of the issuer cert
  159. * except issuer cert is same as subject cert and is not self-signed
  160. */
  161. i = X509_get_ext_by_NID(issuer_cert, NID_subject_key_identifier, -1);
  162. if (i >= 0 && (ext = X509_get_ext(issuer_cert, i)) != NULL
  163. && !(same_issuer && !ss)) {
  164. ikeyid = X509V3_EXT_d2i(ext);
  165. if (ASN1_STRING_length(ikeyid) == 0) /* indicating "none" */ {
  166. ASN1_OCTET_STRING_free(ikeyid);
  167. ikeyid = NULL;
  168. }
  169. }
  170. if (ikeyid == NULL && same_issuer && ctx->issuer_pkey != NULL) {
  171. /* generate fallback AKID, emulating s2i_skey_id(..., "hash") */
  172. X509_PUBKEY *pubkey = NULL;
  173. if (X509_PUBKEY_set(&pubkey, ctx->issuer_pkey))
  174. ikeyid = ossl_x509_pubkey_hash(pubkey);
  175. X509_PUBKEY_free(pubkey);
  176. }
  177. if (keyid == 2 && ikeyid == NULL) {
  178. ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
  179. goto err;
  180. }
  181. }
  182. if (issuer == 2 || (issuer == 1 && !ss && ikeyid == NULL)) {
  183. isname = X509_NAME_dup(X509_get_issuer_name(issuer_cert));
  184. serial = ASN1_INTEGER_dup(X509_get0_serialNumber(issuer_cert));
  185. if (isname == NULL || serial == NULL) {
  186. ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
  187. goto err;
  188. }
  189. }
  190. if (isname != NULL) {
  191. if ((gens = sk_GENERAL_NAME_new_null()) == NULL
  192. || (gen = GENERAL_NAME_new()) == NULL) {
  193. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  194. goto err;
  195. }
  196. if (!sk_GENERAL_NAME_push(gens, gen)) {
  197. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  198. goto err;
  199. }
  200. gen->type = GEN_DIRNAME;
  201. gen->d.dirn = isname;
  202. }
  203. akeyid->issuer = gens;
  204. gen = NULL;
  205. gens = NULL;
  206. akeyid->serial = serial;
  207. akeyid->keyid = ikeyid;
  208. return akeyid;
  209. err:
  210. sk_GENERAL_NAME_free(gens);
  211. GENERAL_NAME_free(gen);
  212. X509_NAME_free(isname);
  213. ASN1_INTEGER_free(serial);
  214. ASN1_OCTET_STRING_free(ikeyid);
  215. AUTHORITY_KEYID_free(akeyid);
  216. return NULL;
  217. }