|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <stdio.h>
|
|
Packit Service |
084de1 |
#include "internal/cryptlib.h"
|
|
Packit Service |
084de1 |
#include <openssl/asn1.h>
|
|
Packit Service |
084de1 |
#include <openssl/asn1t.h>
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
ASN1_STRING *os;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((os = ASN1_OCTET_STRING_new()) == NULL)
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
if (!ASN1_OCTET_STRING_set(os, data, len)) {
|
|
Packit Service |
084de1 |
ASN1_OCTET_STRING_free(os);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os);
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* int max_len: for returned value */
|
|
Packit Service |
084de1 |
int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ret, num;
|
|
Packit Service |
084de1 |
const unsigned char *p;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
|
|
Packit Service |
084de1 |
ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
|
|
Packit Service |
084de1 |
return -1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
p = ASN1_STRING_get0_data(a->value.octet_string);
|
|
Packit Service |
084de1 |
ret = ASN1_STRING_length(a->value.octet_string);
|
|
Packit Service |
084de1 |
if (ret < max_len)
|
|
Packit Service |
084de1 |
num = ret;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
num = max_len;
|
|
Packit Service |
084de1 |
memcpy(data, p, num);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
typedef struct {
|
|
Packit Service |
084de1 |
int32_t num;
|
|
Packit Service |
084de1 |
ASN1_OCTET_STRING *oct;
|
|
Packit Service |
084de1 |
} asn1_int_oct;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ASN1_SEQUENCE(asn1_int_oct) = {
|
|
Packit Service |
084de1 |
ASN1_EMBED(asn1_int_oct, num, INT32),
|
|
Packit Service |
084de1 |
ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
|
|
Packit Service |
084de1 |
} static_ASN1_SEQUENCE_END(asn1_int_oct)
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
DECLARE_ASN1_ITEM(asn1_int_oct)
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
|
|
Packit Service |
084de1 |
int len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
asn1_int_oct atmp;
|
|
Packit Service |
084de1 |
ASN1_OCTET_STRING oct;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
atmp.num = num;
|
|
Packit Service |
084de1 |
atmp.oct = &oct;
|
|
Packit Service |
084de1 |
oct.data = data;
|
|
Packit Service |
084de1 |
oct.type = V_ASN1_OCTET_STRING;
|
|
Packit Service |
084de1 |
oct.length = len;
|
|
Packit Service |
084de1 |
oct.flags = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* we return the actual length...
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
/* int max_len: for returned value */
|
|
Packit Service |
084de1 |
int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
|
|
Packit Service |
084de1 |
unsigned char *data, int max_len)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
asn1_int_oct *atmp = NULL;
|
|
Packit Service |
084de1 |
int ret = -1, n;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (atmp == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (num != NULL)
|
|
Packit Service |
084de1 |
*num = atmp->num;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = ASN1_STRING_length(atmp->oct);
|
|
Packit Service |
084de1 |
if (max_len > ret)
|
|
Packit Service |
084de1 |
n = ret;
|
|
Packit Service |
084de1 |
else
|
|
Packit Service |
084de1 |
n = max_len;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (data != NULL)
|
|
Packit Service |
084de1 |
memcpy(data, ASN1_STRING_get0_data(atmp->oct), n);
|
|
Packit Service |
084de1 |
if (ret == -1) {
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
M_ASN1_free_of(atmp, asn1_int_oct);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|