2
0

pk7_attr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 1999-2020 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 <stdlib.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/pem.h>
  15. #include <openssl/pkcs7.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/err.h>
  18. int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
  19. STACK_OF(X509_ALGOR) *cap)
  20. {
  21. ASN1_STRING *seq;
  22. if ((seq = ASN1_STRING_new()) == NULL) {
  23. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  24. return 0;
  25. }
  26. seq->length = ASN1_item_i2d((ASN1_VALUE *)cap, &seq->data,
  27. ASN1_ITEM_rptr(X509_ALGORS));
  28. if (!PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
  29. V_ASN1_SEQUENCE, seq)) {
  30. ASN1_STRING_free(seq);
  31. return 0;
  32. }
  33. return 1;
  34. }
  35. STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
  36. {
  37. ASN1_TYPE *cap;
  38. const unsigned char *p;
  39. cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
  40. if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
  41. return NULL;
  42. p = cap->value.sequence->data;
  43. return (STACK_OF(X509_ALGOR) *)
  44. ASN1_item_d2i(NULL, &p, cap->value.sequence->length,
  45. ASN1_ITEM_rptr(X509_ALGORS));
  46. }
  47. /* Basic smime-capabilities OID and optional integer arg */
  48. int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
  49. {
  50. ASN1_INTEGER *nbit = NULL;
  51. X509_ALGOR *alg;
  52. if ((alg = X509_ALGOR_new()) == NULL) {
  53. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  54. return 0;
  55. }
  56. ASN1_OBJECT_free(alg->algorithm);
  57. alg->algorithm = OBJ_nid2obj(nid);
  58. if (arg > 0) {
  59. if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
  60. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  61. goto err;
  62. }
  63. if ((nbit = ASN1_INTEGER_new()) == NULL) {
  64. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  65. goto err;
  66. }
  67. if (!ASN1_INTEGER_set(nbit, arg)) {
  68. ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
  69. goto err;
  70. }
  71. alg->parameter->value.integer = nbit;
  72. alg->parameter->type = V_ASN1_INTEGER;
  73. nbit = NULL;
  74. }
  75. if (!sk_X509_ALGOR_push(sk, alg)) {
  76. ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
  77. goto err;
  78. }
  79. return 1;
  80. err:
  81. ASN1_INTEGER_free(nbit);
  82. X509_ALGOR_free(alg);
  83. return 0;
  84. }
  85. int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
  86. {
  87. if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
  88. return 0;
  89. if (!coid)
  90. coid = OBJ_nid2obj(NID_pkcs7_data);
  91. return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
  92. V_ASN1_OBJECT, coid);
  93. }
  94. int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
  95. {
  96. ASN1_TIME *tmp = NULL;
  97. if (t == NULL && (tmp = t = X509_gmtime_adj(NULL, 0)) == NULL) {
  98. ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
  99. return 0;
  100. }
  101. if (!PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
  102. V_ASN1_UTCTIME, t)) {
  103. ASN1_TIME_free(tmp);
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
  109. const unsigned char *md, int mdlen)
  110. {
  111. ASN1_OCTET_STRING *os;
  112. os = ASN1_OCTET_STRING_new();
  113. if (os == NULL)
  114. return 0;
  115. if (!ASN1_STRING_set(os, md, mdlen)
  116. || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
  117. V_ASN1_OCTET_STRING, os)) {
  118. ASN1_OCTET_STRING_free(os);
  119. return 0;
  120. }
  121. return 1;
  122. }