v3_akey.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 1999-2016 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 "ext_dat.h"
  16. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  17. AUTHORITY_KEYID *akeyid,
  18. STACK_OF(CONF_VALUE)
  19. *extlist);
  20. static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  21. X509V3_CTX *ctx,
  22. STACK_OF(CONF_VALUE) *values);
  23. const X509V3_EXT_METHOD v3_akey_id = {
  24. NID_authority_key_identifier,
  25. X509V3_EXT_MULTILINE, ASN1_ITEM_ref(AUTHORITY_KEYID),
  26. 0, 0, 0, 0,
  27. 0, 0,
  28. (X509V3_EXT_I2V) i2v_AUTHORITY_KEYID,
  29. (X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
  30. 0, 0,
  31. NULL
  32. };
  33. static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  34. AUTHORITY_KEYID *akeyid,
  35. STACK_OF(CONF_VALUE)
  36. *extlist)
  37. {
  38. char *tmp;
  39. if (akeyid->keyid) {
  40. tmp = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length);
  41. X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL, tmp, &extlist);
  42. OPENSSL_free(tmp);
  43. }
  44. if (akeyid->issuer)
  45. extlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
  46. if (akeyid->serial) {
  47. tmp = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length);
  48. X509V3_add_value("serial", tmp, &extlist);
  49. OPENSSL_free(tmp);
  50. }
  51. return extlist;
  52. }
  53. /*-
  54. * Currently two options:
  55. * keyid: use the issuers subject keyid, the value 'always' means its is
  56. * an error if the issuer certificate doesn't have a key id.
  57. * issuer: use the issuers cert issuer and serial number. The default is
  58. * to only use this if keyid is not present. With the option 'always'
  59. * this is always included.
  60. */
  61. static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
  62. X509V3_CTX *ctx,
  63. STACK_OF(CONF_VALUE) *values)
  64. {
  65. char keyid = 0, issuer = 0;
  66. int i;
  67. CONF_VALUE *cnf;
  68. ASN1_OCTET_STRING *ikeyid = NULL;
  69. X509_NAME *isname = NULL;
  70. GENERAL_NAMES *gens = NULL;
  71. GENERAL_NAME *gen = NULL;
  72. ASN1_INTEGER *serial = NULL;
  73. X509_EXTENSION *ext;
  74. X509 *cert;
  75. AUTHORITY_KEYID *akeyid;
  76. for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
  77. cnf = sk_CONF_VALUE_value(values, i);
  78. if (strcmp(cnf->name, "keyid") == 0) {
  79. keyid = 1;
  80. if (cnf->value && strcmp(cnf->value, "always") == 0)
  81. keyid = 2;
  82. } else if (strcmp(cnf->name, "issuer") == 0) {
  83. issuer = 1;
  84. if (cnf->value && strcmp(cnf->value, "always") == 0)
  85. issuer = 2;
  86. } else {
  87. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID, X509V3_R_UNKNOWN_OPTION);
  88. ERR_add_error_data(2, "name=", cnf->name);
  89. return NULL;
  90. }
  91. }
  92. if (!ctx || !ctx->issuer_cert) {
  93. if (ctx && (ctx->flags == CTX_TEST))
  94. return AUTHORITY_KEYID_new();
  95. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,
  96. X509V3_R_NO_ISSUER_CERTIFICATE);
  97. return NULL;
  98. }
  99. cert = ctx->issuer_cert;
  100. if (keyid) {
  101. i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
  102. if ((i >= 0) && (ext = X509_get_ext(cert, i)))
  103. ikeyid = X509V3_EXT_d2i(ext);
  104. if (keyid == 2 && !ikeyid) {
  105. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,
  106. X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
  107. return NULL;
  108. }
  109. }
  110. if ((issuer && !ikeyid) || (issuer == 2)) {
  111. isname = X509_NAME_dup(X509_get_issuer_name(cert));
  112. serial = ASN1_INTEGER_dup(X509_get_serialNumber(cert));
  113. if (!isname || !serial) {
  114. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID,
  115. X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
  116. goto err;
  117. }
  118. }
  119. if ((akeyid = AUTHORITY_KEYID_new()) == NULL)
  120. goto err;
  121. if (isname) {
  122. if ((gens = sk_GENERAL_NAME_new_null()) == NULL
  123. || (gen = GENERAL_NAME_new()) == NULL
  124. || !sk_GENERAL_NAME_push(gens, gen)) {
  125. X509V3err(X509V3_F_V2I_AUTHORITY_KEYID, ERR_R_MALLOC_FAILURE);
  126. goto err;
  127. }
  128. gen->type = GEN_DIRNAME;
  129. gen->d.dirn = isname;
  130. }
  131. akeyid->issuer = gens;
  132. gen = NULL;
  133. gens = NULL;
  134. akeyid->serial = serial;
  135. akeyid->keyid = ikeyid;
  136. return akeyid;
  137. err:
  138. sk_GENERAL_NAME_free(gens);
  139. GENERAL_NAME_free(gen);
  140. X509_NAME_free(isname);
  141. ASN1_INTEGER_free(serial);
  142. ASN1_OCTET_STRING_free(ikeyid);
  143. return NULL;
  144. }