/src/openssl/crypto/asn1/evp_asn1.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1.h> |
13 | | #include <openssl/asn1t.h> |
14 | | |
15 | | int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len) |
16 | 0 | { |
17 | 0 | ASN1_STRING *os; |
18 | 0 |
|
19 | 0 | if ((os = ASN1_OCTET_STRING_new()) == NULL) |
20 | 0 | return 0; |
21 | 0 | if (!ASN1_OCTET_STRING_set(os, data, len)) { |
22 | 0 | ASN1_OCTET_STRING_free(os); |
23 | 0 | return 0; |
24 | 0 | } |
25 | 0 | ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os); |
26 | 0 | return 1; |
27 | 0 | } |
28 | | |
29 | | /* int max_len: for returned value */ |
30 | | int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len) |
31 | 0 | { |
32 | 0 | int ret, num; |
33 | 0 | const unsigned char *p; |
34 | 0 |
|
35 | 0 | if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) { |
36 | 0 | ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING, ASN1_R_DATA_IS_WRONG); |
37 | 0 | return -1; |
38 | 0 | } |
39 | 0 | p = ASN1_STRING_get0_data(a->value.octet_string); |
40 | 0 | ret = ASN1_STRING_length(a->value.octet_string); |
41 | 0 | if (ret < max_len) |
42 | 0 | num = ret; |
43 | 0 | else |
44 | 0 | num = max_len; |
45 | 0 | memcpy(data, p, num); |
46 | 0 | return ret; |
47 | 0 | } |
48 | | |
49 | | typedef struct { |
50 | | int32_t num; |
51 | | ASN1_OCTET_STRING *oct; |
52 | | } asn1_int_oct; |
53 | | |
54 | | ASN1_SEQUENCE(asn1_int_oct) = { |
55 | | ASN1_EMBED(asn1_int_oct, num, INT32), |
56 | | ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING) |
57 | | } static_ASN1_SEQUENCE_END(asn1_int_oct) |
58 | | |
59 | | DECLARE_ASN1_ITEM(asn1_int_oct) |
60 | | |
61 | | int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, |
62 | | int len) |
63 | 0 | { |
64 | 0 | asn1_int_oct atmp; |
65 | 0 | ASN1_OCTET_STRING oct; |
66 | 0 |
|
67 | 0 | atmp.num = num; |
68 | 0 | atmp.oct = &oct; |
69 | 0 | oct.data = data; |
70 | 0 | oct.type = V_ASN1_OCTET_STRING; |
71 | 0 | oct.length = len; |
72 | 0 | oct.flags = 0; |
73 | 0 |
|
74 | 0 | if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a)) |
75 | 0 | return 1; |
76 | 0 | return 0; |
77 | 0 | } |
78 | | |
79 | | /* |
80 | | * we return the actual length... |
81 | | */ |
82 | | /* int max_len: for returned value */ |
83 | | int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num, |
84 | | unsigned char *data, int max_len) |
85 | 0 | { |
86 | 0 | asn1_int_oct *atmp = NULL; |
87 | 0 | int ret = -1, n; |
88 | 0 |
|
89 | 0 | if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) { |
90 | 0 | goto err; |
91 | 0 | } |
92 | 0 | |
93 | 0 | atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a); |
94 | 0 |
|
95 | 0 | if (atmp == NULL) |
96 | 0 | goto err; |
97 | 0 | |
98 | 0 | if (num != NULL) |
99 | 0 | *num = atmp->num; |
100 | 0 |
|
101 | 0 | ret = ASN1_STRING_length(atmp->oct); |
102 | 0 | if (max_len > ret) |
103 | 0 | n = ret; |
104 | 0 | else |
105 | 0 | n = max_len; |
106 | 0 |
|
107 | 0 | if (data != NULL) |
108 | 0 | memcpy(data, ASN1_STRING_get0_data(atmp->oct), n); |
109 | 0 | if (ret == -1) { |
110 | 0 | err: |
111 | 0 | ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING, ASN1_R_DATA_IS_WRONG); |
112 | 0 | } |
113 | 0 | M_ASN1_free_of(atmp, asn1_int_oct); |
114 | 0 | return ret; |
115 | 0 | } |