diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/asn_pack.c')
| -rw-r--r-- | src/lib/libcrypto/asn1/asn_pack.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/lib/libcrypto/asn1/asn_pack.c b/src/lib/libcrypto/asn1/asn_pack.c index 9752a68206..8eb39e6a9b 100644 --- a/src/lib/libcrypto/asn1/asn_pack.c +++ b/src/lib/libcrypto/asn1/asn_pack.c | |||
| @@ -60,6 +60,103 @@ | |||
| 60 | #include "cryptlib.h" | 60 | #include "cryptlib.h" |
| 61 | #include <openssl/asn1.h> | 61 | #include <openssl/asn1.h> |
| 62 | 62 | ||
| 63 | #ifndef NO_ASN1_OLD | ||
| 64 | |||
| 65 | /* ASN1 packing and unpacking functions */ | ||
| 66 | |||
| 67 | /* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */ | ||
| 68 | |||
| 69 | STACK_OF(OPENSSL_BLOCK) * | ||
| 70 | ASN1_seq_unpack(const unsigned char *buf, int len, d2i_of_void *d2i, | ||
| 71 | void (*free_func)(OPENSSL_BLOCK)) | ||
| 72 | { | ||
| 73 | STACK_OF(OPENSSL_BLOCK) *sk; | ||
| 74 | const unsigned char *pbuf; | ||
| 75 | |||
| 76 | pbuf = buf; | ||
| 77 | if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func, | ||
| 78 | V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL))) | ||
| 79 | ASN1err(ASN1_F_ASN1_SEQ_UNPACK,ASN1_R_DECODE_ERROR); | ||
| 80 | return sk; | ||
| 81 | } | ||
| 82 | |||
| 83 | /* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a | ||
| 84 | * OPENSSL_malloc'ed buffer | ||
| 85 | */ | ||
| 86 | |||
| 87 | unsigned char * | ||
| 88 | ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d, | ||
| 89 | unsigned char **buf, int *len) | ||
| 90 | { | ||
| 91 | int safelen; | ||
| 92 | unsigned char *safe, *p; | ||
| 93 | |||
| 94 | if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE, | ||
| 95 | V_ASN1_UNIVERSAL, IS_SEQUENCE))) { | ||
| 96 | ASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR); | ||
| 97 | return NULL; | ||
| 98 | } | ||
| 99 | if (!(safe = malloc(safelen))) { | ||
| 100 | ASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE); | ||
| 101 | return NULL; | ||
| 102 | } | ||
| 103 | p = safe; | ||
| 104 | i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, | ||
| 105 | IS_SEQUENCE); | ||
| 106 | if (len) | ||
| 107 | *len = safelen; | ||
| 108 | if (buf) | ||
| 109 | *buf = safe; | ||
| 110 | return safe; | ||
| 111 | } | ||
| 112 | |||
| 113 | /* Extract an ASN1 object from an ASN1_STRING */ | ||
| 114 | |||
| 115 | void * | ||
| 116 | ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i) | ||
| 117 | { | ||
| 118 | const unsigned char *p; | ||
| 119 | char *ret; | ||
| 120 | |||
| 121 | p = oct->data; | ||
| 122 | if (!(ret = d2i(NULL, &p, oct->length))) | ||
| 123 | ASN1err(ASN1_F_ASN1_UNPACK_STRING,ASN1_R_DECODE_ERROR); | ||
| 124 | return ret; | ||
| 125 | } | ||
| 126 | |||
| 127 | /* Pack an ASN1 object into an ASN1_STRING */ | ||
| 128 | |||
| 129 | ASN1_STRING * | ||
| 130 | ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct) | ||
| 131 | { | ||
| 132 | unsigned char *p; | ||
| 133 | ASN1_STRING *octmp; | ||
| 134 | |||
| 135 | if (!oct || !*oct) { | ||
| 136 | if (!(octmp = ASN1_STRING_new())) { | ||
| 137 | ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE); | ||
| 138 | return NULL; | ||
| 139 | } | ||
| 140 | if (oct) | ||
| 141 | *oct = octmp; | ||
| 142 | } else | ||
| 143 | octmp = *oct; | ||
| 144 | |||
| 145 | if (!(octmp->length = i2d(obj, NULL))) { | ||
| 146 | ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR); | ||
| 147 | return NULL; | ||
| 148 | } | ||
| 149 | if (!(p = malloc (octmp->length))) { | ||
| 150 | ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE); | ||
| 151 | return NULL; | ||
| 152 | } | ||
| 153 | octmp->data = p; | ||
| 154 | i2d (obj, &p); | ||
| 155 | return octmp; | ||
| 156 | } | ||
| 157 | |||
| 158 | #endif | ||
| 159 | |||
| 63 | /* ASN1_ITEM versions of the above */ | 160 | /* ASN1_ITEM versions of the above */ |
| 64 | 161 | ||
| 65 | ASN1_STRING * | 162 | ASN1_STRING * |
