Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | #include "secdig.h" |
michael@0 | 5 | |
michael@0 | 6 | #include "secoid.h" |
michael@0 | 7 | #include "secasn1.h" |
michael@0 | 8 | #include "secerr.h" |
michael@0 | 9 | |
michael@0 | 10 | /* |
michael@0 | 11 | * XXX Want to have a SGN_DecodeDigestInfo, like: |
michael@0 | 12 | * SGNDigestInfo *SGN_DecodeDigestInfo(SECItem *didata); |
michael@0 | 13 | * that creates a pool and allocates from it and decodes didata into |
michael@0 | 14 | * the newly allocated DigestInfo structure. Then fix secvfy.c (it |
michael@0 | 15 | * will no longer need an arena itself) to call this and then call |
michael@0 | 16 | * DestroyDigestInfo when it is done, then can remove the old template |
michael@0 | 17 | * above and keep our new template static and "hidden". |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | /* |
michael@0 | 21 | * XXX It might be nice to combine the following two functions (create |
michael@0 | 22 | * and encode). I think that is all anybody ever wants to do anyway. |
michael@0 | 23 | */ |
michael@0 | 24 | |
michael@0 | 25 | SECItem * |
michael@0 | 26 | SGN_EncodeDigestInfo(PLArenaPool *poolp, SECItem *dest, SGNDigestInfo *diginfo) |
michael@0 | 27 | { |
michael@0 | 28 | return SEC_ASN1EncodeItem (poolp, dest, diginfo, sgn_DigestInfoTemplate); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | SGNDigestInfo * |
michael@0 | 32 | SGN_CreateDigestInfo(SECOidTag algorithm, const unsigned char *sig, |
michael@0 | 33 | unsigned len) |
michael@0 | 34 | { |
michael@0 | 35 | SGNDigestInfo *di; |
michael@0 | 36 | SECStatus rv; |
michael@0 | 37 | PLArenaPool *arena; |
michael@0 | 38 | SECItem *null_param; |
michael@0 | 39 | SECItem dummy_value; |
michael@0 | 40 | |
michael@0 | 41 | switch (algorithm) { |
michael@0 | 42 | case SEC_OID_MD2: |
michael@0 | 43 | case SEC_OID_MD5: |
michael@0 | 44 | case SEC_OID_SHA1: |
michael@0 | 45 | case SEC_OID_SHA224: |
michael@0 | 46 | case SEC_OID_SHA256: |
michael@0 | 47 | case SEC_OID_SHA384: |
michael@0 | 48 | case SEC_OID_SHA512: |
michael@0 | 49 | break; |
michael@0 | 50 | default: |
michael@0 | 51 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
michael@0 | 52 | return NULL; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
michael@0 | 56 | if (arena == NULL) { |
michael@0 | 57 | return NULL; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | di = (SGNDigestInfo *) PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo)); |
michael@0 | 61 | if (di == NULL) { |
michael@0 | 62 | PORT_FreeArena(arena, PR_FALSE); |
michael@0 | 63 | return NULL; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | di->arena = arena; |
michael@0 | 67 | |
michael@0 | 68 | /* |
michael@0 | 69 | * PKCS #1 specifies that the AlgorithmID must have a NULL parameter |
michael@0 | 70 | * (as opposed to no parameter at all). |
michael@0 | 71 | */ |
michael@0 | 72 | dummy_value.data = NULL; |
michael@0 | 73 | dummy_value.len = 0; |
michael@0 | 74 | null_param = SEC_ASN1EncodeItem(NULL, NULL, &dummy_value, SEC_NullTemplate); |
michael@0 | 75 | if (null_param == NULL) { |
michael@0 | 76 | goto loser; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | rv = SECOID_SetAlgorithmID(arena, &di->digestAlgorithm, algorithm, |
michael@0 | 80 | null_param); |
michael@0 | 81 | |
michael@0 | 82 | SECITEM_FreeItem(null_param, PR_TRUE); |
michael@0 | 83 | |
michael@0 | 84 | if (rv != SECSuccess) { |
michael@0 | 85 | goto loser; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | di->digest.data = (unsigned char *) PORT_ArenaAlloc(arena, len); |
michael@0 | 89 | if (di->digest.data == NULL) { |
michael@0 | 90 | goto loser; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | di->digest.len = len; |
michael@0 | 94 | PORT_Memcpy(di->digest.data, sig, len); |
michael@0 | 95 | return di; |
michael@0 | 96 | |
michael@0 | 97 | loser: |
michael@0 | 98 | SGN_DestroyDigestInfo(di); |
michael@0 | 99 | return NULL; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | SGNDigestInfo * |
michael@0 | 103 | SGN_DecodeDigestInfo(SECItem *didata) |
michael@0 | 104 | { |
michael@0 | 105 | PLArenaPool *arena; |
michael@0 | 106 | SGNDigestInfo *di; |
michael@0 | 107 | SECStatus rv = SECFailure; |
michael@0 | 108 | SECItem diCopy = {siBuffer, NULL, 0}; |
michael@0 | 109 | |
michael@0 | 110 | arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); |
michael@0 | 111 | if(arena == NULL) |
michael@0 | 112 | return NULL; |
michael@0 | 113 | |
michael@0 | 114 | rv = SECITEM_CopyItem(arena, &diCopy, didata); |
michael@0 | 115 | if (rv != SECSuccess) { |
michael@0 | 116 | PORT_FreeArena(arena, PR_FALSE); |
michael@0 | 117 | return NULL; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | di = (SGNDigestInfo *)PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo)); |
michael@0 | 121 | if (di != NULL) { |
michael@0 | 122 | di->arena = arena; |
michael@0 | 123 | rv = SEC_QuickDERDecodeItem(arena, di, sgn_DigestInfoTemplate, &diCopy); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | if ((di == NULL) || (rv != SECSuccess)) { |
michael@0 | 127 | PORT_FreeArena(arena, PR_FALSE); |
michael@0 | 128 | di = NULL; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | return di; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | void |
michael@0 | 135 | SGN_DestroyDigestInfo(SGNDigestInfo *di) |
michael@0 | 136 | { |
michael@0 | 137 | if (di && di->arena) { |
michael@0 | 138 | PORT_FreeArena(di->arena, PR_FALSE); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | return; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | SECStatus |
michael@0 | 145 | SGN_CopyDigestInfo(PLArenaPool *poolp, SGNDigestInfo *a, SGNDigestInfo *b) |
michael@0 | 146 | { |
michael@0 | 147 | SECStatus rv; |
michael@0 | 148 | void *mark; |
michael@0 | 149 | |
michael@0 | 150 | if((poolp == NULL) || (a == NULL) || (b == NULL)) |
michael@0 | 151 | return SECFailure; |
michael@0 | 152 | |
michael@0 | 153 | mark = PORT_ArenaMark(poolp); |
michael@0 | 154 | a->arena = poolp; |
michael@0 | 155 | rv = SECOID_CopyAlgorithmID(poolp, &a->digestAlgorithm, |
michael@0 | 156 | &b->digestAlgorithm); |
michael@0 | 157 | if (rv == SECSuccess) |
michael@0 | 158 | rv = SECITEM_CopyItem(poolp, &a->digest, &b->digest); |
michael@0 | 159 | |
michael@0 | 160 | if (rv != SECSuccess) { |
michael@0 | 161 | PORT_ArenaRelease(poolp, mark); |
michael@0 | 162 | } else { |
michael@0 | 163 | PORT_ArenaUnmark(poolp, mark); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | return rv; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | SECComparison |
michael@0 | 170 | SGN_CompareDigestInfo(SGNDigestInfo *a, SGNDigestInfo *b) |
michael@0 | 171 | { |
michael@0 | 172 | SECComparison rv; |
michael@0 | 173 | |
michael@0 | 174 | /* Check signature algorithm's */ |
michael@0 | 175 | rv = SECOID_CompareAlgorithmID(&a->digestAlgorithm, &b->digestAlgorithm); |
michael@0 | 176 | if (rv) return rv; |
michael@0 | 177 | |
michael@0 | 178 | /* Compare signature block length's */ |
michael@0 | 179 | rv = SECITEM_CompareItem(&a->digest, &b->digest); |
michael@0 | 180 | return rv; |
michael@0 | 181 | } |