2
0

v3_skid.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/x509v3.h>
  12. #include "crypto/x509.h"
  13. #include "ext_dat.h"
  14. static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
  15. X509V3_CTX *ctx, char *str);
  16. const X509V3_EXT_METHOD ossl_v3_skey_id = {
  17. NID_subject_key_identifier, 0, ASN1_ITEM_ref(ASN1_OCTET_STRING),
  18. 0, 0, 0, 0,
  19. (X509V3_EXT_I2S)i2s_ASN1_OCTET_STRING,
  20. (X509V3_EXT_S2I)s2i_skey_id,
  21. 0, 0, 0, 0,
  22. NULL
  23. };
  24. char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
  25. const ASN1_OCTET_STRING *oct)
  26. {
  27. return OPENSSL_buf2hexstr(oct->data, oct->length);
  28. }
  29. ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
  30. X509V3_CTX *ctx, const char *str)
  31. {
  32. ASN1_OCTET_STRING *oct;
  33. long length;
  34. if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
  35. ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
  36. return NULL;
  37. }
  38. if ((oct->data = OPENSSL_hexstr2buf(str, &length)) == NULL) {
  39. ASN1_OCTET_STRING_free(oct);
  40. return NULL;
  41. }
  42. oct->length = length;
  43. return oct;
  44. }
  45. ASN1_OCTET_STRING *ossl_x509_pubkey_hash(X509_PUBKEY *pubkey)
  46. {
  47. ASN1_OCTET_STRING *oct;
  48. const unsigned char *pk;
  49. int pklen;
  50. unsigned char pkey_dig[EVP_MAX_MD_SIZE];
  51. unsigned int diglen;
  52. const char *propq;
  53. OSSL_LIB_CTX *libctx;
  54. EVP_MD *md;
  55. if (pubkey == NULL) {
  56. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_PUBLIC_KEY);
  57. return NULL;
  58. }
  59. if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey))
  60. return NULL;
  61. if ((md = EVP_MD_fetch(libctx, SN_sha1, propq)) == NULL)
  62. return NULL;
  63. if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
  64. EVP_MD_free(md);
  65. return NULL;
  66. }
  67. X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey);
  68. if (EVP_Digest(pk, pklen, pkey_dig, &diglen, md, NULL)
  69. && ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) {
  70. EVP_MD_free(md);
  71. return oct;
  72. }
  73. EVP_MD_free(md);
  74. ASN1_OCTET_STRING_free(oct);
  75. return NULL;
  76. }
  77. static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
  78. X509V3_CTX *ctx, char *str)
  79. {
  80. if (strcmp(str, "none") == 0)
  81. return ASN1_OCTET_STRING_new(); /* dummy */
  82. if (strcmp(str, "hash") != 0)
  83. return s2i_ASN1_OCTET_STRING(method, ctx /* not used */, str);
  84. if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
  85. return ASN1_OCTET_STRING_new();
  86. if (ctx == NULL
  87. || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
  88. ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
  89. return NULL;
  90. }
  91. return ossl_x509_pubkey_hash(ctx->subject_cert != NULL ?
  92. ctx->subject_cert->cert_info.key :
  93. ctx->subject_req->req_info.pubkey);
  94. }