v3_akid.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 1999-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/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 = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length);
  43. if (tmp == NULL) {
  44. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  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 = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length);
  65. if (tmp == NULL) {
  66. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  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. * Currently two options:
  83. * keyid: use the issuers subject keyid, the value 'always' means its is
  84. * an error if the issuer certificate doesn't have a key id.
  85. * issuer: use the issuers cert issuer and serial number. The default is
  86. * to only use this if keyid is not present. With the option 'always'
  87. * this is always included.
  88. */
  89. static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  90. X509V3_CTX *ctx,
  91. STACK_OF(CONF_VALUE) *values)
  92. {
  93. char keyid = 0, issuer = 0;
  94. int i, n = sk_CONF_VALUE_num(values);
  95. CONF_VALUE *cnf;
  96. ASN1_OCTET_STRING *ikeyid = NULL;
  97. X509_NAME *isname = NULL;
  98. GENERAL_NAMES *gens = NULL;
  99. GENERAL_NAME *gen = NULL;
  100. ASN1_INTEGER *serial = NULL;
  101. X509_EXTENSION *ext;
  102. X509 *issuer_cert;
  103. int same_issuer, ss;
  104. AUTHORITY_KEYID *akeyid = AUTHORITY_KEYID_new();
  105. if (akeyid == NULL)
  106. goto err;
  107. if (n == 1 && strcmp(sk_CONF_VALUE_value(values, 0)->name, "none") == 0) {
  108. return akeyid;
  109. }
  110. for (i = 0; i < n; i++) {
  111. cnf = sk_CONF_VALUE_value(values, i);
  112. if (strcmp(cnf->name, "keyid") == 0) {
  113. keyid = 1;
  114. if (cnf->value && strcmp(cnf->value, "always") == 0)
  115. keyid = 2;
  116. } else if (strcmp(cnf->name, "issuer") == 0) {
  117. issuer = 1;
  118. if (cnf->value && strcmp(cnf->value, "always") == 0)
  119. issuer = 2;
  120. } else {
  121. ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION,
  122. "name=%s", cnf->name);
  123. goto err;
  124. }
  125. }
  126. if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
  127. return akeyid;
  128. if (ctx == NULL) {
  129. ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
  130. goto err;
  131. }
  132. if ((issuer_cert = ctx->issuer_cert) == NULL) {
  133. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE);
  134. goto err;
  135. }
  136. same_issuer = ctx->subject_cert == ctx->issuer_cert;
  137. ERR_set_mark();
  138. if (ctx->issuer_pkey != NULL)
  139. ss = X509_check_private_key(ctx->subject_cert, ctx->issuer_pkey);
  140. else
  141. ss = same_issuer;
  142. ERR_pop_to_mark();
  143. /* unless forced with "always", AKID is suppressed for self-signed certs */
  144. if (keyid == 2 || (keyid == 1 && !ss)) {
  145. /*
  146. * prefer any pre-existing subject key identifier of the issuer cert
  147. * except issuer cert is same as subject cert and is not self-signed
  148. */
  149. i = X509_get_ext_by_NID(issuer_cert, NID_subject_key_identifier, -1);
  150. if (i >= 0 && (ext = X509_get_ext(issuer_cert, i)) != NULL
  151. && !(same_issuer && !ss))
  152. ikeyid = X509V3_EXT_d2i(ext);
  153. if (ikeyid == NULL && same_issuer && ctx->issuer_pkey != NULL) {
  154. /* generate fallback AKID, emulating s2i_skey_id(..., "hash") */
  155. X509_PUBKEY *pubkey = NULL;
  156. if (X509_PUBKEY_set(&pubkey, ctx->issuer_pkey))
  157. ikeyid = ossl_x509_pubkey_hash(pubkey);
  158. X509_PUBKEY_free(pubkey);
  159. }
  160. if ((keyid == 2 || issuer == 0)
  161. && (ikeyid == NULL
  162. || ASN1_STRING_length(ikeyid) <= 2) /* indicating "none" */) {
  163. ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
  164. goto err;
  165. }
  166. }
  167. if (issuer == 2 || (issuer == 1 && ikeyid == NULL)) {
  168. isname = X509_NAME_dup(X509_get_issuer_name(issuer_cert));
  169. serial = ASN1_INTEGER_dup(X509_get0_serialNumber(issuer_cert));
  170. if (isname == NULL || serial == NULL) {
  171. ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
  172. goto err;
  173. }
  174. }
  175. if (isname != NULL) {
  176. if ((gens = sk_GENERAL_NAME_new_null()) == NULL
  177. || (gen = GENERAL_NAME_new()) == NULL
  178. || !sk_GENERAL_NAME_push(gens, gen)) {
  179. ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
  180. goto err;
  181. }
  182. gen->type = GEN_DIRNAME;
  183. gen->d.dirn = isname;
  184. }
  185. akeyid->issuer = gens;
  186. gen = NULL;
  187. gens = NULL;
  188. akeyid->serial = serial;
  189. akeyid->keyid = ikeyid;
  190. return akeyid;
  191. err:
  192. sk_GENERAL_NAME_free(gens);
  193. GENERAL_NAME_free(gen);
  194. X509_NAME_free(isname);
  195. ASN1_INTEGER_free(serial);
  196. ASN1_OCTET_STRING_free(ikeyid);
  197. AUTHORITY_KEYID_free(akeyid);
  198. return NULL;
  199. }