2
0

pk7_attr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <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. PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP, ERR_R_MALLOC_FAILURE);
  24. return 0;
  25. }
  26. seq->length = ASN1_item_i2d((ASN1_VALUE *)cap, &seq->data,
  27. ASN1_ITEM_rptr(X509_ALGORS));
  28. return PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
  29. V_ASN1_SEQUENCE, seq);
  30. }
  31. STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
  32. {
  33. ASN1_TYPE *cap;
  34. const unsigned char *p;
  35. cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
  36. if (cap == NULL || (cap->type != V_ASN1_SEQUENCE))
  37. return NULL;
  38. p = cap->value.sequence->data;
  39. return (STACK_OF(X509_ALGOR) *)
  40. ASN1_item_d2i(NULL, &p, cap->value.sequence->length,
  41. ASN1_ITEM_rptr(X509_ALGORS));
  42. }
  43. /* Basic smime-capabilities OID and optional integer arg */
  44. int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
  45. {
  46. ASN1_INTEGER *nbit = NULL;
  47. X509_ALGOR *alg;
  48. if ((alg = X509_ALGOR_new()) == NULL) {
  49. PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
  50. return 0;
  51. }
  52. ASN1_OBJECT_free(alg->algorithm);
  53. alg->algorithm = OBJ_nid2obj(nid);
  54. if (arg > 0) {
  55. if ((alg->parameter = ASN1_TYPE_new()) == NULL) {
  56. goto err;
  57. }
  58. if ((nbit = ASN1_INTEGER_new()) == NULL) {
  59. goto err;
  60. }
  61. if (!ASN1_INTEGER_set(nbit, arg)) {
  62. goto err;
  63. }
  64. alg->parameter->value.integer = nbit;
  65. alg->parameter->type = V_ASN1_INTEGER;
  66. nbit = NULL;
  67. }
  68. if (!sk_X509_ALGOR_push(sk, alg)) {
  69. goto err;
  70. }
  71. return 1;
  72. err:
  73. PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP, ERR_R_MALLOC_FAILURE);
  74. ASN1_INTEGER_free(nbit);
  75. X509_ALGOR_free(alg);
  76. return 0;
  77. }
  78. int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid)
  79. {
  80. if (PKCS7_get_signed_attribute(si, NID_pkcs9_contentType))
  81. return 0;
  82. if (!coid)
  83. coid = OBJ_nid2obj(NID_pkcs7_data);
  84. return PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
  85. V_ASN1_OBJECT, coid);
  86. }
  87. int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t)
  88. {
  89. if (t == NULL && (t = X509_gmtime_adj(NULL, 0)) == NULL) {
  90. PKCS7err(PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME,
  91. ERR_R_MALLOC_FAILURE);
  92. return 0;
  93. }
  94. return PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime,
  95. V_ASN1_UTCTIME, t);
  96. }
  97. int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
  98. const unsigned char *md, int mdlen)
  99. {
  100. ASN1_OCTET_STRING *os;
  101. os = ASN1_OCTET_STRING_new();
  102. if (os == NULL)
  103. return 0;
  104. if (!ASN1_STRING_set(os, md, mdlen)
  105. || !PKCS7_add_signed_attribute(si, NID_pkcs9_messageDigest,
  106. V_ASN1_OCTET_STRING, os)) {
  107. ASN1_OCTET_STRING_free(os);
  108. return 0;
  109. }
  110. return 1;
  111. }