]>
Commit | Line | Data |
---|---|---|
4f22f405 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
58964a49 | 3 | * |
4f22f405 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
58964a49 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 | 12 | #include <openssl/asn1.h> |
b9395187 | 13 | #include <openssl/asn1t.h> |
58964a49 | 14 | |
6343829a | 15 | int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len) |
0f113f3e MC |
16 | { |
17 | ASN1_STRING *os; | |
18 | ||
f422a514 | 19 | if ((os = ASN1_OCTET_STRING_new()) == NULL) |
26a7d938 | 20 | return 0; |
f422a514 DSH |
21 | if (!ASN1_OCTET_STRING_set(os, data, len)) { |
22 | ASN1_OCTET_STRING_free(os); | |
0f113f3e MC |
23 | return 0; |
24 | } | |
25 | ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os); | |
208fb891 | 26 | return 1; |
0f113f3e | 27 | } |
58964a49 | 28 | |
6b691a5c | 29 | /* int max_len: for returned value */ |
08275a29 | 30 | int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len) |
0f113f3e MC |
31 | { |
32 | int ret, num; | |
17ebf85a | 33 | const unsigned char *p; |
0f113f3e MC |
34 | |
35 | if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) { | |
36 | ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING, ASN1_R_DATA_IS_WRONG); | |
26a7d938 | 37 | return -1; |
0f113f3e | 38 | } |
17ebf85a | 39 | p = ASN1_STRING_get0_data(a->value.octet_string); |
f422a514 | 40 | ret = ASN1_STRING_length(a->value.octet_string); |
0f113f3e MC |
41 | if (ret < max_len) |
42 | num = ret; | |
43 | else | |
44 | num = max_len; | |
45 | memcpy(data, p, num); | |
26a7d938 | 46 | return ret; |
0f113f3e | 47 | } |
58964a49 | 48 | |
b9395187 | 49 | typedef struct { |
6a32a3c0 | 50 | int32_t num; |
b9395187 DSH |
51 | ASN1_OCTET_STRING *oct; |
52 | } asn1_int_oct; | |
0f113f3e | 53 | |
b9395187 | 54 | ASN1_SEQUENCE(asn1_int_oct) = { |
9612e157 | 55 | ASN1_EMBED(asn1_int_oct, num, INT32), |
b9395187 | 56 | ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING) |
df2ee0e2 | 57 | } static_ASN1_SEQUENCE_END(asn1_int_oct) |
0f113f3e | 58 | |
b9395187 | 59 | DECLARE_ASN1_ITEM(asn1_int_oct) |
0f113f3e | 60 | |
b9395187 DSH |
61 | int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, |
62 | int len) | |
63 | { | |
64 | asn1_int_oct atmp; | |
65 | ASN1_OCTET_STRING oct; | |
66 | ||
67 | atmp.num = num; | |
68 | atmp.oct = &oct; | |
69 | oct.data = data; | |
70 | oct.type = V_ASN1_OCTET_STRING; | |
71 | oct.length = len; | |
72 | oct.flags = 0; | |
73 | ||
74 | if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a)) | |
75 | return 1; | |
76 | return 0; | |
0f113f3e MC |
77 | } |
78 | ||
79 | /* | |
b9395187 | 80 | * we return the actual length... |
0f113f3e | 81 | */ |
6b691a5c | 82 | /* int max_len: for returned value */ |
08275a29 | 83 | int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num, |
0f113f3e MC |
84 | unsigned char *data, int max_len) |
85 | { | |
b9395187 | 86 | asn1_int_oct *atmp = NULL; |
0f113f3e | 87 | int ret = -1, n; |
0f113f3e MC |
88 | |
89 | if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) { | |
90 | goto err; | |
91 | } | |
0f113f3e | 92 | |
b9395187 | 93 | atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a); |
0f113f3e | 94 | |
b9395187 | 95 | if (atmp == NULL) |
0f113f3e MC |
96 | goto err; |
97 | ||
98 | if (num != NULL) | |
b9395187 | 99 | *num = atmp->num; |
0f113f3e | 100 | |
b9395187 | 101 | ret = ASN1_STRING_length(atmp->oct); |
0f113f3e MC |
102 | if (max_len > ret) |
103 | n = ret; | |
104 | else | |
105 | n = max_len; | |
106 | ||
107 | if (data != NULL) | |
17ebf85a | 108 | memcpy(data, ASN1_STRING_get0_data(atmp->oct), n); |
b9395187 | 109 | if (ret == -1) { |
0f113f3e MC |
110 | err: |
111 | ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING, ASN1_R_DATA_IS_WRONG); | |
112 | } | |
b9395187 DSH |
113 | M_ASN1_free_of(atmp, asn1_int_oct); |
114 | return ret; | |
0f113f3e | 115 | } |