/src/openssl/crypto/asn1/p8_pkey.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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/asn1t.h> |
13 | | #include <openssl/x509.h> |
14 | | #include "crypto/x509.h" |
15 | | |
16 | | /* Minor tweak to operation: zero private key data */ |
17 | | static int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
18 | | void *exarg) |
19 | 1.81M | { |
20 | 1.81M | PKCS8_PRIV_KEY_INFO *key; |
21 | 1.81M | int version; |
22 | | |
23 | 1.81M | switch (operation) { |
24 | 358k | case ASN1_OP_FREE_PRE: |
25 | | /* The structure is still valid during ASN1_OP_FREE_PRE */ |
26 | 358k | key = (PKCS8_PRIV_KEY_INFO *)*pval; |
27 | 358k | if (key->pkey) |
28 | 358k | OPENSSL_cleanse(key->pkey->data, key->pkey->length); |
29 | 358k | break; |
30 | 21.5k | case ASN1_OP_D2I_POST: |
31 | | /* Insist on a valid version now that the structure is decoded */ |
32 | 21.5k | key = (PKCS8_PRIV_KEY_INFO *)*pval; |
33 | 21.5k | version = ASN1_INTEGER_get(key->version); |
34 | 21.5k | if (version < 0 || version > 1) |
35 | 9.25k | return 0; |
36 | 12.3k | if (version == 0 && key->kpub != NULL) |
37 | 94 | return 0; |
38 | 12.2k | break; |
39 | 1.81M | } |
40 | 1.80M | return 1; |
41 | 1.81M | } |
42 | | |
43 | | ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = { |
44 | | ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER), |
45 | | ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR), |
46 | | ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING), |
47 | | ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0), |
48 | | ASN1_IMP_OPT(PKCS8_PRIV_KEY_INFO, kpub, ASN1_BIT_STRING, 1) |
49 | | } ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) |
50 | | |
51 | | IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO) |
52 | | |
53 | | int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj, |
54 | | int version, |
55 | | int ptype, void *pval, unsigned char *penc, int penclen) |
56 | 27 | { |
57 | 27 | if (version >= 0) { |
58 | | /* We only support PKCS#8 v1 (0) and v2 (1). */ |
59 | 27 | if (version > 1) |
60 | 0 | return 0; |
61 | 27 | if (!ASN1_INTEGER_set(priv->version, version)) |
62 | 0 | return 0; |
63 | 27 | } |
64 | 27 | if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval)) |
65 | 0 | return 0; |
66 | 27 | if (penc) |
67 | 27 | ASN1_STRING_set0(priv->pkey, penc, penclen); |
68 | 27 | return 1; |
69 | 27 | } |
70 | | |
71 | | int PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg, |
72 | | const unsigned char **pk, int *ppklen, |
73 | | const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8) |
74 | 31.5k | { |
75 | 31.5k | if (ppkalg) |
76 | 2.70k | *ppkalg = p8->pkeyalg->algorithm; |
77 | 31.5k | if (pk) { |
78 | 8.45k | *pk = ASN1_STRING_get0_data(p8->pkey); |
79 | 8.45k | *ppklen = ASN1_STRING_length(p8->pkey); |
80 | 8.45k | } |
81 | 31.5k | if (pa) |
82 | 28.8k | *pa = p8->pkeyalg; |
83 | 31.5k | return 1; |
84 | 31.5k | } |
85 | | |
86 | | const STACK_OF(X509_ATTRIBUTE) * |
87 | | PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8) |
88 | 0 | { |
89 | 0 | return p8->attributes; |
90 | 0 | } |
91 | | |
92 | | int PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type, |
93 | | const unsigned char *bytes, int len) |
94 | 0 | { |
95 | 0 | if (X509at_add1_attr_by_NID(&p8->attributes, nid, type, bytes, len) != NULL) |
96 | 0 | return 1; |
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | | int PKCS8_pkey_add1_attr_by_OBJ(PKCS8_PRIV_KEY_INFO *p8, const ASN1_OBJECT *obj, int type, |
101 | | const unsigned char *bytes, int len) |
102 | 0 | { |
103 | 0 | return (X509at_add1_attr_by_OBJ(&p8->attributes, obj, type, bytes, len) != NULL); |
104 | 0 | } |
105 | | |
106 | | int PKCS8_pkey_add1_attr(PKCS8_PRIV_KEY_INFO *p8, X509_ATTRIBUTE *attr) |
107 | 0 | { |
108 | 0 | return (X509at_add1_attr(&p8->attributes, attr) != NULL); |
109 | 0 | } |