summaryrefslogtreecommitdiffstats
path: root/crypto/der_writer.c
blob: 876278750470374c1c3577122374082729ff7aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdlib.h>
#include <string.h>
#include "internal/cryptlib.h"
#include "internal/der.h"
#include "crypto/bn.h"

static int int_start_context(WPACKET *pkt, int tag)
{
    if (tag < 0)
        return 1;
    if (!ossl_assert(tag <= 30))
        return 0;
    return WPACKET_start_sub_packet(pkt);
}

static int int_end_context(WPACKET *pkt, int tag)
{
    /*
     * If someone set the flag WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH on this
     * sub-packet and this sub-packet has nothing written to it, the DER length
     * will not be written, and the total written size will be unchanged before
     * and after WPACKET_close().  We use size1 and size2 to determine if
     * anything was written, and only write our tag if it has.
     *
     */
    size_t size1, size2;

    if (tag < 0)
        return 1;
    if (!ossl_assert(tag <= 30))
        return 0;
    return WPACKET_get_total_written(pkt, &size1)
        && WPACKET_close(pkt)
        && WPACKET_get_total_written(pkt, &size2)
        && (size1 == size2 || WPACKET_put_bytes_u8(pkt, DER_C_CONTEXT | tag));
}

int DER_w_precompiled(WPACKET *pkt, int tag,
                      const unsigned char *precompiled, size_t precompiled_n)
{
    return int_start_context(pkt, tag)
        && WPACKET_memcpy(pkt, precompiled, precompiled_n)
        && int_end_context(pkt, tag);
}

int DER_w_boolean(WPACKET *pkt, int tag, int b)
{
    return int_start_context(pkt, tag)
        && WPACKET_start_sub_packet(pkt)
        && (!b || WPACKET_put_bytes_u8(pkt, 0xFF))
        && !WPACKET_close(pkt)
        && !WPACKET_put_bytes_u8(pkt, DER_P_BOOLEAN)
        && int_end_context(pkt, tag);
}

static int int_der_w_integer(WPACKET *pkt, int tag,
                             int (*put_bytes)(WPACKET *pkt, const void *v,
                                              unsigned int *top_byte),
                             const void *v)
{
    unsigned int top_byte = 0;

    return int_start_context(pkt, tag)
        && WPACKET_start_sub_packet(pkt)
        && put_bytes(pkt, v, &top_byte)
        && ((top_byte & 0x80) == 0 || WPACKET_put_bytes_u8(pkt, 0))
        && WPACKET_close(pkt)
        && WPACKET_put_bytes_u8(pkt, DER_P_INTEGER)
        && int_end_context(pkt, tag);
}

static int int_put_bytes_ulong(WPACKET *pkt, const void *v,
                               unsigned int *top_byte)
{
    const unsigned long *value = v;
    unsigned long tmp = *value;
    size_t n = 0;

    while (tmp != 0) {
        n++;
        *top_byte = (tmp & 0xFF);
        tmp >>= 8;
    }
    if (n == 0)
        n = 1;

    return WPACKET_put_bytes__(pkt, *value, n);
}

/* For integers, we only support unsigned values for now */
int DER_w_ulong(WPACKET *pkt, int tag, unsigned long v)
{
    return int_der_w_integer(pkt, tag, int_put_bytes_ulong, &v);
}

static int int_put_bytes_bn(WPACKET *pkt, const void *v,
                            unsigned int *top_byte)
{
    unsigned char *p = NULL;
    size_t n = BN_num_bytes(v);

    /* The BIGNUM limbs are in LE order */
    *top_byte =
        ((bn_get_words(v) [(n - 1) / BN_BYTES]) >> (8 * ((n - 1) % BN_BYTES)))
        & 0xFF;

    if (!WPACKET_allocate_bytes(pkt, n, &p))
        return 0;
    if (p != NULL)
        BN_bn2bin(v, p);
    return 1;
}

int DER_w_bn(WPACKET *pkt, int tag, const BIGNUM *v)
{
    if (v == NULL || BN_is_negative(v))
        return 0;
    if (BN_is_zero(v))
        return DER_w_ulong(pkt, tag, 0);

    return int_der_w_integer(pkt, tag, int_put_bytes_bn, v);
}

int DER_w_null(WPACKET *pkt, int tag)
{
    return int_start_context(pkt, tag)
        && WPACKET_start_sub_packet(pkt)
        && WPACKET_close(pkt)
        && WPACKET_put_bytes_u8(pkt, DER_P_NULL)
        && int_end_context(pkt, tag);
}

/* Constructed things need a start and an end */
int DER_w_begin_sequence(WPACKET *pkt, int tag)
{
    return int_start_context(pkt, tag)
        && WPACKET_start_sub_packet(pkt);
}

int DER_w_end_sequence(WPACKET *pkt, int tag)
{
    /*
     * If someone set the flag WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH on this
     * sub-packet and this sub-packet has nothing written to it, the DER length
     * will not be written, and the total written size will be unchanged before
     * and after WPACKET_close().  We use size1 and size2 to determine if
     * anything was written, and only write our tag if it has.
     *
     * Because we know that int_end_context() needs to do the same check,
     * we reproduce this flag if the written length was unchanged, or we will
     * have an erroneous context tag.
     */
    size_t size1, size2;

    return WPACKET_get_total_written(pkt, &size1)
        && WPACKET_close(pkt)
        && WPACKET_get_total_written(pkt, &size2)
        && (size1 == size2
            ? WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
            : WPACKET_put_bytes_u8(pkt, DER_F_CONSTRUCTED | DER_P_SEQUENCE))
        && int_end_context(pkt, tag);
}