xref: /openbmc/qemu/crypto/der.h (revision 9f22020b)
1 /*
2  * Copyright (c) 2022 Bytedance
3  * Author: lei he <helei.sig11@bytedance.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef QCRYPTO_ASN1_DECODER_H
21 #define QCRYPTO_ASN1_DECODER_H
22 
23 #include "qapi/error.h"
24 
25 /* Simple decoder used to parse DER encoded rsa keys. */
26 
27 /**
28  *  @opaque: user context.
29  *  @value: the starting address of |value| part of 'Tag-Length-Value' pattern.
30  *  @vlen: length of the |value|.
31  *  Returns: 0 for success, any other value is considered an error.
32  */
33 typedef int (*QCryptoDERDecodeCb) (void *opaque, const uint8_t *value,
34                                    size_t vlen, Error **errp);
35 
36 /**
37  * qcrypto_der_decode_int:
38  * @data: pointer to address of input data
39  * @dlen: pointer to length of input data
40  * @cb: callback invoked when decode succeed, if cb equals NULL, no
41  * callback will be invoked
42  * @opaque: parameter passed to cb
43  *
44  * Decode integer from DER-encoded data.
45  *
46  * Returns: On success, *data points to rest data, and *dlen
47  * will be set to the rest length of data, if cb is not NULL, must
48  * return 0 to make decode success, at last, the length of the data
49  * part of the decoded INTEGER will be returned. Otherwise, -1 is
50  * returned.
51  */
52 int qcrypto_der_decode_int(const uint8_t **data,
53                            size_t *dlen,
54                            QCryptoDERDecodeCb cb,
55                            void *opaque,
56                            Error **errp);
57 
58 /**
59  * qcrypto_der_decode_seq:
60  *
61  * Decode sequence from DER-encoded data, similar with der_decode_int.
62  *
63  * @data: pointer to address of input data
64  * @dlen: pointer to length of input data
65  * @cb: callback invoked when decode succeed, if cb equals NULL, no
66  * callback will be invoked
67  * @opaque: parameter passed to cb
68  *
69  * Returns: On success, *data points to rest data, and *dlen
70  * will be set to the rest length of data, if cb is not NULL, must
71  * return 0 to make decode success, at last, the length of the data
72  * part of the decoded SEQUENCE will be returned. Otherwise, -1 is
73  * returned.
74  */
75 int qcrypto_der_decode_seq(const uint8_t **data,
76                            size_t *dlen,
77                            QCryptoDERDecodeCb cb,
78                            void *opaque,
79                            Error **errp);
80 
81 #endif  /* QCRYPTO_ASN1_DECODER_H */
82