v3_skey.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 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. X509V3err(X509V3_F_S2I_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
  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. static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
  46. X509V3_CTX *ctx, char *str)
  47. {
  48. ASN1_OCTET_STRING *oct;
  49. X509_PUBKEY *pubkey;
  50. const unsigned char *pk;
  51. int pklen;
  52. unsigned char pkey_dig[EVP_MAX_MD_SIZE];
  53. unsigned int diglen;
  54. if (strcmp(str, "hash"))
  55. return s2i_ASN1_OCTET_STRING(method, ctx, str);
  56. if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
  57. X509V3err(X509V3_F_S2I_SKEY_ID, ERR_R_MALLOC_FAILURE);
  58. return NULL;
  59. }
  60. if (ctx && (ctx->flags == CTX_TEST))
  61. return oct;
  62. if (!ctx || (!ctx->subject_req && !ctx->subject_cert)) {
  63. X509V3err(X509V3_F_S2I_SKEY_ID, X509V3_R_NO_PUBLIC_KEY);
  64. goto err;
  65. }
  66. if (ctx->subject_req)
  67. pubkey = ctx->subject_req->req_info.pubkey;
  68. else
  69. pubkey = ctx->subject_cert->cert_info.key;
  70. if (pubkey == NULL) {
  71. X509V3err(X509V3_F_S2I_SKEY_ID, X509V3_R_NO_PUBLIC_KEY);
  72. goto err;
  73. }
  74. X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey);
  75. if (!EVP_Digest(pk, pklen, pkey_dig, &diglen, EVP_sha1(), NULL))
  76. goto err;
  77. if (!ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) {
  78. X509V3err(X509V3_F_S2I_SKEY_ID, ERR_R_MALLOC_FAILURE);
  79. goto err;
  80. }
  81. return oct;
  82. err:
  83. ASN1_OCTET_STRING_free(oct);
  84. return NULL;
  85. }