📄 wtls_pdusupport.c
字号:
*charpos += length; return opaque;}Octstr * unpack_octstr16(Octstr *data, long *charpos) { long length; Octstr *opaque; length = unpack_int16(data, charpos); opaque = octstr_copy(data, *charpos, length); *charpos += length; return opaque;}Octstr * unpack_octstr_fixed(Octstr *data, long *charpos, long length) { Octstr *opaque; opaque = octstr_copy(data, *charpos, length); *charpos += length; return opaque;}Random * unpack_random(Octstr *data, long *charpos) { Random *random; /* create the Random structure */ random = (Random *)gw_malloc(sizeof(Random)); random->gmt_unix_time = unpack_int32(data, charpos); random->random_bytes = unpack_octstr_fixed(data, charpos, 12); return random;} DHParameters * unpack_dhparams(Octstr *data, long *charpos) { DHParameters *dhparams; /* create the DHParameters */ dhparams = (DHParameters *)gw_malloc(sizeof(DHParameters)); dhparams->dh_e = octstr_get_char(data, *charpos); *charpos += 1; dhparams->dh_p = unpack_octstr16(data, charpos); dhparams->dh_g = unpack_octstr16(data, charpos); return dhparams;}ECParameters * unpack_ecparams(Octstr *data, long *charpos) { ECParameters *ecparams; /* create the ECParameters */ ecparams = (ECParameters *)gw_malloc(sizeof(ECParameters)); /* field */ ecparams->field = octstr_get_char(data, *charpos); *charpos += 1; switch (ecparams->field) { case ec_prime_p: ecparams->prime_p = unpack_octstr(data, charpos); break; case ec_characteristic_two: /* m (16 bits) */ ecparams->m = unpack_int16(data, charpos); /* basis */ ecparams->basis = octstr_get_char(data, *charpos); *charpos += 1; switch (ecparams->basis) { case ec_basis_onb: break; case ec_basis_trinomial: ecparams->k = unpack_int16(data, charpos); break; case ec_basis_pentanomial: ecparams->k1 = unpack_int16(data, charpos); ecparams->k2 = unpack_int16(data, charpos); ecparams->k3 = unpack_int16(data, charpos); break; case ec_basis_polynomial: ecparams->irreducible = unpack_octstr(data, charpos); break; } break; } /* pack the ECCurve */ ecparams->curve->a = unpack_octstr(data, charpos); ecparams->curve->b = unpack_octstr(data, charpos); ecparams->curve->seed = unpack_octstr(data, charpos); /* pack the ECPoint */ ecparams->base->point = unpack_octstr(data, charpos); /* order and cofactor */ ecparams->order = unpack_octstr(data, charpos); ecparams->cofactor = unpack_octstr(data, charpos); return ecparams; }ParameterSpecifier * unpack_param_spec(Octstr *data, long *charpos) { ParameterSpecifier *pspec; /* create the ParameterSpecifier */ pspec = (ParameterSpecifier *)gw_malloc(sizeof(ParameterSpecifier)); /* index */ pspec->param_index = octstr_get_char(data, *charpos); *charpos += 1; /* ParameterSet struct */ if(pspec->param_index == 255) { pspec->param_set = (ParameterSet *)gw_malloc(sizeof(ParameterSet)); pspec->param_set->length = octstr_get_char(data, *charpos); *charpos += 1; switch (public_key_algo) { case diffie_hellman_pubkey: pspec->param_set->dhparams = unpack_dhparams(data, charpos); break; case elliptic_curve_pubkey: pspec->param_set->ecparams = unpack_ecparams(data, charpos); break; } } return pspec;}RSAPublicKey * unpack_rsa_pubkey(Octstr *data, long *charpos) { RSAPublicKey *key; /* create the RSAPublicKey */ key = (RSAPublicKey *)gw_malloc(sizeof(RSAPublicKey)); key->rsa_exponent = unpack_octstr16( data, charpos); key->rsa_modulus = unpack_octstr16( data, charpos); return key;}DHPublicKey * unpack_dh_pubkey(Octstr *data, long *charpos) { DHPublicKey *key; /* create the DHPublicKey */ key = (DHPublicKey *)gw_malloc(sizeof(DHPublicKey)); key->dh_Y = unpack_octstr16( data, charpos); return key;}ECPublicKey * unpack_ec_pubkey(Octstr *data, long *charpos) { ECPublicKey *key; /* create the ECPublicKey */ key = (ECPublicKey *)gw_malloc(sizeof(ECPublicKey)); key->point = unpack_octstr( data, charpos); return key;}RSASecret * unpack_rsa_secret(Octstr *data, long *charpos) { RSASecret *secret; /* create the RSASecret */ secret = (RSASecret *)gw_malloc(sizeof(RSASecret)); secret->client_version = octstr_get_char(data, *charpos); *charpos += 1; secret->random = unpack_array(data, charpos); return secret;}RSAEncryptedSecret * unpack_rsa_encrypted_secret(Octstr *data, long *charpos) { RSAEncryptedSecret *secret; /* create the RSASecret */ secret = (RSAEncryptedSecret *)gw_malloc(sizeof(RSAEncryptedSecret)); //secret->encrypted_secret = unpack_octstr16(data, charpos); secret->encrypted_secret = unpack_octstr_fixed(data, charpos, octstr_len(data) - *charpos); return secret;}PublicKey * unpack_public_key(Octstr *data, long *charpos, PublicKeyType key_type) { PublicKey *key; /* create the PublicKey */ key = (PublicKey *)gw_malloc(sizeof(PublicKey)); switch (key_type) { case ecdh_key: key->ecdh_pubkey = unpack_ec_pubkey(data, charpos); break; case ecdsa_key: key->ecdsa_pubkey = unpack_ec_pubkey(data, charpos); break; case rsa_key: key->rsa_pubkey = unpack_rsa_pubkey(data, charpos); break; } return key;}KeyExchangeId * unpack_key_exchange_id(Octstr *data, long *charpos) { KeyExchangeId *keyexid; /* create the KeyExchangeID */ keyexid = (KeyExchangeId *)gw_malloc(sizeof(KeyExchangeId)); keyexid->key_exchange_suite = octstr_get_char(data, *charpos); *charpos += 1; keyexid->param_specif = unpack_param_spec(data, charpos); keyexid->identifier = unpack_identifier(data, charpos); return keyexid;}List * unpack_array(Octstr *data, long *charpos) { int i; int array_length; List *array; /* create the list */ array = list_create(); /* get the size of the array */ array_length = octstr_get_char(data, *charpos); *charpos += 1; /* store each entry in the list */ for (i=0; i<array_length; i++) { list_append(array, (void *)unpack_octstr(data, charpos)); } return array;}List * unpack_key_list(Octstr *data, long *charpos) { KeyExchangeId *keyexid; List *key_list; int list_length; long endpos; /* create the list */ key_list = list_create(); /* get the size of the array */ list_length = unpack_int16(data, charpos); endpos = *charpos + list_length; /* unpack the KeyExchangeIds */ while (*charpos < endpos) { keyexid = unpack_key_exchange_id(data, charpos); list_append(key_list, (void *)keyexid); } return key_list;}List * unpack_ciphersuite_list(Octstr *data, long *charpos){ List *ciphersuites; int list_length; int i; CipherSuite *cs; /* create the list */ ciphersuites = list_create(); /* get the size of the array (in bytes, not elements)*/ list_length = octstr_get_char(data, *charpos); *charpos += 1; /* unpack the CipherSuites */ for (i=0; i<list_length; i+=2) { cs = (CipherSuite *)gw_malloc(sizeof(CipherSuite)); cs->bulk_cipher_algo = octstr_get_char(data, *charpos); *charpos += 1; cs->mac_algo = octstr_get_char(data, *charpos); *charpos += 1; list_append(ciphersuites, (void *)cs); } return ciphersuites;}List * unpack_compression_method_list(Octstr *data, long *charpos) { List *compmethod_list; int list_length; int i; CompressionMethod *cm; /* create the list */ compmethod_list = list_create(); /* get the size of the array */ list_length = octstr_get_char(data, *charpos); *charpos += 1; /* unpack the CompressionMethods */ for (i=0; i<list_length; i++) { cm = gw_malloc(sizeof(CompressionMethod)); *cm = octstr_get_char(data, *charpos); list_append(compmethod_list, (void *)cm); } return compmethod_list;}Identifier * unpack_identifier(Octstr *data, long *charpos) { Identifier *ident; /* create Identifier */ ident = (Identifier *)gw_malloc(sizeof(Identifier)); ident->id_type = octstr_get_char(data, *charpos); *charpos += 1; switch (ident->id_type) { case text: ident->charset = octstr_get_char(data, *charpos); *charpos += 1; ident->name = unpack_octstr(data, charpos); break; case binary: ident->identifier = unpack_octstr(data, charpos); break; case key_hash_sha: ident->key_hash = unpack_octstr(data, charpos); break; case x509_name: ident->distinguished_name = unpack_octstr(data, charpos); break; } return ident;}Signature * unpack_signature(Octstr *data, long *charpos) { Signature *sig; /* create Signature */ sig = (Signature *)gw_malloc(sizeof(Signature)); switch (signature_algo) { case ecdsa_sha: case rsa_sha: sig->sha_hash = unpack_array(data, charpos); break; } return sig;}WTLSCertificate * unpack_wtls_certificate(Octstr *data, long *charpos) { WTLSCertificate *cert; /* create the Certificate */ cert = (WTLSCertificate *)gw_malloc(sizeof(WTLSCertificate)); /* === unpack ToBeSignedCertificate === */ cert->tobesigned_cert = (ToBeSignedCertificate *)gw_malloc(sizeof(ToBeSignedCertificate)); /* version */ cert->tobesigned_cert->certificate_version = octstr_get_char(data, *charpos); *charpos += 1; /* sig algo */ cert->tobesigned_cert->signature_algo = octstr_get_char(data, *charpos); *charpos += 1; /* identifier */ cert->tobesigned_cert->issuer->id_type = octstr_get_char(data, *charpos); *charpos += 1; /* issuer Identifier */ cert->tobesigned_cert->issuer = unpack_identifier(data, charpos); /* validity periods */ cert->tobesigned_cert->valid_not_before = unpack_int32(data, charpos); cert->tobesigned_cert->valid_not_after = unpack_int32(data, charpos); /* subject Identifier */ cert->tobesigned_cert->subject = unpack_identifier(data, charpos); /* public_key_type */ cert->tobesigned_cert->pubkey_type = octstr_get_char(data, *charpos); *charpos += 1; /* parameter specifier */ cert->tobesigned_cert->param_spec = unpack_param_spec(data, charpos); /* public key */ cert->tobesigned_cert->pubkey = unpack_public_key(data, charpos, cert->tobesigned_cert->pubkey_type); /* === pack Signature === */ cert->signature = unpack_signature(data, charpos); return cert;}/***************************************************************** * DESTROY functions */ void destroy_octstr(Octstr *data) { octstr_destroy(data);}void destroy_octstr16(Octstr *data) { octstr_destroy(data);}void destroy_octstr_fixed(Octstr *data) { octstr_destroy(data);}void destroy_random(Random *random) { octstr_destroy(random->random_bytes); gw_free(random);}void destroy_dhparams(DHParameters *dhparams) { destroy_octstr16(dhparams->dh_p); destroy_octstr16(dhparams->dh_g); gw_free(dhparams);}void destroy_ecparams(ECParameters *ecparams) { /* field */ switch (ecparams->field) { case ec_prime_p: octstr_destroy(ecparams->prime_p); break; case ec_characteristic_two: switch (ecparams->basis) { case ec_basis_onb: break; case ec_basis_trinomial: break; case ec_basis_pentanomial: break; case ec_basis_polynomial: octstr_destroy(ecparams->irreducible); break; } break; } /* pack the ECCurve */ octstr_destroy(ecparams->curve->a); octstr_destroy(ecparams->curve->b); octstr_destroy(ecparams->curve->seed); /* pack the ECPoint */ octstr_destroy(ecparams->base->point); /* order and cofactor */ octstr_destroy(ecparams->order); octstr_destroy(ecparams->cofactor); gw_free(ecparams);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -