int i2d_X509_EXTENSION(X509_EXTENSION *a, unsigned char **pp) { int k=0; int r=0,ret=0; unsigned char **p=NULL; if (a == NULL) return(0); p=NULL; for (;;) { if (k) { r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); if (pp == NULL) return(r); p=pp; ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); } ret+=i2d_ASN1_OBJECT(a->object,p); if ((a->critical) || a->netscape_hack) ret+=i2d_ASN1_BOOLEAN(a->critical,p); ret+=i2d_ASN1_OCTET_STRING(a->value,p); if (k++) return(r); } }
/* sequence */ int i2d_X509_ATTRIBUTE(X509_ATTRIBUTE *a, unsigned char **pp) { int k=0; int r=0,ret=0; unsigned char **p=NULL; if (a == NULL) return(0); p=NULL; for (;;) { if (k) { r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); if (pp == NULL) return(r); p=pp; ASN1_put_object(p,1,ret,V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); } ret+=i2d_ASN1_OBJECT(a->object,p); if (a->set) ret+=i2d_ASN1_SET_OF_ASN1_TYPE(a->value.set,p,(i2d_func_t)i2d_ASN1_TYPE, V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET); else ret+=i2d_ASN1_TYPE(a->value.single,p); if (k++) return(r); } }
int btls_change_obj_data(ASN1_OBJECT **a, const char* pp) { int res = 1; unsigned char *buf; unsigned char *p; const unsigned char *cp; int i, j; i=a2d_ASN1_OBJECT(NULL,0, pp,-1); if (i <= 0) { /* Don't clear the error */ /*ERR_clear_error();*/ return 0; } /* Work out total size */ j = ASN1_object_size(0,i,V_ASN1_OBJECT); if((buf=(unsigned char *)OPENSSL_malloc(j)) == NULL) return 0; p = buf; /* Write out tag+length */ ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); /* Write out contents */ a2d_ASN1_OBJECT(p,i,pp,-1); cp=buf; (*a)=btls_d2i_ASN1_OBJECT(a,&cp,j); OPENSSL_free(buf); return 1; }
static VALUE ossl_asn1data_to_der(VALUE self) { VALUE value, der, inf_length; int tag, tag_class, is_cons = 0, tmp_cons = 1; long length; unsigned char *p; value = ossl_asn1_get_value(self); if(rb_obj_is_kind_of(value, rb_cArray)){ is_cons = 1; value = join_der(value); } StringValue(value); tag = ossl_asn1_tag(self); tag_class = ossl_asn1_tag_class(self); inf_length = ossl_asn1_get_infinite_length(self); if (inf_length == Qtrue) { is_cons = 2; tmp_cons = 2; } if((length = ASN1_object_size(tmp_cons, RSTRING_LENINT(value), tag)) <= 0) ossl_raise(eASN1Error, NULL); der = rb_str_new(0, length); p = (unsigned char *)RSTRING_PTR(der); ASN1_put_object(&p, is_cons, RSTRING_LENINT(value), tag, tag_class); memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value)); p += RSTRING_LEN(value); ossl_str_adjust(der, p); return der; }
int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass) { int ret,r,constructed; unsigned char *p; if (a == NULL) return(0); if (tag == V_ASN1_BIT_STRING) return(i2d_ASN1_BIT_STRING(a,pp)); ret=a->length; r=ASN1_object_size(0,ret,tag); if (pp == NULL) return(r); p= *pp; if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET)) constructed=1; else constructed=0; ASN1_put_object(&p,constructed,ret,tag,xclass); memcpy(p,a->data,a->length); p+=a->length; *pp= p; return(r); }
int ECDSA_size(const EC_KEY *r) { int ret,i; ASN1_INTEGER bs; BIGNUM *order=NULL; unsigned char buf[4]; const EC_GROUP *group; if (r == NULL) return 0; group = EC_KEY_get0_group(r); if (group == NULL) return 0; if ((order = BN_new()) == NULL) return 0; if (!EC_GROUP_get_order(group,order,NULL)) { BN_clear_free(order); return 0; } i=BN_num_bits(order); bs.length=(i+7)/8; bs.data=buf; bs.type=V_ASN1_INTEGER; /* If the top bit is set the asn1 encoding is 1 larger. */ buf[0]=0xff; i=i2d_ASN1_INTEGER(&bs,NULL); i+=i; /* r and s */ ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); BN_clear_free(order); return(ret); }
static VALUE ossl_asn1prim_to_der(VALUE self, SEL sel) { ASN1_TYPE *asn1; int tn, tc, explicit; long len, reallen; unsigned char *buf, *p; VALUE str; tn = NUM2INT(ossl_asn1_get_tag(self)); tc = ossl_asn1_tag_class(self); explicit = ossl_asn1_is_explicit(self); asn1 = ossl_asn1_get_asn1type(self); len = ASN1_object_size(1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn); if(!(buf = OPENSSL_malloc(len))){ ossl_ASN1_TYPE_free(asn1); ossl_raise(eASN1Error, "cannot alloc buffer"); } p = buf; if (tc == V_ASN1_UNIVERSAL) { ossl_i2d_ASN1_TYPE(asn1, &p); } else if (explicit) { ASN1_put_object(&p, 1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn, tc); ossl_i2d_ASN1_TYPE(asn1, &p); } else {
static VALUE ossl_asn1data_to_der(VALUE self, SEL sel) { VALUE value, der; int tag, tag_class, is_cons = 0; long length; unsigned char *p; value = ossl_asn1_get_value(self); if(rb_obj_is_kind_of(value, rb_cArray)){ is_cons = 1; value = join_der(value); } StringValue(value); tag = ossl_asn1_tag(self); tag_class = ossl_asn1_tag_class(self); if((length = ASN1_object_size(1, RSTRING_LEN(value), tag)) <= 0) ossl_raise(eASN1Error, NULL); der = rb_bstr_new(); rb_bstr_resize(der, length); p = (unsigned char *)rb_bstr_bytes(der); ASN1_put_object(&p, is_cons, RSTRING_LEN(value), tag, tag_class); memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value)); p += RSTRING_LEN(value); ossl_str_adjust(der, p); return der; }
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass) { int len; int utype; int usetag; int ndef = 0; utype = it->utype; /* * Get length of content octets and maybe find out the underlying type. */ len = asn1_ex_i2c(pval, NULL, &utype, it); /* * If SEQUENCE, SET or OTHER then header is included in pseudo content * octets so don't include tag+length. We need to check here because the * call to asn1_ex_i2c() could change utype. */ if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) usetag = 0; else usetag = 1; /* -1 means omit type */ if (len == -1) return 0; /* -2 return is special meaning use ndef */ if (len == -2) { ndef = 2; len = 0; } /* If not implicitly tagged get tag from underlying type */ if (tag == -1) tag = utype; /* Output tag+length followed by content octets */ if (out) { if (usetag) ASN1_put_object(out, ndef, len, tag, aclass); asn1_ex_i2c(pval, *out, &utype, it); if (ndef) ASN1_put_eoc(out); else *out += len; } if (usetag) return ASN1_object_size(ndef, len, tag); return len; }
int i2d_ASN1_BOOLEAN(int a, unsigned char **pp) { int r; unsigned char *p; r=ASN1_object_size(0,1,V_ASN1_BOOLEAN); if (pp == NULL) return(r); p= *pp; ASN1_put_object(&p,0,1,V_ASN1_BOOLEAN,V_ASN1_UNIVERSAL); *(p++)= (unsigned char)a; *pp=p; return(r); }
size_t ECDSA_size(const EC_KEY *key) { size_t ret, i, group_order_size; ASN1_INTEGER bs; BIGNUM *order = NULL; unsigned char buf[4]; const EC_GROUP *group; if (key->ecdsa_meth && key->ecdsa_meth->group_order_size) { group_order_size = key->ecdsa_meth->group_order_size(key); } else { size_t num_bits; if (key == NULL) { return 0; } group = EC_KEY_get0_group(key); if (group == NULL) { return 0; } order = BN_new(); if (order == NULL) { return 0; } if (!EC_GROUP_get_order(group, order, NULL)) { BN_clear_free(order); return 0; } num_bits = BN_num_bits(order); group_order_size = (num_bits + 7) / 8; } bs.length = group_order_size; bs.data = buf; bs.type = V_ASN1_INTEGER; /* If the top bit is set the ASN.1 encoding is 1 larger. */ buf[0] = 0xff; i = i2d_ASN1_INTEGER(&bs, NULL); i += i; /* r and s */ ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); BN_clear_free(order); return ret; }
int DSA_size(DSA *r) { int ret,i; ASN1_INTEGER bs; unsigned char buf[4]; i=BN_num_bits(r->q); bs.length=(i+7)/8; bs.data=buf; bs.type=V_ASN1_INTEGER; /* If the top bit is set the asn1 encoding is 1 larger. */ buf[0]=0xff; i=i2d_ASN1_INTEGER(&bs,NULL); i+=i; /* r and s */ ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); return(ret); }
int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp) { unsigned char *p; int objsize; if ((a == NULL) || (a->data == NULL)) return(0); objsize = ASN1_object_size(0,a->length,V_ASN1_OBJECT); if (pp == NULL) return objsize; p= *pp; ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); memcpy(p,a->data,a->length); p+=a->length; *pp=p; return(objsize); }
int DSA_size(const DSA *dsa) { int ret, i; ASN1_INTEGER bs; unsigned char buf[4]; /* 4 bytes looks really small. However, i2d_ASN1_INTEGER() will not look beyond the first byte, as long as the second parameter is NULL. */ i = BN_num_bits(dsa->q); bs.length = (i + 7) / 8; bs.data = buf; bs.type = V_ASN1_INTEGER; /* If the top bit is set the asn1 encoding is 1 larger. */ buf[0] = 0xff; i = i2d_ASN1_INTEGER(&bs, NULL); i += i; /* r and s */ ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); return ret; }
int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, int len) { int n, size; ASN1_OCTET_STRING os, *osp; ASN1_INTEGER in; unsigned char *p; unsigned char buf[32]; /* when they have 256bit longs, * I'll be in trouble */ in.data = buf; in.length = 32; os.data = data; os.type = V_ASN1_OCTET_STRING; os.length = len; ASN1_INTEGER_set(&in, num); n = i2d_ASN1_INTEGER(&in, NULL); n += M_i2d_ASN1_OCTET_STRING(&os, NULL); size = ASN1_object_size(1, n, V_ASN1_SEQUENCE); if ((osp = ASN1_STRING_new()) == NULL) return (0); /* Grow the 'string' */ if (!ASN1_STRING_set(osp, NULL, size)) { ASN1_STRING_free(osp); return (0); } M_ASN1_STRING_length_set(osp, size); p = M_ASN1_STRING_data(osp); ASN1_put_object(&p, 1,n, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); i2d_ASN1_INTEGER(&in, &p); M_i2d_ASN1_OCTET_STRING(&os, &p); ASN1_TYPE_set(a, V_ASN1_SEQUENCE, osp); return (1); }
/* ########################################### */ int add_signed_seq2string(PKCS7_SIGNER_INFO *si, char *str1, char *str2) { /* To add an object of OID 1.9.999, which is a sequence containing * 2 octet strings */ unsigned char *p; ASN1_OCTET_STRING *os1, *os2; ASN1_STRING *seq; unsigned char *data; int i, total; if (signed_seq2string_nid == -1) signed_seq2string_nid = OBJ_create("1.9.9999","OID_example","Our example OID"); os1 = ASN1_OCTET_STRING_new(); os2 = ASN1_OCTET_STRING_new(); ASN1_OCTET_STRING_set(os1, (unsigned char*)str1, strlen(str1)); ASN1_OCTET_STRING_set(os2, (unsigned char*)str1, strlen(str1)); i = i2d_ASN1_OCTET_STRING(os1, NULL); i += i2d_ASN1_OCTET_STRING(os2, NULL); total = ASN1_object_size(1, i, V_ASN1_SEQUENCE); data = malloc(total); p = data; ASN1_put_object(&p, 1,i, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); i2d_ASN1_OCTET_STRING(os1, &p); i2d_ASN1_OCTET_STRING(os2, &p); seq = ASN1_STRING_new(); ASN1_STRING_set(seq, data, total); free(data); ASN1_OCTET_STRING_free(os1); ASN1_OCTET_STRING_free(os2); PKCS7_add_signed_attribute(si, signed_seq2string_nid, V_ASN1_SEQUENCE, (char *)seq); return (1); }
ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) { int nid = NID_undef; ASN1_OBJECT *op=NULL; unsigned char *buf; unsigned char *p; const unsigned char *cp; int i, j; if(!no_name) { if( ((nid = OBJ_sn2nid(s)) != NID_undef) || ((nid = OBJ_ln2nid(s)) != NID_undef) ) return OBJ_nid2obj(nid); } /* Work out size of content octets */ i=a2d_ASN1_OBJECT(NULL,0,s,-1); if (i <= 0) { /* Clear the error */ ERR_clear_error(); return NULL; } /* Work out total size */ j = ASN1_object_size(0,i,V_ASN1_OBJECT); if((buf=(unsigned char *)OPENSSL_malloc(j)) == NULL) return NULL; p = buf; /* Write out tag+length */ ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); /* Write out contents */ a2d_ASN1_OBJECT(p,i,s,-1); cp=buf; op=d2i_ASN1_OBJECT(NULL,&cp,j); OPENSSL_free(buf); return op; }
int i2d_RSA_NET(RSA *a, unsigned char **pp, int (*cb)(), int sgckey) { int i,j,l[6]; NETSCAPE_PKEY *pkey; unsigned char buf[256],*zz; unsigned char key[EVP_MAX_KEY_LENGTH]; EVP_CIPHER_CTX ctx; X509_ALGOR *alg=NULL; ASN1_OCTET_STRING os,os2; M_ASN1_I2D_vars(a); if (a == NULL) return(0); #ifdef WIN32 r=r; /* shut the damn compiler up :-) */ #endif os.data=os2.data=NULL; if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err; if (!ASN1_INTEGER_set(pkey->version,0)) goto err; if (pkey->algor->algorithm != NULL) ASN1_OBJECT_free(pkey->algor->algorithm); pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption); if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err; pkey->algor->parameter->type=V_ASN1_NULL; l[0]=i2d_RSAPrivateKey(a,NULL); pkey->private_key->length=l[0]; os2.length=i2d_NETSCAPE_PKEY(pkey,NULL); l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL); if ((alg=X509_ALGOR_new()) == NULL) goto err; if (alg->algorithm != NULL) ASN1_OBJECT_free(alg->algorithm); alg->algorithm=OBJ_nid2obj(NID_rc4); if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err; alg->parameter->type=V_ASN1_NULL; l[2]=i2d_X509_ALGOR(alg,NULL); l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE); #ifndef CONST_STRICT os.data=(unsigned char *)"private-key"; #endif os.length=11; l[4]=i2d_ASN1_OCTET_STRING(&os,NULL); l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE); if (pp == NULL) { if (pkey != NULL) NETSCAPE_PKEY_free(pkey); if (alg != NULL) X509_ALGOR_free(alg); return(l[5]); } if (pkey->private_key->data != NULL) OPENSSL_free(pkey->private_key->data); if ((pkey->private_key->data=(unsigned char *)OPENSSL_malloc(l[0])) == NULL) { ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE); goto err; } zz=pkey->private_key->data; i2d_RSAPrivateKey(a,&zz); if ((os2.data=(unsigned char *)OPENSSL_malloc(os2.length)) == NULL) { ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE); goto err; } zz=os2.data; i2d_NETSCAPE_PKEY(pkey,&zz); if (cb == NULL) cb=EVP_read_pw_string; i=cb(buf,256,"Enter Private Key password:"******"SGCKEYSALT", 10); i = 26; } EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL); memset(buf,0,256); EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL); EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length); EVP_EncryptFinal(&ctx,&(os2.data[i]),&j); EVP_CIPHER_CTX_cleanup(&ctx); p= *pp; ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); i2d_ASN1_OCTET_STRING(&os,&p); ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); i2d_X509_ALGOR(alg,&p); i2d_ASN1_OCTET_STRING(&os2,&p); ret=l[5]; err: if (os2.data != NULL) OPENSSL_free(os2.data); if (alg != NULL) X509_ALGOR_free(alg); if (pkey != NULL) NETSCAPE_PKEY_free(pkey); r=r; return(ret); }
ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf) { ASN1_TYPE *ret; tag_exp_arg asn1_tags; tag_exp_type *etmp; int i, len; unsigned char *orig_der = NULL, *new_der = NULL; const unsigned char *cpy_start; unsigned char *p; const unsigned char *cp; int cpy_len; long hdr_len; int hdr_constructed = 0, hdr_tag, hdr_class; int r; asn1_tags.imp_tag = -1; asn1_tags.imp_class = -1; asn1_tags.format = ASN1_GEN_FORMAT_ASCII; asn1_tags.exp_count = 0; if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0) return NULL; if ((asn1_tags.utype == V_ASN1_SEQUENCE) || (asn1_tags.utype == V_ASN1_SET)) { if (!cnf) { ASN1err(ASN1_F_ASN1_GENERATE_V3, ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG); return NULL; } ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf); } else ret = asn1_str2type(asn1_tags.str, asn1_tags.format, asn1_tags.utype); if (!ret) return NULL; /* If no tagging return base type */ if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0)) return ret; /* Generate the encoding */ cpy_len = i2d_ASN1_TYPE(ret, &orig_der); ASN1_TYPE_free(ret); ret = NULL; /* Set point to start copying for modified encoding */ cpy_start = orig_der; /* Do we need IMPLICIT tagging? */ if (asn1_tags.imp_tag != -1) { /* If IMPLICIT we will replace the underlying tag */ /* Skip existing tag+len */ r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag, &hdr_class, cpy_len); if (r & 0x80) goto err; /* Update copy length */ cpy_len -= (int)(cpy_start - orig_der); /* For IMPLICIT tagging the length should match the * original length and constructed flag should be * consistent. */ if (r & 0x1) { /* Indefinite length constructed */ hdr_constructed = 2; hdr_len = 0; } else /* Just retain constructed flag */ hdr_constructed = r & V_ASN1_CONSTRUCTED; /* Work out new length with IMPLICIT tag: ignore constructed * because it will mess up if indefinite length */ len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag); } else len = cpy_len; /* Work out length in any EXPLICIT, starting from end */ for(i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1; i < asn1_tags.exp_count; i++, etmp--) { /* Content length: number of content octets + any padding */ len += etmp->exp_pad; etmp->exp_len = len; /* Total object length: length including new header */ len = ASN1_object_size(0, len, etmp->exp_tag); } /* Allocate buffer for new encoding */ new_der = OPENSSL_malloc(len); /* Generate tagged encoding */ p = new_der; /* Output explicit tags first */ for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count; i++, etmp++) { ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len, etmp->exp_tag, etmp->exp_class); if (etmp->exp_pad) *p++ = 0; } /* If IMPLICIT, output tag */ if (asn1_tags.imp_tag != -1) ASN1_put_object(&p, hdr_constructed, hdr_len, asn1_tags.imp_tag, asn1_tags.imp_class); /* Copy across original encoding */ memcpy(p, cpy_start, cpy_len); cp = new_der; /* Obtain new ASN1_TYPE structure */ ret = d2i_ASN1_TYPE(NULL, &cp, len); err: if (orig_der) OPENSSL_free(orig_der); if (new_der) OPENSSL_free(new_der); return ret; }
/* int is_set: if TRUE, then sort the contents (i.e. it isn't a SEQUENCE) */ int i2d_ASN1_SET(STACK *a, unsigned char **pp, i2d_of_void *i2d, int ex_tag, int ex_class, int is_set) { int ret=0,r; int i; unsigned char *p; unsigned char *pStart, *pTempMem; MYBLOB *rgSetBlob; int totSize; if (a == NULL) return(0); for (i=sk_num(a)-1; i>=0; i--) ret+=i2d(sk_value(a,i),NULL); r=ASN1_object_size(1,ret,ex_tag); if (pp == NULL) return(r); p= *pp; ASN1_put_object(&p,1,ret,ex_tag,ex_class); /* Modified by [email protected] */ /* And then again by Ben */ /* And again by Steve */ if(!is_set || (sk_num(a) < 2)) { for (i=0; i<sk_num(a); i++) i2d(sk_value(a,i),&p); *pp=p; return(r); } pStart = p; /* Catch the beg of Setblobs*/ /* In this array we will store the SET blobs */ rgSetBlob = (MYBLOB *)OPENSSL_malloc(sk_num(a) * sizeof(MYBLOB)); if (rgSetBlob == NULL) { ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE); return(0); } for (i=0; i<sk_num(a); i++) { rgSetBlob[i].pbData = p; /* catch each set encode blob */ i2d(sk_value(a,i),&p); rgSetBlob[i].cbData = (int)(p - rgSetBlob[i].pbData); /* Length of this SetBlob */ } *pp=p; totSize = (int)(p - pStart); /* This is the total size of all set blobs */ /* Now we have to sort the blobs. I am using a simple algo. *Sort ptrs *Copy to temp-mem *Copy from temp-mem to user-mem*/ qsort( rgSetBlob, sk_num(a), sizeof(MYBLOB), SetBlobCmp); if (!(pTempMem = OPENSSL_malloc(totSize))) { ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE); return(0); } /* Copy to temp mem */ p = pTempMem; for(i=0; i<sk_num(a); ++i) { memcpy(p, rgSetBlob[i].pbData, rgSetBlob[i].cbData); p += rgSetBlob[i].cbData; } /* Copy back to user mem*/ memcpy(pStart, pTempMem, totSize); OPENSSL_free(pTempMem); OPENSSL_free(rgSetBlob); return(r); }
static int asn1_bio_write(BIO *b, const char *in, int inl) { BIO_ASN1_BUF_CTX *ctx; int wrmax, wrlen, ret; unsigned char *p; if (!in || (inl < 0) || (b->next_bio == NULL)) return 0; ctx = (BIO_ASN1_BUF_CTX *)b->ptr; if (ctx == NULL) return 0; wrlen = 0; ret = -1; for (;;) { switch (ctx->state) { /* Setup prefix data, call it */ case ASN1_STATE_START: if (!asn1_bio_setup_ex(b, ctx, ctx->prefix, ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER)) return 0; break; /* Copy any pre data first */ case ASN1_STATE_PRE_COPY: ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free, ASN1_STATE_HEADER); if (ret <= 0) goto done; break; case ASN1_STATE_HEADER: ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl; OPENSSL_assert(ctx->buflen <= ctx->bufsize); p = ctx->buf; ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class); ctx->copylen = inl; ctx->state = ASN1_STATE_HEADER_COPY; break; case ASN1_STATE_HEADER_COPY: ret = BIO_write(b->next_bio, ctx->buf + ctx->bufpos, ctx->buflen); if (ret <= 0) goto done; ctx->buflen -= ret; if (ctx->buflen) ctx->bufpos += ret; else { ctx->bufpos = 0; ctx->state = ASN1_STATE_DATA_COPY; } break; case ASN1_STATE_DATA_COPY: if (inl > ctx->copylen) wrmax = ctx->copylen; else wrmax = inl; ret = BIO_write(b->next_bio, in, wrmax); if (ret <= 0) break; wrlen += ret; ctx->copylen -= ret; in += ret; inl -= ret; if (ctx->copylen == 0) ctx->state = ASN1_STATE_HEADER; if (inl == 0) goto done; break; default: BIO_clear_retry_flags(b); return 0; } } done: BIO_clear_retry_flags(b); BIO_copy_next_retry(b); return (wrlen > 0) ? wrlen : ret; }
static int i2d_X509_NAME_entries(X509_NAME *a) { X509_NAME_ENTRY *ne,*fe=NULL; STACK_OF(X509_NAME_ENTRY) *sk; BUF_MEM *buf=NULL; int set=0,r,ret=0; int i; unsigned char *p; int size=0; sk=a->entries; for (i=0; i<sk_X509_NAME_ENTRY_num(sk); i++) { ne=sk_X509_NAME_ENTRY_value(sk,i); if (fe == NULL) { fe=ne; size=0; } if (ne->set != set) { ret+=ASN1_object_size(1,size,V_ASN1_SET); fe->size=size; fe=ne; size=0; set=ne->set; } size+=i2d_X509_NAME_ENTRY(ne,NULL); } if (fe != NULL) { /* SET OF needed only if entries is non empty */ ret+=ASN1_object_size(1,size,V_ASN1_SET); fe->size=size; } r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); buf=a->bytes; if (!BUF_MEM_grow(buf,r)) goto err; p=(unsigned char *)buf->data; ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); set= -1; for (i=0; i<sk_X509_NAME_ENTRY_num(sk); i++) { ne=sk_X509_NAME_ENTRY_value(sk,i); if (set != ne->set) { set=ne->set; ASN1_put_object(&p,1,ne->size, V_ASN1_SET,V_ASN1_UNIVERSAL); } i2d_X509_NAME_ENTRY(ne,&p); } a->modified=0; return(r); err: return(-1); }