pk7_attr.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* pk7_attr.c */
  2. /* S/MIME code.
  3. * Copyright (C) 1997-8 Dr S N Henson (shenson@bigfoot.com)
  4. * All Rights Reserved.
  5. * Redistribution of this code without the authors permission is expressly
  6. * prohibited.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/asn1.h>
  12. #include <openssl/pem.h>
  13. #include <openssl/pkcs7.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/err.h>
  16. int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, STACK_OF(X509_ALGOR) *cap)
  17. {
  18. ASN1_STRING *seq;
  19. unsigned char *p, *pp;
  20. int len;
  21. len=i2d_ASN1_SET_OF_X509_ALGOR(cap,NULL,i2d_X509_ALGOR,
  22. V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL,
  23. IS_SEQUENCE);
  24. if(!(pp=(unsigned char *)OPENSSL_malloc(len))) {
  25. PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE);
  26. return 0;
  27. }
  28. p=pp;
  29. i2d_ASN1_SET_OF_X509_ALGOR(cap,&p,i2d_X509_ALGOR, V_ASN1_SEQUENCE,
  30. V_ASN1_UNIVERSAL, IS_SEQUENCE);
  31. if(!(seq = ASN1_STRING_new())) {
  32. PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE);
  33. return 0;
  34. }
  35. if(!ASN1_STRING_set (seq, pp, len)) {
  36. PKCS7err(PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP,ERR_R_MALLOC_FAILURE);
  37. return 0;
  38. }
  39. OPENSSL_free (pp);
  40. return PKCS7_add_signed_attribute(si, NID_SMIMECapabilities,
  41. V_ASN1_SEQUENCE, seq);
  42. }
  43. STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si)
  44. {
  45. ASN1_TYPE *cap;
  46. unsigned char *p;
  47. cap = PKCS7_get_signed_attribute(si, NID_SMIMECapabilities);
  48. if (!cap) return NULL;
  49. p = cap->value.sequence->data;
  50. return d2i_ASN1_SET_OF_X509_ALGOR(NULL, &p,
  51. cap->value.sequence->length,
  52. d2i_X509_ALGOR, X509_ALGOR_free,
  53. V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
  54. }
  55. /* Basic smime-capabilities OID and optional integer arg */
  56. int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
  57. {
  58. X509_ALGOR *alg;
  59. if(!(alg = X509_ALGOR_new())) {
  60. PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
  61. return 0;
  62. }
  63. ASN1_OBJECT_free(alg->algorithm);
  64. alg->algorithm = OBJ_nid2obj (nid);
  65. if (arg > 0) {
  66. ASN1_INTEGER *nbit;
  67. if(!(alg->parameter = ASN1_TYPE_new())) {
  68. PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
  69. return 0;
  70. }
  71. if(!(nbit = ASN1_INTEGER_new())) {
  72. PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
  73. return 0;
  74. }
  75. if(!ASN1_INTEGER_set (nbit, arg)) {
  76. PKCS7err(PKCS7_F_PKCS7_SIMPLE_SMIMECAP,ERR_R_MALLOC_FAILURE);
  77. return 0;
  78. }
  79. alg->parameter->value.integer = nbit;
  80. alg->parameter->type = V_ASN1_INTEGER;
  81. }
  82. sk_X509_ALGOR_push (sk, alg);
  83. return 1;
  84. }