Skip to content

Commit 4b95ee6

Browse files
committed
replace add_signed_attribute with set_signed_attributes
1 parent 45d19cb commit 4b95ee6

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
@@ -973,87 +973,26 @@ ossl_pkcs7si_get_signed_time(VALUE self)
973973
}
974974

975975
static VALUE
976-
ossl_pkcs7si_add_signed_attribute(VALUE self, VALUE oid, VALUE value) {
977-
// PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *si, int nid, int attrtype, void *value)
978-
//
979-
// argument sources:
980-
// si - signer_info from OpenSSL, OpenSSL::PKCS7#signers.first, then rubyobj -> OpenSSL struct
981-
// nid - "numerical id" of OID, how to pivot from OpenSSL::ASN1::ObjectId???
982-
// - nightmare
983-
// - ObjectId has little connection with reality, it's a subclass of Primitive
984-
// attrtype - "and it adds a new ASN.1 ANY object of type attrtype with the given value to the new attribute."
985-
// - what is this?
986-
// value - void PTR, ossl_asn1_get_asn1type has a case statement that produces a correct value
987-
976+
ossl_pkcs7si_set_signed_attributes(VALUE self, VALUE ary)
977+
{
988978
PKCS7_SIGNER_INFO *p7si;
989-
ASN1_OBJECT *a1obj;
990-
ASN1_TYPE *value_as_type; // hacks to use ossl_asn1_get_asn1type's case statement
991-
int nid, tag = 0;
979+
STACK_OF(X509_ATTRIBUTE) *sk;
980+
int status, result;
992981

993982
GetPKCS7si(self, p7si);
983+
Check_Type(ary, T_ARRAY);
994984

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

990+
result = PKCS7_set_signed_attributes(p7si, sk);
991+
992+
fprintf(stderr, "set signed attributes result is: '%d'\n", result);
993+
994+
return Qtrue;
995+
}
1057996

1058997
/*
1059998
* RECIPIENT INFO
@@ -1173,7 +1112,7 @@ Init_ossl_pkcs7(void)
11731112
rb_define_method(cPKCS7Signer, "issuer", ossl_pkcs7si_get_issuer, 0);
11741113
rb_define_method(cPKCS7Signer, "serial", ossl_pkcs7si_get_serial,0);
11751114
rb_define_method(cPKCS7Signer,"signed_time",ossl_pkcs7si_get_signed_time,0);
1176-
rb_define_method(cPKCS7Signer, "add_signed_attribute", ossl_pkcs7si_add_signed_attribute, 2);
1115+
rb_define_method(cPKCS7Signer, "signed_attributes=", ossl_pkcs7si_set_signed_attributes, 1);
11771116

11781117
cPKCS7Recipient = rb_define_class_under(cPKCS7,"RecipientInfo",rb_cObject);
11791118
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)