Skip to content

Commit e6e33ed

Browse files
committed
replace add_signed_attribute with set_signed_attributes
1 parent 9d0398c commit e6e33ed

File tree

5 files changed

+34
-77
lines changed

5 files changed

+34
-77
lines changed

ext/openssl/ossl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ ossl_##name##_ary2sk(VALUE ary) \
6161
return sk; \
6262
}
6363
OSSL_IMPL_ARY2SK(x509, X509, cX509Cert, DupX509CertPtr)
64+
OSSL_IMPL_ARY2SK(x509_attr, X509_ATTRIBUTE, cX509Attr, DupX509AttrPtr)
6465

6566
#define OSSL_IMPL_SK2ARY(name, type) \
6667
VALUE \

ext/openssl/ossl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern VALUE eOSSLError;
8080
*/
8181
STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
8282
STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
83+
STACK_OF(X509_ATTRIBUTE) *ossl_protect_x509_attr_ary2sk(VALUE,int*);
8384
VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs);
8485
VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl);
8586
VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names);

ext/openssl/ossl_pkcs7.c

Lines changed: 16 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -988,87 +988,26 @@ ossl_pkcs7si_get_signed_time(VALUE self)
988988
}
989989

990990
static VALUE
991-
ossl_pkcs7si_add_signed_attribute(VALUE self, VALUE oid, VALUE value) {
992-
// PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *si, int nid, int attrtype, void *value)
993-
//
994-
// argument sources:
995-
// si - signer_info from OpenSSL, OpenSSL::PKCS7#signers.first, then rubyobj -> OpenSSL struct
996-
// nid - "numerical id" of OID, how to pivot from OpenSSL::ASN1::ObjectId???
997-
// - nightmare
998-
// - ObjectId has little connection with reality, it's a subclass of Primitive
999-
// attrtype - "and it adds a new ASN.1 ANY object of type attrtype with the given value to the new attribute."
1000-
// - what is this?
1001-
// value - void PTR, ossl_asn1_get_asn1type has a case statement that produces a correct value
1002-
991+
ossl_pkcs7si_set_signed_attributes(VALUE self, VALUE ary)
992+
{
1003993
PKCS7_SIGNER_INFO *p7si;
1004-
ASN1_OBJECT *a1obj;
1005-
ASN1_TYPE *value_as_type; // hacks to use ossl_asn1_get_asn1type's case statement
1006-
int nid, tag = 0;
994+
STACK_OF(X509_ATTRIBUTE) *sk;
995+
int status, result;
1007996

1008997
GetPKCS7si(self, p7si);
998+
Check_Type(ary, T_ARRAY);
1009999

1010-
// convert OpenSSL::ASN1::ObjectId to a nid
1011-
a1obj = obj_to_asn1obj(ossl_asn1_get_value(oid)); // TODO: error check
1012-
nid = OBJ_obj2nid(a1obj); // TODO: error check (NID_undef)
1013-
// it's completely possible someone's using an unknown NID here
1014-
// we should raise an informative error if this happens
1015-
1016-
// so about attrtype...
1017-
// i'm assuming this would be something like "OpenSSL::ASN1::Sequence" in Ruby
1018-
// we can determine attrtype from the "value", as essentially the "value" is an attrtype plus value
1019-
// so attrtype is eventually passed into ASN1_TYPE_set
1020-
// ASN1_type_set docs makes explicit references to "V_ASN1_SEQUENCE"/"V_ASN1_BOOLEAN"/"V_ASN1_OTHER"
1021-
// so i'm now pretty convinced i should be able to get this from a1obj
1022-
1023-
// though should we use ossl_asn1_get_tag?
1024-
// ossl_asn1_tag takes VALUE obj -> int
1025-
tag = ossl_asn1_tag(value); // TODO: error check
1026-
1027-
// how tf do we go from value -> ruby -> openssl somehow? -> "value pointer"
1028-
// maybe this:
1029-
// value = ossl_asn1_get_value(obj); // no, becaue value is a ruby — ossl_asn1_get_asn1type might have something useful
1030-
1031-
// struct asn1_object_st {
1032-
// const char *sn, *ln;
1033-
// int nid;
1034-
// int length;
1035-
// const unsigned char *data;
1036-
// int flags;
1037-
// }
1038-
1039-
1040-
// "void *value" goes PKCS7_signed_attribute -> add_attribute -> X509_ATTRIBUTE_create (x_attrib.c)
1041-
// -> ASN1_TYPE_set(val, atrtype, value)
1042-
// -> a->value.ptr = value;
1043-
// a is an ASN1_TYPE struct (asn1_type_st)
1044-
// include/openssl/asn1.h.in as:
1045-
//
1046-
// struct asn1_type_st {
1047-
// int type;
1048-
// union {
1049-
// char *ptr;
1050-
// // a bunch of other specifically typed attributes like:
1051-
// ASN1_BOOLEAN boolean;
1052-
// ASN1_UNIVERSALSTRING *universalstring;
1053-
// }
1054-
// very unclear on if this matters though?
1055-
//
1056-
// so what do we need? i'm still no closer to actually answering that question
1057-
// i can get an ASN1_OBJECT but that's still not much use to me
1058-
//
1059-
// i can actually get an ASN1_TYPE from ossl_asn1_get_asn1type, which calls
1060-
// ASN1_TYPE_set under the hood via a crazy case statement.
1061-
value_as_type = ossl_asn1_get_asn1type(value);
1062-
1063-
// method sig would be .add_signed_attribute(oid/type, value)
1064-
// where ObjectId is actually a Primitive (???) so how do I handle that?
1065-
// both oid and value are ultimately primitives tbh
1066-
PKCS7_add_signed_attribute(p7si, nid, tag, value_as_type.value);
1067-
1068-
// return the value of the attribute we've just stuck in
1069-
return value;
1070-
}
1000+
// TODO: reset attributes
1001+
1002+
// build list of x509 attrs of length RARRAY_LEN(ary)
1003+
sk = ossl_protect_x509_attr_ary2sk(ary, &status);
10711004

1005+
result = PKCS7_set_signed_attributes(p7si, sk);
1006+
1007+
fprintf(stderr, "set signed attributes result is: '%d'\n", result);
1008+
1009+
return Qtrue;
1010+
}
10721011

10731012
/*
10741013
* RECIPIENT INFO
@@ -1188,7 +1127,7 @@ Init_ossl_pkcs7(void)
11881127
rb_define_method(cPKCS7Signer, "issuer", ossl_pkcs7si_get_issuer, 0);
11891128
rb_define_method(cPKCS7Signer, "serial", ossl_pkcs7si_get_serial,0);
11901129
rb_define_method(cPKCS7Signer,"signed_time",ossl_pkcs7si_get_signed_time,0);
1191-
rb_define_method(cPKCS7Signer, "add_signed_attribute", ossl_pkcs7si_add_signed_attribute, 2);
1130+
rb_define_method(cPKCS7Signer, "signed_attributes=", ossl_pkcs7si_set_signed_attributes, 1);
11921131

11931132
cPKCS7Recipient = rb_define_class_under(cPKCS7,"RecipientInfo",rb_cObject);
11941133
rb_define_alloc_func(cPKCS7Recipient, ossl_pkcs7ri_alloc);

ext/openssl/ossl_x509.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern VALUE eX509AttrError;
3232

3333
VALUE ossl_x509attr_new(X509_ATTRIBUTE *);
3434
X509_ATTRIBUTE *GetX509AttrPtr(VALUE);
35+
X509_ATTRIBUTE *DupX509AttrPtr(VALUE);
3536
void Init_ossl_x509attr(void);
3637

3738
/*

ext/openssl/ossl_x509attr.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ GetX509AttrPtr(VALUE obj)
7777
return attr;
7878
}
7979

80+
X509_ATTRIBUTE *
81+
DupX509AttrPtr(VALUE obj)
82+
{
83+
X509_ATTRIBUTE *attr, *new;
84+
85+
GetX509Attr(obj, attr);
86+
87+
// XXX: maybe missing an up_ref here?
88+
if (!(new = X509_ATTRIBUTE_dup(attr))) {
89+
fprintf(stderr, "dup failed unlucky\n");
90+
}
91+
92+
return new;
93+
}
94+
8095
/*
8196
* Private
8297
*/

0 commit comments

Comments
 (0)